diff options
Diffstat (limited to 'Spear/Math/MatrixUtils.hs')
-rw-r--r-- | Spear/Math/MatrixUtils.hs | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs index 44af2c0..87d84e5 100644 --- a/Spear/Math/MatrixUtils.hs +++ b/Spear/Math/MatrixUtils.hs | |||
@@ -5,7 +5,7 @@ module Spear.Math.MatrixUtils | |||
5 | , pltTransform | 5 | , pltTransform |
6 | , rpgInverse | 6 | , rpgInverse |
7 | , pltInverse | 7 | , pltInverse |
8 | , toClip | 8 | , objToClip |
9 | ) | 9 | ) |
10 | where | 10 | where |
11 | 11 | ||
@@ -27,31 +27,28 @@ fastNormalMatrix m = | |||
27 | (M4.m02 m') (M4.m12 m') (M4.m22 m') | 27 | (M4.m02 m') (M4.m12 m') (M4.m22 m') |
28 | 28 | ||
29 | 29 | ||
30 | -- | Maps the given 2D transformation matrix to a 3D transformation matrix. | 30 | -- | Map an object's transform in view space to world space. |
31 | rpgTransform | 31 | rpgTransform |
32 | :: Float -- ^ The height above the ground | 32 | :: Float -- ^ The height above the ground |
33 | -> Float -- ^ Angle of rotation | 33 | -> Float -- ^ Angle of rotation |
34 | -> Vector3 -- ^ Axis of rotation | 34 | -> Vector3 -- ^ Axis of rotation |
35 | -> Vector2 -- ^ Object's position. | 35 | -> Vector2 -- ^ Object's position. |
36 | -> Matrix4 -- ^ Inverse view matrix. | ||
36 | -> Matrix4 | 37 | -> Matrix4 |
37 | rpgTransform h a axis pos = | 38 | rpgTransform h a axis pos viewInverse = |
38 | {-let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | ||
39 | u = V3.unity | ||
40 | f = let f' = M3.forward mat in vec3 (V2.x f') 0 (V2.y f') | ||
41 | t = (vec3 0 h 0) + let t' = M3.position mat in vec3 (V2.x t') 0 (V2.y t')-} | ||
42 | let mat' = axisAngle axis a | 39 | let mat' = axisAngle axis a |
43 | r = M4.right mat' | 40 | r = M4.right mat' |
44 | u = M4.up mat' | 41 | u = M4.up mat' |
45 | f = M4.forward mat' | 42 | f = M4.forward mat' |
46 | t = vec3 0 h 0 + vec3 (V2.x pos) 0 (-V2.y pos) | 43 | t = vec3 0 h 0 + vec3 (V2.x pos) 0 (-V2.y pos) |
47 | in mat4 | 44 | in mat4 |
48 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) | 45 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) |
49 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) | 46 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) |
50 | (V3.z r) (V3.z u) (V3.z f) (V3.z t) | 47 | (V3.z r) (V3.z u) (V3.z f) (V3.z t) |
51 | 0 0 0 1 | 48 | 0 0 0 1 |
52 | 49 | ||
53 | 50 | ||
54 | -- | Maps the given 2D transformation matrix to a 3D transformation matrix. | 51 | -- | Map an object's transform in view space to world space. |
55 | pltTransform :: Matrix3 -> Matrix4 | 52 | pltTransform :: Matrix3 -> Matrix4 |
56 | pltTransform mat = | 53 | pltTransform mat = |
57 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | 54 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 |
@@ -65,23 +62,22 @@ pltTransform mat = | |||
65 | 0 0 0 1 | 62 | 0 0 0 1 |
66 | 63 | ||
67 | 64 | ||
68 | -- | Compute the inverse transform of the given transformation matrix. | 65 | -- | Map an object's transform in world space to view space. |
69 | -- | ||
70 | -- This function maps an object's transform in 2D to the object's inverse in 3D. | ||
71 | -- | 66 | -- |
72 | -- The XY plane in 2D translates to the X(-Z) plane in 3D. | 67 | -- The XY plane in 2D translates to the X(-Z) plane in 3D. |
73 | -- | 68 | -- |
74 | -- Use this in games such as RPGs and RTSs. | 69 | -- Use this in games such as RPGs and RTSs. |
75 | rpgInverse | 70 | rpgInverse |
76 | :: Float -- ^ The height above the ground | 71 | :: Float -- ^ The height above the ground |
77 | -> Float -- ^ Angle of rotation | 72 | -> Float -- ^ Angle of rotation |
78 | -> Vector3 -- ^ Axis of rotation | 73 | -> Vector3 -- ^ Axis of rotation |
79 | -> Vector2 | 74 | -> Vector2 -- ^ Object's position. |
75 | -> Matrix4 -- ^ Inverse view matrix. | ||
80 | -> Matrix4 | 76 | -> Matrix4 |
81 | rpgInverse h a rot pos = M4.inverseTransform $ rpgTransform h a rot pos | 77 | rpgInverse h a rot pos viewInv = M4.inverseTransform $ rpgTransform h a rot pos viewInv |
82 | 78 | ||
83 | 79 | ||
84 | -- | Compute the inverse transform of the given transformation matrix. | 80 | -- | Map an object's transform in world space to view space. |
85 | -- | 81 | -- |
86 | -- This function maps an object's transform in 2D to the object's inverse in 3D. | 82 | -- This function maps an object's transform in 2D to the object's inverse in 3D. |
87 | -- | 83 | -- |
@@ -93,8 +89,8 @@ pltInverse = M4.inverseTransform . pltTransform | |||
93 | 89 | ||
94 | 90 | ||
95 | -- | Transform an object from object to clip space coordinates. | 91 | -- | Transform an object from object to clip space coordinates. |
96 | toClip :: Camera -> Matrix4 -> Vector3 -> Vector2 | 92 | objToClip :: Camera -> Matrix4 -> Vector3 -> Vector2 |
97 | toClip cam model p = | 93 | objToClip cam model p = |
98 | let | 94 | let |
99 | view = M4.inverseTransform $ Cam.transform cam | 95 | view = M4.inverseTransform $ Cam.transform cam |
100 | proj = Cam.projection cam | 96 | proj = Cam.projection cam |