matrix rotation/translation question


I am hoping someone expert at matrix transformations can help me. I have an openGL app where I have a scene set up, and I allow the user to ‘twist’ rotate around a certain point in the scene. I use these calls to do it:


	glLoadMatrixf((GLfloat *)currentRotationMatrix);
	glTranslatef(xTrans, yTrans, zTrans);
	glTranslatef(twistx, twisty, twistz);
	glGetFloatv(GL_MODELVIEW_MATRIX, TempRotationMatrix);	
	glRotatef(-zr, ((float *)TempRotationMatrix)[2], ((float *)TempRotationMatrix)[6], ((float *)TempRotationMatrix)[10]);
	glTranslatef(-twistx, -twisty, -twistz);
	glTranslatef(-xTrans, -yTrans, -zTrans);

	glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)currentRotationMatrix);  // save for later

So, I have a currentRotationMatrix and then the other transforms are to get it out to a point in space where I want to do the twist. Then I do it and reverse my setup transformations.

This works, but what I end up with is a rotationMatrix that has been dirtied with extra translation values. I want to keep my rotationMatrix as only rotations. Is there any way to take the translation values that appear in my final matrix and incorporate them into my translation values xTrans yTrans zTrans? I have experimented but cannot figure out how to do it.

I hope this question makes sense – basically I try to compose my scene from xyz translation variables and XYZ euler rotation variables. But when I interactively make changes, I don’t quite know how to separate them back out.

Thanks for any advice

So what you want to do is to make a rotation matrix that rotates around the point (twistx, twisty, twistz), and then separate the rotation part and the translation parts of that matrix? And then separately save those two matrices?

If you want to rotate around a point, starting from an identity matrix, you first call glTranslate with the coords of the point you want to rotate around, then do the rotation, and then glTranslate with the negated values of the coords you rotated around.

If you want to extract the translation part of a matrix, you can call glGetFloatc(GL_MODELVIEW_MATRIX, m), and the translation components will be in m[12], m[13], and m[14]. If you then call glTranslatef(-m[12], -m[13], -m[14]) you will get the matrix with the translation removed. This will however not be a true rotation matrix.

I hope I explained what you wanted to know.