Jump to content

View more

Image of the Day

WIP title screen for #DeathOfAPartisan #screenshotsaturday #gamedev https://t.co/qJNhfZCvd4
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

help me understand rendering a quad down the z-axis

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1 bradbobak   Members   

1803
Like
0Likes
Like

Posted 26 November 2012 - 12:10 AM

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.

#2 Aks9   Members   

1488
Like
0Likes
Like

Posted 26 November 2012 - 01:44 AM

Because you probably didn't enable depth test. In that case OpenGL draws objects in the order you issue commands (like in 2D drawing).

#3 bradbobak   Members   

1803
Like
0Likes
Like

Posted 26 November 2012 - 02:00 AM

Depth testing is enabled. Even if I switch the drawing order, the red quad is still up front.

#4 Olof Hedman   Members   

5951
Like
0Likes
Like

Posted 26 November 2012 - 04:02 AM

If you use lesson 2, you set glDepthFunc(GL_LEQUAL);, and therefore, lesser depth values will be drawn over higher depth values.

#5 Brother Bob   Moderators   

10280
Like
0Likes
Like

Posted 26 November 2012 - 04:06 AM

Two points:
  • 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.
  • 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, 26 November 2012 - 04:07 AM.


#6 bradbobak   Members   

1803
Like
0Likes
Like

Posted 26 November 2012 - 12:59 PM

Ok. I get the idea. Thanks for the help.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.