# rotation about an arbitrary vector

does anyone know how to develop a matrix to rotate a vertex about an arbitrary vector like int openGL.

glRotatef(angle, vector.x, vector.y, vector.z);

where ‘vector’ is the vector to be rotated around.

thanks

If you first rotate the z-axis of your coordinate system to your arbitrary vector then you can

glRotatef(angle, 0.0, 0.0, 1.0) to get what you want.

If you want some details on how to rotate the z-axis to an arbitrary vector see
http://www.mfcogl.com/OpenGL%20-%20draw%20cylinder%20between%202%20pts.htm

for an explaination and source.

http://www.makegames.com/3drotation/

look here under the section “Other Ways to Build a Rotation Matrix”. there are some equations there to help you. you set a few variables using the axis(x,y,z) and the angle(in radians). then it’s just plug’n’chug into the matrix equation.

good luck
b

Originally posted by angryGLfan:
[b]does anyone know how to develop a matrix to rotate a vertex about an arbitrary vector like int openGL.

glRotatef(angle, vector.x, vector.y, vector.z);

where ‘vector’ is the vector to be rotated around.

thanks[/b]

Hi,

I understand that you want to get the Matrix wich perfoms that rotation, isn´t it?.

If it is, you must think that OpenGL gives you glRotate to make your work easier but In a lower level It is calculating that matrix so you can get it like this:

// Will store your matrix
GLdouble mvmatrix;

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// Put matrix to Identity

// Now actualizes the identity matrix with
// the rotation matrix corresponding to the
// rotation you want.
glRotatef(angle,axe.x,axe.y,axe.y);

// Now you want to retrieve it so:
glGetDoublev( GL_MODELVIEW_MATRIX , mvmatrix );

glPopMatrix(); // Returns to older matrix

I think It should work because I have used it. I would advice you to read about glGet… operations, they are very usefull.

There are other ways to calculate the matrix but I think that if you can use the opengl tools why to go around of it? Hope It works.
Bye

Originally posted by angryGLfan:
does anyone know how to develop a matrix to rotate a vertex about an arbitrary vector like int openGL.

If I understand the question correctly, you want to understand the math involved in an arbitrary rotation? The RedBook tries to explain the math involved for doing this. A link to that portion can be found here:
http://fly.cc.fer.hr/~unreal/theredbook/appendixg.html

I have to admit, that the formulas presented there for arbitrary rotation can be a bit daunting. Awhile ago, I had started my own little library for performing matrix operations, and I have code somewhere for doing the calculation. I’ll have to wait until I get home to find it for ya, though.

I haven’t looked at the other links provided, so maybe they explain it better.

Here’s a code snippet that should help if what you’re trying to do is create your own glRotate-like routine. It’s not complete code, but you should be able to figure out what things like my Matrix4x4 and Matrix3x3 structs, and the DEGTORAD macros are.

void mat4Rotate(Matrix4x4 *mat, float angle, float x, float y, float z)
{
float len, ux, uy, uz;
float sinAng, cosAng;
Matrix3x3 S, uuT;

memset(mat, 0, sizeof(Matrix4x4));

len = (float)sqrt(xx + yy + z*z);

ux = x/len;
uy = y/len;
uz = z/len;

S.m11 = 0.0;
S.m12 = -uz;
S.m13 = uy;
S.m21 = uz;
S.m22 = 0.0;
S.m23 = -ux;
S.m31 = -uy;
S.m32 = ux;
S.m33 = 0.0;

uuT.m11 = uxux;
uuT.m12 = ux
uy;
uuT.m13 = uxuz;
uuT.m21 = uy
ux;
uuT.m22 = uyuy;
uuT.m23 = uy
uz;
uuT.m31 = uzux;
uuT.m32 = uz
uy;
uuT.m33 = uz*uz;

mat->m11 = uuT.m11 + cosAng*(1.0f-uuT.m11) + sinAngS.m11;
mat->m12 = uuT.m12 + cosAng
(0.0f-uuT.m12) + sinAngS.m12;
mat->m13 = uuT.m13 + cosAng
(0.0f-uuT.m13) + sinAngS.m13;
mat->m21 = uuT.m21 + cosAng
(0.0f-uuT.m21) + sinAngS.m21;
mat->m22 = uuT.m22 + cosAng
(1.0f-uuT.m22) + sinAngS.m22;
mat->m23 = uuT.m23 + cosAng
(0.0f-uuT.m23) + sinAngS.m23;
mat->m31 = uuT.m31 + cosAng
(0.0f-uuT.m31) + sinAngS.m31;
mat->m32 = uuT.m32 + cosAng
(0.0f-uuT.m32) + sinAngS.m32;
mat->m33 = uuT.m33 + cosAng
(1.0f-uuT.m33) + sinAng*S.m33;
mat->m44 = 1.0;
}

i believe youre all making it to complicated
just do straight linear algebra

glTranslatef(position_To_rotate_around.x,position_To_rotate_around.y,position_To_rotate_around.z)
glRotatef(angle,axis.x, axis.y,axis.z);

(btw u might need to first do a loadidentity)

Originally posted by zed:
i believe youre all making it to complicated

That could be possible. The original poster wasn’t the most clear on what he wanted and hasn’t seemed to stop by to clarify yet. I was under the impression he wanted to understand the math that OpenGL does, though. Hence his words “like int OpenGL.”

deiussum after reading the original post more clearly youre correct + i was wrong, i must of just been reading the topics title, anyways i believe the original poster like elvis has left the building