Question

I want to write a function for rotating an object around its axis contemporaneously with passing the each angle around its x, y and z axis; I read here but I did not understand it completely. for example for rotating around my shape's axis, which parameters I must pass to the glTranslatef() and the How does the matrix which is multiplied to [0 1 0] (for calculating the new y axis) created?

I do not understand why when I call glRotatef() for several time, I just saw the first call in output; I tested gltranslatef and glPushMatrix and glPopMatrix befor and after my code; but none of them help me; it is very important for me to understand the solution of this topic.

this is my code but it does not work!

glPushMatrix();
glRotatef(90,1,0,0); // rotate around X Axis
glPopMatrix();

glPushMatrix();
glRotatef(90,0,1,0); // rotate around Y Axis
glPopMatrix();

glPushMatrix();
glRotatef(90,0,0,1); // rotate around Z Axis
glPopMatrix();

if you did this tell me your way.

Was it helpful?

Solution

Your above code really doesn't do anything that would be visible because you don't render anything after altering the currently active matrix. The effect of the following code segment is as follows (assume the current stack is MODELVIEW):

glPushMatrix();      // push the stack down one matrix and copy the current matrix
glRotatef(90,1,0,0); // multiply the current matrix M by a rotation matrix R -> M' = M * R

// usually you would render stuff here using the current model-view matrix

glPopMatrix();       // pop the top matrix off the stack

As you can see, by pushing a new matrix onto the stack, you preserve what ever matrix was on top before the push. Then you manipulate the current top matrix in some way, render stuff, and pop the top matrix off to restore the previous state.

I think you can see now why your above code does absolutely nothing of essence - except cause unnecessary overhead.

Using the matrix stack, if you insist on using legacy GL, is generally correct here, because you don't want your modifications affecting subsequently rendered geometry. However, what you really want is simple matrix concatenation. Assuming you want to perform and extrinsic rotation, i.e. a rotation about the fixed basis of world-space in z-y-x, you need to do the following:

glPushMatrix();
glRotatef(90,1,0,0);
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);

// render camera

glPopMatrix();

The above is mathematically equivalent to calculation a rotation matrix R by concatenating the individual rotations R_z, R_y and R_x: R' = R_x * R_y * R_z.

I suggest you also read this and to better understand what you're doing and potential problems with rotations in general.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top