Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL depth buffer using QOpenGLFramebufferObject not working

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 :)

like image 803
matej.svejda Avatar asked Apr 17 '26 10:04

matej.svejda


1 Answers

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.

like image 69
derhass Avatar answered Apr 20 '26 05:04

derhass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!