| 1 |
{-# LANGUAGE DeriveDataTypeable #-} |
| 2 |
{-# LANGUAGE FlexibleContexts #-} |
| 3 |
{-# LANGUAGE MultiParamTypeClasses #-} |
| 4 |
{-# LANGUAGE ScopedTypeVariables #-} |
| 5 |
{-# LANGUAGE TemplateHaskell #-} |
| 6 |
{-# LANGUAGE TypeFamilies #-} |
| 7 |
{-# LANGUAGE UndecidableInstances #-} |
| 8 |
|
| 9 |
----------------------------------------------------------------------------- |
| 10 |
-- | |
| 11 |
-- Module : Diagrams.ThreeD.Camera |
| 12 |
-- Copyright : (c) 2013 diagrams-lib team (see LICENSE) |
| 13 |
-- License : BSD-style (see LICENSE) |
| 14 |
-- Maintainer : [email protected] |
| 15 |
-- |
| 16 |
-- Types to specify viewpoint for 3D rendering. |
| 17 |
-- |
| 18 |
----------------------------------------------------------------------------- |
| 19 |
|
| 20 |
module Diagrams.ThreeD.Camera |
| 21 |
( Camera -- do not export constructor |
| 22 |
-- These are safe to construct manually |
| 23 |
, PerspectiveLens(..), OrthoLens(..) |
| 24 |
, horizontalFieldOfView, verticalFieldOfView |
| 25 |
, orthoWidth, orthoHeight |
| 26 |
, camLoc, camForward, camUp, camRight, camLens |
| 27 |
, facing_ZCamera, mm50Camera |
| 28 |
, mm50, mm50Wide, mm50Narrow |
| 29 |
, aspect, camAspect |
| 30 |
) |
| 31 |
where |
| 32 |
|
| 33 |
import Control.Lens (makeLenses) |
| 34 |
import Data.Monoid |
| 35 |
import Data.Typeable |
| 36 |
|
| 37 |
import Diagrams.Angle |
| 38 |
import Diagrams.Core |
| 39 |
import Diagrams.Direction |
| 40 |
import Diagrams.ThreeD.Vector |
| 41 |
|
| 42 |
import Linear.V3 |
| 43 |
|
| 44 |
-- Parameterize Camera on the lens type, so that Backends can express which |
| 45 |
-- lenses they handle. |
| 46 |
data Camera l n = Camera |
| 47 |
{ camLoc :: Point V3 n |
| 48 |
, forward :: V3 n |
| 49 |
, up :: V3 n |
| 50 |
, lens :: l n |
| 51 |
} |
| 52 |
deriving Typeable |
| 53 |
|
| 54 |
type instance V (Camera l n) = V3 |
| 55 |
type instance N (Camera l n) = n |
| 56 |
|
| 57 |
class Typeable l => CameraLens l where |
| 58 |
|
| 59 |
aspect :: Floating n => l n -> n |
| 60 |
|
| 61 |
|
| 62 |
data PerspectiveLens n = PerspectiveLens |
| 63 |
{ _horizontalFieldOfView :: Angle n |
| 64 |
, _verticalFieldOfView :: Angle n |
| 65 |
} |
| 66 |
deriving Typeable |
| 67 |
|
| 68 |
makeLenses ''PerspectiveLens |
| 69 |
|
| 70 |
type instance V (PerspectiveLens n) = V3 |
| 71 |
type instance N (PerspectiveLens n) = n |
| 72 |
|
| 73 |
instance CameraLens PerspectiveLens where |
| 74 |
aspect (PerspectiveLens h v) = angleRatio h v |
| 75 |
|
| 76 |
|
| 77 |
data OrthoLens n = OrthoLens |
| 78 |
{ _orthoWidth :: n |
| 79 |
, _orthoHeight :: n |
| 80 |
} |
| 81 |
deriving Typeable |
| 82 |
|
| 83 |
makeLenses ''OrthoLens |
| 84 |
|
| 85 |
type instance V (OrthoLens n) = V3 |
| 86 |
type instance N (OrthoLens n) = n |
| 87 |
|
| 88 |
instance CameraLens OrthoLens where |
| 89 |
aspect (OrthoLens h v) = h / v |
| 90 |
|
| 91 |
instance Num n => Transformable (Camera l n) where |
| 92 |
transform t (Camera p f u l) = |
| 93 |
Camera (transform t p) |
| 94 |
(transform t f) |
| 95 |
(transform t u) |
| 96 |
l |
| 97 |
|
| 98 |
instance Num n => Renderable (Camera l n) NullBackend where |
| 99 |
render _ _ = mempty |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
mm50Camera :: (Typeable n, Floating n, Ord n, Renderable (Camera PerspectiveLens n) b) |
| 106 |
=> QDiagram b V3 n Any |
| 107 |
mm50Camera = facing_ZCamera mm50 |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
facing_ZCamera :: (Floating n, Ord n, Typeable n, CameraLens l, Renderable (Camera l n) b) => |
| 113 |
l n -> QDiagram b V3 n Any |
| 114 |
facing_ZCamera l = mkQD (Prim $ Camera origin unit_Z unitY l) |
| 115 |
mempty mempty mempty (Query . const . Any $ False) |
| 116 |
{-# ANN facing_ZCamera ("HLint: ignore Use camelCase" :: String) #-} |
| 117 |
|
| 118 |
mm50, mm50Wide, mm50Narrow :: Floating n => PerspectiveLens n |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
mm50 = PerspectiveLens (40.5 @@ deg) (27 @@ deg) |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
mm50Wide = PerspectiveLens (43.2 @@ deg) (27 @@ deg) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
mm50Narrow = PerspectiveLens (36 @@ deg) (27 @@ deg) |
| 131 |
|
| 132 |
camForward :: Camera l n -> Direction V3 n |
| 133 |
camForward = direction . forward |
| 134 |
|
| 135 |
camUp :: Camera l n -> Direction V3 n |
| 136 |
camUp = direction . up |
| 137 |
|
| 138 |
camRight :: Fractional n => Camera l n -> Direction V3 n |
| 139 |
camRight c = direction right where |
| 140 |
right = cross (forward c) (up c) |
| 141 |
|
| 142 |
camLens :: Camera l n -> l n |
| 143 |
camLens = lens |
| 144 |
|
| 145 |
camAspect :: (Floating n, CameraLens l) => Camera l n -> n |
| 146 |
camAspect = aspect . camLens |