# Rotation in Local Space

## Recommended Posts

soconne    105
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 on other sites
soconne    105
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[i] = xaxis[i];
final[4+i] = yaxis[i];
final[8+i] = zaxis[i];
}

gluLookAt(...);

glMultMatrixd(final);
DrawObject();
}