PDA

View Full Version : glOrtho and depth buffer



fortikur
04-11-2005, 03:39 AM
I want to draw a 3d model and a texture mapped quad in 3d space. The model is transformed according to glTranslate and glRotate data. The quad will be displayed using glOrtho, because it will play the role of a hanging poster. The only problem is, that even if I enable depth test, the poster will always hide the model, even if the model is between the viewer and the poster.
The steps of drawing are the following:
1. Set up the camera:
gluPerspective( GLOBAL_CAM.fov, 1.3, 2.0, 1500.0 );
gluLookAt(GLOBAL_CAM.x, GLOBAL_CAM.y, GLOBAL_CAM.z,
GLOBAL_CAM.target_x, GLOBAL_CAM.target_y, GLOBAL_CAM.target_z,
0,1,0);

2. Draw the poster:
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(0,screen_w,screen_h,0, -1000,1000);

glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();

glEnable(GL_DEPTH_TEST); //needed (?) to use depth
glDisable(GL_LIGHTING);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);

glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0, 1);
//z should be the distance between the viewer and the poster (z=1000). The model is about 200-300 units from the cam away
glVertex3d(x1, y1, z);
glTexCoord2f(0, 0);
glVertex3d(x1, y2, z);
glTexCoord2f(1, 0);
glVertex3d( x2, y2, z);
glTexCoord2f(1, 1);
glVertex3d( x2, y1, z);
glEnd();

glEnable(GL_LIGHTING);
glDisable(GL_BLEND);

glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

3. Draw the model (no ortho used.)

How is it possible to use ortho as a 'normal' depth sorted 3d object?

Thank you in advance

wolf_gfx
04-12-2005, 06:00 AM
Hello,

Don't disable depth testing. If you use an orthographical projection that doesn't mean that you no longer need perform depth tests.

Wolf.

fortikur
04-12-2005, 06:36 AM
I haven't disabled depth test. At least, I don't see, where I did it... :)
I have found the problem's root, but it's more than strange for me. If anyone could explain it, why it works like this, I would be very pleased...

Imagine the above code, but I changed ortho like this:
glOrtho(0,en.screen_w,en.screen_h,0, 0,1000);
The z value of the poster is -340 (!). if I set Near to 0 and Far to 1000, it shouldn't be visible, but it is there. If I set it to 340, it disappears. But 340 is between 0 and 1000.
I don't understand.

charliejay
04-14-2005, 03:58 AM
Not sure on this, but here's a guess.

I think the values for z near and z far are in terms of the projection/camera's (which specifically is one of the things i'm not sure of) z axis.

Also, with a 3D projection (this might apply to ortho too, I can see no reason why it wouldn't in fact, but again, i'm not sure), you look down the negative Z axis, by default.

So, if the projection/camera's z axis has a negative Z component, and you're looking down the negative z axis, then a positive value of zfar is going to produce a negative vaule in world space coordinates, which, if your vertices are also in world space coordinates, will result in negative vertex z values being visible (all else being equal).

Sorry this reply is a bit convoluted... :-)

CJ