Changes between Version 7 and Version 8 of MousePicking
 Timestamp:
 05/18/06 14:07:37 (13 years ago)
Legend:
 Unmodified
 Added
 Removed
 Modified

MousePicking
v7 v8 2 2 April 5, 2005 3 3 4 == Introduction == 4 5 There comes a time in every 3D game where the user needs to click on something in the scene. Maybe he needs to select a unit in an RTS, or open a door in an RPG, or delete some geometry in a level editing tool. This conceptually simple task is easy to screw up since there are so many little steps that can go wrong. 5 6 12 13 Before we worry about the inverse transformation, we need to establish how the standard forward transformation works in a typical graphics pipeline. 13 14 14 This article is under construction! 15 == The View Transformation == 16 17 The standard view transformation pipeline takes a point in model space and transforms it all the way to viewport space (sometimes known as window coordinates) or, for systems without a window system, screen coordinates. It does this by transforming the original point through a series of coordinate systems: 15 18 16 19 {{{ 26 29 }}} 27 30 28 Some text: 31 Not each step is discrete. OpenGL has the {{{GL_MODELVIEW}}} matrix, '''{{{M}}}''', that transforms a point from model space to view space, using a righthanded coordinate system with +Y up, +X to the right, and Z into the screen. 29 32 30 33 {{{ 31 34 #!latexmathhook 32 {\boldsymbol M}{\boldsymbol p}&=&{\boldsymbol v} 35 \begin{eqnarray*} 36 {\boldsymbol M}{\boldsymbol p}&=&{\boldsymbol v} \\ 37 \text{where} \\ 38 {\boldsymbol M}&=&\text{modelview transformation matrix} \\ 39 \boldsymbol{p} &=& \text{point in model space} \\ 40 \boldsymbol{v} &=& \text{point in view space} \\ 41 \end{eqnarray*} 33 42 }}} 34 43 35 More text:44 Another matrix, {{{GL_PROJECTION}}}, then transforms the point from view space to homogeneous clipping space. Clip space is a righthanded coordinate system (+Z into the screen) contained within a canonical clipping volume extending from {{{(1,1,1)}}} to {{{(+1,+1,+1)}}}: 36 45 37 46 {{{ 38 47 #!latexmathhook 39 \ label{eqn:vp to ndc}40 {\boldsymbol n} = \begin{pmatrix}41 \ frac{ 2{\boldsymbol v}_x }w  1\\42 \frac{ 2{\boldsymbol v}_y }{h}\\43 2{\boldsymbol v}_z  1\\44 1 45 \end{ pmatrix}48 \begin{eqnarray*} 49 {\boldsymbol P}{\boldsymbol v}&=&{\boldsymbol c} \\ 50 \text{where} \\ 51 {\boldsymbol P}&=&\text{projection matrix} \\ 52 \boldsymbol{v} &=& \text{point in view space} \\ 53 \boldsymbol{c} &=& \text{point in clip space} \\ 54 \end{eqnarray*} 46 55 }}} 56 57 After clipping is performed the perspective divide transforms the homogeneous coordinate to a Cartesian point in normalized device space. Normalized device coordinates are lefthanded, with ''w'' = 1, and are contained within the canonical view frustum from {{{(1,1,1)}}} to {{{(+1,+1,+1)}}}: 58 59 {{{ 60 #!latexmathhook 61 \begin{eqnarray*} 62 {\boldsymbol n}&=&\frac{{\boldsymbol c}}{{\boldsymbol c}_w} \\ 63 \text{where} \\ 64 {\boldsymbol n}&=&\text{point in normalized device coordinate} \\ 65 {\boldsymbol c}&=&\text{clipped point in homogeneous clipping space} \\ 66 \end{eqnarray*} 67 }}} 68 69 Finally there is the viewport scale and translation, which transforms the normalized device coordinate into viewport (window) coordinates. Another axis inversion occurs here; this time +Y goes down instead of up (some window systems may place the origin at another location, such as the bottom left of the window, so this isn't always true). Viewport depth values are calculated by rescaling normalized device coordinate Z values from the range {{{(1,1)}}} to {{{(0,1)}}}, with 0 at the near clip plane and 1 at the far clip plane. Note: any user specified depth bias may impact our calculations later. 70 71 {{{ 72 #!latexmathhook 73 \begin{eqnarray*} 74 {\boldsymbol V}{\boldsymbol n}&=&{\boldsymbol w} \\ 75 \text{where} \\ 76 {\boldsymbol V}&=&\text{viewport transformation matrix} \\ 77 {\boldsymbol n}&=&\text{point in normalized device coordinates} \\ 78 {\boldsymbol w}&=&\text{point in viewport/window coordinates} \\ 79 \end{eqnarray*} 80 }}} 81 82 This pipeline allows us to take a model space point, apply a series of transformation, and get a window space point at the end. 83 84 Our ultimate goal is to transform the mouse position (in window space) all the way ''back'' to world space. Since we're not rendering a model, model space and and world space are the same thing. 85 86 == The Inverse View Transformation == 87