Public Group

# Rotating a cube in OpenGL

This topic is 4413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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.

bump

##### 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 on other sites
Basically do this. (correct me if im wrong)

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

// 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 on other sites
Yeah, that looks good. Go with that.

##### 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)

// 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.

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632931
• Total Posts
3009292
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

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!