| 88 | To go from mouse coordinate to world coordinates we have to do the exact opposite of the view transformation: |
---|

| 89 | |
---|

| 90 | {{{ |
---|

| 91 | #!latex-math-hook |
---|

| 92 | \begin{quote} |
---|

| 93 | Viewport \to NDC \to Clip \to View \to World/Model |
---|

| 94 | \end{quote} |
---|

| 95 | }}} |
---|

| 96 | |
---|

| 97 | That's a lot of steps, and it's easy to screw up, and if you screw up just a little that's enough to blow everything apart. |
---|

| 98 | |
---|

| 99 | === Viewport to NDC to Clip === |
---|

| 100 | |
---|

| 101 | The first step is to transform the viewport coordinates into clip coordinates. The viewport transformation takes a normalized device coordinate and transforms it into a viewport coordinate: |
---|

| 102 | |
---|

| 103 | {{{ |
---|

| 104 | #!latex-math-hook |
---|

| 105 | \begin{eqnarray*} |
---|

| 106 | {\boldsymbol V}{\boldsymbol n}&=&{\boldsymbol v}&=& |
---|

| 107 | \begin{pmatrix} |
---|

| 108 | \frac{ {\boldsymbol n}_x + 1 }{2}w\\ |
---|

| 109 | \frac{ 1-{\boldsymbol n}_y}{2}h\\ |
---|

| 110 | \frac{ {\boldsymbol n}_z + 1 }{ 2 } |
---|

| 111 | \end{pmatrix} \\ |
---|

| 112 | \text{where}\\ |
---|

| 113 | \boldsymbol{V}&=&\text{viewport transformation matrix}\\ |
---|

| 114 | \boldsymbol{n}&=&\text{normalized device coordinate}\\ |
---|

| 115 | \boldsymbol{v}&=&\text{point in viewport/window space}\\ |
---|

| 116 | \end{eqnarray*} |
---|

| 117 | }}} |