Z Buffer :/

Hi All!

Ok, so I just started out with OpenGL and (suprisingly) got it pretty much working right off the bat with the help of a few tutorials and such.

Anyway, what I’ve done so far is load in an OBJ file exported from 3DSMax with a few meshes inside, loaded the meshes, and rendered them, and rotated the scene around a bit. This worked fine, no probs… except that everything was one color and I couldn’t see anything but silhouettes…

Now I’ve added in some light to better see everything, and I’m noticing that I can VERY clearly see objects being drawn that are behind other objects. For example, seeing the innards of that teapot… seeing a sphere behind a cube… stuff like that…

What am I doing wrong as far as the ZBuffer goes? I’m writing this in GLUT and the only few mentions I have about the Depth buffer are the following:

///////////////
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
///////////////

Is there anything else I need to do as far as the zbuffer is concerned to stop having it draw objects that I should not be able to see?

Thank you!!

Raetin

The problem is most likly with your gluPerspective or glOrtho or whatever call that you use to set up the view frustrum.

The Z buffer is not linear but rather exponential when it’s determining percission at certain depths. So up close it’s super percise and far away it’s bad.

The way to fix this is to put you’re near view plane as far away from you as possible and you’re far plane as near to you as possible.

I#ve got a similar problem some time ago. I used something like “0.5f” as the near clipping plane (i.e. using GluPerspective). Try greater values like “1.5f” or so.

That is not entirely correct, I have the following gluPerspective() call and I have no problems, even with huge models.

gluPerspective (45.0,1.0,0.2,102.2);

Rizo

Hmm … I tried my code in another simple OpenGL prog I made, and it seems the z buffer works fine…

See, I made two versions (I need OpenGL in a window)… one in a Win32 window, and one using GLUT.

The Win32 one works fine when loading the models and all … just need to play with the light a bit to see the differences

But on the GLUT one I can see the polygons bleeding through each other, and I’m not sure how to fix this …
My best guess is that during the initialization I set the Z buffer to 16 bit in the pixel format descriptor …

In GLUT I have no idea what it is nor how to change it.

Apart from the window creation, the initialization (glPerspective, etc) looks identical … :stuck_out_tongue:

Anyone have a thought on this?

What parameters are you using for gluPerspective()? The problem is most certainly the two last parameter, you have them too far away from each other.

did you pay attention to your cull settings ? They may be in reserve order of your vertices order.

My glPerspective is as such:
gluPerspective(60.0f,(GLfloat)width/(GLfloat)height,20.0f,500.0f);

Now this is in the GLUT version, in the Win32 one the last parameter is 1000.0f and I have no problem with that one (no triangle bleeding or anything)…

Also … a.f.a. the cull settings are concerned… cough … I have no idea?