diff options
-rw-r--r-- | Spear/Math/Utils.hs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/Spear/Math/Utils.hs b/Spear/Math/Utils.hs index 28f012e..e86cacc 100644 --- a/Spear/Math/Utils.hs +++ b/Spear/Math/Utils.hs | |||
@@ -3,11 +3,14 @@ module Spear.Math.Utils | |||
3 | Side(..) | 3 | Side(..) |
4 | , Face(..) | 4 | , Face(..) |
5 | , orientation2d | 5 | , orientation2d |
6 | , viewToWorld2d | ||
6 | ) | 7 | ) |
7 | where | 8 | where |
8 | 9 | ||
9 | 10 | ||
10 | import Spear.Math.Vector2 | 11 | import Spear.Math.Matrix4 as M4 |
12 | import Spear.Math.Vector2 as V2 | ||
13 | import qualified Spear.Math.Vector3 as V3 | ||
11 | 14 | ||
12 | 15 | ||
13 | data Side = L | R deriving (Eq, Show) | 16 | data Side = L | R deriving (Eq, Show) |
@@ -19,3 +22,18 @@ data Face = F | B deriving (Eq, Show) | |||
19 | -- | Return the signed area of the triangle defined by the given points. | 22 | -- | Return the signed area of the triangle defined by the given points. |
20 | orientation2d :: Vector2 -> Vector2 -> Vector2 -> Float | 23 | orientation2d :: Vector2 -> Vector2 -> Vector2 -> Float |
21 | orientation2d p q r = (x q - x p) * (y r - y p) - (y q - y p) * (x r - x p) | 24 | orientation2d p q r = (x q - x p) * (y r - y p) - (y q - y p) * (x r - x p) |
25 | |||
26 | |||
27 | -- | Project the given point in view space onto the XZ plane in world space. | ||
28 | viewToWorld2d :: Vector2 | ||
29 | -> Matrix4 | ||
30 | -> Vector2 | ||
31 | viewToWorld2d p viewI = | ||
32 | let | ||
33 | p1' = V3.vec3 (V2.x p) (V2.y p) 0 | ||
34 | p1 = viewI `mulp` p1' | ||
35 | p2 = p1 - M4.forward viewI | ||
36 | lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) | ||
37 | p' = p1 + V3.scale lambda (p2 - p1) | ||
38 | in | ||
39 | vec2 (V3.x p') (-V3.z p') | ||