Files
loop/src/MatrixHelper.hs
T
2021-06-26 22:20:41 +02:00

91 lines
2.0 KiB
Haskell

module MatrixHelper
( perspectiveMatrixb
, perspectiveMatrixc
) where
--import Geometry
import Geometry.Data
import Linear.Matrix
import Linear.V4
import Graphics.Rendering.OpenGL (GLfloat)
perspectiveMatrixb
:: Float -- ^ Rotation
-> Float -- ^ Zoom
-> Point2 -- ^ Translation
-> Point2 -- ^ Window size
-> Point2 -- ^ View froms
-> [GLfloat]
perspectiveMatrixb rot zoom (tranx,trany) (winx,winy) (viewFromx,viewFromy) =
concatMap vToL
. vToL
. lmt
$ scaleMat (2*zoom/winx,2*zoom/winy)
!*! rotMatr (-rot)
!*! transMat (viewFromx-tranx,viewFromy-trany)
!*! perMat
!*! transMat (-viewFromx,-viewFromy)
!*! vertScale
!*! vertTrans
perspectiveMatrixc
:: Float -- ^ Rotation
-> Float -- ^ Zoom
-> Point2 -- ^ Translation
-> Point2 -- ^ Window size
-> [GLfloat]
perspectiveMatrixc rot zoom (tranx,trany) (winx,winy) = concatMap vToL
. vToL
. lmt
$ scaleMat (2*zoom/winx,2*zoom/winy)
!*! rotMatr (-rot)
!*! transMat (-tranx,-trany)
lmt :: V4 (V4 a) -> V4 (V4 a)
lmt = Linear.Matrix.transpose
vToL :: V4 a -> [a]
vToL (V4 a b c d) = [a,b,c,d]
perMat :: V4 (V4 Float)
perMat = V4
(V4 1 0 0 0)
(V4 0 1 0 0)
(V4 0 0 1 0)
(V4 0 0 1 1)
scaleMat :: Point2 -> V4 (V4 Float)
scaleMat (x,y) = V4
(V4 x 0 0 0)
(V4 0 y 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
rotMatr :: Float -> V4 (V4 Float)
rotMatr a = V4
(V4 (cos a) (sin (-a)) 0 0)
(V4 (sin a) (cos a) 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
transMat :: Point2 -> V4 (V4 Float)
transMat (x,y) = V4
(V4 1 0 0 x)
(V4 0 1 0 y)
(V4 0 0 1 0)
(V4 0 0 0 1)
vertScale :: V4 (V4 Float)
vertScale = V4
(V4 1 0 0 0)
(V4 0 1 0 0)
(V4 0 0 (negate 0.005) 0)
(V4 0 0 0 1)
vertTrans :: V4 (V4 Float)
vertTrans = V4
(V4 1 0 0 0)
(V4 0 1 0 0)
(V4 0 0 1 (negate 20))
(V4 0 0 0 1)