glTranslatef() not working

OK, so I’m going through Nehe’s tutorial, and the second lesson doesn’t work. It seems to be the glTranslate() function that doesn’t work. It doesn’t properly translate in the z-axis, so I just get a black screen. If I set the z-translation to zero, I can see the two object up close, and properly translated in the x-axis. I’m not really sure what’s wrong, and I’m using Windows with VC++ if that matters. Here’s the code that should work:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();									// Reset The Current Modelview Matrix
		glVertex3f( 0.0f, 1.0f, 0.0f);	
		glVertex3f(-1.0f,-1.0f, 0.0f);	
		glVertex3f( 1.0f,-1.0f, 0.0f);
		glVertex3f(-1.0f, 1.0f, 0.0f);	
		glVertex3f( 1.0f, 1.0f, 0.0f);		
		glVertex3f( 1.0f,-1.0f, 0.0f);	
		glVertex3f(-1.0f,-1.0f, 0.0f);		

Well, simple mistake: positive Z points into the screen.

And since the camera is located at 0,0,0 by default, that means that the objects are rendered “behind the viewer”.

In other words: try a positive Z instead of a negative Z. That’s all you need to know for this tutorial.

No, the positive Z, by default at least, points out of the screen, Negative Z is into the screen, so unless he have changed the default orientation, he’s translating in the correct direction.

Post some more code, at least everything that has something to do with the projection and modelview matrix. That code alone is not enough to locate the problem without guessing.

If the code is identical to the example, there’s a gluPerspective in there:


Now call me crazy, but that sounds like it will make higher z values end up deeper into the screen.

OK, so that does not mean that Z is always into the screen. But it is always into the screen when you use glOrtho or gluPerspective in the “usual” way (i.e. with zNear < zFar).

Nope, it just sets the near and far clipping planes for the view frustum. Bob’s right. Check out the red book. The reason you sometimes can see this the wrong way (oposite actually) is if you set up your camera with gluLookAt looking at the wrong direction. But by default z increases as we go from the screen to the user’s eyes.

Yeah this get confusing when you don’t distinguish between screen-space z’s and the camera’s frame vectors (eye-space z). The GL keeps a negative z-axis for forward so the coordinate system remains right handed (unlike the other API). I like the way the GL does this.