Jump to content
  • Advertisement
Sign in to follow this  
Yhonatan

glRotatef Coords?

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

Hello all. I got a question: After I Rotate something how can I know its new coords? Like if it 1,3,1 what are the new? how can I know? Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
I assume you mean how can you get the rotated poosition of a point when rotating with glRotatef?

You can get the rotated position by transforming the point yourself. AFAIK you can retreive the modelview matrix from OpenGL but not the transformed vertices (unless you use some shader tricks or so).

Share this post


Link to post
Share on other sites
Transforming a point is a matter of standard matrix math.
All you need is a matrix that represents your rotation and/or translation and then multiply it with your point. Try google on some matrix math tutorials.

For the OpenGL matrices you might look here.

Share this post


Link to post
Share on other sites
Mmm, I suppose the simplest, least math-heavy way, would be to create a pure translation matrix with the coordinates of the source vector. Something like...

1 0 0 1
0 1 0 3
0 0 1 1
0 0 0 1

then load that onto openGL's matrix stack and apply the glRotate function to it. You could then grab the new matrix from the stack and examine the 4th row elements from it.

It's been a while since i've used OpenGL, so apologies if this is pish, but i think the code would be something like..


float m[16];

//
// Clear Matrix
for(int i = 0; i < 16; i++)
m = 0.0f;

//
// Set up identity bit
m[0] = 1.0f;
m[5] = 1.0f;
m[10] = 1.0f;
m[15] = 1.0f;

//
// Setup translation
m[3] = 1.0f;
m[7] = 3.0f;
m[11] = 1.0f;

//
// Save current Matrix
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(m);

//
// Apply Rotation to the Matrix
glRotatef(angle, x_axis, y_axis, z_axis);

//
// Grab Matrix from stack and get result coords
glGetFloatv(GL_MODELVIEW_MATRIX, m);

//
// Get new coords
newCoordsX = m[3];
newCoordsY = m[7];
newCoordsZ = m[11];

//
// Tidy up! :)
glPopMatrix();



There are more math-heavy ways, but i think they involve creating a quaternion for the axis/angle yourself, then converting it to a rotation matrix and applying that manually to your source vector.

Oh, as a slight shortcut, if your rotation is against a world axis plane (as in around a world x/y or z axis), then you can ignore the relative element in the original vector and use sin/cos on the others to quickly find the new coords, however since your using glRotate i don't think thats the case.

Anyways, hope that helps!

Share this post


Link to post
Share on other sites
MattWhite06 is right. The least math-heavy way is by readig back the OpenGL matrices from the stack and then using them to do your own transformations.

Building rotation matrices from quaternions isn't necessary, you could build the matrices from a bunch of sin/cos calculations.

3D graphics, however, involves matrix and vector math in many aspects so I'd recommend learning those.

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!