title: c++ OpenGL display problem for gluLookAt
body: <p>I have been trying to solve this program:</p>
  
  <blockquote>
    <p>Program to draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing. Also the cube should rotate.</p>
  </blockquote>
  
  <p>I have completed the coding and here it is. It seems to work fine except for 1 simple problem:</p>
  
  <pre><code>//Program to draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing
  #include &lt;stdio.h&gt;
  #include &lt;glut.h&gt;
  
  float v[][3] = {{-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1},{-1,1,-1},{1,1,-1},{1,-1,-1},{-1,-1,-1}};
  void drawPolygon(float a[3],float b[3],float c[3],float d[3])
  {
      glBegin(GL_POLYGON);
          glVertex3fv(a);
          glVertex3fv(b);
          glVertex3fv(c);
          glVertex3fv(d);
      glEnd();
      glFlush();
  }
  void drawCube(float v[8][3])
  {
      glColor3f(1,0,0);
      drawPolygon(v[0],v[1],v[2],v[3]);
      glColor3f(0,1,0);
      drawPolygon(v[0],v[1],v[6],v[7]);
      glColor3f(0,0,1);
      drawPolygon(v[7],v[6],v[5],v[4]);
      glColor3f(1,1,0);
      drawPolygon(v[2],v[3],v[4],v[5]);
      glColor3f(0,1,1);
      drawPolygon(v[1],v[2],v[5],v[6]);
      glColor3f(1,0,1);
      drawPolygon(v[0],v[3],v[4],v[7]);
  
      glFlush();
  }
  void display()
  {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
      glColor3f(1,0,0);
      //glRotatef(60,1,1,0);
      drawCube(v);
      glFlush();
  }
  
  void reshape(int width,int height)
  {
      glViewport(0,0,width,height);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(-2,2,-2,2,-2,2);
      glMatrixMode(GL_MODELVIEW);
      glutPostRedisplay();
  }
  
  void mouse(int btn,int state,int x,int y)
  {
      if(btn == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN)
          glRotatef(2,1,0,0);
      if(btn == GLUT_MIDDLE_BUTTON &amp;&amp; state == GLUT_DOWN)
          glRotatef(2,0,1,0);
      if(btn == GLUT_RIGHT_BUTTON &amp;&amp; state == GLUT_DOWN)
          glRotatef(2,0,0,1);
  
      glutPostRedisplay();
  }
  float ex=0,ey=0,ez=-10,cx=0,cy=0,cz=0,ux=0,uy=1,uz=0;
  void keyboard(unsigned char key,int x,int y)
  {
      if(key == 'x')
          ex += 0.1;
      if(key == 'X')
          ex -= 0.1;
      if(key == 'y')
          ey += 0.1;
      if(key == 'Y')
          ey -= 0.1;
      if(key == 'z')
          ez += 0.1;
      if(key == 'Z')
          ez -= 0.1;
      glMatrixMode(GL_PROJECTION);
      gluLookAt(ex,ey,ez,cx,cy,cz,ux,uy,uz);
      glutPostRedisplay();
  }
  void main(int argc,char** argv)
  {
      glutInit(&amp;argc,argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
      glutInitWindowPosition(100,100);
      glutInitWindowSize(800,800);
      glutCreateWindow("spin cube");
  
      glClearColor(1,1,1,0);
  
      glEnable(GL_DEPTH_TEST);
  
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
      glutMouseFunc(mouse);
      glutKeyboardFunc(keyboard);
  
      glutMainLoop();
  }
  </code></pre>
  
  <p>The cube rotates by mouse clicks</p>
  
  <p>The camera moves by x/X y/Y z/Z</p>
  
  <p>But the problem is that when i first press a key the display vanishes. Then the 2nd time it comes with properly moved camera. Why? I want the camera to just move and not vanish the display. What is wrong?</p>