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:
[b]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(yM_PI/180),0.0,sin(yM_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();[/b]

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 :slight_smile:

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

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:

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.

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(yM_PI/180),0.0,sin(yM_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 :slight_smile:

Any solution? I have the same problem …