{-# 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 :: forall a. (Epsilon a, Floating a) => V3 a -> V3 a -> V3 a -> M44 a
lookAt V3 a
eye V3 a
center V3 a
up =
forall a. a -> a -> a -> a -> V4 a
V4 (forall a. a -> a -> a -> a -> V4 a
V4 (V3 a
xaforall s a. s -> Getting a s a -> a
^.forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x) (V3 a
xaforall s a. s -> Getting a s a -> a
^.forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y) (V3 a
xaforall s a. s -> Getting a s a -> a
^.forall (t :: * -> *) a. R3 t => Lens' (t a) a
_z) a
xd)
(forall a. a -> a -> a -> a -> V4 a
V4 (V3 a
yaforall s a. s -> Getting a s a -> a
^.forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x) (V3 a
yaforall s a. s -> Getting a s a -> a
^.forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y) (V3 a
yaforall s a. s -> Getting a s a -> a
^.forall (t :: * -> *) a. R3 t => Lens' (t a) a
_z) a
yd)
(forall a. a -> a -> a -> a -> V4 a
V4 (-V3 a
zaforall s a. s -> Getting a s a -> a
^.forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x) (-V3 a
zaforall s a. s -> Getting a s a -> a
^.forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y) (-V3 a
zaforall s a. s -> Getting a s a -> a
^.forall (t :: * -> *) a. R3 t => Lens' (t a) a
_z) a
zd)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 a
1)
where za :: V3 a
za = forall a (f :: * -> *).
(Floating a, Metric f, Epsilon a) =>
f a -> f a
normalize forall a b. (a -> b) -> a -> b
$ V3 a
center forall a. Num a => a -> a -> a
- V3 a
eye
xa :: V3 a
xa = forall a (f :: * -> *).
(Floating a, Metric f, Epsilon a) =>
f a -> f a
normalize forall a b. (a -> b) -> a -> b
$ forall a. Num a => V3 a -> V3 a -> V3 a
cross V3 a
za V3 a
up
ya :: V3 a
ya = forall a. Num a => V3 a -> V3 a -> V3 a
cross V3 a
xa V3 a
za
xd :: a
xd = -forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
dot V3 a
xa V3 a
eye
yd :: a
yd = -forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
dot V3 a
ya V3 a
eye
zd :: a
zd = 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 :: forall a. Floating a => a -> a -> a -> a -> M44 a
perspective a
fovy a
aspect a
near a
far =
forall a. a -> a -> a -> a -> V4 a
V4 (forall a. a -> a -> a -> a -> V4 a
V4 a
x a
0 a
0 a
0)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
y a
0 a
0)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
z a
w)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (-a
1) a
0)
where tanHalfFovy :: a
tanHalfFovy = forall a. Floating a => a -> a
tan forall a b. (a -> b) -> a -> b
$ a
fovy forall a. Fractional a => a -> a -> a
/ a
2
x :: a
x = a
1 forall a. Fractional a => a -> a -> a
/ (a
aspect forall a. Num a => a -> a -> a
* a
tanHalfFovy)
y :: a
y = a
1 forall a. Fractional a => a -> a -> a
/ a
tanHalfFovy
fpn :: a
fpn = a
far forall a. Num a => a -> a -> a
+ a
near
fmn :: a
fmn = a
far forall a. Num a => a -> a -> a
- a
near
oon :: a
oon = a
0.5forall a. Fractional a => a -> a -> a
/a
near
oof :: a
oof = a
0.5forall a. Fractional a => a -> a -> a
/a
far
z :: a
z = -a
fpnforall a. Fractional a => a -> a -> a
/a
fmn
w :: a
w = a
1forall a. Fractional a => a -> a -> a
/(a
oofforall a. Num a => a -> a -> a
-a
oon)
#ifdef HERBIE
{-# ANN perspective "NoHerbie" #-}
#endif
inversePerspective
:: Floating a
=> a
-> a
-> a
-> a
-> M44 a
inversePerspective :: forall a. Floating a => a -> a -> a -> a -> M44 a
inversePerspective a
fovy a
aspect a
near a
far =
forall a. a -> a -> a -> a -> V4 a
V4 (forall a. a -> a -> a -> a -> V4 a
V4 a
a a
0 a
0 a
0 )
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
b a
0 a
0 )
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 (-a
1))
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
c a
d )
where tanHalfFovy :: a
tanHalfFovy = forall a. Floating a => a -> a
tan forall a b. (a -> b) -> a -> b
$ a
fovy forall a. Fractional a => a -> a -> a
/ a
2
a :: a
a = a
aspect forall a. Num a => a -> a -> a
* a
tanHalfFovy
b :: a
b = a
tanHalfFovy
c :: a
c = a
oon forall a. Num a => a -> a -> a
- a
oof
d :: a
d = a
oon forall a. Num a => a -> a -> a
+ a
oof
oon :: a
oon = a
0.5forall a. Fractional a => a -> a -> a
/a
near
oof :: a
oof = a
0.5forall a. Fractional a => a -> a -> a
/a
far
frustum
:: Floating a
=> a
-> a
-> a
-> a
-> a
-> a
-> M44 a
frustum :: forall a. Floating a => a -> a -> a -> a -> a -> a -> M44 a
frustum a
l a
r a
b a
t a
n a
f =
forall a. a -> a -> a -> a -> V4 a
V4 (forall a. a -> a -> a -> a -> V4 a
V4 a
x a
0 a
a a
0)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
y a
e a
0)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
c a
d)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (-a
1) a
0)
where
rml :: a
rml = a
rforall a. Num a => a -> a -> a
-a
l
tmb :: a
tmb = a
tforall a. Num a => a -> a -> a
-a
b
fmn :: a
fmn = a
fforall a. Num a => a -> a -> a
-a
n
x :: a
x = a
2forall a. Num a => a -> a -> a
*a
nforall a. Fractional a => a -> a -> a
/a
rml
y :: a
y = a
2forall a. Num a => a -> a -> a
*a
nforall a. Fractional a => a -> a -> a
/a
tmb
a :: a
a = (a
rforall a. Num a => a -> a -> a
+a
l)forall a. Fractional a => a -> a -> a
/a
rml
e :: a
e = (a
tforall a. Num a => a -> a -> a
+a
b)forall a. Fractional a => a -> a -> a
/a
tmb
c :: a
c = forall a. Num a => a -> a
negate (a
fforall a. Num a => a -> a -> a
+a
n)forall a. Fractional a => a -> a -> a
/a
fmn
d :: a
d = (-a
2forall a. Num a => a -> a -> a
*a
fforall a. Num a => a -> a -> a
*a
n)forall a. Fractional a => a -> a -> a
/a
fmn
inverseFrustum
:: Floating a
=> a
-> a
-> a
-> a
-> a
-> a
-> M44 a
inverseFrustum :: forall a. Floating a => a -> a -> a -> a -> a -> a -> M44 a
inverseFrustum a
l a
r a
b a
t a
n a
f =
forall a. a -> a -> a -> a -> V4 a
V4 (forall a. a -> a -> a -> a -> V4 a
V4 a
rx a
0 a
0 a
ax)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
ry a
0 a
by)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 (-a
1))
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
rd a
cd)
where
hrn :: a
hrn = a
0.5forall a. Fractional a => a -> a -> a
/a
n
hrnf :: a
hrnf = a
0.5forall a. Fractional a => a -> a -> a
/(a
nforall a. Num a => a -> a -> a
*a
f)
rx :: a
rx = (a
rforall a. Num a => a -> a -> a
-a
l)forall a. Num a => a -> a -> a
*a
hrn
ry :: a
ry = (a
tforall a. Num a => a -> a -> a
-a
b)forall a. Num a => a -> a -> a
*a
hrn
ax :: a
ax = (a
rforall a. Num a => a -> a -> a
+a
l)forall a. Num a => a -> a -> a
*a
hrn
by :: a
by = (a
tforall a. Num a => a -> a -> a
+a
b)forall a. Num a => a -> a -> a
*a
hrn
cd :: a
cd = (a
fforall a. Num a => a -> a -> a
+a
n)forall a. Num a => a -> a -> a
*a
hrnf
rd :: a
rd = (a
nforall a. Num a => a -> a -> a
-a
f)forall a. Num a => a -> a -> a
*a
hrnf
infinitePerspective
:: Floating a
=> a
-> a
-> a
-> M44 a
infinitePerspective :: forall a. Floating a => a -> a -> a -> M44 a
infinitePerspective a
fovy a
a a
n =
forall a. a -> a -> a -> a -> V4 a
V4 (forall a. a -> a -> a -> a -> V4 a
V4 a
x a
0 a
0 a
0)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
y a
0 a
0)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (-a
1) a
w)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (-a
1) a
0)
where
t :: a
t = a
nforall a. Num a => a -> a -> a
*forall a. Floating a => a -> a
tan(a
fovyforall a. Fractional a => a -> a -> a
/a
2)
b :: a
b = -a
t
l :: a
l = a
bforall a. Num a => a -> a -> a
*a
a
r :: a
r = a
tforall a. Num a => a -> a -> a
*a
a
x :: a
x = (a
2forall a. Num a => a -> a -> a
*a
n)forall a. Fractional a => a -> a -> a
/(a
rforall a. Num a => a -> a -> a
-a
l)
y :: a
y = (a
2forall a. Num a => a -> a -> a
*a
n)forall a. Fractional a => a -> a -> a
/(a
tforall a. Num a => a -> a -> a
-a
b)
w :: a
w = -a
2forall a. Num a => a -> a -> a
*a
n
inverseInfinitePerspective
:: Floating a
=> a
-> a
-> a
-> M44 a
inverseInfinitePerspective :: forall a. Floating a => a -> a -> a -> M44 a
inverseInfinitePerspective a
fovy a
a a
n =
forall a. a -> a -> a -> a -> V4 a
V4 (forall a. a -> a -> a -> a -> V4 a
V4 a
rx a
0 a
0 a
0)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
ry a
0 a
0)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 (-a
1))
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
rw (-a
rw))
where
t :: a
t = a
nforall a. Num a => a -> a -> a
*forall a. Floating a => a -> a
tan(a
fovyforall a. Fractional a => a -> a -> a
/a
2)
b :: a
b = -a
t
l :: a
l = a
bforall a. Num a => a -> a -> a
*a
a
r :: a
r = a
tforall a. Num a => a -> a -> a
*a
a
hrn :: a
hrn = a
0.5forall a. Fractional a => a -> a -> a
/a
n
rx :: a
rx = (a
rforall a. Num a => a -> a -> a
-a
l)forall a. Num a => a -> a -> a
*a
hrn
ry :: a
ry = (a
tforall a. Num a => a -> a -> a
-a
b)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 :: forall a. Fractional a => a -> a -> a -> a -> a -> a -> M44 a
ortho a
l a
r a
b a
t a
n a
f =
forall a. a -> a -> a -> a -> V4 a
V4 (forall a. a -> a -> a -> a -> V4 a
V4 (-a
2forall a. Num a => a -> a -> a
*a
x) a
0 a
0 ((a
rforall a. Num a => a -> a -> a
+a
l)forall a. Num a => a -> a -> a
*a
x))
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 (-a
2forall a. Num a => a -> a -> a
*a
y) a
0 ((a
tforall a. Num a => a -> a -> a
+a
b)forall a. Num a => a -> a -> a
*a
y))
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 (a
2forall a. Num a => a -> a -> a
*a
z) ((a
fforall a. Num a => a -> a -> a
+a
n)forall a. Num a => a -> a -> a
*a
z))
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 a
1)
where x :: a
x = forall a. Fractional a => a -> a
recip(a
lforall a. Num a => a -> a -> a
-a
r)
y :: a
y = forall a. Fractional a => a -> a
recip(a
bforall a. Num a => a -> a -> a
-a
t)
z :: a
z = forall a. Fractional a => a -> a
recip(a
nforall a. Num a => a -> a -> a
-a
f)
inverseOrtho
:: Fractional a
=> a
-> a
-> a
-> a
-> a
-> a
-> M44 a
inverseOrtho :: forall a. Fractional a => a -> a -> a -> a -> a -> a -> M44 a
inverseOrtho a
l a
r a
b a
t a
n a
f =
forall a. a -> a -> a -> a -> V4 a
V4 (forall a. a -> a -> a -> a -> V4 a
V4 a
x a
0 a
0 a
c)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
y a
0 a
d)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
z a
e)
(forall a. a -> a -> a -> a -> V4 a
V4 a
0 a
0 a
0 a
1)
where x :: a
x = a
0.5forall a. Num a => a -> a -> a
*(a
rforall a. Num a => a -> a -> a
-a
l)
y :: a
y = a
0.5forall a. Num a => a -> a -> a
*(a
tforall a. Num a => a -> a -> a
-a
b)
z :: a
z = a
0.5forall a. Num a => a -> a -> a
*(a
nforall a. Num a => a -> a -> a
-a
f)
c :: a
c = a
0.5forall a. Num a => a -> a -> a
*(a
lforall a. Num a => a -> a -> a
+a
r)
d :: a
d = a
0.5forall a. Num a => a -> a -> a
*(a
bforall a. Num a => a -> a -> a
+a
t)
e :: a
e = -a
0.5forall a. Num a => a -> a -> a
*(a
nforall a. Num a => a -> a -> a
+a
f)