Quantumcat

05-23-2013, 08:19 PM

I have some objects with vectors of positions. I have been successful getting them to sit on the floor by calculating their centre, and translating them up enough so they sit on the floor object properly (which stretches over the x and z axes with y = 0).

Now I want to try giving them some arbitrary rotations before having them sit on the floor. To do this I'll need to calculate the centre after having rotated each point (so I can make the lowest point on the object touch the floor and not have the object stuck inside the floor or float above it).

I tried searching Google but can only find results about rotation for the purpose of drawing (glRotatef). Is there a ready-made OpenGL function where I can insert a vector (x, y, z) and an angle, and get the vector rotated by that angle?

If not, does anyone have one they have already written that they know works?

If not, if I write out on paper the 4 by 4 rotation matrices, and multiply them in the order X, then Y, then Z, and then multiply the matrix I get by a vector (x, y, z, 1), and use that as the equations in my rotation function, will that work? I have read about gimbal lock, and don't fully understand it, so am not sure if that will cause a problem?

Thanks in advance!

Edit:

I have been playing with the third option and discovered I can get OpenGL to multiply 4 by 4 matrices, so thought I'd let it do the work (maybe I will do it myself when I start looking for ways to speed up my program). I have so far (sorry, I haven't figured out how to make code sections on this forum yet):

float XRot [16] = {1, 0, 0, 0, 0, cos(XAngle), sin(XAngle), 0, 0, -1*sin(XAngle), cos(XAngle), 0, 0, 0, 0, 1};

float YRot [16] = {cos(YAngle), 0, -1*sin(YAngle), 0, 0, 1, 0, 0, sin(YAngle), 0, cos(YAngle), 0, 0, 0, 0, 1};

float ZRot [16] = {cos(ZAngle), sin(ZAngle), 0, 0, -1*sin(ZAngle), cos(ZAngle), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};

glLoadMatrixf(XRot);

glMultMatrixf(YRot);

glMultMatrixf(ZRot);

But how do I get the current matrix out so I can multiply it by the position? I have found how to get, say, the modelview or projection matrix out but not the custom one? Have I done something wrong so far?

Now I want to try giving them some arbitrary rotations before having them sit on the floor. To do this I'll need to calculate the centre after having rotated each point (so I can make the lowest point on the object touch the floor and not have the object stuck inside the floor or float above it).

I tried searching Google but can only find results about rotation for the purpose of drawing (glRotatef). Is there a ready-made OpenGL function where I can insert a vector (x, y, z) and an angle, and get the vector rotated by that angle?

If not, does anyone have one they have already written that they know works?

If not, if I write out on paper the 4 by 4 rotation matrices, and multiply them in the order X, then Y, then Z, and then multiply the matrix I get by a vector (x, y, z, 1), and use that as the equations in my rotation function, will that work? I have read about gimbal lock, and don't fully understand it, so am not sure if that will cause a problem?

Thanks in advance!

Edit:

I have been playing with the third option and discovered I can get OpenGL to multiply 4 by 4 matrices, so thought I'd let it do the work (maybe I will do it myself when I start looking for ways to speed up my program). I have so far (sorry, I haven't figured out how to make code sections on this forum yet):

float XRot [16] = {1, 0, 0, 0, 0, cos(XAngle), sin(XAngle), 0, 0, -1*sin(XAngle), cos(XAngle), 0, 0, 0, 0, 1};

float YRot [16] = {cos(YAngle), 0, -1*sin(YAngle), 0, 0, 1, 0, 0, sin(YAngle), 0, cos(YAngle), 0, 0, 0, 0, 1};

float ZRot [16] = {cos(ZAngle), sin(ZAngle), 0, 0, -1*sin(ZAngle), cos(ZAngle), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};

glLoadMatrixf(XRot);

glMultMatrixf(YRot);

glMultMatrixf(ZRot);

But how do I get the current matrix out so I can multiply it by the position? I have found how to get, say, the modelview or projection matrix out but not the custom one? Have I done something wrong so far?