Hi all,
I have met a problem, I have some objects in the viewport, and the program should let someone to move them, rotate and and scale them.
While, if I rotate object a and move it to somewhere, what if I want rotate in the future? I mean sure I can use one rotatef() to do it, but once I have done that rotation, I lost the axis and the degree it rotated about, then how can I start it to move or ratate again about another axis and dgree?
Sure I can maintain an array of all the action it do, but seems costly.
My idea is to make it rotate about x, y, z seperately instead of use the rotatef(degree, x, y, z), but do OPENGL has a function that return’s the different degrees on the x, y, z?
OpenGL has no functions for returning the corresponding rotations about the original axes. But you can calculate them yourself if you want to using the modelview matrix.
The actual problem wasn’t the same, but the result is the same in both yours and Eric’s problem. What you need to do is obtain the modelview matrix, and identify each elements with a resulting rotation matrix. And don’t worry, in the end of the post there is an example on how to calculate the values, so no need to do the identification yourself.
Your idea is good in principles…
But as a matter of fact, what you will end up doing is add small transformations to the modelview matrix. This can lead to a corrupted matrix due to roundoff errors…
There are algorithms that enable you to check for those errors and correct them but they are usually really heavy…
It is almost always better to define your matrix as a set of parameters (here angles around X,Y and Z) and reconstruct it on each frame…
Eric: Yes, you’re right. But for some application that doesn’t matter. I’m working on a CAD application where the user can rotate the part on the screen. This application sometimes runs very long and I never recognized any corruptions in the matrix. In games I can think of such a corruption, as there is always some movement.
By the way, correction of such a matrix is not very expensive. Assume v1, v2 and v3 are the first three columns of my matrix (x, y and z-vector):
v1.Normalize();
v2.Normalize();
v3=Crossproduct(v1,v2);
v2=Crossproduct(v3,v1);
v1.Normalize();
v2.Normalize();
v3.Normalize();
Normalize sets the length of the vector to 1.
How about calling the correction every 1000 frames?
KurtCob:
Calculate a rotation matrix in an array m like openGL has it (rows are the vectors x, y, z). Then call glMultMatrix(m).