0
votes

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.

1
Could you please describe your question better, I don't seem to understand what your point is in the question.vallentin
There is nothing like "the axis". By convention, geometry is expected to be defined in object-space. This space has a set of three base vectors. If you don't specify either a fixed axis yourself or specify temporary rotation axis, the base is the only thing you have. Can you please post some code? Your question is mighty convoluted and confusing.thokra
yeah! I want to draw a camera shape in openGL screen, the user can determine the position of camera and the angle of it around x,y,z axis; I want to pass this 6 parameters to this function, but using the glRotatef() function I can not change the 3 angle about the axis simultaneously; every time I just saw effect of one of this;Golnaz Saraji

1 Answers

0
votes

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.