Question

I set the format with:

QGLFormat format = QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer);
setFormat(format);

in the constructor.

Then in initializeGL I set depthTesting on.

void VoxelEditor::initializeGL()
{
    glClearDepth(2000.0);                                   // Enables Clearing Of The Depth Buffer
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glDepthFunc(GL_LESS);                               // The Type Of Depth Test To Do
    glShadeModel(GL_SMOOTH);                            // Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations
}

In paintGL I clear the depth buffer.

void VoxelEditor::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    draw();
}

I remember it used to work with less vertices, so it might be that I'm using too many for the depthbuffer to handle(?). I have 32*32*32 voxels which are drawn half of most of the time, so 98304 quads.

Depth testing however still does not work and shows the quads in order of execution.

Was it helpful?

Solution

so it might be that I'm using too many for the depthbuffer to handle(?).

The depth buffer is oblivious to vertices. All it sees are incoming fragments and it doesn't matter how many.

void VoxelEditor::initializeGL()
{
    glClearDepth(2000.0);   // Enables Clearing Of The Depth Buffer

This line does not enable clearing. It set's the value the depth buffer is cleared to. The value must be in the range 0…1. The clearing depth is in Normalized Device Coordinates, i.e. after modelview, projection and homogenous divide have been applied. The default value is 1.

    glEnable(GL_DEPTH_TEST);  // Enables Depth Testing
    glDepthFunc(GL_LESS);     // The Type Of Depth Test To Do
    glShadeModel(GL_SMOOTH)  // Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations

No, that's not what is does. Perspective is just a linear transformation and always works the same. What it means is, that texture coordinates may be interpolated in a different way to enhance quality.

}

I always recommend putting those calls in the drawing functions, because they don't initialize anything. They set drawing state. OpenGL is a state machine and a important rule of state machines is, that either you keep track of their state or you must put them into a known state whenever you're going to use it.

OTHER TIPS

I fixed this by setting setDepthTest in draw().

glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top