# Working with GlTranslate and glRotatef

Dear All,
I am beginner to Opengl.I am always confused with glRotatef and glTranslatef.Is their any specific way by which we assign translation and rotational coordinates to glTranslate and glRotatef before drawing.Whenever I am assiging coordinate it usually somewhere else.How to solve this problem

RAJESH.R

glRotatef and glTranslatef are moving your scene around, so if you want to move the camera, you should inverse your values… f.e:

glTranslatef(0.0,0.0,5.0) moves the scene 5 along the Z-axis.

other example:
you wish to move your camera 7 units along the x axis:
glTranslatef(-7.0,0,0); (notice the negative value for a positive cameramovement)

you have to do the same with the values for rotatef: if you rotate the camera, take the negative value.

You have to be carefull with rotations tough. Rotating works ok for the first time, but if you start combining rotations, the sequence gets very important, if you don’t understand what you’re doing you’ll probably mess it up with rotation-combinations.

f.e.:
glRotatef(70,1.0,0.0,0.0) rotates the camera -70 degr. around the x-axis

Hi !

Mikael

If you think about it using these three rules you will not be confused:

1. glRotate rotates the coordinate system around an axis at the origin in the current coordinate system.
2. glTranslate translates the coordinate system.
3. Transformations are cumulative (except when push and pop are used).

So…

glTranslate before glRotate will translate the current coordinate system, then rotate around the axis at the translated origin.

glRotate before glTranslate will rotate the current coordinate system, then translate in the rotated coordinate system.

glRotate followed by glRotate will rotate the current coordinate system, then rotate the coordinate system again around an axis rotated by the first glRotate.

if you don’t want to learn linear algebra standing behind all that stuff, just note 2 things:

1st - as mentioned above if you want to transform your camera’s position you should instead transform your world by negative transformations

2nd - transformations are applied in order from “drawing code” up to the first transformation function call (the first will be last and the last will be the first