View Full Version : Depth Problem

02-26-2003, 07:28 AM
Hi everyone

I am drawing three groups of objects (lines, triangles and rectangles, while the latter two being solid with their edge-lines drawn too) each in a glPushMatrix,glPopMatrix block and then looking at the entire image from different view-points with gluLookAt.

As far as the drawing is concerned, everything works fine but somehow the "hide/show" property of the objects is not correct. That is, since the rectangles and triangles are solid, I expect them to cover up (i.e. hide) whatever that falls behind them based on my view-point. But they are visible like hell!?!?!

For your added information I have the following commands in my StartUpRC routine (which is only called once at the beginning):

glDisable glcDepthTest
glEnable glcCullFace
glShadeModel smSmooth
glClearColor 1#, 1#, 1#, 1#

I appreciate your help very much.

02-26-2003, 09:27 AM
You should enable depth testing not disable it

02-26-2003, 09:42 AM
With thanks with your help,

I have tried both glEnable glcDepthTest and even commenting it out (i.e. keep it silent on DepthTest) but still it is not correct!?!?

Even the lines (i.e. edges) that are supposed to be hiden, are shown!

02-26-2003, 10:30 AM
What is your projection matrix?

02-26-2003, 10:51 AM
Haven't specified any! left it to whatever the default is.


02-26-2003, 11:09 AM
Bingo! That's your problem. You need to have a valid projection matrix before you can assume the depth values are calculated correctly.

02-26-2003, 11:15 AM
And thank you again!

But what is a "valid projection matrix"?

02-26-2003, 11:26 AM
But the default projection matrix (the identity matrix) is a valid projection matrix, so that shouldn't be a problem.

02-26-2003, 04:02 PM
I agree that identity is valid projection matrix. If I remember correctly, you are using Visual Basic??? If so, I assume you create GL context by specifying a pixel format same as I would in Visual C++. What is your cDepthBits of the PIXELFORMATDESCRIPTOR set at? If it is 0 then that could be your problem. Try setting it to 8 or 16. I think that 16 is the max, but it could be as high as 32 (I dunno). And, enable depth testing.

02-26-2003, 04:27 PM
With many thanks for your help, here is what I have in my pixelformat setup:
pfd.nVersion = 1
pfd.iPixelType = PFD_TYPE_RGBA
pfd.cColorBits = 24
pfd.cDepthBits = 16
pfd.iLayerType = PFD_MAIN_PLANE

which shows DepthBits=16!?!?

If it helps, I have the following in my SetupRC routine:

glEnable glcDepthTest
glDepthFunc dFLAG
glEnable glcCullFace
glShadeModel smSmooth

and I have tried it with dFLAG=GL_GREATER but it clears out my entire image and if I use GL_LESS (or comment it it) it draws such that the "far" comes before "near"?!?!

02-26-2003, 04:30 PM
The identity matrix is not a good projection matrix. It is nothing like a conventional orthognal or perspective projection.

02-26-2003, 04:52 PM
Let me clarify, the identity matrix is similar to an orthognal projection, but there is a significant difference. Look what the far and near planes would have be in order for -2/(far-near)=1. You can see, such a requirement would put the far plane closer than the near plane.

02-26-2003, 05:11 PM
I did try some tests setting my projection matrix to identity. All I saw was black. So, DFrey could be right.

However, before you add perspective or ortho commands, try switching your dFlag to GL_LEQUAL or GL_LESS. Basically, this says show the closest or the same whereas GL_GREATER says draw what is farthest.

Sorry didn't read you had tried that already.

[This message has been edited by shinpaughp (edited 02-26-2003).]

02-26-2003, 05:16 PM
Since you are using identity, glOrtho should give you similar results. If you have some idea what your bounds are for the screen ie(left side is -0.7, top side is 0.7, etc) use glOrthoto set the bounds and have near less than far.

02-26-2003, 05:42 PM
With many thanks I just noticed that when I use:
glLookAt -1,-1,-1,0,0,0,0,0,1
I get what I expect when using :
glLookAt 1,1,1,0,0,0,0,0,1
and vise versa!?!?

I thought maybe this would help solving my frustrating problem.

Thanks again!

02-26-2003, 07:09 PM
yeah DFrey is right. better to use GL_MODELVIEW or GL_PROJECTION.I think this will solve ur problem.

Originally posted by DFrey:
The identity matrix is not a good projection matrix. It is nothing like a conventional orthognal or perspective projection.

02-26-2003, 07:49 PM
Thank you all for the help.

As far as I know, ModelView is the DEFAULT and since I am not specifying any MatrixMode, it should be ModelView!?

But still, I did add the command:
glMatrixMode GL_ModelView

02-26-2003, 08:12 PM
You set a projection matrix by first setting the matrix mode GL_PROJECTION. Then use glOrtho or gluOrtho2D to setup an orthographic projection, glFrustum or gluPerspective to set a perspective projection. Then set your matrix mode back to GL_MODELVIEW.

02-26-2003, 08:38 PM
With thanks, should I be doing this at the start of OpenGL? How do I calculate/know the parameters for glOrtho?

(I have had very bad experience with glOrtho! whatever value that I'd tried, it just blanked out the entire picture!?!?!)

02-26-2003, 08:43 PM
I dunno. I'm running out of ideas. It could be that the projection mode will work, and I agree completely that it should be used. However, identity is equivalent to glOrtho with parameters unknown and should therefor work without a problem.

Do you clear the depth buffer with each pass of your rendering function such as glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
which should be at the start of each rendering frame.

If neither DFrey's advice for setting up orthographic or perspective projection using projection matrix mode nor clearing the depth buffer work, then you will have to post code if you want anyone to help you solve this problem.

02-26-2003, 09:02 PM
If you know the vertices of your object, basically the min and max of x, y, and z values for all. If you wanted to see all of it at the same time, your parameters to glOrtho should be less than the min and more than the max. But this should be done as


Make sure to maintain aspect ratio of window in the glOrtho command or it will appear distorted such that (xright - xleft)/(ytop - ybottom) = winwidth/winheight. Anything outside of the "box" formed by the glOrtho command will not be visible (it will be clipped).

Thats all you need to do. But, again, also verify that you are clearing the depth buffer, though if it is a static (non animated) rendering, it probably wouldn't have any effect.

You should probably check out some of the tutorials on other sites which may give you more insight into OpenGL programming. I don't think any are VB specific but you should be able to translate it into VB quite easily.

02-27-2003, 03:43 AM
I thank all of you from bottom of my heart specially shinpaughp for your kind patience and help. IT WORKED!!

Just for your information, I added all the lines that shinpaughp had suggested, except the ViewPort, to the end of StartUpRC routine (as I had the ViewPort routine called in my Window-Resize sub.) and it works just fine.

I thank you all again.