module Diagrams.ThreeD.Camera
( Camera
, PerspectiveLens(..), OrthoLens(..)
, horizontalFieldOfView, verticalFieldOfView
, orthoWidth, orthoHeight
, camLoc, camForward, camUp, camRight, camLens
, facing_ZCamera, mm50Camera
, mm50, mm50Wide, mm50Narrow
, aspect, camAspect
)
where
import Control.Lens (makeLenses)
import Data.Monoid
import Data.Typeable
import Diagrams.Angle
import Diagrams.Core
import Diagrams.Direction
import Diagrams.ThreeD.Vector
import Linear.V3
data Camera l n = Camera
{ camLoc :: Point V3 n
, forward :: V3 n
, up :: V3 n
, lens :: l n
}
#if __GLASGOW_HASKELL__ >= 707
deriving Typeable
#else
instance forall l. Typeable1 l => Typeable1 (Camera l) where
typeOf1 _ = mkTyConApp (mkTyCon3 "diagrams-lib" "Diagrams.ThreeD.Camera" "Camera") [] `mkAppTy`
typeOf1 (undefined :: l n)
#endif
type instance V (Camera l n) = V3
type instance N (Camera l n) = n
#if __GLASGOW_HASKELL__ > 707
class Typeable l => CameraLens l where
#else
class Typeable1 l => CameraLens l where
#endif
aspect :: Floating n => l n -> n
data PerspectiveLens n = PerspectiveLens
{ _horizontalFieldOfView :: Angle n
, _verticalFieldOfView :: Angle n
}
deriving Typeable
makeLenses ''PerspectiveLens
type instance V (PerspectiveLens n) = V3
type instance N (PerspectiveLens n) = n
instance CameraLens PerspectiveLens where
aspect (PerspectiveLens h v) = angleRatio h v
data OrthoLens n = OrthoLens
{ _orthoWidth :: n
, _orthoHeight :: n
}
deriving Typeable
makeLenses ''OrthoLens
type instance V (OrthoLens n) = V3
type instance N (OrthoLens n) = n
instance CameraLens OrthoLens where
aspect (OrthoLens h v) = h / v
instance Num n => Transformable (Camera l n) where
transform t (Camera p f u l) =
Camera (transform t p)
(transform t f)
(transform t u)
l
instance Num n => Renderable (Camera l n) NullBackend where
render _ _ = mempty
mm50Camera :: (Typeable n, Floating n, Ord n, Renderable (Camera PerspectiveLens n) b)
=> QDiagram b V3 n Any
mm50Camera = facing_ZCamera mm50
facing_ZCamera :: (Floating n, Ord n, Typeable n, CameraLens l, Renderable (Camera l n) b) =>
l n -> QDiagram b V3 n Any
facing_ZCamera l = mkQD (Prim $ Camera origin unit_Z unitY l)
mempty mempty mempty (Query . const . Any $ False)
mm50, mm50Wide, mm50Narrow :: Floating n => PerspectiveLens n
mm50 = PerspectiveLens (40.5 @@ deg) (27 @@ deg)
mm50Wide = PerspectiveLens (43.2 @@ deg) (27 @@ deg)
mm50Narrow = PerspectiveLens (36 @@ deg) (27 @@ deg)
camForward :: Camera l n -> Direction V3 n
camForward = direction . forward
camUp :: Camera l n -> Direction V3 n
camUp = direction . up
camRight :: Fractional n => Camera l n -> Direction V3 n
camRight c = direction right where
right = cross (forward c) (up c)
camLens :: Camera l n -> l n
camLens = lens
camAspect :: (Floating n, CameraLens l) => Camera l n -> n
camAspect = aspect . camLens