title: Rotation using keyboard -- glut / OpenGL
body: <p>I'm trying to make my object rotate depending on which axis is wanted (by pressing either x, y, z) and then starting/stopping the rotaion with the same key.  Here is my code:</p>
  
  <pre><code>#include &lt;glut.h&gt;
  #include &lt;stdio.h&gt;
  
  static GLfloat spin = 0.0;
  
  void init(void){
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glShadeModel(GL_SMOOTH);
  }
  
  void action(void)
  {
  
  }
  
  
  void display(void)
  {
  glClear(GL_COLOR_BUFFER_BIT);
  glPushMatrix();
  
  
  glBegin(GL_LINE_LOOP);
  glColor3f(1.00, 0.00, 0.00);
  glVertex3f(0.00, 0.00, 0.00);
  glVertex3f(0.00, 0.00, -1.00);
  glVertex3f(0.50, 0.00, -1.00);
  glVertex3f(0.75, 0.0, -0.75);
  glVertex3f(0.25, 0.25, -1.0);
  glVertex3f(0.50, 0.50, -0.50);
  
  glColor3f(0.00, 1.00, 0.00);
  glVertex3f(0.00, 0.00, 0.00);
  glVertex3f(0.00, 0.00, 1.00);
  glVertex3f(0.50, 0.00, 1.00);
  glVertex3f(0.75, 0.0, 0.75);
  glVertex3f(0.25, 0.25, 1.0);
  glVertex3f(0.50, 0.50, 0.50);
  
  glEnd();
  
  glPopMatrix();
  glutSwapBuffers();
  glFlush();
  }
  
  //spin function
  void spinDisplay(void){
  spin = spin + 0.02;
  if (spin &gt; 360.0)
      spin = spin - 360.0;
  glutPostRedisplay();
  }
  
  
  void reshape(int w, int h)
  {
  glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-1.0, 1.0, -1.0, 1.0, 0.5, 15.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(1.5, 1.5, 1.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  
  }
  
  
  //keyboard event handler
  void keyboard(unsigned char key, int x, int y)
  {
  switch (key)
  {
  case 27: // Escape key
      exit(0);
      break;
  case 'x':
      glRotatef(spin, 1.0, 0.0, 0.0);
      break;
  case 'y':
      glRotatef(spin, 0.0, 1.0, 0.0);
      break;
  case 'z':
      glRotatef(spin, 0.0, 0.0, 1.0);
      break;
  case 'r':
      glutIdleFunc(action);
      break;
  }
  glutPostRedisplay();
  }
  
  
  void mouse(int button, int state, int x, int y){
  switch (button){
  case GLUT_LEFT_BUTTON:
      if (state == GLUT_DOWN)
          glutIdleFunc(spinDisplay);
      break;
  case GLUT_RIGHT_BUTTON:
      if (state == GLUT_DOWN)
          glutIdleFunc(NULL);
      break;
  default:
      break;
  }
  }
  
  int main(int argc, char** argv)
  {
  glutInit(&amp;argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowSize(500, 500);
  glutInitWindowPosition(100, 100);
  glutCreateWindow(argv[0]);
  init();
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMouseFunc(mouse);
  glutKeyboardFunc(keyboard);
  glutIdleFunc(action);
  glutMainLoop();
  return 0;
  }
  </code></pre>
  
  <p>I have in the keyboard function each key that I want but I am wondering about glRotatef -- that is, whether it belongs there.  I created the function action that I intended on using to be called into the glutIdleFunc() but am unsure of how to incorporate the rotate into it.  Even if I can just get some clarification on how/why my keyboard call is incorrect would be great!</p>