diff options
-rw-r--r-- | Spear/Math/Matrix4.hs | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/Spear/Math/Matrix4.hs b/Spear/Math/Matrix4.hs index ac2761f..a4ad651 100644 --- a/Spear/Math/Matrix4.hs +++ b/Spear/Math/Matrix4.hs | |||
@@ -35,7 +35,8 @@ module Spear.Math.Matrix4 | |||
35 | , reflectZ | 35 | , reflectZ |
36 | -- ** Projection | 36 | -- ** Projection |
37 | , ortho | 37 | , ortho |
38 | , perspective | 38 | , perspective |
39 | , planeProj | ||
39 | -- * Operations | 40 | -- * Operations |
40 | , Spear.Math.Matrix4.zipWith | 41 | , Spear.Math.Matrix4.zipWith |
41 | , Spear.Math.Matrix4.map | 42 | , Spear.Math.Matrix4.map |
@@ -44,7 +45,8 @@ module Spear.Math.Matrix4 | |||
44 | , inverse | 45 | , inverse |
45 | , mul | 46 | , mul |
46 | , mulp | 47 | , mulp |
47 | , muld | 48 | , muld |
49 | , mul' | ||
48 | ) | 50 | ) |
49 | where | 51 | where |
50 | 52 | ||
@@ -420,8 +422,28 @@ perspective fovy r near far = | |||
420 | 0 f 0 0 | 422 | 0 f 0 0 |
421 | 0 0 ((near+far)/a) (2*near*far/a) | 423 | 0 0 ((near+far)/a) (2*near*far/a) |
422 | 0 0 (-1) 0 | 424 | 0 0 (-1) 0 |
425 | |||
426 | |||
427 | -- | Create a plane projection matrix. | ||
428 | planeProj :: Vector3 -- ^ Plane normal | ||
429 | -> Float -- ^ Plane distance from the origin | ||
430 | -> Vector3 -- ^ Projection direction | ||
431 | -> Matrix4 | ||
432 | planeProj n d l = | ||
433 | let c = n `V3.dot` l | ||
434 | nx = V3.x n | ||
435 | ny = V3.y n | ||
436 | nz = V3.z n | ||
437 | lx = V3.x l | ||
438 | ly = V3.y l | ||
439 | lz = V3.z l | ||
440 | in mat4 | ||
441 | (d + c - nx*lx) (-ny*lx) (-nz*lx) (-lx*d) | ||
442 | (-nx*ly) (d + c - ny*ly) (-nz*ly) (-ly*d) | ||
443 | (-nx*lz) (-ny*lz) (d + c - nz*lz) (-lz*d) | ||
444 | (-nx) (-ny) (-nz) c | ||
445 | |||
423 | 446 | ||
424 | |||
425 | -- | Transpose the specified matrix. | 447 | -- | Transpose the specified matrix. |
426 | transpose :: Matrix4 -> Matrix4 | 448 | transpose :: Matrix4 -> Matrix4 |
427 | transpose m = mat4 | 449 | transpose m = mat4 |
@@ -609,7 +631,21 @@ mulp = mul 1 | |||
609 | 631 | ||
610 | -- | Transform the given directional vector in 3D space with the given matrix. | 632 | -- | Transform the given directional vector in 3D space with the given matrix. |
611 | muld :: Matrix4 -> Vector3 -> Vector3 | 633 | muld :: Matrix4 -> Vector3 -> Vector3 |
612 | muld = mul 0 | 634 | muld = mul 0 |
635 | |||
636 | |||
637 | -- | Transform the given vector with the given matrix. | ||
638 | -- | ||
639 | -- The vector is brought from homogeneous space to 3D space by performing a | ||
640 | -- perspective divide. | ||
641 | mul' :: Float -> Matrix4 -> Vector3 -> Vector3 | ||
642 | mul' w m v = vec3 (x'/w') (y'/w') (z'/w') | ||
643 | where | ||
644 | v' = vec4 (V3.x v) (V3.y v) (V3.z v) w | ||
645 | x' = row0 m `V4.dot` v' | ||
646 | y' = row1 m `V4.dot` v' | ||
647 | z' = row2 m `V4.dot` v' | ||
648 | w' = row3 m `V4.dot` v' | ||
613 | 649 | ||
614 | 650 | ||
615 | toRAD = (*pi) . (/180) | 651 | toRAD = (*pi) . (/180) |