title: Drawing a polygon when the mouse is clicked
body: <p>Consider this code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdarg.h&gt;
  #include &lt;GLUT/GLUT.h&gt;
  #include &lt;OpenGL/OpenGL.h&gt;
  
  double width=600;
  double height=600;
  
  void processMouse(int button, int state, int x, int y)
  {
      glColor4f(1.0,0.0,0.0,0.0);
      glBegin(GL_POLYGON);
      glVertex3f(0.0, 0.0, 0.0);
      glVertex3f(1.0, 0.0, 0.0);
      glVertex3f(1.0, 1.0, 0.0);
      glVertex3f(0.0, 1.0, 0.0);
      glEnd();
      glFlush();
  }
  
  static void render()
  {
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glClear(GL_COLOR_BUFFER_BIT);
      glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
      glutMouseFunc(processMouse);
  }
  
  int main(int argc, char **argv)
  {
      glutInit(&amp;argc,argv);                                         
      glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   
      glutInitWindowSize(width, height);
      glutCreateWindow("Board");  
      glutDisplayFunc(render);
      glutMainLoop();
  }
  </code></pre>
  
  <p>The render function is executed, and every time a click is performed, it should start the function processMouse.
  So if the mouse is clicked, all the window should become red with the instructions:</p>
  
  <pre><code>    glColor4f(1.0,0.0,0.0,0.0);
      glBegin(GL_POLYGON);
      glVertex3f(0.0, 0.0, 0.0);
      glVertex3f(1.0, 0.0, 0.0);
      glVertex3f(1.0, 1.0, 0.0);
      glVertex3f(0.0, 1.0, 0.0);
      glEnd();
      glFlush();
  </code></pre>
  
  <p>But when I click the mouse I notice a strange behaviour: only a part of the window gets colored, the part at bottom left (instead of all the screen).
  The window remains in this state, until I open a google chrome window.If I open a google chrome (or another graphical application), all the window become colored of red.
  Why this? I also have problems with more complex programs, it seems like sometime the glVertex instructions are ignored.If I try debugging the program with fprintf it appears that everything is ok, and everything seems to go like expected (for example I tried to print mouse coordinates in the processMouse function, they were ok), except for the fact that what I draw is ignored.</p>
  
  <p>Edit:
  I have modified this code but it still has the same problem:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdarg.h&gt;
  #include &lt;GLUT/GLUT.h&gt;
  #include &lt;OpenGL/OpenGL.h&gt;
  
  double width=600;
  double height=600;
  bool down=false;;
  
  // http://elleestcrimi.me/2010/10/06/mouseevents-opengl/
  
  
  static void render()
  {
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glClear(GL_COLOR_BUFFER_BIT);
      glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
      if(down)
      {
          glColor4f(1.0,0.0,0.0,0.0);
          glBegin(GL_POLYGON);
          glVertex3f(0.0, 0.0, 0.0);
          glVertex3f(1.0, 0.0, 0.0);
          glVertex3f(1.0, 1.0, 0.0);
          glVertex3f(0.0, 1.0, 0.0);
          glEnd();
          glFlush();
      }
  }
  
  void processMouse(int button, int state, int x, int y)
  {
      if(state==GLUT_DOWN)
      {
          down=true;
          glutPostRedisplay();
      }
  }
  
  
  int main(int argc, char **argv)
  {
  glutInit(&amp;argc,argv);                                         
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   
  glutInitWindowSize(width, height);
  glutCreateWindow("Board"); 
  glutMouseFunc(processMouse);
  glutDisplayFunc(render);
  glutMainLoop();
  }
  </code></pre>
  
  <p>Still getting only a part of the screen red.</p>
  
  <p>PS: Solved using glutSwapBuffers(), thanks.</p>