diff options
Diffstat (limited to 'Spear/Math/MatrixUtils.hs')
-rw-r--r-- | Spear/Math/MatrixUtils.hs | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs index 567bee1..cca5c48 100644 --- a/Spear/Math/MatrixUtils.hs +++ b/Spear/Math/MatrixUtils.hs | |||
@@ -1,3 +1,5 @@ | |||
1 | {-# LANGUAGE NoImplicitPrelude #-} | ||
2 | |||
1 | module Spear.Math.MatrixUtils | 3 | module Spear.Math.MatrixUtils |
2 | ( | 4 | ( |
3 | fastNormalMatrix | 5 | fastNormalMatrix |
@@ -11,11 +13,12 @@ module Spear.Math.MatrixUtils | |||
11 | ) | 13 | ) |
12 | where | 14 | where |
13 | 15 | ||
14 | import Spear.Math.Camera as Cam | 16 | import Spear.Math.Camera as Cam |
15 | import Spear.Math.Matrix3 as M3 | 17 | import Spear.Math.Matrix3 as M3 |
16 | import Spear.Math.Matrix4 as M4 | 18 | import Spear.Math.Matrix4 as M4 |
17 | import Spear.Math.Spatial3 as S | 19 | import Spear.Math.Spatial3 as S |
18 | import Spear.Math.Vector as V | 20 | import Spear.Math.Vector as V |
21 | import Spear.Prelude | ||
19 | 22 | ||
20 | -- | Compute the normal matrix of the given matrix. | 23 | -- | Compute the normal matrix of the given matrix. |
21 | fastNormalMatrix :: Matrix4 -> Matrix3 | 24 | fastNormalMatrix :: Matrix4 -> Matrix3 |
@@ -39,9 +42,9 @@ unproject :: Matrix4 -- ^ Inverse projection matrix | |||
39 | -> Vector3 | 42 | -> Vector3 |
40 | unproject projI modelviewI vpx vpy w h x y z = | 43 | unproject projI modelviewI vpx vpy w h x y z = |
41 | let | 44 | let |
42 | xmouse = 2*(x-vpx)/w - 1 | 45 | xmouse = (2::Float) * (x-vpx)/w - (1::Float) |
43 | ymouse = 2*(y-vpy)/h - 1 | 46 | ymouse = (2::Float) * (y-vpy)/h - (1::Float) |
44 | zmouse = 2*z - 1 | 47 | zmouse = (2::Float) * z - (1::Float) |
45 | in | 48 | in |
46 | (modelviewI * projI) `M4.mulp` vec3 xmouse ymouse zmouse | 49 | (modelviewI * projI) `M4.mulp` vec3 xmouse ymouse zmouse |
47 | 50 | ||
@@ -64,7 +67,7 @@ rpgUnproject projI viewI vpx vpy w h wx wy = | |||
64 | p1 = unproject projI viewI vpx vpy w h wx wy 0 | 67 | p1 = unproject projI viewI vpx vpy w h wx wy 0 |
65 | p2 = unproject projI viewI vpx vpy w h wx wy (-1) | 68 | p2 = unproject projI viewI vpx vpy w h wx wy (-1) |
66 | lambda = (y p1 / (y p1 - y p2)) | 69 | lambda = (y p1 / (y p1 - y p2)) |
67 | p' = p1 + V.scale lambda (p2 - p1) | 70 | p' = p1 + lambda * (p2 - p1) |
68 | in | 71 | in |
69 | vec2 (x p') (-(z p')) | 72 | vec2 (x p') (-(z p')) |
70 | 73 | ||
@@ -77,10 +80,10 @@ rpgTransform | |||
77 | -> Matrix4 -- ^ Inverse view matrix | 80 | -> Matrix4 -- ^ Inverse view matrix |
78 | -> Matrix4 | 81 | -> Matrix4 |
79 | rpgTransform h a axis pos viewI = | 82 | rpgTransform h a axis pos viewI = |
80 | let p1 = viewI `M4.mulp` (vec3 (x pos) (y pos) 0) | 83 | let p1 = viewI `M4.mulp` vec3 (x pos) (y pos) 0 |
81 | p2 = viewI `M4.mulp` (vec3 (x pos) (y pos) (-1)) | 84 | p2 = viewI `M4.mulp` vec3 (x pos) (y pos) (-1) |
82 | lambda = (y p1 / (y p1 - y p2)) | 85 | lambda = (y p1 / (y p1 - y p2)) |
83 | p = p1 + V.scale lambda (p2 - p1) | 86 | p = p1 + lambda * (p2 - p1) |
84 | mat' = axisAngle axis a | 87 | mat' = axisAngle axis a |
85 | r = M4.right mat' | 88 | r = M4.right mat' |
86 | u = M4.up mat' | 89 | u = M4.up mat' |
@@ -134,8 +137,8 @@ pltInverse = M4.inverseTransform . pltTransform | |||
134 | objToClip :: Camera -> Matrix4 -> Vector3 -> Vector2 | 137 | objToClip :: Camera -> Matrix4 -> Vector3 -> Vector2 |
135 | objToClip cam model p = | 138 | objToClip cam model p = |
136 | let | 139 | let |
137 | view = M4.inverseTransform $ S.transform cam | 140 | view = M4.inverseTransform . transform3Matrix . transform3 $ cam |
138 | proj = Cam.projection cam | 141 | proj = projection cam |
139 | p' = (proj * view * model) `M4.mulp` p | 142 | p' = (proj * view * model) `M4.mulp` p |
140 | in | 143 | in |
141 | vec2 (x p') (y p') | 144 | vec2 (x p') (y p') |