Thread: Cube Rotation openGL

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Cube Rotation openGL

    Hi everyone,
    I've drawn 5 sides of a cube all different colors. I want the cube to 'spin' in place toward it's right side. However, I can only get it to spin in place vertically or if I try to go to the right I have to rotate it about some Z-axis as well. Here is the code for the vertically spinning 'cube'. Any help is greatly appreciated.

    Code:
    void draw ()
    {    
         float z = 4;
         
         glMatrixMode(GL_MODELVIEW);
         
         glPushMatrix();
         
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         
         glTranslatef(-3.0f, -3.0f, -10.0f);
         glRotatef(angle, 0.0f, 0.0f, 4.5f);
              
         glBegin(GL_QUADS);
         
         // RED SQUARE (front)
         glColor3f(1.0f, 0.0f, 0.0f);
         glVertex3f(-0.5f, 0.5f, z);
         glVertex3f(0.5f, 0.5f, z);
         glVertex3f(0.5f, -0.5f, z);
         glVertex3f(-0.5f, -0.5f, z);
         
         // BLUE SQUARE (top)
         glColor3f(0.0f, 0.0f, 1.0f);
         glVertex3f(-0.5f, 0.5f, z);
         glVertex3f(0.5f, 0.5f, z);
         z -= 1;
         glVertex3f(0.5f, 0.5f, z);
         glVertex3f(-0.5f, 0.5f, z);     
         
         // GREEN SQUARE (back)
         glColor3f(0.0f, 1.0f, 0.0f);     
         glVertex3f(-0.5f, -0.5f, z);
         glVertex3f(0.5f, -0.5f, z);
         glVertex3f(0.5f, 0.5f, z);
         glVertex3f(-0.5f, 0.5f, z);
         
         z += 1;
         
         // WHITE SQUARE (right)
         glColor3f(1.0f, 1.0f, 1.0f);
         glVertex3f(0.5f, 0.5f, z);
         glVertex3f(0.5f, -0.5f, z);
         z -= 1;
         glVertex3f(0.5f, -0.5f, z);
         glVertex3f(0.5f, 0.5f, z);  
         
         z += 1;
         
         // YELLOW SQUARE (left)
         glColor3f(1.0f, 1.0f, 0.0f);   
         glVertex3f(-0.5f, 0.5f, z);
         glVertex3f(-0.5f, -0.5f, z);
         z -= 1;
         glVertex3f(-0.5f, -0.5f, z);
         glVertex3f(-0.5f, 0.5f, z);
         
         glEnd();
         
         glPopMatrix();
         if (angle < 360) angle += 5.0f;
         else angle = 0.0f;
    
         SDL_GL_SwapBuffers();
    } // end draw ()
    Also, my settings are:

    Code:
    void initGLsettings ()
    {
         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
         glEnable(GL_DEPTH_TEST);
         glViewport(0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT);
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         gluPerspective(60.0f, 1.0f*SCREEN_WIDTH/SCREEN_HEIGHT, 0.1f, 125.0f);
         glMatrixMode(GL_MODELVIEW);
         glLoadIdentity();
    }
    edit: I said if I go to the right it must about some Z-axis as well. I mean it has to be about a large y-axis.
    Last edited by cliffplaysdrums; 11-23-2008 at 09:16 AM. Reason: mistype

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you want an object to rotate around a point other than it's center, first translate to the desired point, perform your rotation, and then translate to world coordinates.

    You could then, if needed, translate to another point of rotation and then rotate. This would cause the object to rotate around the point in world space.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Just a note, there's no need to keep selecting the model view matrix. You should be doing most of your work on it anyway, if you need to modify another matrix such as the texture matrix then select the texture matrix do your stuff then reselect the model view matrix. The OpenGL FAQ recommends this practice too.

    Last time I checked cubes had 6 sides

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    Quote Originally Posted by Bubba View Post
    If you want an object to rotate around a point other than it's center, first translate to the desired point, perform your rotation, and then translate to world coordinates.

    You could then, if needed, translate to another point of rotation and then rotate. This would cause the object to rotate around the point in world space.
    Thanks for the response,
    The point which the cube rotates isn't a problem, it's the direction. I can rotate it about it's center, but it spins counter-clockwise like a square, where as I want to it spin horizontally in place (the left side comes at you as it goes around and follows the right around but the center of the cube is the same coordinate). Sorry if i misinterpreted anything you said. You make a very useful point though for future reference.

    Also zacs thanks for the information. I modified the code so that the modelview matrix is selected in the draw () function only, and no other matrices are ever specified.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The direction of rotation is based on your yaw, pitch, and roll. If it is rotating the wrong direction then negating yaw should give you the correct direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL hue rotation
    By Blink in forum Game Programming
    Replies: 4
    Last Post: 08-14-2007, 03:42 PM
  2. Cube rotation math problem
    By n3v in forum Game Programming
    Replies: 6
    Last Post: 08-03-2007, 05:41 AM
  3. rotation in OpenGL
    By linuxdude in forum Game Programming
    Replies: 4
    Last Post: 08-09-2006, 11:29 PM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM