module FWGL.Transformation (
transMat4,
rotXMat4,
rotYMat4,
rotZMat4,
rotMat4,
scaleMat4,
orthoMat4,
perspectiveMat4,
cameraMat4,
lookAtMat4,
transMat3,
rotMat3,
scaleMat3
) where
import Control.Applicative
import Data.Vect.Float
import Foreign.Storable
import Foreign.Ptr (castPtr)
transMat4 :: Vec3 -> Mat4
transMat4 (Vec3 x y z) = Mat4 (Vec4 1 0 0 0)
(Vec4 0 1 0 0)
(Vec4 0 0 1 0)
(Vec4 x y z 1)
rotXMat4 :: Float -> Mat4
rotXMat4 a = Mat4 (Vec4 1 0 0 0)
(Vec4 0 (cos a) ( sin a) 0)
(Vec4 0 (sin a) (cos a) 0)
(Vec4 0 0 0 1)
rotYMat4 :: Float -> Mat4
rotYMat4 a = Mat4 (Vec4 (cos a) 0 (sin a) 0)
(Vec4 0 1 0 0)
(Vec4 ( sin a) 0 (cos a) 0)
(Vec4 0 0 0 1)
rotZMat4 :: Float -> Mat4
rotZMat4 a = Mat4 (Vec4 (cos a) ( sin a) 0 0)
(Vec4 (sin a) (cos a) 0 0)
(Vec4 0 0 1 0)
(Vec4 0 0 0 1)
rotMat4 :: Vec3
-> Float
-> Mat4
rotMat4 v a = let (Mat3 x y z) = rotMatrix3 v a
in Mat4 (extendZero x)
(extendZero y)
(extendZero z)
(Vec4 0 0 0 1)
scaleMat4 :: Vec3 -> Mat4
scaleMat4 (Vec3 x y z) = Mat4 (Vec4 x 0 0 0)
(Vec4 0 y 0 0)
(Vec4 0 0 z 0)
(Vec4 0 0 0 1)
perspectiveMat4 :: Float
-> Float
-> Float
-> Float
-> Mat4
perspectiveMat4 n f fov ar =
Mat4 (Vec4 (s / ar) 0 0 0)
(Vec4 0 s 0 0)
(Vec4 0 0 ((f + n) / (n f)) ((2 * f * n) / (n f)))
(Vec4 0 0 ( 1) 0)
where s = 1 / tan (fov * pi / 360)
orthoMat4 :: Float
-> Float
-> Float
-> Float
-> Float
-> Float
-> Mat4
orthoMat4 n f l r b t =
Mat4 (Vec4 (2 / (r l)) 0 0 ((r + l) / (r l)))
(Vec4 0 (2 / (t b)) 0 ((t + b) / (t b)))
(Vec4 0 0 (2 / (n f)) (( f + n) / (n f)))
(Vec4 0 0 0 1)
cameraMat4 :: Vec3
-> Float
-> Float
-> Mat4
cameraMat4 eye pitch yaw =
Mat4 (Vec4 xx yx zx 0)
(Vec4 xy yy zy 0)
(Vec4 xz yz zz 0)
(Vec4 ( dotprod xv eye) ( dotprod yv eye) ( dotprod zv eye) 1)
where cosPitch = cos pitch
sinPitch = sin pitch
cosYaw = cos yaw
sinYaw = sin yaw
xv@(Vec3 xx xy xz) = Vec3 cosYaw 0 $ sinYaw
yv@(Vec3 yx yy yz) = Vec3 (sinYaw * sinPitch) cosPitch $
cosYaw * sinPitch
zv@(Vec3 zx zy zz) = Vec3 (sinYaw * cosPitch) (sinPitch) $
cosPitch * cosYaw
lookAtMat4 :: Vec3
-> Vec3
-> Vec3
-> Mat4
lookAtMat4 eye target up =
Mat4 (Vec4 xx yx zx 0)
(Vec4 xy yy zy 0)
(Vec4 xz yz zz 0)
(Vec4 ( dotprod xv eye) ( dotprod yv eye) ( dotprod zv eye) 1)
where zv@(Vec3 zx zy zz) = normalize $ eye &- target
xv@(Vec3 xx xy xz) = normalize $ crossprod up zv
yv@(Vec3 yx yy yz) = crossprod zv xv
transMat3 :: Vec2 -> Mat3
transMat3 (Vec2 x y) = Mat3 (Vec3 1 0 0)
(Vec3 0 1 0)
(Vec3 x y 1)
rotMat3 :: Float -> Mat3
rotMat3 a = Mat3 (Vec3 (cos a) (sin a) 0)
(Vec3 ( sin a) (cos a) 0)
(Vec3 0 0 1)
scaleMat3 :: Vec2 -> Mat3
scaleMat3 (Vec2 x y) = Mat3 (Vec3 x 0 0)
(Vec3 0 y 0)
(Vec3 0 0 1)
zipWithM_ :: Monad m => (a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ f xs = sequence_ . zipWith f xs