module Matrix(
    modelMatrix
  , cameraMatrix
  , projMatrix
  , gridModelMatrix
  ) where

import Linear
import qualified LambdaCube.Linear as LC
import Game.GoreAndAsh.Math

import Camera

-- | Convert from linear matrix format to LambdaCube format
convLC :: M44 Float -> LC.M44F
convLC (V4 !a !b !c !d) =  LC.V4 (cv a) (cv b) (cv c) (cv d)
  where
    cv (V4 !x !y !z !w) = LC.V4 x y z w

-- | Model matrix, maps from local model coords to world coords
modelMatrix :: Float -> LC.M44F
modelMatrix _ = convLC identity -- . quatMatrix $ axisAngle (normalize $ V3 1 1 3) 0.1

-- | Grid is static
gridModelMatrix :: Camera -> Float -> LC.M44F
gridModelMatrix Camera{..} gridSize = convLC $ translate ((* gridSize) . fromIntegral . (round :: Float -> Int) . (/ gridSize) <$> cameraEye)

-- | Camera matrix, maps from world coords to camera coords
cameraMatrix :: Camera -> LC.M44F
cameraMatrix Camera{..} = convLC $ lookAt cameraEye (cameraEye + cameraForward) cameraUp

-- | Projection matrix, maps from camera coords to device normalized coords
projMatrix :: Float -> LC.M44F
projMatrix !aspect = convLC $ perspective (pi/3) aspect 0.1 100

-- | Transform quaternion to rotation matrix
quatMatrix :: Quaternion Float -> M44 Float
quatMatrix q@(Quaternion !w (V3 !x !y !z)) = V4
  (V4 m00 m01 m02 0)
  (V4 m10 m11 m12 0)
  (V4 m20 m21 m22 0)
  (V4 0 0 0 1)
  where
    s = 2 / norm q
    x2 = x * s
    y2 = y * s
    z2 = z * s
    xx = x * x2
    xy = x * y2
    xz = x * z2
    yy = y * y2
    yz = y * z2
    zz = z * z2
    wx = w * x2
    wy = w * y2
    wz = w * z2

    m00 = 1 - (yy + zz)
    m10 = xy - wz
    m20 = xz + wy

    m01 = xy + wz
    m11 = 1 - (xx + zz)
    m21 = yz - wx

    m02 = xz - wy
    m12 = yz + wx
    m22 = 1 - (xx + yy)
