Jump to content
  • Advertisement
Sign in to follow this  
PunaProgrammer chris

3D Model Rotations

This topic is 4487 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

My friend and I are working on a game (in C++), and I am currently making a "GameObject" class. The game object has an oriented bounding-box, an MD2, and some other stuff. I have pretty much everything integrated into the class except the MD2, which my friend coded. The MD2 is currently handling rotations using glRotatef, which is not good for me, as the game object is using a forward vector and up vector to express its orientation. The game object uses a rotate function quite similar to glRotatef; it takes an 3d vector contating the axis to rotate on, and a float with the amount to rotate by. The functions modifies the up and forward vectors so they are rotated by the given amount on the given axis. I was thinking of doing something similar with the MD2, but I don't know how to rotate A model using vectors, and I dont know how to reverse the process so I get the axis to rotate on and how much to roatate by based on the orientation vectors. The only solution I could think of is storing all rotations in the MD2, and then just perform them in order upon drawing the model. However, this would be really ineffcient after a lot of rotations. Any help would be great, thanks.

Share this post


Link to post
Share on other sites
Advertisement
If I understant correctly, you are trying to rotate a model about an axis.

To do this, you could build a matrix and multiply each vertex with it.

But seriously, why would you not use glRotatef ()? It should be able to handle all cases that you want (unless you really want something really wierd) and it is probably faster than vector/matrix multiplication...

Share this post


Link to post
Share on other sites
Or you could have a matrix for each instance of the GameObject. So when you are rendering it...


glLoadIdentity();
glPushMatrix();
glMultMatrixf(GameObject1.matrix);
// Render the GameObject1...
glPopMatrix();



And you could apply your transformation and rotations only once (when initializing) to that matrix. And everytime it will draw with that orientation as the matrix is stored.

Share this post


Link to post
Share on other sites
I would use a 4x4 matrix or quaternion + point to store the orientation information. I would not use Euler angles as this is one of those situations where their use results in much more programming effort and many more bugs than other representations.

Share this post


Link to post
Share on other sites
Sorry it took so long to reply, my modem was broken for a while...
Anyways, thanks for the replies. Not having had much experience with OpenGL in quite a while, I looked up glMultMatrixf and it seems like what I need to use.

Quote:
Original post by Anudh
Or you could have a matrix for each instance of the GameObject. So when you are rendering it...

***Source Snippet Removed***


I think that is what I will do, except rather I will use the vectors of the game object for the MD2 and use those to create a matrix. So it will be something like this:

void MD2::Render()
{
glPushMatrix();
float matrix[16];
// Fill matrix with values from axis vectors here.
glMultMatrixf(matrix);
// Draw the model here.
glPopMatrix();
}



I just have one question left: How do I use forward, up, and side vectors (the z, y, and x axes respecitvley) to make a 4 X 4 matrix that I can use with glMultMatrixf()?

Share this post


Link to post
Share on other sites
Once you have an orthnormal basis (x,y,z vectors that are normalized), write them as the rows of the upper 3x3 of the 4x4 rotation matrix. Don't forget to put 1 in the lower-rightmost element as well.

Share this post


Link to post
Share on other sites
So that would would be:

void MD2::Render(Vector3 vX, Vector3 vY, Vector3 vZ)
{
glPushMatrix();
GLfloat matrix[16];
matrix[0] = xAxis.x;
matrix[1] = xAxis.y;
matrix[2] = xAxis.z;
matrix[3] = 0.0f;
matrix[4] = yAxis.x;
matrix[5] = yAxis.y;
matrix[6] = yAxis.z;
matrix[7] = 0.0f;
matrix[8] = zAxis.x;
matrix[9] = zAxis.y;
matrix[10] = zAxis.z;
matrix[11] = 0.0f;
matrix[12] = 0.0f;
matrix[13] = 0.0f;
matrix[14] = 0.0f;
matrix[15] = 1.0f;
glMultMatrixf(matrix);
// Draw the model here.
glPopMatrix();
}




Correct? And this is the right way to use glMultMatrixf, right?
Thanks for all the help.
Edit: Realized a 2 dimensional float array wouldn't work as a GLfloat*, so I changed the code a little.

[Edited by - PunaProgrammer chris on August 6, 2006 11:55:13 PM]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!