diff options
Diffstat (limited to 'Spear/Math/MatrixUtils.hs')
-rw-r--r-- | Spear/Math/MatrixUtils.hs | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs index e6e498f..434e36a 100644 --- a/Spear/Math/MatrixUtils.hs +++ b/Spear/Math/MatrixUtils.hs | |||
@@ -1,6 +1,10 @@ | |||
1 | module Spear.Math.MatrixUtils | 1 | module Spear.Math.MatrixUtils |
2 | ( | 2 | ( |
3 | fastNormalMatrix | 3 | fastNormalMatrix |
4 | , rpgTransform | ||
5 | , pltTransform | ||
6 | , rpgInverse | ||
7 | , pltInverse | ||
4 | ) | 8 | ) |
5 | where | 9 | where |
6 | 10 | ||
@@ -21,6 +25,35 @@ fastNormalMatrix m = | |||
21 | (M4.m02 m') (M4.m12 m') (M4.m22 m') | 25 | (M4.m02 m') (M4.m12 m') (M4.m22 m') |
22 | 26 | ||
23 | 27 | ||
28 | -- | Maps the given 2D transformation matrix to a 3D transformation matrix. | ||
29 | rpgTransform :: Float -- ^ The height above the ground. | ||
30 | -> Matrix3 -> Matrix4 | ||
31 | rpgTransform h mat = | ||
32 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | ||
33 | u = V3.unity | ||
34 | f = let f' = M3.forward mat in vec3 (V2.x f') 0 (-V2.y f') | ||
35 | t = (vec3 0 h 0) + let t' = M3.position mat in -(vec3 (V2.x t') 0 (-V2.y t')) | ||
36 | in mat4 | ||
37 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) | ||
38 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) | ||
39 | (V3.z r) (V3.z u) (V3.z f) (V3.z t) | ||
40 | 0 0 0 1 | ||
41 | |||
42 | |||
43 | -- | Maps the given 2D transformation matrix to a 3D transformation matrix. | ||
44 | pltTransform :: Matrix3 -> Matrix4 | ||
45 | pltTransform mat = | ||
46 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | ||
47 | u = let u' = M3.up mat in vec3 (V2.x u') (V2.y u') 0 | ||
48 | f = V3.unitz | ||
49 | t = let t' = M3.position mat in vec3 (V2.x t') (V2.y t') 0 | ||
50 | in mat4 | ||
51 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) | ||
52 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) | ||
53 | (V3.z r) (V3.z u) (V3.z f) (V3.z t) | ||
54 | 0 0 0 1 | ||
55 | |||
56 | |||
24 | -- | Compute the inverse transform of the given transformation matrix. | 57 | -- | Compute the inverse transform of the given transformation matrix. |
25 | -- | 58 | -- |
26 | -- This function maps an object's transform in 2D to the object's inverse in 3D. | 59 | -- This function maps an object's transform in 2D to the object's inverse in 3D. |
@@ -34,12 +67,12 @@ rpgInverse h mat = | |||
34 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | 67 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 |
35 | u = V3.unity | 68 | u = V3.unity |
36 | f = let f' = M3.forward mat in vec3 (V2.x f') 0 (-V2.y f') | 69 | f = let f' = M3.forward mat in vec3 (V2.x f') 0 (-V2.y f') |
37 | t = let t' = M3.position mat in -(vec3 (V2.x t') 0 (-V2.y t')) | 70 | t = (vec3 0 h 0) + let t' = M3.position mat in -(vec3 (V2.x t') 0 (-V2.y t')) |
38 | in mat4 | 71 | in mat4 |
39 | (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) | 72 | (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) |
40 | (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) | 73 | (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) |
41 | (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) | 74 | (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) |
42 | 0 0 0 1 | 75 | 0 0 0 1 |
43 | 76 | ||
44 | 77 | ||
45 | -- | Compute the inverse transform of the given transformation matrix. | 78 | -- | Compute the inverse transform of the given transformation matrix. |
@@ -59,4 +92,4 @@ pltInverse mat = | |||
59 | (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) | 92 | (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) |
60 | (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) | 93 | (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) |
61 | (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) | 94 | (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) |
62 | 0 0 0 1 | 95 | 0 0 0 1 |