3

I have a problem where the depth of an OpenGL scene is not rendered correctly. Im doing off-screen rendering into a QOpenGLFramebufferObject. If I run the same code in a QGLWidget it renders fine. Here is the code:

// SETUP
SurfaceFormat format;
QWindow window;
window.setSurfaceType(QWindow::OpenGLSurface);
window.setFormat(format);
window.create();

QOpenGLContext context;
context.setFormat(format);
if (!context.create()) {
    qFatal("Cannot create the requested OpenGL context!");
}

QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QOpenGLFramebufferObject::Depth);
QSize drawRectSize(640, 480);

context.makeCurrent(&window);

QOpenGLFramebufferObject fbo(drawRectSize, fboFormat);
fbo.bind();

// OPENGL CODE
glViewport(0, 0, 640, 480);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

glShadeModel(GL_FLAT);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixf(pose->getOpenGLModelView());

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* calculate prjection parameters */
gluPerspective(fov, aspect, 0.01, 1.0f);

glColor3d(1, 0, 0);
glBegin(GL_TRIANGLES);

/* Draw triangles */

glEnd();

glFlush();

QImage result = fbo.toImage());

Any ideas what im doing wrong? I checked GL_DEPTH_BITS and it seems to be 0. Thanks in advance :)

1 Answer 1

1

In your code, you never request a depth attachment for your FBO. All you seem to do is

QOpenGLFramebufferObjectFormat fboFormat;
QOpenGLFramebufferObject fbo(drawRectSize, fboFormat);

According to the Qt docs, the QOpenGLFramebufferObjectFormat constructur will just do the following:

By default the format specifies a non-multisample framebuffer object with no attachments, texture target GL_TEXTURE_2D, and internal format GL_RGBA8. On OpenGL/ES systems, the default internal format is GL_RGBA.

Have a look at the Qt Documentation on how to set the correct format.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah thank you, I forgot to set: fboFormat.setAttachment(QOpenGLFramebufferObject::Depth); Changed it in my original post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.