diff options
Diffstat (limited to 'Spear/Math/MatrixUtils.hs')
-rw-r--r-- | Spear/Math/MatrixUtils.hs | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs index 629b73c..79bd049 100644 --- a/Spear/Math/MatrixUtils.hs +++ b/Spear/Math/MatrixUtils.hs | |||
@@ -15,8 +15,7 @@ where | |||
15 | import Spear.Math.Camera as Cam | 15 | import Spear.Math.Camera as Cam |
16 | import Spear.Math.Matrix3 as M3 | 16 | import Spear.Math.Matrix3 as M3 |
17 | import Spear.Math.Matrix4 as M4 | 17 | import Spear.Math.Matrix4 as M4 |
18 | import Spear.Math.Vector2 as V2 | 18 | import Spear.Math.Vector as V |
19 | import Spear.Math.Vector3 as V3 | ||
20 | 19 | ||
21 | 20 | ||
22 | -- | Compute the normal matrix of the given matrix. | 21 | -- | Compute the normal matrix of the given matrix. |
@@ -39,14 +38,14 @@ unproject :: Matrix4 -- ^ Inverse projection matrix | |||
39 | -> Float -- ^ Window x | 38 | -> Float -- ^ Window x |
40 | -> Float -- ^ Window y | 39 | -> Float -- ^ Window y |
41 | -> Float -- ^ Window z | 40 | -> Float -- ^ Window z |
42 | -> V3.Vector3 | 41 | -> Vector3 |
43 | unproject projI modelviewI vpx vpy w h x y z = | 42 | unproject projI modelviewI vpx vpy w h x y z = |
44 | let | 43 | let |
45 | xmouse = 2*(x-vpx)/w - 1 | 44 | xmouse = 2*(x-vpx)/w - 1 |
46 | ymouse = 2*(y-vpy)/h - 1 | 45 | ymouse = 2*(y-vpy)/h - 1 |
47 | zmouse = 2*z - 1 | 46 | zmouse = 2*z - 1 |
48 | in | 47 | in |
49 | (modelviewI * projI) `M4.mulp` V3.vec3 xmouse ymouse zmouse | 48 | (modelviewI * projI) `M4.mulp` vec3 xmouse ymouse zmouse |
50 | 49 | ||
51 | 50 | ||
52 | -- | Transform the given point in window coordinates to 2d coordinates. | 51 | -- | Transform the given point in window coordinates to 2d coordinates. |
@@ -63,14 +62,14 @@ rpgUnproject | |||
63 | -> Float -- ^ Window x | 62 | -> Float -- ^ Window x |
64 | -> Float -- ^ Window y | 63 | -> Float -- ^ Window y |
65 | -> Vector2 | 64 | -> Vector2 |
66 | rpgUnproject projI viewI vpx vpy w h x y = | 65 | rpgUnproject projI viewI vpx vpy w h wx wy = |
67 | let | 66 | let |
68 | p1 = unproject projI viewI vpx vpy w h x y 0 | 67 | p1 = unproject projI viewI vpx vpy w h wx wy 0 |
69 | p2 = unproject projI viewI vpx vpy w h x y (-1) | 68 | p2 = unproject projI viewI vpx vpy w h wx wy (-1) |
70 | lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) | 69 | lambda = (y p1 / (y p1 - y p2)) |
71 | p' = p1 + V3.scale lambda (p2 - p1) | 70 | p' = p1 + V.scale lambda (p2 - p1) |
72 | in | 71 | in |
73 | vec2 (V3.x p') (-V3.z p') | 72 | vec2 (x p') (-(z p')) |
74 | 73 | ||
75 | 74 | ||
76 | -- | Map an object's transform in view space to world space. | 75 | -- | Map an object's transform in view space to world space. |
@@ -82,33 +81,33 @@ rpgTransform | |||
82 | -> Matrix4 -- ^ Inverse view matrix | 81 | -> Matrix4 -- ^ Inverse view matrix |
83 | -> Matrix4 | 82 | -> Matrix4 |
84 | rpgTransform h a axis pos viewI = | 83 | rpgTransform h a axis pos viewI = |
85 | let p1 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) 0) | 84 | let p1 = viewI `M4.mulp` (vec3 (x pos) (y pos) 0) |
86 | p2 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) (-1)) | 85 | p2 = viewI `M4.mulp` (vec3 (x pos) (y pos) (-1)) |
87 | lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) | 86 | lambda = (y p1 / (y p1 - y p2)) |
88 | p = p1 + V3.scale lambda (p2 - p1) | 87 | p = p1 + V.scale lambda (p2 - p1) |
89 | mat' = axisAngle axis a | 88 | mat' = axisAngle axis a |
90 | r = M4.right mat' | 89 | r = M4.right mat' |
91 | u = M4.up mat' | 90 | u = M4.up mat' |
92 | f = M4.forward mat' | 91 | f = M4.forward mat' |
93 | t = p + vec3 0 h 0 | 92 | t = p + vec3 0 h 0 |
94 | in mat4 | 93 | in mat4 |
95 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) | 94 | (x r) (x u) (x f) (x t) |
96 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) | 95 | (y r) (y u) (y f) (y t) |
97 | (V3.z r) (V3.z u) (V3.z f) (V3.z t) | 96 | (z r) (z u) (z f) (z t) |
98 | 0 0 0 1 | 97 | 0 0 0 1 |
99 | 98 | ||
100 | 99 | ||
101 | -- | Map an object's transform in view space to world space. | 100 | -- | Map an object's transform in view space to world space. |
102 | pltTransform :: Matrix3 -> Matrix4 | 101 | pltTransform :: Matrix3 -> Matrix4 |
103 | pltTransform mat = | 102 | pltTransform mat = |
104 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | 103 | let r = let r' = M3.right mat in vec3 (x r') (y r') 0 |
105 | u = let u' = M3.up mat in vec3 (V2.x u') (V2.y u') 0 | 104 | u = let u' = M3.up mat in vec3 (x u') (y u') 0 |
106 | f = V3.unitz | 105 | f = unitz3 |
107 | t = let t' = M3.position mat in vec3 (V2.x t') (V2.y t') 0 | 106 | t = let t' = M3.position mat in vec3 (x t') (y t') 0 |
108 | in mat4 | 107 | in mat4 |
109 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) | 108 | (x r) (x u) (x f) (x t) |
110 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) | 109 | (y r) (y u) (y f) (y t) |
111 | (V3.z r) (V3.z u) (V3.z f) (V3.z t) | 110 | (z r) (z u) (z f) (z t) |
112 | 0 0 0 1 | 111 | 0 0 0 1 |
113 | 112 | ||
114 | 113 | ||
@@ -147,4 +146,4 @@ objToClip cam model p = | |||
147 | proj = Cam.projection cam | 146 | proj = Cam.projection cam |
148 | p' = (proj * view * model) `M4.mulp` p | 147 | p' = (proj * view * model) `M4.mulp` p |
149 | in | 148 | in |
150 | vec2 (V3.x p') (V3.y p') | 149 | vec2 (x p') (y p') |