glTranslatef not working

I’m new to OpenGL programming, and I was trying to do a GLUT tutorial from NeHe.gamedev.org, and this is what happened. Everything worked fine, except that I was supposed to translate back a bit so I could see the objects:

glTranslatef(0.0f, 0.0f, -6.0f);

Now when I tried to do this, I got a black screen. :eek: When I tried adjusting the z value to 0, all of a sudden I could see everything. Then I tried adjusting some more, and I found out that the objects do not move until past +1 and -1. When the value is beyond this range, the screen goes black. Any sugestions?

Sounds like your projection matrix could be messed up.

What values are you passing to gluPerspective() [I presume that is the function NeHe uses for setting up the projection matrix]

This program is really simple. It doesn’t have a projection matrix. You can view the source if you want.

Then the problem is that you dont set up your projection matrix.

What do you expect to happen when you move your triangles away from the screen?

They wont get any smaller unless you describe a projection matrix.

What is currently happening is that your projection matrix is a -1 to 1 in x y and z. So when you move things outside this volume they are clipped, hence the blank screen.

The reason why nothing appears to move within -1 to 1 is that your projection matrix is identity, hence has no perspective divide to make things smaller as they move away in z. If you try setting the x and y params of glTranslate you will see some movement.

Also before you do any matrix calls (glTranslate etc) you should set the matrix which you want to operate on.

Try adding the following to your code…

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f, (float)resolutionX / (float)resolutionY, 0.1f, 100.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
// call glTranslate here

Oh. lol.

Why didn’t NeHe put that in the instructions?