[SOLVED] Depth Buffer Question

Started by
7 comments, last by Joran 18 years, 5 months ago
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]
Advertisement
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?
You also must enable writing to the depth buffer using glDepthMask( GL_TRUE ).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
You also must enable writing to the depth buffer using glDepthMask( GL_TRUE ).


This is enabled by default, though I suppose it might be worth a shot.
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.
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)
{
if(!h)
h=1;

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,w/h,0.0f,1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


Problem is i understand the gluLookAt function, but i don't seem to understand the last 3 parameters of the function.
gluLookAt(iwanttolookatx,iwanttolookaty,iwanttolookatz,
iamlookingfromx,iamlookingfromy,iamlookingfromz,
?,?,?)

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
The near clip plane in gluPerspective must be greater than zero.
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 opengl.org.
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.

Signed
Jorinski

This topic is closed to new replies.

Advertisement