{-# LANGUAGE CPP #-}
---------------------------------------------------------------------------

-- |

-- Copyright   :  (C) 2015 Edward Kmett

-- License     :  BSD-style (see the file LICENSE)

--

-- Maintainer  :  Edward Kmett <ekmett@gmail.com>

-- Stability   :  experimental

-- Portability :  non-portable

--

-- Common projection matrices: e.g. perspective/orthographic transformation

-- matrices.

--

-- Analytically derived inverses are also supplied, because they can be

-- much more accurate in practice than computing them through general

-- purpose means

---------------------------------------------------------------------------

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

-- $setup

-- >>> import Linear.Matrix

-- >>> import Linear.V2

-- >>> import Linear.V4


-- | Build a look at view matrix

lookAt
  :: (Epsilon a, Floating a)
  => V3 a -- ^ Eye

  -> V3 a -- ^ Center

  -> V3 a -- ^ Up

  -> 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

-- | Build a matrix for a symmetric perspective-view frustum

perspective
  :: Floating a
  => a -- ^ FOV (y direction, in radians)

  -> a -- ^ Aspect ratio

  -> a -- ^ Near plane

  -> a -- ^ Far plane

  -> 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 = 1 / (near/fpn - far/fpn) -- would be better by .5 bits

        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) -- 13 bits error reduced to 0.17

        -- w = -(2 * far * near) / fmn


#ifdef HERBIE
{-# ANN perspective "NoHerbie" #-}
#endif

-- | Build an inverse perspective matrix

inversePerspective
  :: Floating a
  => a -- ^ FOV (y direction, in radians)

  -> a -- ^ Aspect ratio

  -> a -- ^ Near plane

  -> a -- ^ Far plane

  -> 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


-- | Build a perspective matrix per the classic @glFrustum@ arguments.

frustum
  :: Floating a
  => a -- ^ Left

  -> a -- ^ Right

  -> a -- ^ Bottom

  -> a -- ^ Top

  -> a -- ^ Near

  -> a -- ^ Far

  -> 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 -- ^ Left

  -> a -- ^ Right

  -> a -- ^ Bottom

  -> a -- ^ Top

  -> a -- ^ Near

  -> a -- ^ Far

  -> 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

-- | Build a matrix for a symmetric perspective-view frustum with a far plane at infinite

infinitePerspective
  :: Floating a
  => a -- ^ FOV (y direction, in radians)

  -> a -- ^ Aspect Ratio

  -> a -- ^ Near plane

  -> 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 -- ^ FOV (y direction, in radians)

  -> a -- ^ Aspect Ratio

  -> a -- ^ Near plane

  -> 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

-- | Build an orthographic perspective matrix from 6 clipping planes.

-- This matrix takes the region delimited by these planes and maps it

-- to normalized device coordinates between [-1,1]

--

-- This call is designed to mimic the parameters to the OpenGL @glOrtho@

-- call, so it has a slightly strange convention: Notably: the near and

-- far planes are negated.

--

-- Consequently:

--

-- @

-- 'ortho' l r b t n f !* 'V4' l b (-n) 1 = 'V4' (-1) (-1) (-1) 1

-- 'ortho' l r b t n f !* 'V4' r t (-f) 1 = 'V4' 1 1 1 1

-- @

--

-- Examples:

--

-- >>> ortho 1 2 3 4 5 6 !* V4 1 3 (-5) 1

-- V4 (-1.0) (-1.0) (-1.0) 1.0

--

-- >>> ortho 1 2 3 4 5 6 !* V4 2 4 (-6) 1

-- V4 1.0 1.0 1.0 1.0

ortho
  :: Fractional a
  => a -- ^ Left

  -> a -- ^ Right

  -> a -- ^ Bottom

  -> a -- ^ Top

  -> a -- ^ Near

  -> a -- ^ Far

  -> 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)

-- | Build an inverse orthographic perspective matrix from 6 clipping planes

inverseOrtho
  :: Fractional a
  => a -- ^ Left

  -> a -- ^ Right

  -> a -- ^ Bottom

  -> a -- ^ Top

  -> a -- ^ Near

  -> a -- ^ Far

  -> 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)