title: gluUnProject ray picking(opengl/c++/glut)
body: <p>I am trying to see if I can click on a 3D drawn object, using the mouse, in a 3D world. I understood that I have to use gluUnProject so I tried it out in the following way:</p>
  
  <p>Firstly I have a function that gets the x,y coordinates from the mouse(the window coordinates), and tries to transform them into real world coordinates:</p>
  
  <pre><code>void project(int x_cursor, int y_cursor){
  
  GLint viewport[4];
  GLdouble modelview[16];
  GLdouble projection[16];
  GLfloat winX,winY;
  glGetIntegerv(GL_VIEWPORT, viewport);
  glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
  glGetDoublev(GL_PROJECTION_MATRIX, projection);
  
  // obtain the Z position (not world coordinates but in range 0 - 1)
  GLfloat z_cursor;
  winX = (float)x_cursor;
   winY = (float)viewport[3]-(float)y_cursor;
  glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &amp;z_cursor);
  
  // obtain the world coordinates
  
  gluUnProject(winX, winY, z_cursor, modelview, projection, viewport, &amp;x, &amp;y, &amp;z);
  }
  </code></pre>
  
  <p>Then I have the mouse callback function:</p>
  
  <pre><code>void mouse(int button, int state, int x_cursor, int y_cursor){
  if(button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN)
  {
      mouse_x=x_cursor;
      mouse_y=y_cursor;
  
   project(mouse_x,mouse_y);
  
       cout&lt;&lt;x&lt;&lt;" "&lt;&lt;"Printed out variable x"&lt;&lt;endl;
   cout&lt;&lt;y&lt;&lt;" "&lt;&lt;"Printed out variable y"&lt;&lt;endl;
   cout&lt;&lt;z&lt;&lt;" "&lt;&lt;"Printed out variable z"&lt;&lt;endl;
  
   /**for (int i = 0; i &lt; 150; i++) { 
  if ((enemies[i]!=NULL)&amp;&amp;(enemies[i]-&gt;getTranslation().x &gt; x - 1 &amp;&amp;     enemies[i]-&gt;getTranslation().x &lt; x + 1) 
          &amp;&amp; (enemies[i]-&gt;getTranslation().z &gt; z - 1 &amp;&amp; enemies[i]-&gt;getTranslation().z &lt; z + 1)
          &amp;&amp; (enemies[i]-&gt;getTranslation().y &gt; y - 1 &amp;&amp; enemies[i]-&gt;getTranslation().y &lt; y + 1)) {
  
          running=false;
              }
      }**/
  }
  </code></pre>
  
  <p>}</p>
  
  <p>For the moment I am just printing out the coordinates x,y,z, but in the commented code you can see what I intend to do with them: verify if a drawn object is in the immediate vecinity of the spot where I clicked the mouse.</p>
  
  <p>Lastly I added the glutMouseFunc(mouse); line in the main() function, and it compiles ok, but judging by the result of the prints it doesn't indicate the correct values.</p>
  
  <p>I would like to add the fact that I have a moving camera;by this I mean I can change the glulookat parameters, using keyboard input. Could you please look at my code and maybe show me where I'm doing wrong. Thanks a lot. </p>
  
  <p>[EDIT] It only seems to work when I set the camera like: gluLookAt(-35, 0, 0, 0, 100, 0, 0, 0, 1);</p>
  
  <p>[EDIT2] Also if I move the position of the camera, and then return it in the initial position(where it worked), it doesn't work anymore.</p>
  
  <p>Solved. The above code is ok. The problem was I was adding glPushMatrix(); and glPopMatrix(); before and after drawing the objects in the scene. The question can be closed.</p>