Jump to content
  • Advertisement
Sign in to follow this  
soconne

Rotation in Local Space

This topic is 5068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Currently I'm rotating objects in local space by repeating multiplying the new rotation matrix by the previous one then loading that as the modelview matrix. It looks like this: glLoadIdentity(); GLdouble final[16]; glGetDoublev( GL_MODELVIEW_MATRIX, final ); while(1) { glLoadIdentity(); static GLdouble modelview[16]={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,0}; glRotatef(rotY, 0, 1, 0); glRotatef(rotX, 1, 0, 0); glRotatef(rotZ, 0, 0, 1); glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); glLoadMatrixd(final); glMultMatrixd(modelview); glGetDoublev( GL_MODELVIEW_MATRIX, final ); glLoadIdentity(); gluLookAt(...); glMultMatrixd(final); DrawObject(); } This works fine, but is there a way to do the same thing using only the object's local axes? I tried defining 3 vectors, each initially pointing in each axis direction. Then for each rotation I did the following glRotatef(rotY, yaxis[0], yaxis[1], yaxis[2]); glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); ApplyMatrix(modelview, yaxis); ApplyMatrix(modelview, xaxis); ApplyMatrix(modelview, zaxis); glRotatef(rotX, xaxis[0], xaxis[1], xaxis[2]); glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); ApplyMatrix(modelview, yaxis); ApplyMatrix(modelview, xaxis); ApplyMatrix(modelview, zaxis); glRotatef(rotZ, zaxis[0], zaxis[1], zaxis[2]); glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); ApplyMatrix(modelview, yaxis); ApplyMatrix(modelview, xaxis); ApplyMatrix(modelview, zaxis); But I got stuck on exactly what to do next with the transformed vectors. I tried just doing the following right before rendering the object: glRotatef(rotY, yaxis[0], yaxis[1], yaxis[2]); glRotatef(rotX, xaxis[0], xaxis[1], xaxis[2]); glRotatef(rotZ, zaxis[0], zaxis[1], zaxis[2]); But rotations started going crazy very fast. Does anybody have a solution for this? I would like to use actual local vectors so that I can also translate the object locally.

Share this post


Link to post
Share on other sites
Advertisement
I hate it when you post then the answer comes to you. I'm an idiot, but here's how I solved it.

while(1) {

glRotatef(rotY, yaxis[0], yaxis[1], yaxis[2]);
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
ApplyMat(modelview, yaxis);
ApplyMat(modelview, xaxis);
ApplyMat(modelview, zaxis);

glRotatef(rotX, xaxis[0], xaxis[1], xaxis[2]);
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
ApplyMat(modelview, yaxis);
ApplyMat(modelview, xaxis);
ApplyMat(modelview, zaxis);

glRotatef(rotZ, zaxis[0], zaxis[1], zaxis[2]);
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
ApplyMat(modelview, yaxis);
ApplyMat(modelview, xaxis);
ApplyMat(modelview, zaxis);

// final represents object's local transformation
for(int i=0; i < 3; i++) {
final = xaxis;
final[4+i] = yaxis;
final[8+i] = zaxis;
}

glLoadIdentity();
gluLookAt(...);

glMultMatrixd(final);
DrawObject();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!