PDA

View Full Version : glTranslatef not working



Joe the Programmer
01-28-2006, 02:46 PM
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?

killerseven
01-28-2006, 04:39 PM
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]

Joe the Programmer
01-29-2006, 03:29 PM
This program is really simple. It doesn't have a projection matrix. You can view the source (http://www.myfilestash.com/userfiles/johnhatesmicrosoft/main.c) if you want.

killerseven
01-29-2006, 03:43 PM
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

Joe the Programmer
01-30-2006, 02:27 PM
Oh. lol.

Why didn't NeHe put that in the instructions?