Model View Matrix
Whenever you do a glRotatef, the model view matrix changes based on the transformation ryt? I was wondering if there was a way to rotate an object while not changing the model view matrix. What i want to do is rotate the matrix by the xangle, and then rotate it again by the original yangle and not the "transformed" model view matrix. Then i draw the object. Any ideas?
By rotating around any axis you are changing the coordinate system. Your description wasnt clear what rotation you are looking for, but i am taking a guess.
The 1st rotation rotates your object *AND* the y as well as the z-axis. To compensate you have to change the world y-axis to the rotated one. This can be easily done by appling the 1st rotation on the y-axis unit vector (0.0,1.0,0.0). The result of this rotation is the former y-axis of your rotated object. Use the rotated y-axis as axis parameter for glRotate and the result should be what you are looking for.
Michael
The 1st rotation rotates your object *AND* the y as well as the z-axis. To compensate you have to change the world y-axis to the rotated one. This can be easily done by appling the 1st rotation on the y-axis unit vector (0.0,1.0,0.0). The result of this rotation is the former y-axis of your rotated object. Use the rotated y-axis as axis parameter for glRotate and the result should be what you are looking for.
Michael
... ok my explanatioon was probably at least as cryptic as yours so let me try some example code. i cant give any guarantees that it will be correct, since its 4am here and i have no way to test this right now. but i can still try.
void MultMatrix(float *matrix,float *vector)
{
float result[4];
for (int j=0;j != 4;j++){
result[j] = matrix[j*4] * vector[0];
result[j] += matrix[j*4 + 1] * vector[1];
result[j] += matrix[j*4 + 2] * vector[2];
result[j] += matrix[j*4 + 3] * vector[3];
}
vector[0] = result[0];
vector[1] = result[1];
vector[2] = result[2];
vector[3] = result[3];
}
.......
float matrix[4][4];
float yaxis[4] = {0.0,1.0,0.0,0.0};
glLoadIdentity()
glRotatef(anyangle,1.0,0.0,0.0);
glGetFloatv(GL_MODELVIEW_MATRIX,matrix);
MultMatrix(matrix,yaxis);
glRotatef(anyangle2,yaxis[0],yaxis[1],yaxis[2]);
... et voila... that should be it assuming i guessed correct which way GL and C orient arrays... if not you have to play around with the rows and colums of the matrix.
Michael
[edited by - plasm on August 17, 2003 10:12:03 PM]
[edited by - plasm on August 17, 2003 10:14:36 PM]
void MultMatrix(float *matrix,float *vector)
{
float result[4];
for (int j=0;j != 4;j++){
result[j] = matrix[j*4] * vector[0];
result[j] += matrix[j*4 + 1] * vector[1];
result[j] += matrix[j*4 + 2] * vector[2];
result[j] += matrix[j*4 + 3] * vector[3];
}
vector[0] = result[0];
vector[1] = result[1];
vector[2] = result[2];
vector[3] = result[3];
}
.......
float matrix[4][4];
float yaxis[4] = {0.0,1.0,0.0,0.0};
glLoadIdentity()
glRotatef(anyangle,1.0,0.0,0.0);
glGetFloatv(GL_MODELVIEW_MATRIX,matrix);
MultMatrix(matrix,yaxis);
glRotatef(anyangle2,yaxis[0],yaxis[1],yaxis[2]);
... et voila... that should be it assuming i guessed correct which way GL and C orient arrays... if not you have to play around with the rows and colums of the matrix.
Michael
[edited by - plasm on August 17, 2003 10:12:03 PM]
[edited by - plasm on August 17, 2003 10:14:36 PM]
I think he wants to rotate around a global fixed coordinate system. The answer is just to reverse the order of the rotations!
In your example should you first rotate around the Y-axis followed by the rotation around the X-axis.
Euler vs fixed angles is really the same thing as local vs global coordinate system. This is covered in the red book.
In your example should you first rotate around the Y-axis followed by the rotation around the X-axis.
Euler vs fixed angles is really the same thing as local vs global coordinate system. This is covered in the red book.
Hmm... i''m not sure if were referring to the same thing but, what i was thinking is if i do a glrotate, the y-axis and z-axis changes ryt? This means that if i roate by (0,1,0) im going to rotate along the "transformed" y-axis.
Lets say I have a point, i rotate it by 45 degrees along the x-axis. Then i rotate the point again by the original y-axis and not the transformed one. The final location of the point is the effect i want to get. How do i get this using glrotatef?
Lets say I have a point, i rotate it by 45 degrees along the x-axis. Then i rotate the point again by the original y-axis and not the transformed one. The final location of the point is the effect i want to get. How do i get this using glrotatef?
> what i was thinking is if i do a glrotate, the y-axis and z-axis changes ryt?
Correct.
> This means that if i roate by (0,1,0) im going to rotate along the "transformed" y-axis.
You rotate by the y-axis. But its not the original y-axis of the object since the 1st rotation was already applied.
> Lets say I have a point, i rotate it by 45 degrees along the x-axis. Then i rotate the point again by the original y-axis and not the transformed one.
To do that you have to apply the 1st rotation to your 'transformed y-axis'. I tried to do that in my example. Not sure if it does that.
Michael
[edited by - plasm on August 17, 2003 10:55:59 PM]
Correct.
> This means that if i roate by (0,1,0) im going to rotate along the "transformed" y-axis.
You rotate by the y-axis. But its not the original y-axis of the object since the 1st rotation was already applied.
> Lets say I have a point, i rotate it by 45 degrees along the x-axis. Then i rotate the point again by the original y-axis and not the transformed one.
To do that you have to apply the 1st rotation to your 'transformed y-axis'. I tried to do that in my example. Not sure if it does that.
Michael
[edited by - plasm on August 17, 2003 10:55:59 PM]
the easiest way if you want to do it global instead of local:
glGetFloatv(GL_MODELVIEW_MATRIX,matrix);
glRotatef(angle,matrix[1],matrix[5], matrix[9]);
simply using the transposed.. or use mutliply and switch the matrices, the question of local or global is just a matter of if your matrix is on the left or right side of the multiplication.
glGetFloatv(GL_MODELVIEW_MATRIX,matrix);
glRotatef(angle,matrix[1],matrix[5], matrix[9]);
simply using the transposed.. or use mutliply and switch the matrices, the question of local or global is just a matter of if your matrix is on the left or right side of the multiplication.
quote:Original post by Trienco
mutliply and switch the matrices, the question of local or global is just a matter of if your matrix is on the left or right side of the multiplication.
This was exactly my suggestion and is by far the easiest solution in my opinion. A combination of rotations around X-Y-Z has two interpretaions. You can think of a local coordinate systems rotated around X-Y-Z. Or you can think in terms of a fixed coordinate system and then is the order reversed to Z-Y-X. Both interpretations is correct and useful.
Its really non-iniutive to find that reversing the order of rotations switches between local and fixed coordinate systems. Rotating the model or the coordinate system is like moving the camera or the model.
hehe, i always hated that "opengl is doing things backward" explanation. without thinking in local coordinates you will have a pretty hard time in some situations anyway. and what headache would it cause to think "backwards" when rendering a complex hierarchical model?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement