new to GL simple question

Hi,

I’m new to openGL and there are alot of helpful tutorials out there. I have been following the tutorial to create rotating 3D polygon. My question is, how can you use the keyboard to toggle the polygon rotation between different axis during runtime?

I tried to call glRotatef( ) in the keyboard function with different case statements, but that doesn’t seem to work. I might be missing something here.

Suppose the general code is something like this:

void Display (void)
{
glBegin ( GL_Polygon )
//vertices goes here
glEnd();
}

void keyboard ( unsigned char key, int x, int y )
{
switch (key){
case ‘a’:
glRotatef ( 5.0f, 0.0f, 0.0f, 0.1f );
display();
break;
case ‘b’:
glRotatef ( 5.0f, 0.0f, 0.1f, 0.0f );
display();
break;
case ‘c’:
glRotatef ( 5.0f, 0.1f, 0.0f, 0.0f );
display();
break;
}
}

void main ( int argc, char** argv )
{
glutInit();
//other glut window functions goes here
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}

Any suggestion will be appreciated. Thank You!

Hi !

To make a long story short, you can only put OpenGL rendering function calls in the display callback, so what you need to do is to keep the rotation in a variable, update the variable in the keyboard callback and call glutPostRedisplay() (I think the name is correct ;o), this will force an update of the window and display will be called, so then you need to do the rotation in the displasy callback instead.

Mikael