diff options
| author | Jeanne-Kamikaze <jeannekamikaze@gmail.com> | 2013-02-21 19:24:47 +0100 |
|---|---|---|
| committer | Jeanne-Kamikaze <jeannekamikaze@gmail.com> | 2013-02-21 19:24:47 +0100 |
| commit | fc802a58cff52083327b343ba04c9e67331d9d5a (patch) | |
| tree | 710492e605c4567fb061fe53bd99cceb8ca02bd8 | |
| parent | 44a2fc13f57454ed54223d028e287e053609971a (diff) | |
Added Vector and Vector.Class modules
34 files changed, 430 insertions, 455 deletions
diff --git a/Spear.cabal b/Spear.cabal index 051d955..f7d0536 100644 --- a/Spear.cabal +++ b/Spear.cabal | |||
| @@ -22,13 +22,15 @@ library | |||
| 22 | Spear.Math.AABB Spear.Math.Circle Spear.Math.Triangle Spear.Game | 22 | Spear.Math.AABB Spear.Math.Circle Spear.Math.Triangle Spear.Game |
| 23 | Spear.GLSL Spear.Math.Camera Spear.Math.Entity Spear.Math.Matrix3 | 23 | Spear.GLSL Spear.Math.Camera Spear.Math.Entity Spear.Math.Matrix3 |
| 24 | Spear.Math.Matrix4 Spear.Math.MatrixUtils Spear.Math.Plane | 24 | Spear.Math.Matrix4 Spear.Math.MatrixUtils Spear.Math.Plane |
| 25 | Spear.Math.Quaternion Spear.Math.Vector3 Spear.Math.Vector4 | 25 | Spear.Math.Quaternion Spear.Math.Vector Spear.Math.Vector.Class |
| 26 | Spear.Math.Vector.Vector3 Spear.Math.Vector.Vector4 | ||
| 27 | Spear.Math.Vector.Vector2 | ||
| 26 | Spear.Physics Spear.Physics.Rigid Spear.Render.AnimatedModel | 28 | Spear.Physics Spear.Physics.Rigid Spear.Render.AnimatedModel |
| 27 | Spear.Render.Material Spear.Render.Model Spear.Render.Program | 29 | Spear.Render.Material Spear.Render.Model Spear.Render.Program |
| 28 | Spear.Render.StaticModel Spear.Scene.Graph Spear.Scene.Light | 30 | Spear.Render.StaticModel Spear.Scene.Graph Spear.Scene.Light |
| 29 | Spear.Scene.Loader Spear.Scene.Scene Spear.Scene.SceneResources | 31 | Spear.Scene.Loader Spear.Scene.Scene Spear.Scene.SceneResources |
| 30 | Spear.Setup Spear.Sys.Timer Spear.Sys.Store Spear.Sys.Store.ID | 32 | Spear.Setup Spear.Sys.Timer Spear.Sys.Store Spear.Sys.Store.ID |
| 31 | Spear.Math.Vector2 Spear.Math.Quad Spear.Math.Ray | 33 | Spear.Math.Quad Spear.Math.Ray |
| 32 | Spear.Math.Segment Spear.Math.Utils Spear.Math.Spatial2 | 34 | Spear.Math.Segment Spear.Math.Utils Spear.Math.Spatial2 |
| 33 | Spear.Math.Spatial3 | 35 | Spear.Math.Spatial3 |
| 34 | exposed: True | 36 | exposed: True |
diff --git a/Spear/Collision.hs b/Spear/Collision.hs index 8c334c1..3b80696 100644 --- a/Spear/Collision.hs +++ b/Spear/Collision.hs | |||
| @@ -5,13 +5,13 @@ module Spear.Collision | |||
| 5 | , Collisionable(..) | 5 | , Collisionable(..) |
| 6 | -- * Collisioners | 6 | -- * Collisioners |
| 7 | , Collisioner(..) | 7 | , Collisioner(..) |
| 8 | -- ** Construction | 8 | -- ** Construction |
| 9 | , aabbCollisioner | 9 | , aabbCollisioner |
| 10 | , circleCollisioner | 10 | , circleCollisioner |
| 11 | , boxFromCircle | 11 | , boxFromCircle |
| 12 | , buildAABB | 12 | , buildAABB |
| 13 | , mkCols | 13 | , mkCols |
| 14 | -- ** Collision test | 14 | -- ** Collision test |
| 15 | , collide | 15 | , collide |
| 16 | -- ** Manipulation | 16 | -- ** Manipulation |
| 17 | , move | 17 | , move |
| @@ -26,8 +26,7 @@ import Spear.Math.AABB | |||
| 26 | import Spear.Math.Circle | 26 | import Spear.Math.Circle |
| 27 | import qualified Spear.Math.Matrix4 as M4 | 27 | import qualified Spear.Math.Matrix4 as M4 |
| 28 | import Spear.Math.Plane | 28 | import Spear.Math.Plane |
| 29 | import Spear.Math.Vector2 | 29 | import Spear.Math.Vector |
| 30 | import qualified Spear.Math.Vector3 as V3 | ||
| 31 | 30 | ||
| 32 | 31 | ||
| 33 | -- | Encodes several collision situations. | 32 | -- | Encodes several collision situations. |
| @@ -118,54 +117,54 @@ aabbPoints (AABB min max) = [p1,p2,p3,p4,p5,p6,p7,p8] | |||
| 118 | p8 = vec2 (x max) (y max) | 117 | p8 = vec2 (x max) (y max) |
| 119 | 118 | ||
| 120 | 119 | ||
| 121 | -- | A collisioner component. | 120 | -- | A collisioner component. |
| 122 | data Collisioner | 121 | data Collisioner |
| 123 | -- | An axis-aligned bounding box. | 122 | -- | An axis-aligned bounding box. |
| 124 | = AABBCol {-# UNPACK #-} !AABB | 123 | = AABBCol {-# UNPACK #-} !AABB |
| 125 | -- | A bounding circle. | 124 | -- | A bounding circle. |
| 126 | | CircleCol {-# UNPACK #-} !Circle | 125 | | CircleCol {-# UNPACK #-} !Circle |
| 127 | 126 | ||
| 128 | 127 | ||
| 129 | -- | Create a collisioner from the specified box. | 128 | -- | Create a collisioner from the specified box. |
| 130 | aabbCollisioner :: AABB -> Collisioner | 129 | aabbCollisioner :: AABB -> Collisioner |
| 131 | aabbCollisioner = AABBCol | 130 | aabbCollisioner = AABBCol |
| 132 | 131 | ||
| 133 | 132 | ||
| 134 | -- | Create a collisioner from the specified circle. | 133 | -- | Create a collisioner from the specified circle. |
| 135 | circleCollisioner :: Circle -> Collisioner | 134 | circleCollisioner :: Circle -> Collisioner |
| 136 | circleCollisioner = CircleCol | 135 | circleCollisioner = CircleCol |
| 137 | 136 | ||
| 138 | 137 | ||
| 139 | -- | Create the minimal AABB collisioner fully containing the specified circle. | 138 | -- | Create the minimal AABB collisioner fully containing the specified circle. |
| 140 | boxFromCircle :: Circle -> Collisioner | 139 | boxFromCircle :: Circle -> Collisioner |
| 141 | boxFromCircle = AABBCol . aabbFromCircle | 140 | boxFromCircle = AABBCol . aabbFromCircle |
| 142 | 141 | ||
| 143 | 142 | ||
| 144 | -- | Create the minimal AABB fully containing the specified collisioners. | 143 | -- | Create the minimal AABB fully containing the specified collisioners. |
| 145 | buildAABB :: [Collisioner] -> AABB | 144 | buildAABB :: [Collisioner] -> AABB |
| 146 | buildAABB cols = aabb $ generatePoints cols | 145 | buildAABB cols = aabb $ generatePoints cols |
| 147 | 146 | ||
| 148 | 147 | ||
| 149 | generatePoints :: [Collisioner] -> [Vector2] | 148 | generatePoints :: [Collisioner] -> [Vector2] |
| 150 | generatePoints = foldr generate [] | 149 | generatePoints = foldr generate [] |
| 151 | where | 150 | where |
| 152 | generate (AABBCol (AABB min max)) acc = p1:p2:p3:p4:p5:p6:p7:p8:acc | 151 | generate (AABBCol (AABB pmin pmax)) acc = p1:p2:p3:p4:p5:p6:p7:p8:acc |
| 153 | where | 152 | where |
| 154 | p1 = vec2 (x min) (y min) | 153 | p1 = vec2 (x pmin) (y pmin) |
| 155 | p2 = vec2 (x min) (y min) | 154 | p2 = vec2 (x pmin) (y pmin) |
| 156 | p3 = vec2 (x min) (y max) | 155 | p3 = vec2 (x pmin) (y pmax) |
| 157 | p4 = vec2 (x min) (y max) | 156 | p4 = vec2 (x pmin) (y pmax) |
| 158 | p5 = vec2 (x max) (y min) | 157 | p5 = vec2 (x pmax) (y pmin) |
| 159 | p6 = vec2 (x max) (y min) | 158 | p6 = vec2 (x pmax) (y pmin) |
| 160 | p7 = vec2 (x max) (y max) | 159 | p7 = vec2 (x pmax) (y pmax) |
| 161 | p8 = vec2 (x max) (y max) | 160 | p8 = vec2 (x pmax) (y pmax) |
| 162 | 161 | ||
| 163 | generate (CircleCol (Circle c r)) acc = p1:p2:p3:p4:acc | 162 | generate (CircleCol (Circle c r)) acc = p1:p2:p3:p4:acc |
| 164 | where | 163 | where |
| 165 | p1 = c + unitx * (vec2 r r) | 164 | p1 = c + unitx2 * (vec2 r r) |
| 166 | p2 = c - unitx * (vec2 r r) | 165 | p2 = c - unitx2 * (vec2 r r) |
| 167 | p3 = c + unity * (vec2 r r) | 166 | p3 = c + unity2 * (vec2 r r) |
| 168 | p4 = c - unity * (vec2 r r) | 167 | p4 = c - unity2 * (vec2 r r) |
| 169 | 168 | ||
| 170 | 169 | ||
| 171 | -- | Compute AABB collisioners in view space from the given 3D AABB. | 170 | -- | Compute AABB collisioners in view space from the given 3D AABB. |
| @@ -174,22 +173,22 @@ mkCols :: M4.Matrix4 -- ^ Modelview matrix | |||
| 174 | -> [Collisioner] | 173 | -> [Collisioner] |
| 175 | mkCols modelview (Box (Vec3 xmin ymin zmin) (Vec3 xmax ymax zmax)) = | 174 | mkCols modelview (Box (Vec3 xmin ymin zmin) (Vec3 xmax ymax zmax)) = |
| 176 | let | 175 | let |
| 177 | toVec2 v = vec2 (V3.x v) (V3.y v) | 176 | toVec2 v = vec2 (x v) (y v) |
| 178 | p1 = toVec2 $ modelview `M4.mulp` V3.vec3 xmin ymin zmax | 177 | p1 = toVec2 $ modelview `M4.mulp` vec3 xmin ymin zmax |
| 179 | p2 = toVec2 $ modelview `M4.mulp` V3.vec3 xmax ymin zmin | 178 | p2 = toVec2 $ modelview `M4.mulp` vec3 xmax ymin zmin |
| 180 | p3 = toVec2 $ modelview `M4.mulp` V3.vec3 xmax ymax zmin | 179 | p3 = toVec2 $ modelview `M4.mulp` vec3 xmax ymax zmin |
| 181 | col1 = AABBCol $ AABB p1 p2 | 180 | col1 = AABBCol $ AABB p1 p2 |
| 182 | col2 = AABBCol $ AABB p1 p3 | 181 | col2 = AABBCol $ AABB p1 p3 |
| 183 | in | 182 | in |
| 184 | [col1, col2] | 183 | [col1, col2] |
| 185 | 184 | ||
| 186 | 185 | ||
| 187 | -- | Collide the given collisioners. | 186 | -- | Collide the given collisioners. |
| 188 | collide :: Collisioner -> Collisioner -> CollisionType | 187 | collide :: Collisioner -> Collisioner -> CollisionType |
| 189 | collide (AABBCol box1) (AABBCol box2) = collideBox box1 box2 | 188 | collide (AABBCol box1) (AABBCol box2) = collideBox box1 box2 |
| 190 | collide (CircleCol s1) (CircleCol s2) = collideCircle s1 s2 | 189 | collide (CircleCol s1) (CircleCol s2) = collideCircle s1 s2 |
| 191 | collide (AABBCol box) (CircleCol circle) = collideBox box circle | 190 | collide (AABBCol box) (CircleCol circle) = collideBox box circle |
| 192 | collide (CircleCol circle) (AABBCol box) = collideCircle circle box | 191 | collide (CircleCol circle) (AABBCol box) = collideCircle circle box |
| 193 | 192 | ||
| 194 | 193 | ||
| 195 | -- | Move the collisioner. | 194 | -- | Move the collisioner. |
diff --git a/Spear/GLSL.hs b/Spear/GLSL.hs index c00582d..2947515 100644 --- a/Spear/GLSL.hs +++ b/Spear/GLSL.hs | |||
| @@ -93,9 +93,7 @@ where | |||
| 93 | import Spear.Assets.Image | 93 | import Spear.Assets.Image |
| 94 | import Spear.Math.Matrix3 (Matrix3) | 94 | import Spear.Math.Matrix3 (Matrix3) |
| 95 | import Spear.Math.Matrix4 (Matrix4) | 95 | import Spear.Math.Matrix4 (Matrix4) |
| 96 | import Spear.Math.Vector2 as V2 | 96 | import Spear.Math.Vector |
| 97 | import Spear.Math.Vector3 as V3 | ||
| 98 | import Spear.Math.Vector4 as V4 | ||
| 99 | import Spear.Setup | 97 | import Spear.Setup |
| 100 | 98 | ||
| 101 | import Control.Monad | 99 | import Control.Monad |
| @@ -371,25 +369,25 @@ readSource' file = do | |||
| 371 | -- | Load a 2D vector. | 369 | -- | Load a 2D vector. |
| 372 | uniformVec2 :: GLint -> Vector2 -> IO () | 370 | uniformVec2 :: GLint -> Vector2 -> IO () |
| 373 | uniformVec2 loc v = glUniform2f loc x' y' | 371 | uniformVec2 loc v = glUniform2f loc x' y' |
| 374 | where x' = unsafeCoerce $ V2.x v | 372 | where x' = unsafeCoerce $ x v |
| 375 | y' = unsafeCoerce $ V2.y v | 373 | y' = unsafeCoerce $ y v |
| 376 | 374 | ||
| 377 | 375 | ||
| 378 | -- | Load a 3D vector. | 376 | -- | Load a 3D vector. |
| 379 | uniformVec3 :: GLint -> Vector3 -> IO () | 377 | uniformVec3 :: GLint -> Vector3 -> IO () |
| 380 | uniformVec3 loc v = glUniform3f loc x' y' z' | 378 | uniformVec3 loc v = glUniform3f loc x' y' z' |
| 381 | where x' = unsafeCoerce $ V3.x v | 379 | where x' = unsafeCoerce $ x v |
| 382 | y' = unsafeCoerce $ V3.y v | 380 | y' = unsafeCoerce $ y v |
| 383 | z' = unsafeCoerce $ V3.z v | 381 | z' = unsafeCoerce $ z v |
| 384 | 382 | ||
| 385 | 383 | ||
| 386 | -- | Load a 4D vector. | 384 | -- | Load a 4D vector. |
| 387 | uniformVec4 :: GLint -> Vector4 -> IO () | 385 | uniformVec4 :: GLint -> Vector4 -> IO () |
| 388 | uniformVec4 loc v = glUniform4f loc x' y' z' w' | 386 | uniformVec4 loc v = glUniform4f loc x' y' z' w' |
| 389 | where x' = unsafeCoerce $ V4.x v | 387 | where x' = unsafeCoerce $ x v |
| 390 | y' = unsafeCoerce $ V4.y v | 388 | y' = unsafeCoerce $ y v |
| 391 | z' = unsafeCoerce $ V4.z v | 389 | z' = unsafeCoerce $ z v |
| 392 | w' = unsafeCoerce $ V4.w v | 390 | w' = unsafeCoerce $ w v |
| 393 | 391 | ||
| 394 | 392 | ||
| 395 | -- | Load a 3x3 matrix. | 393 | -- | Load a 3x3 matrix. |
diff --git a/Spear/Math/AABB.hs b/Spear/Math/AABB.hs index cd945a6..0dacfa4 100644 --- a/Spear/Math/AABB.hs +++ b/Spear/Math/AABB.hs | |||
| @@ -7,7 +7,7 @@ module Spear.Math.AABB | |||
| 7 | where | 7 | where |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | import Spear.Math.Vector2 | 10 | import Spear.Math.Vector |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | -- | An axis-aligned bounding box. | 13 | -- | An axis-aligned bounding box. |
diff --git a/Spear/Math/Camera.hs b/Spear/Math/Camera.hs index acde7d0..e22f3c2 100644 --- a/Spear/Math/Camera.hs +++ b/Spear/Math/Camera.hs | |||
| @@ -4,7 +4,7 @@ where | |||
| 4 | 4 | ||
| 5 | import qualified Spear.Math.Matrix4 as M | 5 | import qualified Spear.Math.Matrix4 as M |
| 6 | import qualified Spear.Math.Spatial3 as S | 6 | import qualified Spear.Math.Spatial3 as S |
| 7 | import Spear.Math.Vector3 | 7 | import Spear.Math.Vector |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | data Camera = Camera | 10 | data Camera = Camera |
diff --git a/Spear/Math/Circle.hs b/Spear/Math/Circle.hs index daaafc5..ab256a4 100644 --- a/Spear/Math/Circle.hs +++ b/Spear/Math/Circle.hs | |||
| @@ -7,7 +7,7 @@ module Spear.Math.Circle | |||
| 7 | where | 7 | where |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | import Spear.Math.Vector2 | 10 | import Spear.Math.Vector |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | -- | A bounding volume. | 13 | -- | A bounding volume. |
diff --git a/Spear/Math/Entity.hs b/Spear/Math/Entity.hs index 2dc6965..4fc3d87 100644 --- a/Spear/Math/Entity.hs +++ b/Spear/Math/Entity.hs | |||
| @@ -7,7 +7,7 @@ where | |||
| 7 | 7 | ||
| 8 | import qualified Spear.Math.Matrix3 as M | 8 | import qualified Spear.Math.Matrix3 as M |
| 9 | import qualified Spear.Math.Spatial2 as S | 9 | import qualified Spear.Math.Spatial2 as S |
| 10 | import qualified Spear.Math.Vector2 as V | 10 | import qualified Spear.Math.Vector as V |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | -- | An entity in 2D space. | 13 | -- | An entity in 2D space. |
diff --git a/Spear/Math/Matrix3.hs b/Spear/Math/Matrix3.hs index d5e46e9..497cb4e 100644 --- a/Spear/Math/Matrix3.hs +++ b/Spear/Math/Matrix3.hs | |||
| @@ -40,8 +40,7 @@ module Spear.Math.Matrix3 | |||
| 40 | where | 40 | where |
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | import Spear.Math.Vector2 as V2 | 43 | import Spear.Math.Vector |
| 44 | import Spear.Math.Vector3 as V3 | ||
| 45 | 44 | ||
| 46 | import Foreign.Storable | 45 | import Foreign.Storable |
| 47 | 46 | ||
| @@ -142,9 +141,9 @@ mat3 = Matrix3 | |||
| 142 | -- | Build a matrix from three vectors in 3D. | 141 | -- | Build a matrix from three vectors in 3D. |
| 143 | mat3fromVec :: Vector3 -> Vector3 -> Vector3 -> Matrix3 | 142 | mat3fromVec :: Vector3 -> Vector3 -> Vector3 -> Matrix3 |
| 144 | mat3fromVec v0 v1 v2 = Matrix3 | 143 | mat3fromVec v0 v1 v2 = Matrix3 |
| 145 | (V3.x v0) (V3.x v1) (V3.x v2) | 144 | (x v0) (x v1) (x v2) |
| 146 | (V3.y v0) (V3.y v1) (V3.y v2) | 145 | (y v0) (y v1) (y v2) |
| 147 | (V3.z v0) (V3.z v1) (V3.z v2) | 146 | (z v0) (z v1) (z v2) |
| 148 | 147 | ||
| 149 | 148 | ||
| 150 | -- | Build a transformation matrix. | 149 | -- | Build a transformation matrix. |
| @@ -154,8 +153,8 @@ transform :: Vector2 -- ^ Right vector | |||
| 154 | -> Matrix3 -- ^ Transform | 153 | -> Matrix3 -- ^ Transform |
| 155 | 154 | ||
| 156 | transform r f p = mat3 | 155 | transform r f p = mat3 |
| 157 | (V2.x r) (V2.x f) (V2.x p) | 156 | (x r) (x f) (x p) |
| 158 | (V2.y r) (V2.y f) (V2.y p) | 157 | (y r) (y f) (y p) |
| 159 | 0 0 1 | 158 | 0 0 1 |
| 160 | 159 | ||
| 161 | 160 | ||
| @@ -205,8 +204,8 @@ transl tx ty = mat3 | |||
| 205 | -- | Create a translation matrix. | 204 | -- | Create a translation matrix. |
| 206 | translv :: Vector2 -> Matrix3 | 205 | translv :: Vector2 -> Matrix3 |
| 207 | translv v = mat3 | 206 | translv v = mat3 |
| 208 | 1 0 (V2.x v) | 207 | 1 0 (x v) |
| 209 | 0 1 (V2.y v) | 208 | 0 1 (y v) |
| 210 | 0 0 1 | 209 | 0 0 1 |
| 211 | 210 | ||
| 212 | 211 | ||
| @@ -238,9 +237,9 @@ scalev v = mat3 | |||
| 238 | 0 sy 0 | 237 | 0 sy 0 |
| 239 | 0 0 sz | 238 | 0 0 sz |
| 240 | where | 239 | where |
| 241 | sx = V3.x v | 240 | sx = x v |
| 242 | sy = V3.y v | 241 | sy = y v |
| 243 | sz = V3.z v | 242 | sz = z v |
| 244 | 243 | ||
| 245 | 244 | ||
| 246 | -- | Create an X reflection matrix. | 245 | -- | Create an X reflection matrix. |
| @@ -279,9 +278,9 @@ transpose m = mat3 | |||
| 279 | mulp :: Matrix3 -> Vector2 -> Vector2 | 278 | mulp :: Matrix3 -> Vector2 -> Vector2 |
| 280 | mulp m v = vec2 x' y' | 279 | mulp m v = vec2 x' y' |
| 281 | where | 280 | where |
| 282 | v' = vec3 (V2.x v) (V2.y v) 1 | 281 | v' = vec3 (x v) (y v) 1 |
| 283 | x' = row0 m `V3.dot` v' | 282 | x' = row0 m `dot` v' |
| 284 | y' = row1 m `V3.dot` v' | 283 | y' = row1 m `dot` v' |
| 285 | 284 | ||
| 286 | 285 | ||
| 287 | 286 | ||
| @@ -289,19 +288,19 @@ mulp m v = vec2 x' y' | |||
| 289 | muld :: Matrix3 -> Vector2 -> Vector2 | 288 | muld :: Matrix3 -> Vector2 -> Vector2 |
| 290 | muld m v = vec2 x' y' | 289 | muld m v = vec2 x' y' |
| 291 | where | 290 | where |
| 292 | v' = vec3 (V2.x v) (V2.y v) 0 | 291 | v' = vec3 (x v) (y v) 0 |
| 293 | x' = row0 m `V3.dot` v' | 292 | x' = row0 m `dot` v' |
| 294 | y' = row1 m `V3.dot` v' | 293 | y' = row1 m `dot` v' |
| 295 | 294 | ||
| 296 | 295 | ||
| 297 | -- | Transform the given vector in 3D space with the given matrix. | 296 | -- | Transform the given vector in 3D space with the given matrix. |
| 298 | mul :: Matrix3 -> Vector3 -> Vector3 | 297 | mul :: Matrix3 -> Vector3 -> Vector3 |
| 299 | mul m v = vec3 x' y' z' | 298 | mul m v = vec3 x' y' z' |
| 300 | where | 299 | where |
| 301 | v' = vec3 (V3.x v) (V3.y v) (V3.z v) | 300 | v' = vec3 (x v) (y v) (z v) |
| 302 | x' = row0 m `V3.dot` v' | 301 | x' = row0 m `dot` v' |
| 303 | y' = row1 m `V3.dot` v' | 302 | y' = row1 m `dot` v' |
| 304 | z' = row2 m `V3.dot` v' | 303 | z' = row2 m `dot` v' |
| 305 | 304 | ||
| 306 | 305 | ||
| 307 | -- | Zip two 'Matrix3' together with the specified function. | 306 | -- | Zip two 'Matrix3' together with the specified function. |
| @@ -327,11 +326,10 @@ inverseTransform mat = | |||
| 327 | f = forward mat | 326 | f = forward mat |
| 328 | t = -(position mat) | 327 | t = -(position mat) |
| 329 | in mat3 | 328 | in mat3 |
| 330 | (V2.x r) (V2.y r) (t `V2.dot` r) | 329 | (x r) (y r) (t `dot` r) |
| 331 | (V2.x f) (V2.y f) (t `V2.dot` f) | 330 | (x f) (y f) (t `dot` f) |
| 332 | 0 0 1 | 331 | 0 0 1 |
| 333 | 332 | ||
| 334 | 333 | ||
| 335 | fromDeg :: (Floating a) => a -> a | 334 | fromDeg :: (Floating a) => a -> a |
| 336 | fromDeg = (*pi) . (/180) | 335 | fromDeg = (*pi) . (/180) |
| 337 | |||
diff --git a/Spear/Math/Matrix4.hs b/Spear/Math/Matrix4.hs index 41bfadd..1424b28 100644 --- a/Spear/Math/Matrix4.hs +++ b/Spear/Math/Matrix4.hs | |||
| @@ -51,8 +51,7 @@ module Spear.Math.Matrix4 | |||
| 51 | where | 51 | where |
| 52 | 52 | ||
| 53 | 53 | ||
| 54 | import Spear.Math.Vector3 as V3 | 54 | import Spear.Math.Vector |
| 55 | import Spear.Math.Vector4 as V4 | ||
| 56 | 55 | ||
| 57 | import Foreign.Storable | 56 | import Foreign.Storable |
| 58 | 57 | ||
| @@ -171,10 +170,10 @@ mat4 = Matrix4 | |||
| 171 | -- | Build a matrix from four vectors in 4D. | 170 | -- | Build a matrix from four vectors in 4D. |
| 172 | mat4fromVec :: Vector4 -> Vector4 -> Vector4 -> Vector4 -> Matrix4 | 171 | mat4fromVec :: Vector4 -> Vector4 -> Vector4 -> Vector4 -> Matrix4 |
| 173 | mat4fromVec v0 v1 v2 v3 = Matrix4 | 172 | mat4fromVec v0 v1 v2 v3 = Matrix4 |
| 174 | (V4.x v0) (V4.x v1) (V4.x v2) (V4.x v3) | 173 | (x v0) (x v1) (x v2) (x v3) |
| 175 | (V4.y v0) (V4.y v1) (V4.y v2) (V4.y v3) | 174 | (y v0) (y v1) (y v2) (y v3) |
| 176 | (V4.z v0) (V4.z v1) (V4.z v2) (V4.z v3) | 175 | (z v0) (z v1) (z v2) (z v3) |
| 177 | (V4.w v0) (V4.w v1) (V4.w v2) (V4.w v3) | 176 | (w v0) (w v1) (w v2) (w v3) |
| 178 | 177 | ||
| 179 | 178 | ||
| 180 | -- | Build a transformation 'Matrix4' from the given vectors. | 179 | -- | Build a transformation 'Matrix4' from the given vectors. |
| @@ -185,9 +184,9 @@ transform :: Vector3 -- ^ Right vector. | |||
| 185 | -> Matrix4 | 184 | -> Matrix4 |
| 186 | 185 | ||
| 187 | transform right up fwd pos = mat4 | 186 | transform right up fwd pos = mat4 |
| 188 | (V3.x right) (V3.x up) (V3.x fwd) (V3.x pos) | 187 | (x right) (x up) (x fwd) (x pos) |
| 189 | (V3.y right) (V3.y up) (V3.y fwd) (V3.y pos) | 188 | (y right) (y up) (y fwd) (y pos) |
| 190 | (V3.z right) (V3.z up) (V3.z fwd) (V3.z pos) | 189 | (z right) (z up) (z fwd) (z pos) |
| 191 | 0 0 0 1 | 190 | 0 0 0 1 |
| 192 | 191 | ||
| 193 | 192 | ||
| @@ -225,8 +224,8 @@ lookAt :: Vector3 -- ^ Eye position. | |||
| 225 | -> Matrix4 | 224 | -> Matrix4 |
| 226 | 225 | ||
| 227 | lookAt pos target = | 226 | lookAt pos target = |
| 228 | let fwd = V3.normalise $ target - pos | 227 | let fwd = normalise $ target - pos |
| 229 | r = fwd `cross` V3.unity | 228 | r = fwd `cross` unity3 |
| 230 | u = r `cross` fwd | 229 | u = r `cross` fwd |
| 231 | in | 230 | in |
| 232 | transform r u (-fwd) pos | 231 | transform r u (-fwd) pos |
| @@ -271,9 +270,9 @@ transl x y z = mat4 | |||
| 271 | -- | Create a translation matrix. | 270 | -- | Create a translation matrix. |
| 272 | translv :: Vector3 -> Matrix4 | 271 | translv :: Vector3 -> Matrix4 |
| 273 | translv v = mat4 | 272 | translv v = mat4 |
| 274 | 1 0 0 (V3.x v) | 273 | 1 0 0 (x v) |
| 275 | 0 1 0 (V3.y v) | 274 | 0 1 0 (y v) |
| 276 | 0 0 1 (V3.z v) | 275 | 0 0 1 (z v) |
| 277 | 0 0 0 1 | 276 | 0 0 0 1 |
| 278 | 277 | ||
| 279 | 278 | ||
| @@ -320,22 +319,22 @@ rotZ angle = mat4 | |||
| 320 | -- The given angle must be in degrees. | 319 | -- The given angle must be in degrees. |
| 321 | axisAngle :: Vector3 -> Float -> Matrix4 | 320 | axisAngle :: Vector3 -> Float -> Matrix4 |
| 322 | axisAngle v angle = mat4 | 321 | axisAngle v angle = mat4 |
| 323 | (c + omc*x^2) (omc*xy-sz) (omc*xz+sy) 0 | 322 | (c + omc*ax^2) (omc*xy-sz) (omc*xz+sy) 0 |
| 324 | (omc*xy+sz) (c+omc*y^2) (omc*yz-sx) 0 | 323 | (omc*xy+sz) (c+omc*ay^2) (omc*yz-sx) 0 |
| 325 | (omc*xz-sy) (omc*yz+sx) (c+omc*z^2) 0 | 324 | (omc*xz-sy) (omc*yz+sx) (c+omc*az^2) 0 |
| 326 | 0 0 0 1 | 325 | 0 0 0 1 |
| 327 | where | 326 | where |
| 328 | x = V3.x v | 327 | ax = x v |
| 329 | y = V3.y v | 328 | ay = y v |
| 330 | z = V3.z v | 329 | az = z v |
| 331 | s = sin . toRAD $ angle | 330 | s = sin . toRAD $ angle |
| 332 | c = cos . toRAD $ angle | 331 | c = cos . toRAD $ angle |
| 333 | xy = x*y | 332 | xy = ax*ay |
| 334 | xz = x*z | 333 | xz = ax*az |
| 335 | yz = y*z | 334 | yz = ay*az |
| 336 | sx = s*x | 335 | sx = s*ax |
| 337 | sy = s*y | 336 | sy = s*ay |
| 338 | sz = s*z | 337 | sz = s*az |
| 339 | omc = 1 - c | 338 | omc = 1 - c |
| 340 | 339 | ||
| 341 | 340 | ||
| @@ -356,9 +355,9 @@ scalev v = mat4 | |||
| 356 | 0 0 sz 0 | 355 | 0 0 sz 0 |
| 357 | 0 0 0 1 | 356 | 0 0 0 1 |
| 358 | where | 357 | where |
| 359 | sx = V3.x v | 358 | sx = x v |
| 360 | sy = V3.y v | 359 | sy = y v |
| 361 | sz = V3.z v | 360 | sz = z v |
| 362 | 361 | ||
| 363 | 362 | ||
| 364 | -- | Create an X reflection matrix. | 363 | -- | Create an X reflection matrix. |
| @@ -430,13 +429,13 @@ planeProj :: Vector3 -- ^ Plane normal | |||
| 430 | -> Vector3 -- ^ Projection direction | 429 | -> Vector3 -- ^ Projection direction |
| 431 | -> Matrix4 | 430 | -> Matrix4 |
| 432 | planeProj n d l = | 431 | planeProj n d l = |
| 433 | let c = n `V3.dot` l | 432 | let c = n `dot` l |
| 434 | nx = V3.x n | 433 | nx = x n |
| 435 | ny = V3.y n | 434 | ny = y n |
| 436 | nz = V3.z n | 435 | nz = z n |
| 437 | lx = V3.x l | 436 | lx = x l |
| 438 | ly = V3.y l | 437 | ly = y l |
| 439 | lz = V3.z l | 438 | lz = z l |
| 440 | in mat4 | 439 | in mat4 |
| 441 | (d + c - nx*lx) (-ny*lx) (-nz*lx) (-lx*d) | 440 | (d + c - nx*lx) (-ny*lx) (-nz*lx) (-lx*d) |
| 442 | (-nx*ly) (d + c - ny*ly) (-nz*ly) (-ly*d) | 441 | (-nx*ly) (d + c - ny*ly) (-nz*ly) (-ly*d) |
| @@ -463,9 +462,9 @@ inverseTransform mat = | |||
| 463 | t = position mat | 462 | t = position mat |
| 464 | in | 463 | in |
| 465 | mat4 | 464 | mat4 |
| 466 | (V3.x r) (V3.y r) (V3.z r) (-t `V3.dot` r) | 465 | (x r) (y r) (z r) (-t `dot` r) |
| 467 | (V3.x u) (V3.y u) (V3.z u) (-t `V3.dot` u) | 466 | (x u) (y u) (z u) (-t `dot` u) |
| 468 | (V3.x f) (V3.y f) (V3.z f) (-t `V3.dot` f) | 467 | (x f) (y f) (z f) (-t `dot` f) |
| 469 | 0 0 0 1 | 468 | 0 0 0 1 |
| 470 | 469 | ||
| 471 | 470 | ||
| @@ -618,10 +617,10 @@ inverse mat = | |||
| 618 | mul :: Float -> Matrix4 -> Vector3 -> Vector3 | 617 | mul :: Float -> Matrix4 -> Vector3 -> Vector3 |
| 619 | mul w m v = vec3 x' y' z' | 618 | mul w m v = vec3 x' y' z' |
| 620 | where | 619 | where |
| 621 | v' = vec4 (V3.x v) (V3.y v) (V3.z v) w | 620 | v' = vec4 (x v) (y v) (z v) w |
| 622 | x' = row0 m `V4.dot` v' | 621 | x' = row0 m `dot` v' |
| 623 | y' = row1 m `V4.dot` v' | 622 | y' = row1 m `dot` v' |
| 624 | z' = row2 m `V4.dot` v' | 623 | z' = row2 m `dot` v' |
| 625 | 624 | ||
| 626 | 625 | ||
| 627 | -- | Transform the given point vector in 3D space with the given matrix. | 626 | -- | Transform the given point vector in 3D space with the given matrix. |
| @@ -641,11 +640,11 @@ muld = mul 0 | |||
| 641 | mul' :: Float -> Matrix4 -> Vector3 -> Vector3 | 640 | mul' :: Float -> Matrix4 -> Vector3 -> Vector3 |
| 642 | mul' w m v = vec3 (x'/w') (y'/w') (z'/w') | 641 | mul' w m v = vec3 (x'/w') (y'/w') (z'/w') |
| 643 | where | 642 | where |
| 644 | v' = vec4 (V3.x v) (V3.y v) (V3.z v) w | 643 | v' = vec4 (x v) (y v) (z v) w |
| 645 | x' = row0 m `V4.dot` v' | 644 | x' = row0 m `dot` v' |
| 646 | y' = row1 m `V4.dot` v' | 645 | y' = row1 m `dot` v' |
| 647 | z' = row2 m `V4.dot` v' | 646 | z' = row2 m `dot` v' |
| 648 | w' = row3 m `V4.dot` v' | 647 | w' = row3 m `dot` v' |
| 649 | 648 | ||
| 650 | 649 | ||
| 651 | toRAD = (*pi) . (/180) | 650 | toRAD = (*pi) . (/180) |
diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs index 629b73c..79bd049 100644 --- a/Spear/Math/MatrixUtils.hs +++ b/Spear/Math/MatrixUtils.hs | |||
| @@ -15,8 +15,7 @@ where | |||
| 15 | import Spear.Math.Camera as Cam | 15 | import Spear.Math.Camera as Cam |
| 16 | import Spear.Math.Matrix3 as M3 | 16 | import Spear.Math.Matrix3 as M3 |
| 17 | import Spear.Math.Matrix4 as M4 | 17 | import Spear.Math.Matrix4 as M4 |
| 18 | import Spear.Math.Vector2 as V2 | 18 | import Spear.Math.Vector as V |
| 19 | import Spear.Math.Vector3 as V3 | ||
| 20 | 19 | ||
| 21 | 20 | ||
| 22 | -- | Compute the normal matrix of the given matrix. | 21 | -- | Compute the normal matrix of the given matrix. |
| @@ -39,14 +38,14 @@ unproject :: Matrix4 -- ^ Inverse projection matrix | |||
| 39 | -> Float -- ^ Window x | 38 | -> Float -- ^ Window x |
| 40 | -> Float -- ^ Window y | 39 | -> Float -- ^ Window y |
| 41 | -> Float -- ^ Window z | 40 | -> Float -- ^ Window z |
| 42 | -> V3.Vector3 | 41 | -> Vector3 |
| 43 | unproject projI modelviewI vpx vpy w h x y z = | 42 | unproject projI modelviewI vpx vpy w h x y z = |
| 44 | let | 43 | let |
| 45 | xmouse = 2*(x-vpx)/w - 1 | 44 | xmouse = 2*(x-vpx)/w - 1 |
| 46 | ymouse = 2*(y-vpy)/h - 1 | 45 | ymouse = 2*(y-vpy)/h - 1 |
| 47 | zmouse = 2*z - 1 | 46 | zmouse = 2*z - 1 |
| 48 | in | 47 | in |
| 49 | (modelviewI * projI) `M4.mulp` V3.vec3 xmouse ymouse zmouse | 48 | (modelviewI * projI) `M4.mulp` vec3 xmouse ymouse zmouse |
| 50 | 49 | ||
| 51 | 50 | ||
| 52 | -- | Transform the given point in window coordinates to 2d coordinates. | 51 | -- | Transform the given point in window coordinates to 2d coordinates. |
| @@ -63,14 +62,14 @@ rpgUnproject | |||
| 63 | -> Float -- ^ Window x | 62 | -> Float -- ^ Window x |
| 64 | -> Float -- ^ Window y | 63 | -> Float -- ^ Window y |
| 65 | -> Vector2 | 64 | -> Vector2 |
| 66 | rpgUnproject projI viewI vpx vpy w h x y = | 65 | rpgUnproject projI viewI vpx vpy w h wx wy = |
| 67 | let | 66 | let |
| 68 | p1 = unproject projI viewI vpx vpy w h x y 0 | 67 | p1 = unproject projI viewI vpx vpy w h wx wy 0 |
| 69 | p2 = unproject projI viewI vpx vpy w h x y (-1) | 68 | p2 = unproject projI viewI vpx vpy w h wx wy (-1) |
| 70 | lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) | 69 | lambda = (y p1 / (y p1 - y p2)) |
| 71 | p' = p1 + V3.scale lambda (p2 - p1) | 70 | p' = p1 + V.scale lambda (p2 - p1) |
| 72 | in | 71 | in |
| 73 | vec2 (V3.x p') (-V3.z p') | 72 | vec2 (x p') (-(z p')) |
| 74 | 73 | ||
| 75 | 74 | ||
| 76 | -- | Map an object's transform in view space to world space. | 75 | -- | Map an object's transform in view space to world space. |
| @@ -82,33 +81,33 @@ rpgTransform | |||
| 82 | -> Matrix4 -- ^ Inverse view matrix | 81 | -> Matrix4 -- ^ Inverse view matrix |
| 83 | -> Matrix4 | 82 | -> Matrix4 |
| 84 | rpgTransform h a axis pos viewI = | 83 | rpgTransform h a axis pos viewI = |
| 85 | let p1 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) 0) | 84 | let p1 = viewI `M4.mulp` (vec3 (x pos) (y pos) 0) |
| 86 | p2 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) (-1)) | 85 | p2 = viewI `M4.mulp` (vec3 (x pos) (y pos) (-1)) |
| 87 | lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) | 86 | lambda = (y p1 / (y p1 - y p2)) |
| 88 | p = p1 + V3.scale lambda (p2 - p1) | 87 | p = p1 + V.scale lambda (p2 - p1) |
| 89 | mat' = axisAngle axis a | 88 | mat' = axisAngle axis a |
| 90 | r = M4.right mat' | 89 | r = M4.right mat' |
| 91 | u = M4.up mat' | 90 | u = M4.up mat' |
| 92 | f = M4.forward mat' | 91 | f = M4.forward mat' |
| 93 | t = p + vec3 0 h 0 | 92 | t = p + vec3 0 h 0 |
| 94 | in mat4 | 93 | in mat4 |
| 95 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) | 94 | (x r) (x u) (x f) (x t) |
| 96 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) | 95 | (y r) (y u) (y f) (y t) |
| 97 | (V3.z r) (V3.z u) (V3.z f) (V3.z t) | 96 | (z r) (z u) (z f) (z t) |
| 98 | 0 0 0 1 | 97 | 0 0 0 1 |
| 99 | 98 | ||
| 100 | 99 | ||
| 101 | -- | Map an object's transform in view space to world space. | 100 | -- | Map an object's transform in view space to world space. |
| 102 | pltTransform :: Matrix3 -> Matrix4 | 101 | pltTransform :: Matrix3 -> Matrix4 |
| 103 | pltTransform mat = | 102 | pltTransform mat = |
| 104 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | 103 | let r = let r' = M3.right mat in vec3 (x r') (y r') 0 |
| 105 | u = let u' = M3.up mat in vec3 (V2.x u') (V2.y u') 0 | 104 | u = let u' = M3.up mat in vec3 (x u') (y u') 0 |
| 106 | f = V3.unitz | 105 | f = unitz3 |
| 107 | t = let t' = M3.position mat in vec3 (V2.x t') (V2.y t') 0 | 106 | t = let t' = M3.position mat in vec3 (x t') (y t') 0 |
| 108 | in mat4 | 107 | in mat4 |
| 109 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) | 108 | (x r) (x u) (x f) (x t) |
| 110 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) | 109 | (y r) (y u) (y f) (y t) |
| 111 | (V3.z r) (V3.z u) (V3.z f) (V3.z t) | 110 | (z r) (z u) (z f) (z t) |
| 112 | 0 0 0 1 | 111 | 0 0 0 1 |
| 113 | 112 | ||
| 114 | 113 | ||
| @@ -147,4 +146,4 @@ objToClip cam model p = | |||
| 147 | proj = Cam.projection cam | 146 | proj = Cam.projection cam |
| 148 | p' = (proj * view * model) `M4.mulp` p | 147 | p' = (proj * view * model) `M4.mulp` p |
| 149 | in | 148 | in |
| 150 | vec2 (V3.x p') (V3.y p') | 149 | vec2 (x p') (y p') |
diff --git a/Spear/Math/Plane.hs b/Spear/Math/Plane.hs index 6fabbec..b20740c 100644 --- a/Spear/Math/Plane.hs +++ b/Spear/Math/Plane.hs | |||
| @@ -7,7 +7,7 @@ module Spear.Math.Plane | |||
| 7 | where | 7 | where |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | import Spear.Math.Vector3 | 10 | import Spear.Math.Vector |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | data PointPlanePos = Front | Back | Contained deriving (Eq, Ord, Show) | 13 | data PointPlanePos = Front | Back | Contained deriving (Eq, Ord, Show) |
diff --git a/Spear/Math/Quad.hs b/Spear/Math/Quad.hs index e75607c..6b6215c 100644 --- a/Spear/Math/Quad.hs +++ b/Spear/Math/Quad.hs | |||
| @@ -8,7 +8,7 @@ where | |||
| 8 | 8 | ||
| 9 | import Spear.Math.Segment | 9 | import Spear.Math.Segment |
| 10 | import Spear.Math.Utils | 10 | import Spear.Math.Utils |
| 11 | import Spear.Math.Vector2 | 11 | import Spear.Math.Vector |
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | data Quad = Quad | 14 | data Quad = Quad |
diff --git a/Spear/Math/QuadTree.hs b/Spear/Math/QuadTree.hs index e553c88..d6b6353 100644 --- a/Spear/Math/QuadTree.hs +++ b/Spear/Math/QuadTree.hs | |||
| @@ -11,7 +11,7 @@ where | |||
| 11 | 11 | ||
| 12 | import Spear.Collision | 12 | import Spear.Collision |
| 13 | import Spear.Math.AABB | 13 | import Spear.Math.AABB |
| 14 | import Spear.Math.Vector2 | 14 | import Spear.Math.Vector |
| 15 | 15 | ||
| 16 | import Control.Applicative ((<*>)) | 16 | import Control.Applicative ((<*>)) |
| 17 | import Data.List | 17 | import Data.List |
diff --git a/Spear/Math/Quaternion.hs b/Spear/Math/Quaternion.hs index 4eb88d7..cfc6cd2 100644 --- a/Spear/Math/Quaternion.hs +++ b/Spear/Math/Quaternion.hs | |||
| @@ -10,15 +10,14 @@ module Spear.Math.Quaternion | |||
| 10 | , qmul | 10 | , qmul |
| 11 | , qconj | 11 | , qconj |
| 12 | , qinv | 12 | , qinv |
| 13 | , Spear.Math.Quaternion.normalise | 13 | , qnormalise |
| 14 | , Spear.Math.Quaternion.norm | 14 | , qnorm |
| 15 | , qrot | 15 | , qrot |
| 16 | ) | 16 | ) |
| 17 | where | 17 | where |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | import qualified Spear.Math.Vector3 as V3 | 20 | import Spear.Math.Vector |
| 21 | import Spear.Math.Vector4 as V4 | ||
| 22 | 21 | ||
| 23 | 22 | ||
| 24 | newtype Quaternion = Quaternion { getVec :: Vector4 } | 23 | newtype Quaternion = Quaternion { getVec :: Vector4 } |
| @@ -39,23 +38,23 @@ qvec4 = Quaternion | |||
| 39 | 38 | ||
| 40 | 39 | ||
| 41 | -- | Build a 'Quaternion' from the given 'Vector3' and w. | 40 | -- | Build a 'Quaternion' from the given 'Vector3' and w. |
| 42 | qvec3 :: V3.Vector3 -> Float -> Quaternion | 41 | qvec3 :: Vector3 -> Float -> Quaternion |
| 43 | qvec3 v w = Quaternion $ vec4 (V3.x v) (V3.y v) (V3.z v) w | 42 | qvec3 v w = Quaternion $ vec4 (x v) (y v) (z v) w |
| 44 | 43 | ||
| 45 | 44 | ||
| 46 | -- | Build a 'Quaternion' representing the given rotation. | 45 | -- | Build a 'Quaternion' representing the given rotation. |
| 47 | qAxisAngle :: V3.Vector3 -> Float -> Quaternion | 46 | qAxisAngle :: Vector3 -> Float -> Quaternion |
| 48 | qAxisAngle axis angle = | 47 | qAxisAngle axis angle = |
| 49 | let s' = V3.norm axis | 48 | let s' = norm axis |
| 50 | s = if s' == 0 then 1 else s' | 49 | s = if s' == 0 then 1 else s' |
| 51 | a = angle * toRAD * 0.5 | 50 | a = angle * toRAD * 0.5 |
| 52 | sa = sin a | 51 | sa = sin a |
| 53 | w = cos a | 52 | qw = cos a |
| 54 | x = V3.x axis * sa * s | 53 | qx = x axis * sa * s |
| 55 | y = V3.y axis * sa * s | 54 | qy = y axis * sa * s |
| 56 | z = V3.z axis * sa * s | 55 | qz = z axis * sa * s |
| 57 | in | 56 | in |
| 58 | Quaternion $ vec4 x y z w | 57 | Quaternion $ vec4 qx qy qz qw |
| 59 | 58 | ||
| 60 | 59 | ||
| 61 | -- | Compute the product of the given two quaternions. | 60 | -- | Compute the product of the given two quaternions. |
| @@ -90,19 +89,19 @@ qinv (Quaternion q) = | |||
| 90 | 89 | ||
| 91 | 90 | ||
| 92 | -- | Normalise the given 'Quaternion'. | 91 | -- | Normalise the given 'Quaternion'. |
| 93 | normalise :: Quaternion -> Quaternion | 92 | qnormalise :: Quaternion -> Quaternion |
| 94 | normalise = Quaternion . V4.normalise . getVec | 93 | qnormalise = Quaternion . normalise . getVec |
| 95 | 94 | ||
| 96 | 95 | ||
| 97 | -- | Compute the norm of the given 'Quaternion'. | 96 | -- | Compute the norm of the given 'Quaternion'. |
| 98 | norm :: Quaternion -> Float | 97 | qnorm :: Quaternion -> Float |
| 99 | norm = V4.norm . getVec | 98 | qnorm = norm . getVec |
| 100 | 99 | ||
| 101 | 100 | ||
| 102 | -- | Rotate the given 'Vector3'. | 101 | -- | Rotate the given 'Vector3'. |
| 103 | qrot :: Quaternion -> V3.Vector3 -> V3.Vector3 | 102 | qrot :: Quaternion -> Vector3 -> Vector3 |
| 104 | qrot q v = toVec3 $ q `qmul` qvec3 v 0 `qmul` qconj q | 103 | qrot q v = toVec3 $ q `qmul` qvec3 v 0 `qmul` qconj q |
| 105 | where toVec3 (Quaternion q) = V3.vec3 (x q) (y q) (z q) | 104 | where toVec3 (Quaternion q) = vec3 (x q) (y q) (z q) |
| 106 | 105 | ||
| 107 | 106 | ||
| 108 | toRAD = pi / 180 | 107 | toRAD = pi / 180 |
diff --git a/Spear/Math/Ray.hs b/Spear/Math/Ray.hs index 697d609..b0359a1 100644 --- a/Spear/Math/Ray.hs +++ b/Spear/Math/Ray.hs | |||
| @@ -8,7 +8,7 @@ where | |||
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | import Spear.Math.Utils | 10 | import Spear.Math.Utils |
| 11 | import Spear.Math.Vector2 | 11 | import Spear.Math.Vector |
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | data Ray = Ray | 14 | data Ray = Ray |
diff --git a/Spear/Math/Segment.hs b/Spear/Math/Segment.hs index a89ee05..c632838 100644 --- a/Spear/Math/Segment.hs +++ b/Spear/Math/Segment.hs | |||
| @@ -7,7 +7,7 @@ where | |||
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | import Spear.Math.Utils | 9 | import Spear.Math.Utils |
| 10 | import Spear.Math.Vector2 | 10 | import Spear.Math.Vector |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | -- | A line segment in 2D space. | 13 | -- | A line segment in 2D space. |
diff --git a/Spear/Math/Spatial2.hs b/Spear/Math/Spatial2.hs index 49ff3b7..341282b 100644 --- a/Spear/Math/Spatial2.hs +++ b/Spear/Math/Spatial2.hs | |||
| @@ -2,7 +2,7 @@ module Spear.Math.Spatial2 | |||
| 2 | where | 2 | where |
| 3 | 3 | ||
| 4 | 4 | ||
| 5 | import Spear.Math.Vector2 | 5 | import Spear.Math.Vector |
| 6 | import Spear.Math.Matrix3 as M | 6 | import Spear.Math.Matrix3 as M |
| 7 | 7 | ||
| 8 | 8 | ||
diff --git a/Spear/Math/Spatial3.hs b/Spear/Math/Spatial3.hs index 0ee680f..6db3853 100644 --- a/Spear/Math/Spatial3.hs +++ b/Spear/Math/Spatial3.hs | |||
| @@ -2,7 +2,7 @@ module Spear.Math.Spatial3 | |||
| 2 | where | 2 | where |
| 3 | 3 | ||
| 4 | 4 | ||
| 5 | import Spear.Math.Vector3 | 5 | import Spear.Math.Vector |
| 6 | import Spear.Math.Matrix4 as M | 6 | import Spear.Math.Matrix4 as M |
| 7 | 7 | ||
| 8 | 8 | ||
| @@ -57,7 +57,7 @@ class Spatial3 s where | |||
| 57 | lookAt pt s = | 57 | lookAt pt s = |
| 58 | let position = pos s | 58 | let position = pos s |
| 59 | fwd = normalise $ pt - position | 59 | fwd = normalise $ pt - position |
| 60 | r = fwd `cross` unity | 60 | r = fwd `cross` unity3 |
| 61 | u = r `cross` fwd | 61 | u = r `cross` fwd |
| 62 | in | 62 | in |
| 63 | setTransform (M.transform r u (-fwd) position) s | 63 | setTransform (M.transform r u (-fwd) position) s |
diff --git a/Spear/Math/Triangle.hs b/Spear/Math/Triangle.hs index 3c30ea6..96cfa1a 100644 --- a/Spear/Math/Triangle.hs +++ b/Spear/Math/Triangle.hs | |||
| @@ -5,7 +5,7 @@ module Spear.Math.Triangle | |||
| 5 | where | 5 | where |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | import Spear.Math.Vector3 | 8 | import Spear.Math.Vector |
| 9 | 9 | ||
| 10 | import Foreign.C.Types | 10 | import Foreign.C.Types |
| 11 | import Foreign.Storable | 11 | import Foreign.Storable |
diff --git a/Spear/Math/Utils.hs b/Spear/Math/Utils.hs index 1296f25..90ebda9 100644 --- a/Spear/Math/Utils.hs +++ b/Spear/Math/Utils.hs | |||
| @@ -9,8 +9,7 @@ where | |||
| 9 | 9 | ||
| 10 | 10 | ||
| 11 | import Spear.Math.Matrix4 as M4 | 11 | import Spear.Math.Matrix4 as M4 |
| 12 | import Spear.Math.Vector2 as V2 | 12 | import Spear.Math.Vector as V |
| 13 | import qualified Spear.Math.Vector3 as V3 | ||
| 14 | 13 | ||
| 15 | 14 | ||
| 16 | data Side = L | R deriving (Eq, Show) | 15 | data Side = L | R deriving (Eq, Show) |
| @@ -30,10 +29,10 @@ viewToWorld2d :: Vector2 -- ^ Point in view space | |||
| 30 | -> Vector2 -- ^ Projection of the given point | 29 | -> Vector2 -- ^ Projection of the given point |
| 31 | viewToWorld2d p viewI = | 30 | viewToWorld2d p viewI = |
| 32 | let | 31 | let |
| 33 | p1' = V3.vec3 (V2.x p) (V2.y p) 0 | 32 | p1' = vec3 (x p) (y p) 0 |
| 34 | p1 = viewI `mulp` p1' | 33 | p1 = viewI `mulp` p1' |
| 35 | p2 = p1 - M4.forward viewI | 34 | p2 = p1 - M4.forward viewI |
| 36 | lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) | 35 | lambda = (y p1 / (y p1 - y p2)) |
| 37 | p' = p1 + V3.scale lambda (p2 - p1) | 36 | p' = p1 + V.scale lambda (p2 - p1) |
| 38 | in | 37 | in |
| 39 | vec2 (V3.x p') (-V3.z p') | 38 | vec2 (x p') (-z p') |
diff --git a/Spear/Math/Vector.hs b/Spear/Math/Vector.hs new file mode 100644 index 0000000..a1cb9e8 --- /dev/null +++ b/Spear/Math/Vector.hs | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | module Spear.Math.Vector | ||
| 2 | ( | ||
| 3 | module Spear.Math.Vector.Vector2 | ||
| 4 | , module Spear.Math.Vector.Vector3 | ||
| 5 | , module Spear.Math.Vector.Vector4 | ||
| 6 | , module Spear.Math.Vector.Class | ||
| 7 | ) | ||
| 8 | where | ||
| 9 | |||
| 10 | import Spear.Math.Vector.Vector2 | ||
| 11 | import Spear.Math.Vector.Vector3 | ||
| 12 | import Spear.Math.Vector.Vector4 | ||
| 13 | import Spear.Math.Vector.Class | ||
diff --git a/Spear/Math/Vector/Class.hs b/Spear/Math/Vector/Class.hs new file mode 100644 index 0000000..05a7206 --- /dev/null +++ b/Spear/Math/Vector/Class.hs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | module Spear.Math.Vector.Class | ||
| 2 | where | ||
| 3 | |||
| 4 | class (Fractional a, Ord a) => VectorClass a where | ||
| 5 | -- | Create a vector from the given list. | ||
| 6 | fromList :: [Float] -> a | ||
| 7 | |||
| 8 | -- | Return the vector's x coordinate. | ||
| 9 | x :: a -> Float | ||
| 10 | x _ = 0 | ||
| 11 | |||
| 12 | -- | Return the vector's y coordinate. | ||
| 13 | y :: a -> Float | ||
| 14 | y _ = 0 | ||
| 15 | |||
| 16 | -- | Return the vector's z coordinate. | ||
| 17 | z :: a -> Float | ||
| 18 | z _ = 0 | ||
| 19 | |||
| 20 | -- | Return the vector's w coordinate. | ||
| 21 | w :: a -> Float | ||
| 22 | w _ = 0 | ||
| 23 | |||
| 24 | -- | Return the vector's ith coordinate. | ||
| 25 | (!) :: a -> Int -> Float | ||
| 26 | |||
| 27 | -- | Compute the given vectors' dot product. | ||
| 28 | dot :: a -> a -> Float | ||
| 29 | |||
| 30 | -- | Compute the given vector's squared norm. | ||
| 31 | normSq :: a -> Float | ||
| 32 | |||
| 33 | -- | Compute the given vector's norm. | ||
| 34 | norm :: a -> Float | ||
| 35 | |||
| 36 | -- | Multiply the given vector with the given scalar. | ||
| 37 | scale :: Float -> a -> a | ||
| 38 | |||
| 39 | -- | Negate the given vector. | ||
| 40 | neg :: a -> a | ||
| 41 | |||
| 42 | -- | Normalise the given vector. | ||
| 43 | normalise :: a -> a \ No newline at end of file | ||
diff --git a/Spear/Math/Vector2.hs b/Spear/Math/Vector/Vector2.hs index 581a64f..7aaece5 100644 --- a/Spear/Math/Vector2.hs +++ b/Spear/Math/Vector/Vector2.hs | |||
| @@ -1,27 +1,20 @@ | |||
| 1 | module Spear.Math.Vector2 | 1 | module Spear.Math.Vector.Vector2 |
| 2 | ( | 2 | ( |
| 3 | Vector2 | 3 | Vector2 |
| 4 | -- * Accessors | ||
| 5 | , x | ||
| 6 | , y | ||
| 7 | -- * Construction | 4 | -- * Construction |
| 8 | , unitx | 5 | , unitx2 |
| 9 | , unity | 6 | , unity2 |
| 10 | , zero | 7 | , zero2 |
| 11 | , fromList | ||
| 12 | , vec2 | 8 | , vec2 |
| 13 | -- * Operations | 9 | -- * Operations |
| 14 | , perp | 10 | , perp |
| 15 | , dot | ||
| 16 | , normSq | ||
| 17 | , norm | ||
| 18 | , scale | ||
| 19 | , neg | ||
| 20 | , normalise | ||
| 21 | ) | 11 | ) |
| 22 | where | 12 | where |
| 23 | 13 | ||
| 24 | 14 | ||
| 15 | import Spear.Math.Vector.Class | ||
| 16 | |||
| 17 | |||
| 25 | import Foreign.C.Types (CFloat) | 18 | import Foreign.C.Types (CFloat) |
| 26 | import Foreign.Storable | 19 | import Foreign.Storable |
| 27 | 20 | ||
| @@ -53,6 +46,33 @@ instance Ord Vector2 where | |||
| 53 | min (Vector2 ax ay) (Vector2 bx by) = Vector2 (Prelude.min ax bx) (Prelude.min ay by) | 46 | min (Vector2 ax ay) (Vector2 bx by) = Vector2 (Prelude.min ax bx) (Prelude.min ay by) |
| 54 | 47 | ||
| 55 | 48 | ||
| 49 | instance VectorClass Vector2 where | ||
| 50 | fromList (ax:ay:_) = Vector2 ax ay | ||
| 51 | |||
| 52 | x (Vector2 ax _) = ax | ||
| 53 | |||
| 54 | y (Vector2 _ ay) = ay | ||
| 55 | |||
| 56 | (Vector2 ax _) ! 0 = ax | ||
| 57 | (Vector2 _ ay) ! 1 = ay | ||
| 58 | _ ! _ = 0 | ||
| 59 | |||
| 60 | Vector2 ax ay `dot` Vector2 bx by = ax*bx + ay*by | ||
| 61 | |||
| 62 | normSq (Vector2 ax ay) = ax*ax + ay*ay | ||
| 63 | |||
| 64 | norm = sqrt . normSq | ||
| 65 | |||
| 66 | scale s (Vector2 ax ay) = Vector2 (s*ax) (s*ay) | ||
| 67 | |||
| 68 | neg (Vector2 ax ay) = Vector2 (-ax) (-ay) | ||
| 69 | |||
| 70 | normalise v = | ||
| 71 | let n' = norm v | ||
| 72 | n = if n' == 0 then 1 else n' | ||
| 73 | in scale (1.0 / n) v | ||
| 74 | |||
| 75 | |||
| 56 | sizeFloat = sizeOf (undefined :: CFloat) | 76 | sizeFloat = sizeOf (undefined :: CFloat) |
| 57 | 77 | ||
| 58 | 78 | ||
| @@ -71,31 +91,19 @@ instance Storable Vector2 where | |||
| 71 | 91 | ||
| 72 | 92 | ||
| 73 | -- | Get the vector's x coordinate. | 93 | -- | Get the vector's x coordinate. |
| 74 | x (Vector2 ax _) = ax | ||
| 75 | |||
| 76 | 94 | ||
| 77 | -- | Get the vector's y coordinate. | ||
| 78 | y (Vector2 _ ay) = ay | ||
| 79 | 95 | ||
| 80 | 96 | ||
| 81 | -- | Unit vector along the X axis. | 97 | -- | Unit vector along the X axis. |
| 82 | unitx :: Vector2 | 98 | unitx2 = Vector2 1 0 |
| 83 | unitx = Vector2 1 0 | ||
| 84 | 99 | ||
| 85 | 100 | ||
| 86 | -- | Unit vector along the Y axis. | 101 | -- | Unit vector along the Y axis. |
| 87 | unity :: Vector2 | 102 | unity2 = Vector2 0 1 |
| 88 | unity = Vector2 0 1 | ||
| 89 | 103 | ||
| 90 | 104 | ||
| 91 | -- | Zero vector. | 105 | -- | Zero vector. |
| 92 | zero :: Vector2 | 106 | zero2 = Vector2 0 0 |
| 93 | zero = Vector2 0 0 | ||
| 94 | |||
| 95 | |||
| 96 | -- | Create a vector from the given list. | ||
| 97 | fromList :: [Float] -> Vector2 | ||
| 98 | fromList (ax:ay:_) = Vector2 ax ay | ||
| 99 | 107 | ||
| 100 | 108 | ||
| 101 | -- | Create a vector from the given values. | 109 | -- | Create a vector from the given values. |
| @@ -110,36 +118,3 @@ vec2 ax ay = Vector2 ax ay | |||
| 110 | -- perp (Vector2 1 0) = Vector2 0 (-1) | 118 | -- perp (Vector2 1 0) = Vector2 0 (-1) |
| 111 | perp :: Vector2 -> Vector2 | 119 | perp :: Vector2 -> Vector2 |
| 112 | perp (Vector2 x y) = Vector2 y (-x) | 120 | perp (Vector2 x y) = Vector2 y (-x) |
| 113 | |||
| 114 | |||
| 115 | -- | Compute the given vectors' dot product. | ||
| 116 | dot :: Vector2 -> Vector2 -> Float | ||
| 117 | Vector2 ax ay `dot` Vector2 bx by = ax*bx + ay*by | ||
| 118 | |||
| 119 | |||
| 120 | -- | Compute the given vector's squared norm. | ||
| 121 | normSq :: Vector2 -> Float | ||
| 122 | normSq (Vector2 ax ay) = ax*ax + ay*ay | ||
| 123 | |||
| 124 | |||
| 125 | -- | Compute the given vector's norm. | ||
| 126 | norm :: Vector2 -> Float | ||
| 127 | norm = sqrt . normSq | ||
| 128 | |||
| 129 | |||
| 130 | -- | Multiply the given vector with the given scalar. | ||
| 131 | scale :: Float -> Vector2 -> Vector2 | ||
| 132 | scale s (Vector2 ax ay) = Vector2 (s*ax) (s*ay) | ||
| 133 | |||
| 134 | |||
| 135 | -- | Negate the given vector. | ||
| 136 | neg :: Vector2 -> Vector2 | ||
| 137 | neg (Vector2 ax ay) = Vector2 (-ax) (-ay) | ||
| 138 | |||
| 139 | |||
| 140 | -- | Normalise the given vector. | ||
| 141 | normalise :: Vector2 -> Vector2 | ||
| 142 | normalise v = | ||
| 143 | let n' = norm v | ||
| 144 | n = if n' == 0 then 1 else n' | ||
| 145 | in scale (1.0 / n) v | ||
diff --git a/Spear/Math/Vector3.hs b/Spear/Math/Vector/Vector3.hs index d280811..c19b7c7 100644 --- a/Spear/Math/Vector3.hs +++ b/Spear/Math/Vector/Vector3.hs | |||
| @@ -1,30 +1,21 @@ | |||
| 1 | module Spear.Math.Vector3 | 1 | module Spear.Math.Vector.Vector3 |
| 2 | ( | 2 | ( |
| 3 | Vector3 | 3 | Vector3 |
| 4 | -- * Accessors | ||
| 5 | , x | ||
| 6 | , y | ||
| 7 | , z | ||
| 8 | -- * Construction | 4 | -- * Construction |
| 9 | , unitx | 5 | , unitx3 |
| 10 | , unity | 6 | , unity3 |
| 11 | , unitz | 7 | , unitz3 |
| 12 | , zero | 8 | , zero3 |
| 13 | , fromList | ||
| 14 | , vec3 | 9 | , vec3 |
| 15 | , orbit | 10 | , orbit |
| 16 | -- * Operations | 11 | -- * Operations |
| 17 | , dot | ||
| 18 | , cross | 12 | , cross |
| 19 | , normSq | ||
| 20 | , norm | ||
| 21 | , scale | ||
| 22 | , neg | ||
| 23 | , normalise | ||
| 24 | ) | 13 | ) |
| 25 | where | 14 | where |
| 26 | 15 | ||
| 27 | 16 | ||
| 17 | import Spear.Math.Vector.Class | ||
| 18 | |||
| 28 | import Foreign.C.Types (CFloat) | 19 | import Foreign.C.Types (CFloat) |
| 29 | import Foreign.Storable | 20 | import Foreign.Storable |
| 30 | 21 | ||
| @@ -77,6 +68,36 @@ instance Ord Vector3 where | |||
| 77 | min (Vector3 ax ay az) (Vector3 bx by bz) = Vector3 (Prelude.min ax bx) (Prelude.min ay by) (Prelude.min az bz) | 68 | min (Vector3 ax ay az) (Vector3 bx by bz) = Vector3 (Prelude.min ax bx) (Prelude.min ay by) (Prelude.min az bz) |
| 78 | 69 | ||
| 79 | 70 | ||
| 71 | instance VectorClass Vector3 where | ||
| 72 | fromList (ax:ay:az:_) = Vector3 ax ay az | ||
| 73 | |||
| 74 | x (Vector3 ax _ _ ) = ax | ||
| 75 | |||
| 76 | y (Vector3 _ ay _ ) = ay | ||
| 77 | |||
| 78 | z (Vector3 _ _ az) = az | ||
| 79 | |||
| 80 | (Vector3 ax _ _) ! 0 = ax | ||
| 81 | (Vector3 _ ay _) ! 1 = ay | ||
| 82 | (Vector3 _ _ az) ! 2 = az | ||
| 83 | _ ! _ = 0 | ||
| 84 | |||
| 85 | Vector3 ax ay az `dot` Vector3 bx by bz = ax*bx + ay*by + az*bz | ||
| 86 | |||
| 87 | normSq (Vector3 ax ay az) = ax*ax + ay*ay + az*az | ||
| 88 | |||
| 89 | norm = sqrt . normSq | ||
| 90 | |||
| 91 | scale s (Vector3 ax ay az) = Vector3 (s*ax) (s*ay) (s*az) | ||
| 92 | |||
| 93 | neg (Vector3 ax ay az) = Vector3 (-ax) (-ay) (-az) | ||
| 94 | |||
| 95 | normalise v = | ||
| 96 | let n' = norm v | ||
| 97 | n = if n' == 0 then 1 else n' | ||
| 98 | in scale (1.0 / n) v | ||
| 99 | |||
| 100 | |||
| 80 | sizeFloat = sizeOf (undefined :: CFloat) | 101 | sizeFloat = sizeOf (undefined :: CFloat) |
| 81 | 102 | ||
| 82 | 103 | ||
| @@ -96,34 +117,20 @@ instance Storable Vector3 where | |||
| 96 | pokeByteOff ptr (2*sizeFloat) az | 117 | pokeByteOff ptr (2*sizeFloat) az |
| 97 | 118 | ||
| 98 | 119 | ||
| 99 | x (Vector3 ax _ _ ) = ax | ||
| 100 | y (Vector3 _ ay _ ) = ay | ||
| 101 | z (Vector3 _ _ az) = az | ||
| 102 | |||
| 103 | |||
| 104 | -- | Unit vector along the X axis. | 120 | -- | Unit vector along the X axis. |
| 105 | unitx :: Vector3 | 121 | unitx3 = Vector3 1 0 0 |
| 106 | unitx = Vector3 1 0 0 | ||
| 107 | 122 | ||
| 108 | 123 | ||
| 109 | -- | Unit vector along the Y axis. | 124 | -- | Unit vector along the Y axis. |
| 110 | unity :: Vector3 | 125 | unity3 = Vector3 0 1 0 |
| 111 | unity = Vector3 0 1 0 | ||
| 112 | 126 | ||
| 113 | 127 | ||
| 114 | -- | Unit vector along the Z axis. | 128 | -- | Unit vector along the Z axis. |
| 115 | unitz :: Vector3 | 129 | unitz3 = Vector3 0 0 1 |
| 116 | unitz = Vector3 0 0 1 | ||
| 117 | 130 | ||
| 118 | 131 | ||
| 119 | -- | Zero vector. | 132 | -- | Zero vector. |
| 120 | zero :: Vector3 | 133 | zero3 = Vector3 0 0 0 |
| 121 | zero = Vector3 0 0 0 | ||
| 122 | |||
| 123 | |||
| 124 | -- | Create a vector from the given list. | ||
| 125 | fromList :: [Float] -> Vector3 | ||
| 126 | fromList (ax:ay:az:_) = Vector3 ax ay az | ||
| 127 | 134 | ||
| 128 | 135 | ||
| 129 | -- | Create a 3D vector from the given values. | 136 | -- | Create a 3D vector from the given values. |
| @@ -152,36 +159,7 @@ orbit center radius anglex angley = | |||
| 152 | vec3 px py pz | 159 | vec3 px py pz |
| 153 | 160 | ||
| 154 | 161 | ||
| 155 | -- | Compute the given vectors' dot product. | ||
| 156 | dot :: Vector3 -> Vector3 -> Float | ||
| 157 | Vector3 ax ay az `dot` Vector3 bx by bz = ax*bx + ay*by + az*bz | ||
| 158 | |||
| 159 | |||
| 160 | -- | Compute the given vectors' cross product. | 162 | -- | Compute the given vectors' cross product. |
| 161 | cross :: Vector3 -> Vector3 -> Vector3 | 163 | cross :: Vector3 -> Vector3 -> Vector3 |
| 162 | (Vector3 ax ay az) `cross` (Vector3 bx by bz) = | 164 | (Vector3 ax ay az) `cross` (Vector3 bx by bz) = |
| 163 | Vector3 (ay * bz - az * by) (az * bx - ax * bz) (ax * by - ay * bx) | 165 | Vector3 (ay * bz - az * by) (az * bx - ax * bz) (ax * by - ay * bx) |
| 164 | |||
| 165 | |||
| 166 | -- | Compute the given vector's squared norm. | ||
| 167 | normSq (Vector3 ax ay az) = ax*ax + ay*ay + az*az | ||
| 168 | |||
| 169 | |||
| 170 | -- | Compute the given vector's norm. | ||
| 171 | norm = sqrt . normSq | ||
| 172 | |||
| 173 | |||
| 174 | -- | Multiply the given vector with the given scalar. | ||
| 175 | scale s (Vector3 ax ay az) = Vector3 (s*ax) (s*ay) (s*az) | ||
| 176 | |||
| 177 | |||
| 178 | -- | Negate the given vector. | ||
| 179 | neg (Vector3 ax ay az) = Vector3 (-ax) (-ay) (-az) | ||
| 180 | |||
| 181 | |||
| 182 | -- | Normalise the given vector. | ||
| 183 | normalise v = | ||
| 184 | let n' = norm v | ||
| 185 | n = if n' == 0 then 1 else n' | ||
| 186 | in scale (1.0 / n) v | ||
| 187 | |||
diff --git a/Spear/Math/Vector4.hs b/Spear/Math/Vector/Vector4.hs index 554fb27..1f5494d 100644 --- a/Spear/Math/Vector4.hs +++ b/Spear/Math/Vector/Vector4.hs | |||
| @@ -1,28 +1,19 @@ | |||
| 1 | module Spear.Math.Vector4 | 1 | module Spear.Math.Vector.Vector4 |
| 2 | ( | 2 | ( |
| 3 | Vector4 | 3 | Vector4 |
| 4 | -- * Accessors | ||
| 5 | , x | ||
| 6 | , y | ||
| 7 | , z | ||
| 8 | , w | ||
| 9 | -- * Construction | 4 | -- * Construction |
| 10 | , unitX | 5 | , unitx4 |
| 11 | , unitY | 6 | , unity4 |
| 12 | , unitZ | 7 | , unitz4 |
| 13 | , fromList | ||
| 14 | , vec4 | 8 | , vec4 |
| 15 | -- * Operations | 9 | -- * Operations |
| 16 | , dot | 10 | , cross' |
| 17 | , normSq | ||
| 18 | , norm | ||
| 19 | , scale | ||
| 20 | , neg | ||
| 21 | , normalise | ||
| 22 | ) | 11 | ) |
| 23 | where | 12 | where |
| 24 | 13 | ||
| 25 | 14 | ||
| 15 | import Spear.Math.Vector.Class | ||
| 16 | |||
| 26 | import Foreign.C.Types (CFloat) | 17 | import Foreign.C.Types (CFloat) |
| 27 | import Foreign.Storable | 18 | import Foreign.Storable |
| 28 | 19 | ||
| @@ -82,6 +73,39 @@ instance Ord Vector4 where | |||
| 82 | Vector4 (Prelude.max ax bx) (Prelude.max ay by) (Prelude.max az bz) (Prelude.min aw bw) | 73 | Vector4 (Prelude.max ax bx) (Prelude.max ay by) (Prelude.max az bz) (Prelude.min aw bw) |
| 83 | 74 | ||
| 84 | 75 | ||
| 76 | instance VectorClass Vector4 where | ||
| 77 | fromList (ax:ay:az:aw:_) = Vector4 ax ay az aw | ||
| 78 | |||
| 79 | x (Vector4 ax _ _ _ ) = ax | ||
| 80 | |||
| 81 | y (Vector4 _ ay _ _ ) = ay | ||
| 82 | |||
| 83 | z (Vector4 _ _ az _ ) = az | ||
| 84 | |||
| 85 | w (Vector4 _ _ _ aw) = aw | ||
| 86 | |||
| 87 | (Vector4 ax _ _ _) ! 0 = ax | ||
| 88 | (Vector4 _ ay _ _) ! 1 = ay | ||
| 89 | (Vector4 _ _ az _) ! 2 = az | ||
| 90 | (Vector4 _ _ _ aw) ! 3 = aw | ||
| 91 | _ ! _ = 0 | ||
| 92 | |||
| 93 | Vector4 ax ay az aw `dot` Vector4 bx by bz bw = ax*bx + ay*by + az*bz + aw*bw | ||
| 94 | |||
| 95 | normSq (Vector4 ax ay az aw) = ax*ax + ay*ay + az*az + aw*aw | ||
| 96 | |||
| 97 | norm = sqrt . normSq | ||
| 98 | |||
| 99 | scale s (Vector4 ax ay az aw) = Vector4 (s*ax) (s*ay) (s*az) (s*aw) | ||
| 100 | |||
| 101 | neg (Vector4 ax ay az aw) = Vector4 (-ax) (-ay) (-az) (-aw) | ||
| 102 | |||
| 103 | normalise v = | ||
| 104 | let n' = norm v | ||
| 105 | n = if n' == 0 then 1 else n' | ||
| 106 | in scale (1.0 / n) v | ||
| 107 | |||
| 108 | |||
| 85 | sizeFloat = sizeOf (undefined :: CFloat) | 109 | sizeFloat = sizeOf (undefined :: CFloat) |
| 86 | 110 | ||
| 87 | 111 | ||
| @@ -103,30 +127,19 @@ instance Storable Vector4 where | |||
| 103 | pokeByteOff ptr (3 * sizeFloat) aw | 127 | pokeByteOff ptr (3 * sizeFloat) aw |
| 104 | 128 | ||
| 105 | 129 | ||
| 106 | x (Vector4 ax _ _ _ ) = ax | ||
| 107 | y (Vector4 _ ay _ _ ) = ay | ||
| 108 | z (Vector4 _ _ az _ ) = az | ||
| 109 | w (Vector4 _ _ _ aw) = aw | ||
| 110 | |||
| 111 | |||
| 112 | -- | Unit vector along the X axis. | 130 | -- | Unit vector along the X axis. |
| 113 | unitX :: Vector4 | 131 | unitx4 = Vector4 1 0 0 0 |
| 114 | unitX = Vector4 1 0 0 0 | ||
| 115 | 132 | ||
| 116 | 133 | ||
| 117 | -- | Unit vector along the Y axis. | 134 | -- | Unit vector along the Y axis. |
| 118 | unitY :: Vector4 | 135 | unity4 = Vector4 0 1 0 0 |
| 119 | unitY = Vector4 0 1 0 0 | ||
| 120 | 136 | ||
| 121 | 137 | ||
| 122 | -- | Unit vector along the Z axis. | 138 | -- | Unit vector along the Z axis. |
| 123 | unitZ :: Vector4 | 139 | unitz4 = Vector4 0 0 1 0 |
| 124 | unitZ = Vector4 0 0 1 0 | ||
| 125 | 140 | ||
| 126 | 141 | -- | Unit vector along the W axis. | |
| 127 | -- | Create a vector from the given list. | 142 | unitw4 = Vector4 0 0 0 1 |
| 128 | fromList :: [Float] -> Vector4 | ||
| 129 | fromList (ax:ay:az:aw:_) = Vector4 ax ay az aw | ||
| 130 | 143 | ||
| 131 | 144 | ||
| 132 | -- | Create a 4D vector from the given values. | 145 | -- | Create a 4D vector from the given values. |
| @@ -134,43 +147,8 @@ vec4 :: Float -> Float -> Float -> Float -> Vector4 | |||
| 134 | vec4 ax ay az aw = Vector4 ax ay az aw | 147 | vec4 ax ay az aw = Vector4 ax ay az aw |
| 135 | 148 | ||
| 136 | 149 | ||
| 137 | -- | Compute the given vectors' dot product. | ||
| 138 | dot :: Vector4 -> Vector4 -> Float | ||
| 139 | Vector4 ax ay az aw `dot` Vector4 bx by bz bw = ax*bx + ay*by + az*bz + aw*bw | ||
| 140 | |||
| 141 | |||
| 142 | -- | Compute the given vectors' cross product. | 150 | -- | Compute the given vectors' cross product. |
| 143 | -- The vectors are projected to 3D space. The resulting vector is the cross product of the vectors' projections with w=0. | 151 | -- The vectors are projected to 3D space. The resulting vector is the cross product of the vectors' projections with w=0. |
| 144 | cross :: Vector4 -> Vector4 -> Vector4 | 152 | cross' :: Vector4 -> Vector4 -> Vector4 |
| 145 | (Vector4 ax ay az _) `cross` (Vector4 bx by bz _) = | 153 | (Vector4 ax ay az _) `cross'` (Vector4 bx by bz _) = |
| 146 | Vector4 (ay * bz - az * by) (az * bx - ax * bz) (ax * by - ay * bx) 0 | 154 | Vector4 (ay * bz - az * by) (az * bx - ax * bz) (ax * by - ay * bx) 0 |
| 147 | |||
| 148 | |||
| 149 | -- | Compute the given vector's squared norm. | ||
| 150 | normSq :: Vector4 -> Float | ||
| 151 | normSq (Vector4 ax ay az aw) = ax*ax + ay*ay + az*az + aw*aw | ||
| 152 | |||
| 153 | |||
| 154 | -- | Compute the given vector's norm. | ||
| 155 | norm :: Vector4 -> Float | ||
| 156 | norm = sqrt . normSq | ||
| 157 | |||
| 158 | |||
| 159 | -- | Multiply the given vector with the given scalar. | ||
| 160 | scale :: Float -> Vector4 -> Vector4 | ||
| 161 | scale s (Vector4 ax ay az aw) = Vector4 (s*ax) (s*ay) (s*az) (s*aw) | ||
| 162 | |||
| 163 | |||
| 164 | -- | Negate the given vector. | ||
| 165 | neg :: Vector4 -> Vector4 | ||
| 166 | neg (Vector4 ax ay az aw) = Vector4 (-ax) (-ay) (-az) (-aw) | ||
| 167 | |||
| 168 | |||
| 169 | -- | Normalise the given vector. | ||
| 170 | normalise :: Vector4 -> Vector4 | ||
| 171 | normalise v = | ||
| 172 | let n' = norm v | ||
| 173 | n = if n' == 0 then 1 else n' | ||
| 174 | in | ||
| 175 | scale (1.0 / n) v | ||
| 176 | |||
diff --git a/Spear/Physics/Rigid.hs b/Spear/Physics/Rigid.hs index 9147553..99a9d5a 100644 --- a/Spear/Physics/Rigid.hs +++ b/Spear/Physics/Rigid.hs | |||
| @@ -12,7 +12,7 @@ where | |||
| 12 | 12 | ||
| 13 | import qualified Spear.Math.Matrix3 as M3 | 13 | import qualified Spear.Math.Matrix3 as M3 |
| 14 | import Spear.Math.Spatial2 | 14 | import Spear.Math.Spatial2 |
| 15 | import Spear.Math.Vector2 | 15 | import Spear.Math.Vector |
| 16 | import Spear.Physics.Types | 16 | import Spear.Physics.Types |
| 17 | 17 | ||
| 18 | import Data.List (foldl') | 18 | import Data.List (foldl') |
| @@ -31,13 +31,13 @@ instance Spatial2 RigidBody where | |||
| 31 | 31 | ||
| 32 | move v body = body { position = v + position body } | 32 | move v body = body { position = v + position body } |
| 33 | 33 | ||
| 34 | moveFwd speed body = body { position = position body + scale speed unity } | 34 | moveFwd speed body = body { position = position body + scale speed unity2 } |
| 35 | 35 | ||
| 36 | moveBack speed body = body { position = position body + scale (-speed) unity } | 36 | moveBack speed body = body { position = position body + scale (-speed) unity2 } |
| 37 | 37 | ||
| 38 | strafeLeft speed body = body { position = position body + scale (-speed) unitx } | 38 | strafeLeft speed body = body { position = position body + scale (-speed) unitx2 } |
| 39 | 39 | ||
| 40 | strafeRight speed body = body { position = position body + scale speed unitx } | 40 | strafeRight speed body = body { position = position body + scale speed unitx2 } |
| 41 | 41 | ||
| 42 | rotate angle = id | 42 | rotate angle = id |
| 43 | 43 | ||
| @@ -45,13 +45,13 @@ instance Spatial2 RigidBody where | |||
| 45 | 45 | ||
| 46 | pos = position | 46 | pos = position |
| 47 | 47 | ||
| 48 | fwd _ = unity | 48 | fwd _ = unity2 |
| 49 | 49 | ||
| 50 | up _ = unity | 50 | up _ = unity2 |
| 51 | 51 | ||
| 52 | right _ = unitx | 52 | right _ = unitx2 |
| 53 | 53 | ||
| 54 | transform body = M3.transform unitx unity $ position body | 54 | transform body = M3.transform unitx2 unity2 $ position body |
| 55 | 55 | ||
| 56 | setTransform transf body = body { position = M3.position transf } | 56 | setTransform transf body = body { position = M3.position transf } |
| 57 | 57 | ||
| @@ -60,13 +60,13 @@ instance Spatial2 RigidBody where | |||
| 60 | 60 | ||
| 61 | -- | Build a 'RigidBody'. | 61 | -- | Build a 'RigidBody'. |
| 62 | rigidBody :: Mass -> Position -> RigidBody | 62 | rigidBody :: Mass -> Position -> RigidBody |
| 63 | rigidBody m x = RigidBody m x zero zero | 63 | rigidBody m x = RigidBody m x zero2 zero2 |
| 64 | 64 | ||
| 65 | 65 | ||
| 66 | -- | Update the given 'RigidBody'. | 66 | -- | Update the given 'RigidBody'. |
| 67 | update :: [Force] -> Dt -> RigidBody -> RigidBody | 67 | update :: [Force] -> Dt -> RigidBody -> RigidBody |
| 68 | update forces dt body = | 68 | update forces dt body = |
| 69 | let netforce = foldl' (+) zero forces | 69 | let netforce = foldl' (+) zero2 forces |
| 70 | m = mass body | 70 | m = mass body |
| 71 | r1 = position body | 71 | r1 = position body |
| 72 | v1 = velocity body | 72 | v1 = velocity body |
diff --git a/Spear/Physics/Types.hs b/Spear/Physics/Types.hs index de889ee..62e0c04 100644 --- a/Spear/Physics/Types.hs +++ b/Spear/Physics/Types.hs | |||
| @@ -2,7 +2,7 @@ module Spear.Physics.Types | |||
| 2 | where | 2 | where |
| 3 | 3 | ||
| 4 | 4 | ||
| 5 | import Spear.Math.Vector2 | 5 | import Spear.Math.Vector |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | type Dt = Float | 8 | type Dt = Float |
diff --git a/Spear/Render/AnimatedModel.hs b/Spear/Render/AnimatedModel.hs index 76e9e7f..dfaadfd 100644 --- a/Spear/Render/AnimatedModel.hs +++ b/Spear/Render/AnimatedModel.hs | |||
| @@ -34,8 +34,7 @@ import Spear.Collision | |||
| 34 | import Spear.GLSL | 34 | import Spear.GLSL |
| 35 | import Spear.Math.AABB | 35 | import Spear.Math.AABB |
| 36 | import Spear.Math.Matrix4 (Matrix4) | 36 | import Spear.Math.Matrix4 (Matrix4) |
| 37 | import Spear.Math.Vector2 (vec2) | 37 | import Spear.Math.Vector |
| 38 | import Spear.Math.Vector3 (vec3, x, y, z, scale) | ||
| 39 | import Spear.Render.Material | 38 | import Spear.Render.Material |
| 40 | import Spear.Render.Model | 39 | import Spear.Render.Model |
| 41 | import Spear.Render.Program | 40 | import Spear.Render.Program |
diff --git a/Spear/Render/Material.hs b/Spear/Render/Material.hs index f504036..83d8742 100644 --- a/Spear/Render/Material.hs +++ b/Spear/Render/Material.hs | |||
| @@ -4,7 +4,7 @@ module Spear.Render.Material | |||
| 4 | where | 4 | where |
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | import Spear.Math.Vector4 | 7 | import Spear.Math.Vector |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | data Material = Material | 10 | data Material = Material |
diff --git a/Spear/Render/StaticModel.hs b/Spear/Render/StaticModel.hs index c67405a..ed8d065 100644 --- a/Spear/Render/StaticModel.hs +++ b/Spear/Render/StaticModel.hs | |||
| @@ -24,7 +24,7 @@ import Spear.Collision | |||
| 24 | import Spear.GLSL | 24 | import Spear.GLSL |
| 25 | import Spear.Math.AABB | 25 | import Spear.Math.AABB |
| 26 | import Spear.Math.Matrix4 (Matrix4) | 26 | import Spear.Math.Matrix4 (Matrix4) |
| 27 | import Spear.Math.Vector2 (vec2) | 27 | import Spear.Math.Vector |
| 28 | import Spear.Render.Material | 28 | import Spear.Render.Material |
| 29 | import Spear.Render.Model | 29 | import Spear.Render.Model |
| 30 | import Spear.Render.Program | 30 | import Spear.Render.Program |
diff --git a/Spear/Scene/GameObject.hs b/Spear/Scene/GameObject.hs index d98299c..37f9260 100644 --- a/Spear/Scene/GameObject.hs +++ b/Spear/Scene/GameObject.hs | |||
| @@ -40,8 +40,7 @@ import qualified Spear.Math.Matrix4 as M4 | |||
| 40 | import Spear.Math.MatrixUtils | 40 | import Spear.Math.MatrixUtils |
| 41 | import qualified Spear.Math.Spatial2 as S2 | 41 | import qualified Spear.Math.Spatial2 as S2 |
| 42 | import Spear.Math.Utils | 42 | import Spear.Math.Utils |
| 43 | import Spear.Math.Vector2 as V2 | 43 | import Spear.Math.Vector |
| 44 | import Spear.Math.Vector3 as V3 | ||
| 45 | import qualified Spear.Render.AnimatedModel as AM | 44 | import qualified Spear.Render.AnimatedModel as AM |
| 46 | import Spear.Render.Program | 45 | import Spear.Render.Program |
| 47 | import Spear.Render.StaticModel as SM | 46 | import Spear.Render.StaticModel as SM |
| @@ -90,7 +89,7 @@ instance S2.Spatial2 GameObject where | |||
| 90 | 89 | ||
| 91 | moveFwd s go = | 90 | moveFwd s go = |
| 92 | let m = transform go | 91 | let m = transform go |
| 93 | v = V2.scale s $ M3.forward m | 92 | v = scale s $ M3.forward m |
| 94 | in go | 93 | in go |
| 95 | { collisioners = fmap (Col.move v) $ collisioners go | 94 | { collisioners = fmap (Col.move v) $ collisioners go |
| 96 | , transform = M3.translv v * m | 95 | , transform = M3.translv v * m |
| @@ -98,7 +97,7 @@ instance S2.Spatial2 GameObject where | |||
| 98 | 97 | ||
| 99 | moveBack s go = | 98 | moveBack s go = |
| 100 | let m = transform go | 99 | let m = transform go |
| 101 | v = V2.scale (-s) $ M3.forward m | 100 | v = scale (-s) $ M3.forward m |
| 102 | in go | 101 | in go |
| 103 | { collisioners = fmap (Col.move v) $ collisioners go | 102 | { collisioners = fmap (Col.move v) $ collisioners go |
| 104 | , transform = M3.translv v * m | 103 | , transform = M3.translv v * m |
| @@ -106,7 +105,7 @@ instance S2.Spatial2 GameObject where | |||
| 106 | 105 | ||
| 107 | strafeLeft s go = | 106 | strafeLeft s go = |
| 108 | let m = transform go | 107 | let m = transform go |
| 109 | v = V2.scale (-s) $ M3.right m | 108 | v = scale (-s) $ M3.right m |
| 110 | in go | 109 | in go |
| 111 | { collisioners = fmap (Col.move v) $ collisioners go | 110 | { collisioners = fmap (Col.move v) $ collisioners go |
| 112 | , transform = M3.translv v * m | 111 | , transform = M3.translv v * m |
| @@ -114,7 +113,7 @@ instance S2.Spatial2 GameObject where | |||
| 114 | 113 | ||
| 115 | strafeRight s go = | 114 | strafeRight s go = |
| 116 | let m = transform go | 115 | let m = transform go |
| 117 | v = V2.scale s $ M3.right m | 116 | v = scale s $ M3.right m |
| 118 | in go | 117 | in go |
| 119 | { collisioners = fmap (Col.move v) $ collisioners go | 118 | { collisioners = fmap (Col.move v) $ collisioners go |
| 120 | , transform = M3.translv v * m | 119 | , transform = M3.translv v * m |
| @@ -150,20 +149,20 @@ instance S2.Spatial2 GameObject where | |||
| 150 | 149 | ||
| 151 | lookAt p go = | 150 | lookAt p go = |
| 152 | let position = S2.pos go | 151 | let position = S2.pos go |
| 153 | fwd = V2.normalise $ p - position | 152 | fwd = normalise $ p - position |
| 154 | r = perp fwd | 153 | r = perp fwd |
| 155 | toDeg = (*(180/pi)) | 154 | toDeg = (*(180/pi)) |
| 156 | viewI = viewInv . window $ go | 155 | viewI = viewInv . window $ go |
| 157 | p1 = viewToWorld2d position viewI | 156 | p1 = viewToWorld2d position viewI |
| 158 | p2 = viewToWorld2d (position + fwd) viewI | 157 | p2 = viewToWorld2d (position + fwd) viewI |
| 159 | f = V2.normalise $ p2 - p1 | 158 | f = normalise $ p2 - p1 |
| 160 | in | 159 | in |
| 161 | go | 160 | go |
| 162 | { transform = M3.transform r fwd position | 161 | { transform = M3.transform r fwd position |
| 163 | , angle = 180 - | 162 | , angle = 180 - |
| 164 | if V2.x f > 0 | 163 | if x f > 0 |
| 165 | then toDeg . acos $ f `V2.dot` V2.unity | 164 | then toDeg . acos $ f `dot` unity2 |
| 166 | else (+180) . toDeg . acos $ f `V2.dot` (-V2.unity) | 165 | else (+180) . toDeg . acos $ f `dot` (-unity2) |
| 167 | } | 166 | } |
| 168 | 167 | ||
| 169 | 168 | ||
diff --git a/Spear/Scene/Light.hs b/Spear/Scene/Light.hs index f482560..5f43b19 100644 --- a/Spear/Scene/Light.hs +++ b/Spear/Scene/Light.hs | |||
| @@ -7,8 +7,7 @@ where | |||
| 7 | 7 | ||
| 8 | import qualified Spear.Math.Matrix4 as M | 8 | import qualified Spear.Math.Matrix4 as M |
| 9 | import qualified Spear.Math.Spatial3 as S | 9 | import qualified Spear.Math.Spatial3 as S |
| 10 | import Spear.Math.Vector3 | 10 | import Spear.Math.Vector |
| 11 | import qualified Spear.Math.Vector4 as V4 | ||
| 12 | 11 | ||
| 13 | 12 | ||
| 14 | data Light | 13 | data Light |
diff --git a/Spear/Scene/Loader.hs b/Spear/Scene/Loader.hs index 3bc29fa..07d4f05 100644 --- a/Spear/Scene/Loader.hs +++ b/Spear/Scene/Loader.hs | |||
| @@ -24,9 +24,7 @@ import qualified Spear.GLSL as GLSL | |||
| 24 | import Spear.Math.Matrix3 as M3 | 24 | import Spear.Math.Matrix3 as M3 |
| 25 | import Spear.Math.Matrix4 as M4 | 25 | import Spear.Math.Matrix4 as M4 |
| 26 | import Spear.Math.MatrixUtils (fastNormalMatrix) | 26 | import Spear.Math.MatrixUtils (fastNormalMatrix) |
| 27 | import Spear.Math.Vector2 as V2 | 27 | import Spear.Math.Vector |
| 28 | import Spear.Math.Vector3 as V3 | ||
| 29 | import Spear.Math.Vector4 as V4 | ||
| 30 | import Spear.Render.AnimatedModel as AM | 28 | import Spear.Render.AnimatedModel as AM |
| 31 | import Spear.Render.Material | 29 | import Spear.Render.Material |
| 32 | import Spear.Render.Program | 30 | import Spear.Render.Program |
| @@ -204,27 +202,27 @@ loadModel' file rotation scale = do | |||
| 204 | (case scale of | 202 | (case scale of |
| 205 | Nothing -> Prelude.id | 203 | Nothing -> Prelude.id |
| 206 | Just s -> flip Model.transformVerts $ | 204 | Just s -> flip Model.transformVerts $ |
| 207 | \(Vec3 x' y' z') -> Vec3 (V3.x s * x') (V3.y s * y') (V3.z s * z')) | 205 | \(Vec3 x' y' z') -> Vec3 (x s * x') (y s * y') (z s * z')) |
| 208 | 206 | ||
| 209 | (fmap transform $ Model.loadModel file) >>= setupIO . toGround | 207 | (fmap transform $ Model.loadModel file) >>= setupIO . toGround |
| 210 | 208 | ||
| 211 | 209 | ||
| 212 | rotateModel :: Rotation -> Model -> Model | 210 | rotateModel :: Rotation -> Model -> Model |
| 213 | rotateModel (Rotation x y z order) model = | 211 | rotateModel (Rotation ax ay az order) model = |
| 214 | let mat = case order of | 212 | let mat = case order of |
| 215 | XYZ -> rotZ z * rotY y * rotX x | 213 | XYZ -> rotZ az * rotY ay * rotX ax |
| 216 | XZY -> rotY y * rotZ z * rotX x | 214 | XZY -> rotY ay * rotZ az * rotX ax |
| 217 | YXZ -> rotZ z * rotX x * rotY y | 215 | YXZ -> rotZ az * rotX ax * rotY ay |
| 218 | YZX -> rotX x * rotZ z * rotY y | 216 | YZX -> rotX ax * rotZ az * rotY ay |
| 219 | ZXY -> rotY y * rotX x * rotZ z | 217 | ZXY -> rotY ay * rotX ax * rotZ az |
| 220 | ZYX -> rotX x * rotY y * rotZ z | 218 | ZYX -> rotX ax * rotY ay * rotZ az |
| 221 | normalMat = fastNormalMatrix mat | 219 | normalMat = fastNormalMatrix mat |
| 222 | 220 | ||
| 223 | vTransform (Vec3 x' y' z') = | 221 | vTransform (Vec3 x' y' z') = |
| 224 | let v = mat `M4.mulp` (vec3 x' y' z') in Vec3 (V3.x v) (V3.y v) (V3.z v) | 222 | let v = mat `M4.mulp` (vec3 x' y' z') in Vec3 (x v) (y v) (z v) |
| 225 | 223 | ||
| 226 | nTransform (Vec3 x' y' z') = | 224 | nTransform (Vec3 x' y' z') = |
| 227 | let v = normalMat `M3.mul` (vec3 x' y' z') in Vec3 (V3.x v) (V3.y v) (V3.z v) | 225 | let v = normalMat `M3.mul` (vec3 x' y' z') in Vec3 (x v) (y v) (z v) |
| 228 | in | 226 | in |
| 229 | flip Model.transformVerts vTransform . flip Model.transformNormals nTransform $ model | 227 | flip Model.transformVerts vTransform . flip Model.transformNormals nTransform $ model |
| 230 | 228 | ||
| @@ -404,7 +402,7 @@ newObject' newGO sceneRes nid props = do | |||
| 404 | 402 | ||
| 405 | vectors :: Maybe Vector2 -> (Vector2, Vector2) | 403 | vectors :: Maybe Vector2 -> (Vector2, Vector2) |
| 406 | vectors up = case up of | 404 | vectors up = case up of |
| 407 | Nothing -> (V2.unitx, V2.unity) | 405 | Nothing -> (unitx2, unity2) |
| 408 | Just u -> (perp u, u) | 406 | Just u -> (perp u, u) |
| 409 | 407 | ||
| 410 | 408 | ||
diff --git a/Spear/Scene/SceneResources.hs b/Spear/Scene/SceneResources.hs index ab96dc6..c2dabcf 100644 --- a/Spear/Scene/SceneResources.hs +++ b/Spear/Scene/SceneResources.hs | |||
| @@ -20,7 +20,7 @@ where | |||
| 20 | 20 | ||
| 21 | import Spear.Assets.Model as Model | 21 | import Spear.Assets.Model as Model |
| 22 | import Spear.GLSL as GLSL | 22 | import Spear.GLSL as GLSL |
| 23 | import Spear.Math.Vector3 | 23 | import Spear.Math.Vector |
| 24 | import Spear.Render.AnimatedModel | 24 | import Spear.Render.AnimatedModel |
| 25 | import Spear.Render.Material | 25 | import Spear.Render.Material |
| 26 | import Spear.Render.Program | 26 | import Spear.Render.Program |
