Skip to main content
GameDev.net gamedev.net
🔒 Locked

Depth Buffer Bug is 'bugging' me...

Started by OpenGL_Guru Mar 22, 2005 at 10:20 AM 3 replies 1k views
Original Post
OpenGL_Guru
OpenGL_Guru
let me explain my problem then give you a little code... my problem originally start when drawing my terrain as a sphere and the min and max was about a range from -6000 to 6000. i had to lower the difference in my gluPerspective call so i wouldnt get Z-fighting(i am using gluLookAt instead of glTranslatef). so originally my call was: gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,20000.0f); now the call in the reshape function is: gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1000.0f,20000.0f); this solved my depth buffer problem for the data, but this now brings up a new problem. i have things drawn on the screen like a legend and some text drawn in ortho mode. using the new gluPerspective call these dont 'show' up. so what i decided to do was in my class where i draw the ortho stuff(text, 2D legend etc) is to add the code for the original gluPerspective calls:


//**********in class that draws all the 2D ortho stuff************

glPushMatrix();
    glClear(GL_DEPTH_BUFFER_BIT);
    glViewport(0,0,w,h);						// Reset The Current Viewport
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix
	setWidthHeight(w, h);
	gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,20000.0f);
    glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();	

 //***********draw_2D stuff here***************

...........
...........

glPopMatrix();


...... ...... //draw stuff here glPopMatrix(); to make sure that my terrain back in the main drawscene shows up before i draw the terrain i set the gluPerspective back to the new change as is here:


//********main drawScene*************

//*******object->class(draw2D stuff) -- this is where the code is called from above..*******

glClear(GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix
	gluPerspective(45.0f,(GLfloat)1280/(GLfloat)1024, 1000.0f,20000.0f);
    glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();	

	gluLookAt(x, y, z, 0.0, 0.0, 500000.0, 0.0, 1.0, 0.0);  //cam pos, eye, up
	glRotatef(rotationY, 0.0, -1.0, 0.0);
	glRotatef(rotationX, -1.0, 0.0, 0.0);

//draw the terrain next****************






ok so now this works i say to myself, i run it and behold i can see my 2D stuff in ortho and my terrain but i notice another problem. somehow the depth buffer has been 'corrupted'? the 2D stuff should be in front of the terrain, but actually it looks like everything is being drawn backwards.. i.e. the terrain(3D) covers the 2D stuff when it gets moved closer to the screen... so do you have any other suggestions? did i take the wrong approach from the very beginning when changing the gluPerspective call for the 2D stuff? i would appreciate your input.. thanks!
heh
daireq
daireq
Quote:
Original post by OpenGL_Guru

so originally my call was: gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,20000.0f);


yikes ! I know that you're not actually using this anymore for your
perspective calls, but that would absolutely result in a whole PILE of depth buffer aliasing. Its the ratio between the far and near values that causes the aliasing, not the actual values themselves.

as an ACTUAL answer to your query, the common way to do this is to use glOrtho for the 2D stuff, not gluPerspective. I don't know what depth position you're drawing your 2D stuff at, but don't forget that whatever values you select in the near/far part of the call will always be transformed to 0-1 normalised depth coordinates. So for example when you draw a pile of stuff at (say) 500 with a near/far of 1/1000 then they'll be drawn at (approx) 0.5. If you then go and draw stuff at 1000 with near far values of 9000/11000 then they will ALSO be drawn at 0.5 z values. That -could- be your problem right there.

As I was saying above, the best way to do this is to use GLOrtho.
I use the following for my GUI stuff:

gl.glOrtho( 0,width,height, 0, 0, 1 );

with near and far set to 0/1 and then draw everything at 0. Does the job without any clipping of geometry against the GUI.
Alternatively you could draw your 3D stuff then clear the depth buffer and draw your 2D stuff on top. It wouldn't matter where it was in relation to the 3D stuff then.

D.
kburkhart84
kburkhart84
I'm pretty sure it is the huge depth requirement. Honestly, what do you need 20,000 units in the distance for. In real life the human eyes can't see near that far. The extreme for the far value is usually 1000 as far as I have used and it doesn't cause any Z-fighting. The Z-near value as lone as it isn't 0 should work fine as 0.1 or 1. As stated above, OpenGL turns the Z values into between 0 and 1 but even the double variable type only has 14 digits of precision, I'm not sure if OpenGL uses float or doubles internally. I recommend you use 0.1 to 1000.


OpenGL_Guru
OpenGL_Guru
well my terrain is turned in a sphere , like the earth. the raduis of the earth is what like -6 million to +6 million? something like that. i divided my 1000 so now my range goes from -6000 to 6000. this is why i need 20,000 cos otherwise you would move the camera forward or backward more than once and then the clipping plane would interfere with the actual terrain. i hope all that makes sense, i am trying not to overcomplicate things. i will try disabling the depth test then re-enabling it afterwards, thanks for the tips --

about the 2D stuff --

i DO indeed drawing my 2D stuff in ortho mode by the ortho code off of lighthouse site that i got and then i swap back when i am done. for some reason i am not allowed to edit any of my post, it just times out so i am unable to change/add/edit my post...
heh

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.