| 647 | } |
| 648 | |
| 649 | vec3 windowToObjectCoord( const mat4 &modelMatrix, const ci::vec2 &coordinate, float z ) |
| 650 | { |
| 651 | // Build the viewport (x, y, width, height). |
| 652 | vec2 offset = gl::getViewport().first; |
| 653 | vec2 size = gl::getViewport().second; |
| 654 | vec4 viewport = vec4( offset.x, offset.y, size.x, size.y ); |
| 655 | |
| 656 | // Calculate the view-projection matrix. |
| 657 | mat4 viewProjectionMatrix = gl::getProjectionMatrix() * gl::getViewMatrix(); |
| 658 | |
| 659 | // Calculate the intersection of the mouse ray with the near (z=0) and far (z=1) planes. |
| 660 | vec3 nearPlane = glm::unProject( vec3( coordinate.x, size.y - coordinate.y, 0 ), modelMatrix, viewProjectionMatrix, viewport ); |
| 661 | vec3 farPlane = glm::unProject( vec3( coordinate.x, size.y - coordinate.y, 1 ), modelMatrix, viewProjectionMatrix, viewport ); |
| 662 | |
| 663 | // Calculate world position. |
| 664 | return ci::lerp( nearPlane, farPlane, ( z - nearPlane.z ) / ( farPlane.z - nearPlane.z ) ); |
| 665 | } |
| 666 | |
| 667 | vec2 objectToWindowCoord( const mat4 &modelMatrix, const ci::vec3 &coordinate ) |
| 668 | { |