aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Spear/Math/Matrix4.hs72
1 files changed, 36 insertions, 36 deletions
diff --git a/Spear/Math/Matrix4.hs b/Spear/Math/Matrix4.hs
index a03f1da..85ab39f 100644
--- a/Spear/Math/Matrix4.hs
+++ b/Spear/Math/Matrix4.hs
@@ -46,8 +46,8 @@ module Spear.Math.Matrix4
46where 46where
47 47
48 48
49import Spear.Math.Vector3 as Vector3 49import Spear.Math.Vector3 as V3
50import Spear.Math.Vector4 as Vector4 50import Spear.Math.Vector4 as V4
51 51
52import Foreign.Storable 52import Foreign.Storable
53 53
@@ -166,10 +166,10 @@ mat4 = Matrix4
166-- | Build a matrix from four vectors in 4D. 166-- | Build a matrix from four vectors in 4D.
167mat4fromVec :: Vector4 -> Vector4 -> Vector4 -> Vector4 -> Matrix4 167mat4fromVec :: Vector4 -> Vector4 -> Vector4 -> Vector4 -> Matrix4
168mat4fromVec v0 v1 v2 v3 = Matrix4 168mat4fromVec v0 v1 v2 v3 = Matrix4
169 (Vector4.x v0) (Vector4.x v1) (Vector4.x v2) (Vector4.x v3) 169 (V4.x v0) (V4.x v1) (V4.x v2) (V4.x v3)
170 (Vector4.y v0) (Vector4.y v1) (Vector4.y v2) (Vector4.y v3) 170 (V4.y v0) (V4.y v1) (V4.y v2) (V4.y v3)
171 (Vector4.z v0) (Vector4.z v1) (Vector4.z v2) (Vector4.z v3) 171 (V4.z v0) (V4.z v1) (V4.z v2) (V4.z v3)
172 (Vector4.w v0) (Vector4.w v1) (Vector4.w v2) (Vector4.w v3) 172 (V4.w v0) (V4.w v1) (V4.w v2) (V4.w v3)
173 173
174 174
175-- | Build a transformation 'Matrix4' from the given vectors. 175-- | Build a transformation 'Matrix4' from the given vectors.
@@ -180,9 +180,9 @@ transform :: Vector3 -- ^ Right vector.
180 -> Matrix4 180 -> Matrix4
181 181
182transform right up fwd pos = mat4 182transform right up fwd pos = mat4
183 (Vector3.x right) (Vector3.x up) (Vector3.x fwd) (Vector3.x pos) 183 (V3.x right) (V3.x up) (V3.x fwd) (V3.x pos)
184 (Vector3.y right) (Vector3.y up) (Vector3.y fwd) (Vector3.y pos) 184 (V3.y right) (V3.y up) (V3.y fwd) (V3.y pos)
185 (Vector3.z right) (Vector3.z up) (Vector3.z fwd) (Vector3.z pos) 185 (V3.z right) (V3.z up) (V3.z fwd) (V3.z pos)
186 0 0 0 1 186 0 0 0 1
187 187
188 188
@@ -192,8 +192,8 @@ lookAt :: Vector3 -- ^ Eye position.
192 -> Matrix4 192 -> Matrix4
193 193
194lookAt pos target = 194lookAt pos target =
195 let fwd = Vector3.normalise $ target - pos 195 let fwd = V3.normalise $ target - pos
196 r = fwd `cross` Vector3.unity 196 r = fwd `cross` V3.unity
197 u = r `cross` fwd 197 u = r `cross` fwd
198 in 198 in
199 transform r u (-fwd) pos 199 transform r u (-fwd) pos
@@ -238,9 +238,9 @@ transl x y z = mat4
238-- | Create a translation matrix. 238-- | Create a translation matrix.
239translv :: Vector3 -> Matrix4 239translv :: Vector3 -> Matrix4
240translv v = mat4 240translv v = mat4
241 1 0 0 (Vector3.x v) 241 1 0 0 (V3.x v)
242 0 1 0 (Vector3.y v) 242 0 1 0 (V3.y v)
243 0 0 1 (Vector3.z v) 243 0 0 1 (V3.z v)
244 0 0 0 1 244 0 0 0 1
245 245
246 246
@@ -292,9 +292,9 @@ axisAngle v angle = mat4
292 (omc*xz-sy) (omc*yz+sx) (c+omc*z^2) 0 292 (omc*xz-sy) (omc*yz+sx) (c+omc*z^2) 0
293 0 0 0 1 293 0 0 0 1
294 where 294 where
295 x = Vector3.x v 295 x = V3.x v
296 y = Vector3.y v 296 y = V3.y v
297 z = Vector3.z v 297 z = V3.z v
298 s = sin . toRAD $ angle 298 s = sin . toRAD $ angle
299 c = cos . toRAD $ angle 299 c = cos . toRAD $ angle
300 xy = x*y 300 xy = x*y
@@ -323,9 +323,9 @@ scalev v = mat4
323 0 0 sz 0 323 0 0 sz 0
324 0 0 0 1 324 0 0 0 1
325 where 325 where
326 sx = Vector3.x v 326 sx = V3.x v
327 sy = Vector3.y v 327 sy = V3.y v
328 sz = Vector3.z v 328 sz = V3.z v
329 329
330 330
331-- | Create an X reflection matrix. 331-- | Create an X reflection matrix.
@@ -402,28 +402,28 @@ transpose m = mat4
402 402
403-- | Invert the given transformation matrix. 403-- | Invert the given transformation matrix.
404inverseTransform :: Matrix4 -> Matrix4 404inverseTransform :: Matrix4 -> Matrix4
405inverseTransform mat = mat4fromVec u v w p where 405inverseTransform mat =
406 v0 = row0 mat 406 let
407 v1 = row1 mat 407 r = right mat
408 v2 = row2 mat 408 u = up mat
409 u = vec4 (Vector4.x v0) (Vector4.y v0) (Vector4.z v0) 0 409 f = forward mat
410 v = vec4 (Vector4.x v1) (Vector4.y v1) (Vector4.z v1) 0 410 t = position mat
411 w = vec4 (Vector4.x v2) (Vector4.y v2) (Vector4.z v2) 0 411 in
412 p = vec4 tdotu tdotv tdotw 1 412 mat4
413 t = -(col3 mat) 413 (V3.x r) (V3.y r) (V3.z r) (-t `V3.dot` r)
414 tdotu = t `Vector4.dot` col0 mat 414 (V3.x u) (V3.y u) (V3.z u) (-t `V3.dot` u)
415 tdotv = t `Vector4.dot` col1 mat 415 (V3.x f) (V3.y f) (V3.z f) (-t `V3.dot` f)
416 tdotw = t `Vector4.dot` col2 mat 416 0 0 0 1
417 417
418 418
419-- | Transform the given vector in 3D space with the given matrix. 419-- | Transform the given vector in 3D space with the given matrix.
420mul :: Float -> Matrix4 -> Vector3 -> Vector3 420mul :: Float -> Matrix4 -> Vector3 -> Vector3
421mul w m v = vec3 x' y' z' 421mul w m v = vec3 x' y' z'
422 where 422 where
423 v' = vec4 (Vector3.x v) (Vector3.y v) (Vector3.z v) w 423 v' = vec4 (V3.x v) (V3.y v) (V3.z v) w
424 x' = row0 m `Vector4.dot` v' 424 x' = row0 m `V4.dot` v'
425 y' = row1 m `Vector4.dot` v' 425 y' = row1 m `V4.dot` v'
426 z' = row2 m `Vector4.dot` v' 426 z' = row2 m `V4.dot` v'
427 427
428 428
429-- | Transform the given point vector in 3D space with the given matrix. 429-- | Transform the given point vector in 3D space with the given matrix.