Rotating a cube in OpenGL
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.
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]
Basically do this. (correct me if im wrong)
<FOR EVERY FRAME>// Restore to identity (1)glLoadIdentity ();// Apply camera transformationglTranslatef (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 ();}
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):
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.
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 transformationglRotatef (-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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement