Sign in to follow this  

Rotating a cube in OpenGL

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

I'm working on a simple OpenGL app. I have a cube drawn at the origin. Now I want to implement rotation using the mouse. However i'm having trouble trying to get a fixed coordinate. For example if I flip the cube upside down and i try to rotate left it rotates right. Could someone please point me in the right direction? I'm fairly new to all this. Thanks in advance.

Share this post


Link to post
Share on other sites
Hmm I'm sort of learning 3D too... and I am going the other way in DX. But perhaps I think you need to set the 'World Matrix' back to the 'Identity Matrix' and then apply the rotation. Otherwise the rotation is applied relative to the cube's orientation instead and because it's upside down, it will appear to rotate the wrong way! Good luck [smile]

Share this post


Link to post
Share on other sites
Basically do this. (correct me if im wrong)

<FOR EVERY FRAME>
// Restore to identity (1)
glLoadIdentity ();

// Apply camera transformation
glTranslatef (cameraX, cameraY, cameraZ);
glRotatef (cameraPitch, 1, 0, 0);
glRotatef (cameraYaw, 0, 1, 0);
glRotatef (cameraRoll, 0, 0, 1);

for (everyObjectInTheGame)
{
// Save the camera matrix
glPushMatrix();

// Apply object transformation
glTranslatef (objectX, objectY, objectZ);
glRotatef (objectPitch, 1, 0, 0);
glRotatef (objectYaw, 0, 1, 0);
glRotatef (objectRoll, 0, 0, 1);

// Draw The object
DrawObject ();

// Restore Camera matrix
glPopMatrix ();
}

Share this post


Link to post
Share on other sites
Just for clarity: The camera transformation normally defines how the camera is arranged in world space as is any other object, too. The VIEW transformation as part of OpenGL's MODELVIEW matrix is then just the inverse of the camera transformation, since you want to model the overall transformation
local -> global -> view

If this is also in your case, then the correct way would be something like (expressed with diablo_tk's code snippet):

<FOR EVERY FRAME>
// Restore to identity (1)
glLoadIdentity ();

// Apply INVERSE camera transformation
glRotatef (-cameraRoll, 0, 0, 1);
glRotatef (-cameraYaw, 0, 1, 0);
glRotatef (-cameraPitch, 1, 0, 0);
glTranslatef (-cameraX, -cameraY, -cameraZ);

for (everyObjectInTheGame)
{
// Save the camera matrix
glPushMatrix();

// Apply object transformation
glTranslatef (objectX, objectY, objectZ);
glRotatef (objectPitch, 1, 0, 0);
glRotatef (objectYaw, 0, 1, 0);
glRotatef (objectRoll, 0, 0, 1);

// Draw The object
DrawObject ();

// Restore Camera matrix
glPopMatrix ();
}


Notice the reversed order and negated arguments from the camera set-up to specifiy the VIEW matrix. This is the effect of inverting the transformation.

Share this post


Link to post
Share on other sites

This topic is 4109 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this