title: Why do I have to divide by Z?
body: <p>I needed to implement 'choosing an object' in a 3D environment. So instead of going with robust, accurate approach, such as raycasting, I decided to take the easy way out. First, I transform the objects world position onto screen coordinates:</p>
  
  <pre><code>glm::mat4 modelView, projection, accum;
  glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat*)&amp;projection);
  glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&amp;modelView);
  accum = projection * modelView;
  glm::mat4 transformed = accum * glm::vec4(objectLocation, 1);
  </code></pre>
  
  <p>Followed by some trivial code to transform the opengl coordinate system to normal window coordinates, and do a simple distance from the mouse check. <strong>BUT</strong> that doesn't quite work. In order to translate from world space to screen space, I need one more calculation added on to the end of the function shown above:</p>
  
  <pre><code>transformed.x /= transformed.z;
  transformed.y /= transformed.z;
  </code></pre>
  
  <p>I don't understand why I have to do this. I was under the impression that, once one multiplied your vertex by the accumulated modelViewProjection matrix, you had your screen coordinates. But I have to divide by Z to get it to work properly. In my openGL 3.3 shaders, I never have to divide by Z. Why is this?</p>
  
  <p>EDIT: The code to transform from from opengl coordinate system to screen coordinates is this:</p>
  
  <pre><code>int screenX = (int)((trans.x + 1.f)*640.f); //640 = 1280/2
  int screenY = (int)((-trans.y + 1.f)*360.f); //360 = 720/2
  </code></pre>
  
  <p>And then I test if the mouse is near that point by doing:</p>
  
  <pre><code>float length = glm::distance(glm::vec2(screenX, screenY), glm::vec2(mouseX, mouseY));
  if(length &lt; 50) {//you can guess the rest
  </code></pre>
  
  <p>EDIT #2<br>
  This method is called upon a mouse click event:</p>
  
  <pre><code>glm::mat4 modelView;
  glm::mat4 projection;
  glm::mat4 accum;
  glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat*)&amp;projection);
  glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&amp;modelView);
  accum = projection * modelView;
  float nearestDistance = 1000.f;
  gameObject* nearest = NULL;
  for(uint i = 0; i &lt; objects.size(); i++) {
      gameObject* o = objects[i];
      o-&gt;selected = false;
      glm::vec4 trans = accum * glm::vec4(o-&gt;location,1);
      trans.x /= trans.z;
      trans.y /= trans.z;
      int clipX = (int)((trans.x+1.f)*640.f);
      int clipY = (int)((-trans.y+1.f)*360.f);
      float length = glm::distance(glm::vec2(clipX,clipY), glm::vec2(mouseX, mouseY));
      if(length&lt;50) {
          nearestDistance = trans.z;
          nearest = o;
      }
  }
  if(nearest) {
      nearest-&gt;selected = true;
  }
  
  mouseRightPressed = true;
  </code></pre>
  
  <p>The code as a whole is incomplete, but the parts relevant to my question works fine. The 'objects' vector contains only one element for my tests, so the loop doesn't get in the way at all.</p>