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

[OGL] Understanding Depth

Started by Boder Sep 12, 2006 at 7:04 PM 5 replies 800+ views
Original Post
Boder
Boder
Where is my logic wrong? near plane = -2 far plane = 2 clear depth = farthest away = 2 Now I want quads with smaller z values to appear in front, so i choose GL_LESS. ""Passes if the incoming z value is less than the stored z value."" But the quad at z=1 appears in front of the quad with z=0!
glClearDepth( 2.0f );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -15.0f, 115.0f, 0.0f, 100.0f, -2.0f, 2.0f );

glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
swordfish
swordfish
As far as I know, your near-plane must be >0.
http://blog.protonovus.com/
Boder
Boder
<0 is okay for an ortho projection
multisample
multisample
This is just a guess, and I am not an OpenGL expert, but ...

Depth clearing values usually range from 0-1.0, as they are not world depth values, but zbuffer values. Its probably being clamped to 1.0.

Second, if you haven't rotated the view any (can't be sure since all we see is projection code) ...

I believe the default OpenGL view is right-handed looking down the negative 'z' axis. That means higher values of 'z' are "less than" the lower values in the default state. When you supply values for glOrtho, they are not world z values, its relative to the current view matrix.

So lets say your camera is at 0,0,0, with a near plane at 1 and far at 10. Since we are looking down the negatize z... Objects at z=-1 will map to a zbuffer value of 0, and items at z=-10 will map to a zbuffer value of 1.0 .

If you rotate your view 180 degrees about the 'y' axis, you will be looking down the positive 'z' axis (note: positive 'x' will be to the left) and everything should work. Or you can just change your z values for drawing to account for the negatize z axis view (generally easier for simple things).

Hope that helps.
Boder
Boder
Thanks, you were right about the clamping and the handedness issue.

Right handed seems strange, I just naturally thought of positive Z going away from me.

By setting the near plane positive and the far plane negative, I flipped it around.
Boder
Boder
Damn, that flipped my normals. I'm just going to get used to a right handed system.

Very strange thing about glOrtho is that zNear and zFar are actually distances to the near and far clipping planes and the inverse of the actual Z-coord.

[Edited by - Boder on September 13, 2006 2:58:30 AM]
multisample
multisample

Personally I would probably only ever use positive values for near and far. Its easier to think of all of this in terms of a virtual camera placed in space with the near and far plane in front of that camera as planes. Try and visual the camera placed at a point in space (like 0,0,0) with a near and far plane in front of it. How would you place something between those planes so that it would be visible ? What would there coordinates be ?

A positive near and far plane will be directly in front of the camera always, with the near plane just telling the camera where it starts rendering, and the far being the cutoff point. Since OpenGL is right handed, if positive-x is right, and positive-y is up, then negative-z has to be pointing in the same direction the camera is looking. That means you would need to place items in the world at negative z locations (between the near and far plane) to be visible.

Obviously, you can change the camera direction, but this is the default. If you have to, draw it out in 2d space on graph paper by removing the up or y axis and just graphing x and z directions. Its extremely important to understand the space your in and how your camera maps to that space before going any further.


Topic Locked

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

Sign in to reply to this topic.