Cleanup matrices

This commit is contained in:
jgk
2021-06-24 18:49:10 +02:00
parent d4795749e3
commit bae8a4ed94
7 changed files with 35 additions and 141 deletions
+22 -113
View File
@@ -1,9 +1,6 @@
module MatrixHelper
( perspectiveMatrix
, perspectiveMatrixa
, perspectiveMatrixb
( perspectiveMatrixb
, perspectiveMatrixc
, isoMatrix
) where
--import Geometry
import Geometry.Data
@@ -12,97 +9,6 @@ import Linear.Matrix
import Linear.V4
import Graphics.Rendering.OpenGL (GLfloat)
perspectiveMatrix
:: Float -- ^ Rotation
-> Float -- ^ Zoom
-> Point2 -- ^ Translation
-> Point2 -- ^ Window size
-> Point2 -- ^ View froms
-> [GLfloat]
perspectiveMatrix rot zoom (tranx,trany) (winx,winy) (viewFromx,viewFromy) =
let scalMat = Linear.Matrix.transpose $ V4
(V4 (2*zoom/winx) 0 0 (0::GLfloat))
(V4 0 (2*zoom/winy) 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
rotMat = Linear.Matrix.transpose $
V4 (V4 (cos rot) (sin (-rot)) 0 0)
(V4 (sin rot) (cos rot) 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
tranMat2 = Linear.Matrix.transpose $
V4 (V4 1 0 0 0)
(V4 0 1 0 0)
(V4 0 0 1 0)
(V4 (viewFromx-tranx) (viewFromy-trany) 0 1)
tranMat1 = Linear.Matrix.transpose $
V4 (V4 1 0 0 0)
(V4 0 1 0 0)
(V4 0 0 1 0)
(V4 (-viewFromx) (-viewFromy) 0 1)
wmat = scalMat !*! rotMat !*! tranMat2 !*! lmt perMat !*! tranMat1
vToL (V4 a b c d) = [a,b,c,d]
in concatMap vToL $ vToL wmat
{- | A matrix that gives a top down view with no perspective. -}
isoMatrix
:: Float -- ^ Rotation
-> Float -- ^ Zoom
-> Point2 -- ^ Translation
-> Point2 -- ^ Window size
-> Point2 -- ^ View froms
-> [GLfloat]
isoMatrix rot czoom (tranx,trany) (winx,winy) _ =
let scalMat = Linear.Matrix.transpose $
V4 (V4 (2*czoom/winx) 0 0 (0::GLfloat))
(V4 0 (2*czoom/winy) 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
rotMat = Linear.Matrix.transpose $
V4 (V4 (cos rot) (sin (-rot)) 0 0)
(V4 (sin rot) (cos rot) 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
tranMat = Linear.Matrix.transpose $
V4 (V4 1 0 0 0)
(V4 0 1 0 0)
(V4 0 0 1 0)
(V4 (-tranx) (-trany) 0 1)
wmat = scalMat !*! rotMat !*! tranMat
vToL (V4 a b c d) = [a,b,c,d]
in concatMap vToL $ vToL wmat
perspectiveMatrixa
:: Float -- ^ Rotation
-> Float -- ^ Zoom
-> Point2 -- ^ Translation
-> Point2 -- ^ Window size
-> Point2 -- ^ View froms
-> [Float]
perspectiveMatrixa rot zoom (tranx,trany) (winx,winy) (viewFromx,viewFromy) =
let scalMat = V4
(V4 (2*zoom/winx) 0 0 (0))
(V4 0 (2*zoom/winy) 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
rotMat =
V4 (V4 (cos rot) (sin (-rot)) 0 0)
(V4 (sin rot) (cos rot) 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
tranMat2 =
V4 (V4 1 0 0 0)
(V4 0 1 0 0)
(V4 0 0 1 0)
(V4 (viewFromx-tranx) (viewFromy-trany) 0 1)
tranMat1 =
V4 (V4 1 0 0 0)
(V4 0 1 0 0)
(V4 0 0 1 0)
(V4 (-viewFromx) (-viewFromy) 0 1)
wmat = scalMat !*! rotMat !*! tranMat2 !*! lmt perMat !*! tranMat1
vToL (V4 a b c d) = [a,b,c,d]
in concatMap vToL $ vToL wmat
perspectiveMatrixb
:: Float -- ^ Rotation
-> Float -- ^ Zoom
@@ -111,13 +17,14 @@ perspectiveMatrixb
-> Point2 -- ^ View froms
-> [GLfloat]
perspectiveMatrixb rot zoom (tranx,trany) (winx,winy) (viewFromx,viewFromy) =
let wmat = lmt $ lmt (scaleMat (2*zoom/winx,2*zoom/winy))
!*! rotMatr (-rot)
!*! lmt (transMat (viewFromx-tranx,viewFromy-trany))
!*! lmt perMat
!*! lmt (transMat (-viewFromx,-viewFromy))
vToL (V4 a b c d) = [a,b,c,d]
in concatMap vToL $ vToL wmat
concatMap vToL
. vToL
. lmt
$ scaleMat (2*zoom/winx,2*zoom/winy)
!*! rotMatr (-rot)
!*! transMat (viewFromx-tranx,viewFromy-trany)
!*! perMat
!*! transMat (-viewFromx,-viewFromy)
perspectiveMatrixc
:: Float -- ^ Rotation
@@ -125,22 +32,25 @@ perspectiveMatrixc
-> Point2 -- ^ Translation
-> Point2 -- ^ Window size
-> [GLfloat]
perspectiveMatrixc rot zoom (tranx,trany) (winx,winy) =
let wmat = lmt $ lmt (scaleMat (2*zoom/winx,2*zoom/winy))
!*! rotMatr (-rot)
!*! lmt (transMat (-tranx,-trany))
vToL (V4 a b c d) = [a,b,c,d]
in concatMap vToL $ vToL wmat
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)
(V4 0 0 0 1)
scaleMat :: Point2 -> V4 (V4 Float)
scaleMat (x,y) = V4
@@ -158,8 +68,7 @@ rotMatr a = V4
transMat :: Point2 -> V4 (V4 Float)
transMat (x,y) = V4
(V4 1 0 0 0)
(V4 0 1 0 0)
(V4 1 0 0 x)
(V4 0 1 0 y)
(V4 0 0 1 0)
(V4 x y 0 1)
(V4 0 0 0 1)