PDA

View Full Version : GL_DEPTH_TEST -> What am I doing wrong?



Aaron McConnell
05-15-2008, 09:49 AM
Just learning OpenGL, so I'm playing with simple programs to get the hang of things. I'm experimenting with 3d and I swear the following code should result with the red square under the blue square, but when I run it, the red's always above (because it is drawn later) even though it should be hidden by the depth testing. What's the problem?
Relevant code (in its entirety, no camera tricks or anything)
glClearColor(0.0, 0.0, 0.0, 1.0);
glOrtho(-3.0, 3.0, -3.0, 3.0, -20.0, 20.0);

glEnable(GL_DEPTH_TEST);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
/* FRONT , blue */
glColor3f(0.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
/* BACK , red */
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.25, -1.25, -1.25);
glVertex3f(1.25, 0.75, -1.25);
glVertex3f(-0.75, 0.75, -1.25);
glVertex3f(-0.75, -1.25, -1.25);
glEnd();
glFlush();

dletozeun
05-15-2008, 10:12 AM
glOrtho(-3.0, 3.0, -3.0, 3.0, -20.0, 20.0);


why do you put the near plane behind the eye? Try something like:

glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 20.0);

Aaron McConnell
05-15-2008, 10:56 AM
Even if I bring the planes in very tight

glOrtho(-3.0, 3.0, -3.0, 3.0, 0.9, 1.26);

red is still on top.

Bob
05-15-2008, 11:04 AM
Check the pixel format if you have a depth buffer at all.

Aaron McConnell
05-15-2008, 11:50 AM
You were right on target. The fault, Bob, lay not in my GL, but in my GDK. When I made the window, I didn't bother to ask for a depth buffer.

There are times when I've felt like a bigger idiot, but not many...