Sign in to follow this  

[SOLVED] Depth Buffer Question

Recommended Posts

Hey, my name is Jöran. I have been learning opengl but there are somethings i do not seem to understand yet. I hoped you guys and ofcourse girls could give me an answer to a question i have. I have a code that looks something like this. void Render(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f,0.0f,-5.0f); glPushMatrix(); glTranslatef(1.25f,0.0f,0.0f); DrawWheel(); glPopMatrix(); glPushMatrix(); glTranslatef(-1.25f,0.0f,0.0f); DrawWheel(); glPopMatrix(); glPushMatrix(); glTranslatef(-1.25f,0.0f,-2.0f); DrawWheel(); glPopMatrix(); } The function DrawWheel is just somecode that draws a thick wheel of quads. Doesnt touch the z-axis or doesnt do translations. The problem is that somehow the last wheel, the third one comes in front of the second one but i dont understand how. Shouldnt suppose the depthbuffer test this. My depthbuffer settings are. glDepthTest(1.0); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTHTEST); if i change it to glDepthFunc(GL_GEQUEL) i dont see anything. if i change my glDepthTest(-1.0f) suddenly it draw the same as before. I am completly lost here and i need some help. Thx, for the people who want to help me. [Edited by - Joran on October 24, 2005 6:25:31 AM]

Share this post

Link to post
Share on other sites
Your depth buffer settings are all right, (except I'd probably set the depth test function to GL_LESS instead of GL_LEQUAL).

But how do you mean the third one appears in front? Looking at the code, there seems to be now way of telling the three wheels apart, as they're drawn in the exact same way. Are the polygons somehow overlapping, or does it look like there is something wrong with the depth testing?

Share this post

Link to post
Share on other sites
I agree with James that atleast for debugging purposes you should have some way to differentiate the wheels (i.e. allow DrawWheel() to take in a color parameter). It may not be a true error but instead just a conflict of what you expect to see on the screen and what openGL actually draws. A difference like coloring can allow you to clearly see what each object is doing and where it is placed.

Aside from that there are many other things that could be causing this problem. Some things to check:
For any reason are you clearing the depth buffer in the wheel drawing function?
Where is you camera pointing from? Is it possible your looking from the vantage point of the -Z axis instead of the +Z axis? (This can happen if you rotate your world coords [i.e. rotating around the Y axis 180 degrees])
Does all your glPushMatrix/glPopMatrix and glBegin/glEnd calls pair up correctly? If those do not pair up correctly that can cause tons of obscure errors. As a habit I treat all glPushMatrix/glPopMatrix and glBegin/glEnd pairs the same as brackets and indent lines inbetween them for greater readabilty.

Something else to try is rotating your seen around the Y axis 180 degrees (or any other angle to see different views) so that you can see your image from the back. If your third wheel apears in front in both views (front and back) then your looking at some sort of depth error. If it appears in front in one view and in back in the other then it may be simply a matter of misunderstanding where you camera is relative to your scene.

Clearly there are many other things that can cause this problem, but those are always good things to do/check when ever you first encounter a wierd bug related to what your drawing.

Hope that helps you some. If you try those things post back with your results and that may make it easier for others to further diagnose your problem.

Share this post

Link to post
Share on other sites
Guys, thanks for the possible solutions on this problem.

I really appreciate it, but i have one more question before i can go testing
your theories. Suppose that indeed i am looking at the wrong site of the scene let's say from the back. How can i make sure the camera looks at the wrong coordinates. I understand gluLookAt is normally the function that provides the
wright camera position but how do i make this happen. I have checked and rechecked glbegin/glend and they are all perfectly opened and closed. The only thing that i have on is the GL_CCW, this is to make sure i have all primitives drawing in the same order. GL_CCW is enabled in my drawwheel function, my viewpoint is normally untouched.

Code looks like this.

void Reshape(float w,float h)


Problem is i understand the gluLookAt function, but i don't seem to understand the last 3 parameters of the function.

If you guys could help me with this i can finaly begin to test the possible scenarios. But what i dont have enabled is my glDeptMask(GL_TRUE)
Thx, again for your help

Share this post

Link to post
Share on other sites
The three last parameters describe the up vector, which should be self-explanatory.

The MSDN says:
"The direction described by the up vector projected onto the viewing plane is mapped to the positive y-axis so that it points upward in the viewport."

In most cases the up vector would be set to (0.0f, 1.0f, 0.0f), which has the positive y-axis going upwards.

Here's a FAQ about gluLookAt on

Share this post

Link to post
Share on other sites
Guys, i thank you a lot, the problem is fixed,
somehow i dont know the glDepthMask was not enabled.
I tryed the statement and suddenly it worked, i rotated the
camera to test just to be sure and it also looked fine,
thanks a lot for the effort and time to help me.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this