Jump to content
  • Advertisement
Sign in to follow this  
bradbobak

OpenGL help me understand rendering a quad down the z-axis

This topic is 2155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've used the code from nehe's lesson2 to build a simple app that demonstrates the problem I'm having.
The code is original, except I've removed the gluPerspective line, made it non-fullscreen and changed the drawGLScene() routine.

Here is the new drawGLScene() :

int drawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0.0f, -0.5f);
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0.5f, 0.2f);
glVertex3f(0.5f, 0.5f, 0.2f);
glVertex3f(0.5f, -0.5f, 0.2f);
glVertex3f(-0.5f, -0.5f, 0.2f);
glEnd();
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0.5f, -0.2f);
glVertex3f(0.5f, 0.5f, -0.2f);
glVertex3f(0.5f, -0.5f, -0.2f);
glVertex3f(-0.5f, -0.5f, -0.2f);
glEnd();
if (GLWin.doubleBuffered)
{
glXSwapBuffers(GLWin.dpy, GLWin.win);
}
return True;
}


As you can see, I'm drawing 2 rectangles at the same position, except along the z-axis.
The first is colored white, and has its vectices for z at 0.2f. The second is red, and z is at -0.2f.
For some reason, the red quad appears in front of the white one. Seeing how opengl treats the negative z axis as going into the screen, I'm trying to understand why the white quad (z at 0.2f) isn't drawn in front of the red one.

Share this post


Link to post
Share on other sites
Advertisement
Because you probably didn't enable depth test. In that case OpenGL draws objects in the order you issue commands (like in 2D drawing).

Share this post


Link to post
Share on other sites
If you use lesson 2, you set glDepthFunc(GL_LEQUAL);, and therefore, lesser depth values will be drawn over higher depth values.

Share this post


Link to post
Share on other sites
Two points:

  1. The near and far clip plane values, as specified by the glOrtho or glFrustum (or equivalent functions) are along the negative Z-axis. That means a clip plane value of 5 means that the clip plane is located at z=-5.
  2. If you leave out the projection matrix, the default matrix is the identity matrix which is equivalent to glOrtho(-1, 1, -1, 1, 1, -1).


Pay attention to the default clip plane values; near=1 and far=-1. This means that the near clip plane is located at z=-1, and the far clip plane is located at z=1. This means that a quad at z=-0.2 is close to the near clip plane than a quad at z=0.2, and thus your red quad occludes your white quad. Edited by Brother Bob

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!