Results 1 to 1 of 1

Thread: [SOLVED] Depth buffer in QWindow?

  1. #1
    Join Date
    Mar 2014
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default [SOLVED] Depth buffer in QWindow?

    I'm trying to set up a QWindow with an depth buffer, and I can't seem to get it to work. I started with the QWindow OpenGL example, and simplified it heavily. I'm drawing two triangles: one with coloured vertices, one with white vertices. I draw the white triangle second, but with larger Z coordinates, so it should appear to be behind the coloured triangle if I turn on depth buffering.

    glEnable(GL_DEPTH_TEST) didn't work. I requested a 24 bit depth buffer, and nothing happened. Printing the QWindow's format reveals that the depth buffer's size is 0. The output:

    format: QSurfaceFormat(version 2.0, options QFlags() , depthBufferSize 0 , redBufferSize 8 , greenBufferSize 8 , blueBufferSize 8 , alphaBufferSize 8 , stencilBufferSize 0 , samples -1 , swapBehavior 0 , profile 0 )
    It doesn't seem like anyone else is asking this question, so I assume I'm doing something stupid (I'm an OpenGL beginner). How do I make depth buffering work?

    I've set up the same code with a QGLWidget, and had no trouble turning on depth buffering. I'm using Qt 5.2 on an Arch Linux system.

    My code:

    SphereWindow.h:
    Qt Code:
    1. #include <QColor>
    2. #include <QEvent>
    3. #include <QExposeEvent>
    4. #include <QOpenGLContext>
    5. #include <QOpenGLFunctions>
    6. #include <QOpenGLPaintDevice>
    7. #include <QOpenGLShaderProgram>
    8. #include <QPainter>
    9. #include <QResizeEvent>
    10. #include <QSize>
    11. #include <QWindow>
    12.  
    13.  
    14. class SphereWindow : public QWindow, protected QOpenGLFunctions
    15. {
    16. Q_OBJECT
    17. public:
    18. SphereWindow(QWindow * = 0);
    19. virtual ~SphereWindow();
    20. virtual void render();
    21. virtual void initialize();
    22.  
    23. public slots:
    24. void resizeViewport(const QSize &);
    25.  
    26. protected:
    27. virtual void resizeEvent(QResizeEvent *);
    28.  
    29.  
    30. private:
    31. bool _initialized;
    32. QOpenGLContext *_context;
    33. QOpenGLPaintDevice *_device;
    34. QOpenGLShaderProgram *_program;
    35.  
    36. QColor _backgroundColour;
    37.  
    38. GLuint _posAttr;
    39. GLuint _colAttr;
    40. };
    To copy to clipboard, switch view to plain text mode 

    SphereWindow.cpp:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QMatrix4x4>
    3. #include <QOpenGLShader>
    4. #include <QScreen>
    5. #include <QSurfaceFormat>
    6.  
    7. #include <QDebug>
    8.  
    9. #include "SphereWindow.h"
    10.  
    11. SphereWindow::SphereWindow(QWindow *parent)
    12. : QWindow(parent),
    13. _initialized(0),
    14. _program(0),
    15. _backgroundColour(Qt::black)
    16. {
    17. setSurfaceType(QWindow::OpenGLSurface);
    18. create();
    19.  
    20. qDebug() << "format:" << format();
    21.  
    22.  
    23. _context = new QOpenGLContext(this);
    24. _context->setFormat(requestedFormat());
    25. _context->create();
    26. }
    27.  
    28. SphereWindow::~SphereWindow()
    29. { }
    30.  
    31.  
    32. void SphereWindow::resizeEvent(QResizeEvent *event)
    33. {
    34. resizeViewport(event->size());
    35. }
    36.  
    37. void SphereWindow::resizeViewport(const QSize &size)
    38. {
    39. int width = size.width();
    40. int height = size.height();
    41. int side = qMin(width, height);
    42.  
    43. int hoffset = (int)((width - side) / 2.0 + 0.5);
    44. int voffset = (int)((height - side) / 2.0 + 0.5);
    45.  
    46. glViewport(hoffset, voffset, side, side);
    47. }
    48.  
    49. void SphereWindow::render()
    50. {
    51.  
    52. if (! _initialized)
    53. initialize();
    54.  
    55. if (! isExposed()) return;
    56. glEnable(GL_DEPTH_TEST);
    57.  
    58. _context->makeCurrent(this);
    59. glClearColor(_backgroundColour.redF(), _backgroundColour.greenF(), _backgroundColour.blueF(), 1.0);
    60.  
    61. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    62. _program->bind();
    63.  
    64. GLfloat vertices[] = {
    65. -0.75f, 0.75f, 0.0f,
    66. -0.75f, -0.75f, 0.0f,
    67. 0.75f, -0.75f, 0.0f,
    68.  
    69. 0.75f, 0.75f, 0.5f,
    70. 0.75f, -0.75f, 0.5f,
    71. -0.75f, -0.75f, 0.5f,
    72.  
    73. };
    74.  
    75. GLfloat colors[] = {
    76. 1.0f, 0.0f, 0.0f,
    77. 0.0f, 1.0f, 0.0f,
    78. 0.0f, 0.0f, 1.0f,
    79. 1.0f, 1.0f, 1.0f,
    80. 1.0f, 1.0f, 1.0f,
    81. 1.0f, 1.0f, 1.0f,
    82. };
    83.  
    84. glVertexAttribPointer(_posAttr, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    85. glVertexAttribPointer(_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
    86.  
    87. glEnableVertexAttribArray(0);
    88. glEnableVertexAttribArray(1);
    89.  
    90. glDrawArrays(GL_TRIANGLES, 0, 6);
    91.  
    92. glDisableVertexAttribArray(1);
    93. glDisableVertexAttribArray(0);
    94.  
    95. _program->release();
    96. _context->swapBuffers(this);
    97. _context->doneCurrent();
    98. }
    99.  
    100. static const char *vertexShaderSource =
    101. "attribute highp vec4 posAttr;\n"
    102. "attribute lowp vec4 colAttr;\n"
    103. "varying lowp vec4 col;\n"
    104. "void main() {\n"
    105. " col = colAttr;\n"
    106. " gl_Position = posAttr;\n"
    107. "}\n";
    108.  
    109. static const char *fragmentShaderSource =
    110. "varying lowp vec4 col;\n"
    111. "void main() {\n"
    112. " gl_FragColor = col;\n"
    113. "}\n";
    114.  
    115.  
    116. void SphereWindow::initialize()
    117. {
    118. _context->makeCurrent(this);
    119. _program = new QOpenGLShaderProgram(this);
    120. _program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
    121. _program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
    122. _program->link();
    123.  
    124. _program->bind();
    125.  
    126. _posAttr = _program->attributeLocation("posAttr");
    127. _colAttr = _program->attributeLocation("colAttr");
    128.  
    129. _program->release();
    130.  
    131.  
    132.  
    133. initializeOpenGLFunctions();
    134. }
    To copy to clipboard, switch view to plain text mode 

    testWindow.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include <QDialog>
    3. #include <QLabel>
    4. #include <QResizeEvent>
    5. #include <QSurfaceFormat>
    6. #include <QWidget>
    7. #include <QWindow>
    8. #include <QVBoxLayout>
    9.  
    10. #include "SphereWindow.h"
    11.  
    12. class GLDialog : public QDialog
    13. {
    14. public:
    15. GLDialog(QWidget *parent = 0, Qt::WindowFlags f = 0) : QDialog(parent, f)
    16. {
    17. QVBoxLayout *layout = new QVBoxLayout(this);
    18.  
    19. QSurfaceFormat format;
    20. format.setSamples(16);
    21. format.setDepthBufferSize(24);
    22.  
    23. window = new SphereWindow;
    24. window->setFormat(format);
    25. window->render();
    26.  
    27. QWidget *glWidget = QWidget::createWindowContainer(window, this);
    28. layout->addWidget(glWidget);
    29.  
    30. }
    31.  
    32. ~GLDialog()
    33. {
    34. delete window;
    35. }
    36.  
    37. protected:
    38. void resizeEvent(QResizeEvent *event)
    39. {
    40. window->resize(event->size());
    41. window->render();
    42. }
    43.  
    44. private:
    45. SphereWindow *window;
    46. };
    47.  
    48.  
    49. int main(int argc, char **argv)
    50. {
    51. QApplication app(argc, argv);
    52.  
    53. QDialog *dlg = new GLDialog;
    54.  
    55. dlg->resize(640,480);
    56. dlg->show();
    57.  
    58. return app.exec();
    59. }
    To copy to clipboard, switch view to plain text mode 

    testwindow.pro:
    Qt Code:
    1. ######################################################################
    2. # Automatically generated by qmake (3.0) Sat May 3 05:01:55 2014
    3. ######################################################################
    4.  
    5. TEMPLATE = app
    6. TARGET = testqwindow
    7. INCLUDEPATH += .
    8.  
    9. QT += widgets
    10. CONFIG += debug
    11.  
    12. # Input
    13. HEADERS += SphereWindow.h
    14. SOURCES += SphereWindow.cpp testWindow.cpp
    To copy to clipboard, switch view to plain text mode 


    Added after 18 minutes:


    I knew I was doing something stupid. I was setting the context's format in my SphereWindow constructor, but not setting the SphereWindow instance's format until after... so the context was ending up with no depth buffer. I moved the calls to QOpenGLContext::setFormat() and QOpenGLContext::create into my initialize function, like so:


    Qt Code:
    1. SphereWindow::SphereWindow(QWindow *parent)
    2. : QWindow(parent),
    3. _initialized(0),
    4. _program(0),
    5. _backgroundColour(Qt::black)
    6. {
    7. setSurfaceType(QWindow::OpenGLSurface);
    8. create();
    9.  
    10. qDebug() << "format:" << format();
    11.  
    12.  
    13. _context = new QOpenGLContext(this);
    14. /* Move these lines into initialize() */
    15. // _context->setFormat(requestedFormat());
    16. // _context->create();
    17. }
    18.  
    19. ...
    20.  
    21. void SphereWindow::initialize()
    22. {
    23. _context->setFormat(requestedFormat());
    24. _context->create();
    25.  
    26. _context->makeCurrent(this);
    27. _program = new QOpenGLShaderProgram(this);
    28. _program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
    29. _program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
    30. _program->link();
    31.  
    32. _program->bind();
    33.  
    34. _posAttr = _program->attributeLocation("posAttr");
    35. _colAttr = _program->attributeLocation("colAttr");
    36.  
    37. _program->release();
    38.  
    39.  
    40.  
    41. initializeOpenGLFunctions();
    42. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rainbowgoblin; 5th May 2014 at 00:07. Reason: Marked solved

Similar Threads

  1. example using Qwindow
    By PstdEr in forum Qt Programming
    Replies: 4
    Last Post: 14th June 2013, 13:31
  2. QglWidget: get depth buffer allowed values
    By papillon in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2011, 15:18
  3. other windows affecting depth buffer in QGLwidget
    By mullwaden in forum Qt Programming
    Replies: 0
    Last Post: 10th September 2008, 13:43
  4. Image 32 bit depth to 8 bit depth
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 2nd September 2008, 09:35
  5. Parenting QWindow...
    By roomie in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2006, 09:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.