Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt & OpenGL - QGLWidget & QPainter: depth buffering

I subclass QGLWidget and have my painting code in paintEvent instead of paintGL as I want to paint a 2D overlay using QPainter over my 3D stuff done with OpenGL.

My depth buffering works fine when I don't have an overlay. If the overlay is painted, my depth buffer goes AWOL: I can see stuff that should be hidden by objects in front.

initializeGL looks like this:

qglClearColor(Qt::black);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

The structure of my paintEvent code is as follows:

makeCurrent();

...openGLStuff...

if (I need my overlay)
{
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();

    QPainter painter(this);

    ... do QPainter stuff ...

    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
}

swapBuffers();

Depending on the if, the same scene looks alright (overlay off) or wrong (overlay on). Apart from the weird depth buffer problem, it works perfectly well.

My (wild) guess is that construction of the QPainter disables depth buffering. Any hint would be greatly appreciated. I suppose a fallback solution would be to render my overlay into a texture and have OpenGL blend it in.

like image 464
user816098 Avatar asked Jul 15 '11 15:07

user816098


1 Answers

Why don't you just enable and disable depth testing as needed? You don't "initialize" OpenGL, it's a state machine. Those "initializing" statements belong in your drawing code, into the context they where they are needed.

like image 78
datenwolf Avatar answered Sep 27 '22 23:09

datenwolf