nebuch
nebuch

Reputation: 7055

Opengl failing to depth test properly

I'm was working on a really simple voxel like game, but for some reason OpenGL isn't depth testing properly.

Here's the relevant code:

//Render function:
void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(2.5, 2.5, -5.0);
    glColor3f(1.0, 0.5, 0.2);

    glBegin(GL_QUADS);
    int i, j, k;
    for (i=0; i<4; i++) {
        for (j=0; j<4; j++) {
            for (k=0; k<4; k++) {
                glPushMatrix();
                glTranslatef(-(float)i, -(float)j, -(float)k);
                glutSolidSphere(0.2, 32, 32);
                glPopMatrix();
            }
        }
    }
    glEnd();

    glutSwapBuffers();
}

//Set the initial state of OpenGL:
void initGL() {
    glEnable(GL_DEPTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_CULL_FACE);

    glClearColor(0.0, 0.0, 0.0, 1.0);
}

Made sure to include this is window initilization:

glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

Here's a screencap:

The result of rendering this code.

I've tried disabling lighting features, but then it's difficult to tell what's on top of what anyway. I've tried disabling culling, nothing except the spheres are rendered inside out, which makes me think that maybe the problem is in the glutSolidSphere function and not my code. I'm using OpenGL 2.3.

Upvotes: 0

Views: 338

Answers (1)

datenwolf
datenwolf

Reputation: 162164

It's called

glEnable(GL_DEPTH_TEST);

Not just GL_DEPTH

Upvotes: 4

Related Questions