{-# LANGUAGE CPP #-}
module Linear.Projection
( lookAt
, perspective, inversePerspective
, infinitePerspective, inverseInfinitePerspective
, frustum, inverseFrustum
, ortho, inverseOrtho
) where
import Control.Lens hiding (index)
import Linear.V3
import Linear.V4
import Linear.Matrix
import Linear.Epsilon
import Linear.Metric
lookAt
:: (Epsilon a, Floating a)
=> V3 a
-> V3 a
-> V3 a
-> M44 a
lookAt :: V3 a -> V3 a -> V3 a -> M44 a
lookAt V3 a
eye V3 a
center V3 a
up =
V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 (V3 a
xaV3 a -> Getting a (V3 a) a -> a
forall s a. s -> Getting a s a -> a
^.Getting a (V3 a) a
forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x) (V3 a
xaV3 a -> Getting a (V3 a) a -> a
forall s a. s -> Getting a s a -> a
^.Getting a (V3 a) a
forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y) (V3 a
xaV3 a -> Getting a (V3 a) a -> a
forall s a. s -> Getting a s a -> a
^.Getting a (V3 a) a
forall (t :: * -> *) a. R3 t => Lens' (t a) a
_z) a
xd)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 (V3 a
yaV3 a -> Getting a (V3 a) a -> a
forall s a. s -> Getting a s a -> a
^.Getting a (V3 a) a
forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x) (V3 a
yaV3 a -> Getting a (V3 a) a -> a
forall s a. s -> Getting a s a -> a
^.Getting a (V3 a) a
forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y) (V3 a
yaV3 a -> Getting a (V3 a) a -> a
forall s a. s -> Getting a s a -> a
^.Getting a (V3 a) a
forall (t :: * -> *) a. R3 t => Lens' (t a) a
_z) a
yd)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 (-V3 a
zaV3 a -> Getting a (V3 a) a -> a
forall s a. s -> Getting a s a -> a
^.Getting a (V3 a) a
forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x) (-V3 a
zaV3 a -> Getting a (V3 a) a -> a
forall s a. s -> Getting a s a -> a
^.Getting a (V3 a) a
forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y) (-V3 a
zaV3 a -> Getting a (V3 a) a -> a
forall s a. s -> Getting a s a -> a
^.Getting a (V3 a) a
forall (t :: * -> *) a. R3 t => Lens' (t a) a
_z) a
zd)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 a
1)
where za :: V3 a
za = V3 a -> V3 a
forall a (f :: * -> *).
(Floating a, Metric f, Epsilon a) =>
f a -> f a
normalize (V3 a -> V3 a) -> V3 a -> V3 a
forall a b. (a -> b) -> a -> b
$ V3 a
center V3 a -> V3 a -> V3 a
forall a. Num a => a -> a -> a
- V3 a
eye
xa :: V3 a
xa = V3 a -> V3 a
forall a (f :: * -> *).
(Floating a, Metric f, Epsilon a) =>
f a -> f a
normalize (V3 a -> V3 a) -> V3 a -> V3 a
forall a b. (a -> b) -> a -> b
$ V3 a -> V3 a -> V3 a
forall a. Num a => V3 a -> V3 a -> V3 a
cross V3 a
za V3 a
up
ya :: V3 a
ya = V3 a -> V3 a -> V3 a
forall a. Num a => V3 a -> V3 a -> V3 a
cross V3 a
xa V3 a
za
xd :: a
xd = -V3 a -> V3 a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
dot V3 a
xa V3 a
eye
yd :: a
yd = -V3 a -> V3 a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
dot V3 a
ya V3 a
eye
zd :: a
zd = V3 a -> V3 a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
dot V3 a
za V3 a
eye
perspective
:: Floating a
=> a
-> a
-> a
-> a
-> M44 a
perspective :: a -> a -> a -> a -> M44 a
perspective a
fovy a
aspect a
near a
far =
V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
x a
0 a
0 a
0)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
y a
0 a
0)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
z a
w)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (-a
1) a
0)
where tanHalfFovy :: a
tanHalfFovy = a -> a
forall a. Floating a => a -> a
tan (a -> a) -> a -> a
forall a b. (a -> b) -> a -> b
$ a
fovy a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2
x :: a
x = a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
aspect a -> a -> a
forall a. Num a => a -> a -> a
* a
tanHalfFovy)
y :: a
y = a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
tanHalfFovy
fpn :: a
fpn = a
far a -> a -> a
forall a. Num a => a -> a -> a
+ a
near
fmn :: a
fmn = a
far a -> a -> a
forall a. Num a => a -> a -> a
- a
near
oon :: a
oon = a
0.5a -> a -> a
forall a. Fractional a => a -> a -> a
/a
near
oof :: a
oof = a
0.5a -> a -> a
forall a. Fractional a => a -> a -> a
/a
far
z :: a
z = -a
fpna -> a -> a
forall a. Fractional a => a -> a -> a
/a
fmn
w :: a
w = a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/(a
oofa -> a -> a
forall a. Num a => a -> a -> a
-a
oon)
#ifdef HERBIE
{-# ANN perspective "NoHerbie" #-}
#endif
inversePerspective
:: Floating a
=> a
-> a
-> a
-> a
-> M44 a
inversePerspective :: a -> a -> a -> a -> M44 a
inversePerspective a
fovy a
aspect a
near a
far =
V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
a a
0 a
0 a
0 )
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
b a
0 a
0 )
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 (-a
1))
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
c a
d )
where tanHalfFovy :: a
tanHalfFovy = a -> a
forall a. Floating a => a -> a
tan (a -> a) -> a -> a
forall a b. (a -> b) -> a -> b
$ a
fovy a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2
a :: a
a = a
aspect a -> a -> a
forall a. Num a => a -> a -> a
* a
tanHalfFovy
b :: a
b = a
tanHalfFovy
c :: a
c = a
oon a -> a -> a
forall a. Num a => a -> a -> a
- a
oof
d :: a
d = a
oon a -> a -> a
forall a. Num a => a -> a -> a
+ a
oof
oon :: a
oon = a
0.5a -> a -> a
forall a. Fractional a => a -> a -> a
/a
near
oof :: a
oof = a
0.5a -> a -> a
forall a. Fractional a => a -> a -> a
/a
far
frustum
:: Floating a
=> a
-> a
-> a
-> a
-> a
-> a
-> M44 a
frustum :: a -> a -> a -> a -> a -> a -> M44 a
frustum a
l a
r a
b a
t a
n a
f =
V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
x a
0 a
a a
0)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
y a
e a
0)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
c a
d)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (-a
1) a
0)
where
rml :: a
rml = a
ra -> a -> a
forall a. Num a => a -> a -> a
-a
l
tmb :: a
tmb = a
ta -> a -> a
forall a. Num a => a -> a -> a
-a
b
fmn :: a
fmn = a
fa -> a -> a
forall a. Num a => a -> a -> a
-a
n
x :: a
x = a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
na -> a -> a
forall a. Fractional a => a -> a -> a
/a
rml
y :: a
y = a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
na -> a -> a
forall a. Fractional a => a -> a -> a
/a
tmb
a :: a
a = (a
ra -> a -> a
forall a. Num a => a -> a -> a
+a
l)a -> a -> a
forall a. Fractional a => a -> a -> a
/a
rml
e :: a
e = (a
ta -> a -> a
forall a. Num a => a -> a -> a
+a
b)a -> a -> a
forall a. Fractional a => a -> a -> a
/a
tmb
c :: a
c = a -> a
forall a. Num a => a -> a
negate (a
fa -> a -> a
forall a. Num a => a -> a -> a
+a
n)a -> a -> a
forall a. Fractional a => a -> a -> a
/a
fmn
d :: a
d = (-a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
fa -> a -> a
forall a. Num a => a -> a -> a
*a
n)a -> a -> a
forall a. Fractional a => a -> a -> a
/a
fmn
inverseFrustum
:: Floating a
=> a
-> a
-> a
-> a
-> a
-> a
-> M44 a
inverseFrustum :: a -> a -> a -> a -> a -> a -> M44 a
inverseFrustum a
l a
r a
b a
t a
n a
f =
V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
rx a
0 a
0 a
ax)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
ry a
0 a
by)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 (-a
1))
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
rd a
cd)
where
hrn :: a
hrn = a
0.5a -> a -> a
forall a. Fractional a => a -> a -> a
/a
n
hrnf :: a
hrnf = a
0.5a -> a -> a
forall a. Fractional a => a -> a -> a
/(a
na -> a -> a
forall a. Num a => a -> a -> a
*a
f)
rx :: a
rx = (a
ra -> a -> a
forall a. Num a => a -> a -> a
-a
l)a -> a -> a
forall a. Num a => a -> a -> a
*a
hrn
ry :: a
ry = (a
ta -> a -> a
forall a. Num a => a -> a -> a
-a
b)a -> a -> a
forall a. Num a => a -> a -> a
*a
hrn
ax :: a
ax = (a
ra -> a -> a
forall a. Num a => a -> a -> a
+a
l)a -> a -> a
forall a. Num a => a -> a -> a
*a
hrn
by :: a
by = (a
ta -> a -> a
forall a. Num a => a -> a -> a
+a
b)a -> a -> a
forall a. Num a => a -> a -> a
*a
hrn
cd :: a
cd = (a
fa -> a -> a
forall a. Num a => a -> a -> a
+a
n)a -> a -> a
forall a. Num a => a -> a -> a
*a
hrnf
rd :: a
rd = (a
na -> a -> a
forall a. Num a => a -> a -> a
-a
f)a -> a -> a
forall a. Num a => a -> a -> a
*a
hrnf
infinitePerspective
:: Floating a
=> a
-> a
-> a
-> M44 a
infinitePerspective :: a -> a -> a -> M44 a
infinitePerspective a
fovy a
a a
n =
V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
x a
0 a
0 a
0)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
y a
0 a
0)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (-a
1) a
w)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (-a
1) a
0)
where
t :: a
t = a
na -> a -> a
forall a. Num a => a -> a -> a
*a -> a
forall a. Floating a => a -> a
tan(a
fovya -> a -> a
forall a. Fractional a => a -> a -> a
/a
2)
b :: a
b = -a
t
l :: a
l = a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
a
r :: a
r = a
ta -> a -> a
forall a. Num a => a -> a -> a
*a
a
x :: a
x = (a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
n)a -> a -> a
forall a. Fractional a => a -> a -> a
/(a
ra -> a -> a
forall a. Num a => a -> a -> a
-a
l)
y :: a
y = (a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
n)a -> a -> a
forall a. Fractional a => a -> a -> a
/(a
ta -> a -> a
forall a. Num a => a -> a -> a
-a
b)
w :: a
w = -a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
n
inverseInfinitePerspective
:: Floating a
=> a
-> a
-> a
-> M44 a
inverseInfinitePerspective :: a -> a -> a -> M44 a
inverseInfinitePerspective a
fovy a
a a
n =
V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
rx a
0 a
0 a
0)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
ry a
0 a
0)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 (-a
1))
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
rw (-a
rw))
where
t :: a
t = a
na -> a -> a
forall a. Num a => a -> a -> a
*a -> a
forall a. Floating a => a -> a
tan(a
fovya -> a -> a
forall a. Fractional a => a -> a -> a
/a
2)
b :: a
b = -a
t
l :: a
l = a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
a
r :: a
r = a
ta -> a -> a
forall a. Num a => a -> a -> a
*a
a
hrn :: a
hrn = a
0.5a -> a -> a
forall a. Fractional a => a -> a -> a
/a
n
rx :: a
rx = (a
ra -> a -> a
forall a. Num a => a -> a -> a
-a
l)a -> a -> a
forall a. Num a => a -> a -> a
*a
hrn
ry :: a
ry = (a
ta -> a -> a
forall a. Num a => a -> a -> a
-a
b)a -> a -> a
forall a. Num a => a -> a -> a
*a
hrn
rw :: a
rw = -a
hrn
ortho
:: Fractional a
=> a
-> a
-> a
-> a
-> a
-> a
-> M44 a
ortho :: a -> a -> a -> a -> a -> a -> M44 a
ortho a
l a
r a
b a
t a
n a
f =
V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 (-a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
x) a
0 a
0 ((a
ra -> a -> a
forall a. Num a => a -> a -> a
+a
l)a -> a -> a
forall a. Num a => a -> a -> a
*a
x))
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 (-a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
y) a
0 ((a
ta -> a -> a
forall a. Num a => a -> a -> a
+a
b)a -> a -> a
forall a. Num a => a -> a -> a
*a
y))
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
z) ((a
fa -> a -> a
forall a. Num a => a -> a -> a
+a
n)a -> a -> a
forall a. Num a => a -> a -> a
*a
z))
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 a
1)
where x :: a
x = a -> a
forall a. Fractional a => a -> a
recip(a
la -> a -> a
forall a. Num a => a -> a -> a
-a
r)
y :: a
y = a -> a
forall a. Fractional a => a -> a
recip(a
ba -> a -> a
forall a. Num a => a -> a -> a
-a
t)
z :: a
z = a -> a
forall a. Fractional a => a -> a
recip(a
na -> a -> a
forall a. Num a => a -> a -> a
-a
f)
inverseOrtho
:: Fractional a
=> a
-> a
-> a
-> a
-> a
-> a
-> M44 a
inverseOrtho :: a -> a -> a -> a -> a -> a -> M44 a
inverseOrtho a
l a
r a
b a
t a
n a
f =
V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
x a
0 a
0 a
c)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
y a
0 a
d)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
z a
e)
(a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 a
1)
where x :: a
x = a
0.5a -> a -> a
forall a. Num a => a -> a -> a
*(a
ra -> a -> a
forall a. Num a => a -> a -> a
-a
l)
y :: a
y = a
0.5a -> a -> a
forall a. Num a => a -> a -> a
*(a
ta -> a -> a
forall a. Num a => a -> a -> a
-a
b)
z :: a
z = a
0.5a -> a -> a
forall a. Num a => a -> a -> a
*(a
na -> a -> a
forall a. Num a => a -> a -> a
-a
f)
c :: a
c = a
0.5a -> a -> a
forall a. Num a => a -> a -> a
*(a
la -> a -> a
forall a. Num a => a -> a -> a
+a
r)
d :: a
d = a
0.5a -> a -> a
forall a. Num a => a -> a -> a
*(a
ba -> a -> a
forall a. Num a => a -> a -> a
+a
t)
e :: a
e = -a
0.5a -> a -> a
forall a. Num a => a -> a -> a
*(a
na -> a -> a
forall a. Num a => a -> a -> a
+a
f)