title: Draw a square in opengl at mouse position
body: <p>I am trying to draw a simple square wherever I press the left mouse button using opengl/glut. My program runs perfectly except for the part where it does <strong>not</strong> draw the square where I click the left mouse button :). Can someone point out what I am doing wrong?</p>
  
  <pre><code>#include &lt;stdlib.h&gt; 
  #include &lt;glut.h&gt;
  
  GLsizei WIDTH = 1300, HEIGHT = 700;
  GLsizei MOUSEx, MOUSEy;
  GLfloat SIDE=1;
  
  GLfloat RED[3] = {1,0,0};
  GLfloat GREEN[3] = {0,1,0};
  GLfloat BLUE[3] = {0,0,1};
  GLfloat WHITE[3] = {1,1,1};
  GLfloat BLACK[3] = {0,0,0};
  GLfloat YELLOW[3] = {1,1,0};
  GLfloat CYAN[3] = {0,1,1};
  GLfloat MAGENTA[3] = {1,0,1};
  
  void drawSquare(int x, int y)
  {
      glColor3fv(YELLOW);
      glBegin(GL_POLYGON);
          glVertex3f(x+SIDE, y+SIDE,0);
          glVertex3f(x-SIDE, y+SIDE,0);
          glVertex3f(x-SIDE, y-SIDE,0);
          glVertex3f(x+SIDE, y-SIDE,0);
      glEnd();
      glFlush();
  }
  
  void drawSquare1()
  {
      int x=0,y=0;
      glColor3fv(BLUE);
      glBegin(GL_POLYGON);
          glVertex3f(x+SIDE, y+SIDE,0);
          glVertex3f(x-SIDE, y+SIDE,0);
          glVertex3f(x-SIDE, y-SIDE,0);
          glVertex3f(x+SIDE, y-SIDE,0);
      glEnd();
      glFlush();
  }
  
  void display (void) {
      glClearColor (0.0,0.0,0.0,1.0);
      glClear (GL_COLOR_BUFFER_BIT);
      glLoadIdentity(); 
      glTranslatef(0,0,-5);
      drawSquare1();
      glFlush();
  }
  
  void reshape (int w, int h) {
      glViewport (0, 0, (GLsizei)w, (GLsizei)h);
      glMatrixMode (GL_PROJECTION);
      glLoadIdentity ();
      gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
      glMatrixMode (GL_MODELVIEW);
      WIDTH=w;
      HEIGHT=h;
  }
  
  void setX(int x)
  {
      MOUSEx=x;
  }
  
  void setY(int y)
  {
      MOUSEy=y;
  }
  
  void mouse(int btn, int state, int x, int y)
  {
      if(btn==GLUT_LEFT_BUTTON &amp;&amp; state==GLUT_DOWN)   
      {
          setX(x);
          setY(y);
          drawSquare(MOUSEx,HEIGHT-MOUSEy);
          glutPostRedisplay();
      }
      if(btn==GLUT_RIGHT_BUTTON &amp;&amp; state==GLUT_DOWN)   
      {
          exit(1);
      }
  }
  
  int main (int argc, char **argv) {
      glutInit (&amp;argc, argv);
      glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
      glutInitWindowSize (WIDTH, HEIGHT);
      glutInitWindowPosition (10, 10);
      glutCreateWindow ("New Window");
      glutDisplayFunc (display);
      glutReshapeFunc (reshape);
      glutMouseFunc(mouse);
      //glutMotionFunc(drawSquare);
      glutMainLoop ();
      return 0;
  }
  </code></pre>