Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: rotate Cube using keys

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2012
    Posts
    2

    rotate Cube using keys

    Hi everyone!
    I want to rotate a cube on the x-Axis and y-Axis, but the global (fixed).
    The rotation about the x-Axis is already working, but y-Axis is still a problem. A tried many codes, but nothing is working... here my code(it's old opengl)
    Part for Drawing:
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-10.0,10.0,-10.0,10.0,100.0,120.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0,0.0,-110.0);

    glRotatef(25.0,1.0,0.0,0.0);
    glRotatef(-5.0,0.0,1.0,0.0);
    glRotatef(y,0.0,1.0,0.0);
    glRotatef(x,cos(y*M_PI/180),0.0,sin(y*M_PI/180));


    glBegin(GL_QUADS);
    glColor3f(1.0,0.0,0.0);
    glVertex3f(-5.0,-5.0,5.0);
    glVertex3f(5.0,-5.0,5.0);
    glVertex3f(5.0,5.0,5.0);
    glVertex3f(-5.0,5.0,5.0);
    glVertex3f(-5.0,5.0,-5.0);
    glVertex3f(5.0,5.0,-5.0);
    glVertex3f(5.0,-5.0,-5.0);
    glVertex3f(-5.0,-5.0,-5.0);
    glColor3f(0.0,1.0,0.0);
    glVertex3f(-5.0,-5.0,-5.0);
    glVertex3f(-5.0,-5.0,5.0);
    glVertex3f(-5.0,5.0,5.0);
    glVertex3f(-5.0,5.0,-5.0);
    glVertex3f(5.0,5.0,-5.0);
    glVertex3f(5.0,5.0,5.0);
    glVertex3f(5.0,-5.0,5.0);
    glVertex3f(5.0,-5.0,-5.0);
    glColor3f(0.0,0.0,1.0);
    glVertex3f(-5.0,-5.0,-5.0);
    glVertex3f(5.0,-5.0,-5.0);
    glVertex3f(5.0,-5.0,5.0);
    glVertex3f(-5.0,-5.0,5.0);
    glVertex3f(-5.0,5.0,5.0);
    glVertex3f(5.0,5.0,5.0);
    glVertex3f(5.0,5.0,-5.0);
    glVertex3f(-5.0,5.0,-5.0);
    glEnd();


    Key-Routine:
    if(key==GLUT_KEY_LEFT)
    {
    y=y-1.0;
    glutPostRedisplay();
    }
    else
    if(key==GLUT_KEY_RIGHT)
    {
    y=y+1.0;
    glutPostRedisplay();
    }
    else
    if(key==GLUT_KEY_DOWN)
    {
    x=x+1.0;
    glutPostRedisplay();
    }
    else
    if(key==GLUT_KEY_UP)
    {
    x=x-1.0;
    glutPostRedisplay();
    }


    Thanks for your help,
    I just need a idea for finding the right rotation axis

  2. #2
    Junior Member Newbie
    Join Date
    Mar 2012
    Posts
    4

    Re: rotate Cube using keys

    First off you arent't actually roating about the x axis (singularly) in your code at all:

    Code :
    glRotatef(y,0.0,1.0,0.0);
    glRotatef(x,cos(y*M_PI/180),0.0,sin(y*M_PI/180));

    The first line says you are rotating about the y axis (ie turning the object left or right)
    The second line says you are roating about the x axis (up and down) as well as the z axis. It seems you really just want to roate about x itself to get the up and down motion.

    Try this intead:
    Code :
    glRotatef(y,0.0,1.0,0.0);
    glRotatef(x,1.0,0.0,0.0));

    Note too that calling glutPostRedisplay() everytime a keyboard key hit is both inefficient and can be resource heavy.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2012
    Posts
    2

    Re: rotate Cube using keys

    Thanks for your quick response Eimrik,
    but your code isn't my aim:
    glRotatef(y,0.0,1.0,0.0);
    glRotatef(x,1.0,0.0,0.0));

    ...the problem is, that I want to rotate about the camera-x-Axis (fixed, global coordinates, not local coordinates) and after the y-Axis-Rotation also my local x-Axis moved, so I used glRotatef(x,cos(y*M_PI/180),0.0,sin(y*M_PI/180));
    for doing a vector-projection and finally rotating about my new x-Axis (after first rotation, local x-Axis and global x-Axis are not the same) --> this is working, but not for y-Axis

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    1
    Any solution? I have the same problem ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •