Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The GL has clear conecpets of object space, clip space, normalized device coords and window space. Your usage of <code>glOrtho()</code> and <code>glTranslate()</code> implies that you use the old fixed function, where also the eye-space is well-defined.</p> <p>When you want to map window sapce points (like mouse coords) back into object space, you need to invert the calculations the gl does. The transformation pipeline for vertices is the following:</p> <ol> <li>multiply modelview matrix MV by object space vertex position to get eye space coords (only in fixed-function GL, in the programmable pipeline, this step is completely user-defined)</li> <li>multiply projection matrix P by eye space position to get clip-space position (only in fixed-function GL, in the programmable pipeline, this step is completely user-defined)</li> <li>divide by by clip space w component to get cartesian normalized device coords. In this space, the viewing volume is an axis-aligne cube in the range [-1,1] along each axis. whit z=-1 reprensenting the near plane and z=1 the far plane</li> <li>transform the NDCs to window space by taking the viewport into accout. the point (-1,1) is to be mapped to the (bottom left corner of the) bottom-left pixel of the viewport, (1,1) is mapped to the (top-right corner of the) top-right pixel of the viewport. Z is transformed from [-1,1] to [0,1] and might be further transfrom by the specified <code>glDepthRange</code>, and later might by converted to some integer representation.</li> </ol> <p>If you have a given 2D mouse position, you can simply invert that scheme: </p> <ol> <li>Find the viewport coords (which is trivial if you use the full window, just be aware that GL uses the origin at bottom-left, while most window systems put the origin at top-left corner).</li> <li>Undo the viewport transform to put x and y in the range of [-1,-1].</li> <li>Now you can directly use these as clip space coords by assuimg w=1. In the general case, each 2d mouse position represents a ray, so you can use <em>two</em> points to project the ray back, like z=-1 and z=1. However, in your orthogonal projection case, one point is enough. So multiply the inverse Projection P^-1 by the clip space coords of your point(s).</li> <li>You now have the eye space position. Multiply the inverse modelview matrix MV^-1 by the eye space coords to get back to object space.(In the general case, you should be aware that you have to divide the final result by the w component, since you were in a projective space. With orthogonal projection, and only affine transformations in ModelView, w will be 1 so you can ignore it).</li> </ol> <p><strong>EDIT</strong></p> <p>Since that operation is common, there are also some utility functions implementing this. As you are using the matrix stack of the GL, the easiest solution is just to use <a href="http://www.opengl.org/sdk/docs/man2/xhtml/gluUnProject.xml" rel="nofollow"><code>gluUnproject()</code></a> from the GLu library. All you need are the 3D window coords of the point, and the matrices adn viewport setting which where used when you did draw the object, which can be queried using <code>glGetDoublev()</code> using the various <code>glGet</code> functions. There is a <a href="http://nehe.gamedev.net/article/using_gluunproject/16013/" rel="nofollow">full example at the old NeHe site</a>.</p> <p>With modern GL, you have to do this all by yourself. However, the popular GLM library also has a <a href="http://glm.g-truc.net/api/a00151.html#ga540d5f6bb3f41e5dfa38d6ebd8771765" rel="nofollow"><code>glm::unProject</code></a> method similiar to the GLU one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload