diff options
Diffstat (limited to 'Spear/Math/MatrixUtils.hs')
-rw-r--r-- | Spear/Math/MatrixUtils.hs | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs index 354c840..629b73c 100644 --- a/Spear/Math/MatrixUtils.hs +++ b/Spear/Math/MatrixUtils.hs | |||
@@ -1,6 +1,8 @@ | |||
1 | module Spear.Math.MatrixUtils | 1 | module Spear.Math.MatrixUtils |
2 | ( | 2 | ( |
3 | fastNormalMatrix | 3 | fastNormalMatrix |
4 | , unproject | ||
5 | , rpgUnproject | ||
4 | , rpgTransform | 6 | , rpgTransform |
5 | , pltTransform | 7 | , pltTransform |
6 | , rpgInverse | 8 | , rpgInverse |
@@ -27,6 +29,50 @@ fastNormalMatrix m = | |||
27 | (M4.m02 m') (M4.m12 m') (M4.m22 m') | 29 | (M4.m02 m') (M4.m12 m') (M4.m22 m') |
28 | 30 | ||
29 | 31 | ||
32 | -- | Transform the given point in window coordinates to object coordinates. | ||
33 | unproject :: Matrix4 -- ^ Inverse projection matrix | ||
34 | -> Matrix4 -- ^ Inverse modelview matrix. | ||
35 | -> Float -- ^ Viewport x | ||
36 | -> Float -- ^ Viewport y | ||
37 | -> Float -- ^ Viewport width | ||
38 | -> Float -- ^ Viewport height | ||
39 | -> Float -- ^ Window x | ||
40 | -> Float -- ^ Window y | ||
41 | -> Float -- ^ Window z | ||
42 | -> V3.Vector3 | ||
43 | unproject projI modelviewI vpx vpy w h x y z = | ||
44 | let | ||
45 | xmouse = 2*(x-vpx)/w - 1 | ||
46 | ymouse = 2*(y-vpy)/h - 1 | ||
47 | zmouse = 2*z - 1 | ||
48 | in | ||
49 | (modelviewI * projI) `M4.mulp` V3.vec3 xmouse ymouse zmouse | ||
50 | |||
51 | |||
52 | -- | Transform the given point in window coordinates to 2d coordinates. | ||
53 | -- | ||
54 | -- The line defined by the given point in window space is intersected with | ||
55 | -- the XZ plane in world space to yield the resulting 2d point. | ||
56 | rpgUnproject | ||
57 | :: Matrix4 -- ^ Inverse projection matrix | ||
58 | -> Matrix4 -- ^ Inverse viewI matrix. | ||
59 | -> Float -- ^ Viewport x | ||
60 | -> Float -- ^ Viewport y | ||
61 | -> Float -- ^ Viewport width | ||
62 | -> Float -- ^ Viewport height | ||
63 | -> Float -- ^ Window x | ||
64 | -> Float -- ^ Window y | ||
65 | -> Vector2 | ||
66 | rpgUnproject projI viewI vpx vpy w h x y = | ||
67 | let | ||
68 | p1 = unproject projI viewI vpx vpy w h x y 0 | ||
69 | p2 = unproject projI viewI vpx vpy w h x y (-1) | ||
70 | lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) | ||
71 | p' = p1 + V3.scale lambda (p2 - p1) | ||
72 | in | ||
73 | vec2 (V3.x p') (-V3.z p') | ||
74 | |||
75 | |||
30 | -- | Map an object's transform in view space to world space. | 76 | -- | Map an object's transform in view space to world space. |
31 | rpgTransform | 77 | rpgTransform |
32 | :: Float -- ^ The height above the ground | 78 | :: Float -- ^ The height above the ground |
@@ -37,7 +83,7 @@ rpgTransform | |||
37 | -> Matrix4 | 83 | -> Matrix4 |
38 | rpgTransform h a axis pos viewI = | 84 | rpgTransform h a axis pos viewI = |
39 | let p1 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) 0) | 85 | let p1 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) 0) |
40 | p2 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) (-100)) | 86 | p2 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) (-1)) |
41 | lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) | 87 | lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) |
42 | p = p1 + V3.scale lambda (p2 - p1) | 88 | p = p1 + V3.scale lambda (p2 - p1) |
43 | mat' = axisAngle axis a | 89 | mat' = axisAngle axis a |