title: Rotation using keys in GLUT
body: <p>I have created a that moves the square around using arrow keys. Now I want my square turn 90 degrees on y axis everytime I press 'r' key. I have already added case statements for arrow keys that allows my square to move around.</p>
  <p>here is my program;</p>
  <pre><code>#include &quot;include\freeglut.h&quot;   // OpenGL toolkit - in the local shared folder
  #include &lt;cmath&gt;
  #include &lt;iostream&gt;
  
  //set up some constants
  #define X_CENTRE 0.0      /* centre point of square */
  #define Y_CENTRE 0.0
  #define LENGTH   5.0      /* lengths of sides of square */
  
  
  //forward declaration - best in the header
  void drawStar(GLfloat radius, GLfloat x, GLfloat y);
  void drawSquare(GLfloat length, GLfloat x, GLfloat y);
  void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y);
  void drawCar(GLfloat length, GLfloat x, GLfloat y);
  void greenSquare(GLfloat length, GLfloat x, GLfloat y);
  
  
  GLfloat red = 1.0, green = 1.0, blue = 1.0;
  GLint   xmove = -13.0, ymove = -13.5, zmove = 0.0;
  
  static GLfloat spin = 0.0;
  
  GLboolean square = false;
  
  
  
  
  
  
  
  
  
  /* reshape callback function
     executed when window is moved or resized. This function should be used in Tutorial 1 */
  void reshape(int width, int height)
  {
      GLfloat aspect = (GLfloat)width / (GLfloat)height;
  
      glViewport(0, 0, width, height);
      glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
      glLoadIdentity();
  
    //  glOrtho(-10.0, 10.0, -10.0, 10.0, -1.0, 1.0);   //sets the x,y,z plane from -1 to 1
      if (width &lt;= height) //if aspect is less or equal to 1
          glOrtho(-10.0, 10.0, -10.0/aspect, 10.0/aspect, -1.0, 1.0);
      else // aspect is greater than 1
          glOrtho(-10.0 * aspect, 10.0* aspect, -10.0 , 10.0, -1.0, 1.0);
  
      glMatrixMode(GL_MODELVIEW);
  }
  
  
  /* display callback function
     called whenever contents of window need to be re-displayed */
     //this is the all important drawing method - all drawing code goes in here
  void display(void)
  {
      glClear(GL_COLOR_BUFFER_BIT);     /* clear window */
      /*glColor3f(1.0, 1.0, 1.0);        /* white drawing objects */
  
      glColor3f(0.0, 0.0, 1.0);          /* blue drawing objects */
      /* define object to be drawn as a square polygon */
  
  
  
      glColor3f(0.3, -3.0, 4.0);
      drawSquare(2, -3, 8);
  
      glColor3f(0.3, -1.0, 2.0);
      drawSquare(2, -3, 8);
  
      glColor3f(1.0, 0.0, 0.0);
      drawSquare(2, -5, -6);
  
      glColor3f(0.0, 1.0, 0.0);
      drawSquare(2, 0.0, 0.0);
  
      glColor3f(2.0, 3.0, 1.1);
      drawSquare(2, 6.0, 0.0);
  
      glColor3f(0.3, -1.0, 2.0);
      drawSquare(2, -6, 5);
  
      glColor3f(0.7, -1.0, 2.0);
      drawSquare(2, 5, -5);
  
      glColor3f(0.0, 0.0, 1.0);
      drawSquare(2, 4, 6);
  
      glColor3f(1.0, 0.0, 0.0);
      drawSquare(5, -8, -12);
  
      glColor3f(0.0, 1.0, 0.0);
      greenSquare(5,8,10.5);
  
      if (!square) {
          glColor3f(0.75, 0.5, 0.25);
          drawCar(1, 0, 0);
      }
      
  
  
      glFlush();     /* execute drawing commands in buffer */
  }
  
  
  void myMenu(GLint id) {
  
      if (id == 1) {
          square = 1.0;
      }
      else {
          square = 0.0;
  
      }
      glutPostRedisplay();
  }
  
  
  
  
  
  
  
  
  
  void drawCar(GLfloat length, GLfloat x, GLfloat y) {
      glPushMatrix();
      glTranslatef(xmove, ymove, zmove);
      //glBegin(GL_TRIANGLES);
      ////specify the vertices (points in 3D space) of the shape - note that these are 2D points
      //glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE - LENGTH / 3);
      //glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE + LENGTH / 3);
      //glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE + LENGTH / 3);
      //glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE - LENGTH / 3);
      //glEnd();
  
     glRectf(3, 4, 6, 2);
      glPopMatrix();
      glFlush();
     
  }
  
  //void spinDisplay(void) {
  //    spin = spin + 90.0;
  //    
  //    glutPostRedisplay();
  //}
  //
  //
  //void rotate (unsigned char ,GLint x,GLint y){
  //    switch (x) {
  //    case 'r':
  //        glRotatef(spin, 1.0, 90.0, 90.0);
  //     break;
  //    }
  //
  //
  //
  //}
  
  void keyInput(int z, int x, int y)
  {
      switch (z)
      {
      case GLUT_KEY_UP:
          ymove++;
          if (ymove &gt;= 5) ymove = 5.0;
  
          break;
      case GLUT_KEY_DOWN:
          ymove--;
          //  if (ymove &lt;= -5) ymove = 0.0;
          break;
      case GLUT_KEY_RIGHT:
          xmove++;
          //  if (xmove &gt;= 5) xmove = 0.0;
          break;
      case GLUT_KEY_LEFT:
          xmove--;
          //  if (xmove &lt;= -5) xmove = 0.0;
          break;
      }
          
      glutPostRedisplay();
  }
  
  void greenSquare(GLfloat length, GLfloat x, GLfloat y) {
      //x1,y1 is the top left-hand corner coordinate
      GLfloat x1, y1, x2, y2, x3, y3, x4, y4;
  
      //glColor3f(0.1, 1.0, 0.2);
      x1 = x - length / 2;
      y1 = y + length / 2;
      x2 = x + length / 2;
      y2 = y + length / 2;
      x3 = x + length / 2;
      y3 = y - length / 2;
      x4 = x - length / 2;
      y4 = y - length / 2;
  
      glBegin(GL_POLYGON);
      glVertex2f(x1, y1);
      glVertex2f(x2, y2);
      glVertex2f(x3, y3);
      glVertex2f(x4, y4);
      glEnd();
  
      glFlush();
  }
  
  
  
  
  void drawSquare(GLfloat length, GLfloat x, GLfloat y)
  {
      //x1,y1 is the top left-hand corner coordinate
      GLfloat x1, y1, x2, y2, x3, y3, x4, y4;
  
      //glColor3f(0.1, 1.0, 0.2);
      x1 = x - length / 2;
      y1 = y + length / 2;
      x2 = x + length / 2;
      y2 = y + length / 2;
      x3 = x + length / 2;
      y3 = y - length / 2;
      x4 = x - length / 2;
      y4 = y - length / 2;
  
      glBegin(GL_POLYGON);
      glVertex2f(x1, y1);
      glVertex2f(x2, y2);
      glVertex2f(x3, y3);
      glVertex2f(x4, y4);
      glEnd();
  
      glFlush();
  }
  
  void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y)
  {
      //x1,y1 is the top left-hand corner coordinate
      GLfloat x1, y1, x2, y2, x3, y3, x4, y4;
  
      //This example is for a rectangle 
      x1 = x - lengthX / 2;
      y1 = y + lengthY / 2;
      x2 = x + lengthX / 2;
      y2 = y + lengthY / 2;
      x3 = x + lengthX / 2;
      y3 = y - lengthY / 2;
      x4 = x - lengthX / 2;
      y4 = y - lengthY / 2;
  
      glBegin(GL_POLYGON);
      glVertex2f(x1, y1);
      glVertex2f(x2, y2);
      glVertex2f(x3, y3);
      glVertex2f(x4, y4);
      glEnd();
  
      glFlush();
  }
  
  //Draws a 5 pointed star using lines
  void drawStar(GLfloat radius, GLfloat x, GLfloat y)
  {
      //x1,y1 is the top coordinate
      //glColor3f(0.0, 1.0, 1.0);
  
      GLfloat x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
  
      x1 = x;
      y1 = y + radius;
      x2 = x + 0.90 * radius;
      y2 = y + 0.40 * radius;
      x3 = x + 0.65 * radius;
      y3 = y - 0.55 * radius;
      x4 = x - 0.65 * radius;
      y4 = y - 0.55 * radius;
      x5 = x - 0.90 * radius;
      y5 = y + 0.40 * radius;
  
      glLineWidth(1.0);
      glBegin(GL_LINES);
      glVertex2f(x1, y1);
      glVertex2f(x3, y3);
      glVertex2f(x1, y1);
      glVertex2f(x4, y4);
      glVertex2f(x2, y2);
      glVertex2f(x4, y4);
      glVertex2f(x2, y2);
      glVertex2f(x5, y5);
      glVertex2f(x3, y3);
      glVertex2f(x5, y5);
      glEnd();
  
  
      glFlush();
  }
  
  
  /* graphics initialisation */
  void init(void)
  {
        
      glClearColor(0.0, 0.0, 0.0, 0.0);  //Setting up the background color 
  }
  
  
  int main(int argc, char** argv)
  {
  
  
      /* window management code ... */
  /* initialises GLUT and processes any command line arguments */
      glutInit(&amp;argc, argv);
  
      /* use single-buffered window and RGBA colour model */
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);    
      glutInitWindowSize(750, 750);
  
      /* window upper left corner at (100, 100) */
      glutInitWindowPosition(100, 100);
  
      /* creates an OpenGL window with command argument in its title bar */
      glutCreateWindow(&quot;Car game&quot;);
      // glutKeyboardFunc(rotate);
      glutSpecialFunc(keyInput);
  
     //Pop menu using with right click 
      glutCreateMenu(myMenu);
      glutAddMenuEntry(&quot;Hide Square&quot;, 1);
      glutAddMenuEntry(&quot;Show Square&quot;, 2);
      glutAttachMenu(GLUT_RIGHT_BUTTON);
     
      
  
      init();
      
      
  
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
      glutMainLoop();
      return 0;
  }
  </code></pre>