module Graphics.LambdaCube.Utility where
import Data.List hiding (transpose)
import Graphics.LambdaCube.Types
groupSetBy :: (a -> a -> Ordering) -> [a] -> [[a]]
groupSetBy f = groupBy (\x y -> f x y == EQ) . sortBy f
perspective :: FloatType
-> FloatType
-> FloatType
-> FloatType
-> Mat4
perspective n f fovy aspect = transpose $
Mat4 (Vec4 (2*n/(rl)) 0 ((r+l)/(rl)) 0)
(Vec4 0 (2*n/(tb)) ((t+b)/(tb)) 0)
(Vec4 0 0 ((f+n)/(fn)) (2*f*n/(fn)))
(Vec4 0 0 (1) 0)
where
t = n*tan(fovy/2)
b = t
r = aspect*t
l = r
rotationEuler :: Vec3 -> Proj4
rotationEuler (Vec3 a b c) = orthogonal $ toOrthoUnsafe $ rotMatrixY a .*. rotMatrixX b .*. rotMatrixZ c
lookat :: Vec3
-> Vec3
-> Vec3
-> Proj4
lookat pos target up = translateBefore4 (neg pos) (orthogonal $ toOrthoUnsafe r)
where
w = normalize $ pos &- target
u = normalize $ up &^ w
v = w &^ u
r = transpose $ Mat3 u v w
foldM' :: Monad m => (a -> b -> m a) -> a -> [b] -> m a
foldM' _ a [] = return a
foldM' f a (x:xs) = f a x >>= \fax -> fax `seq` foldM' f fax xs