summaryrefslogtreecommitdiff
path: root/MeshSketch.hs
diff options
context:
space:
mode:
Diffstat (limited to 'MeshSketch.hs')
-rw-r--r--MeshSketch.hs48
1 files changed, 1 insertions, 47 deletions
diff --git a/MeshSketch.hs b/MeshSketch.hs
index c63420e..1019d72 100644
--- a/MeshSketch.hs
+++ b/MeshSketch.hs
@@ -67,6 +67,7 @@ import qualified VectorRing as Vector
67import RingBuffer 67import RingBuffer
68import MaskableStream (AttributeKey,(@<-)) 68import MaskableStream (AttributeKey,(@<-))
69import SmallRing 69import SmallRing
70import Camera
70 71
71 72
72prettyDebug :: GL.DebugMessage -> String 73prettyDebug :: GL.DebugMessage -> String
@@ -118,21 +119,6 @@ data State = State
118 , stAngle :: IORef Int 119 , stAngle :: IORef Int
119 } 120 }
120 121
121data Camera = Camera
122 { camHeightAngle :: Float
123 , camTarget :: Vector Float -- 3-vector
124 , camDirection :: Vector Float -- 3-vector
125 , camDistance :: Float
126 , camWidth :: Float
127 , camHeight :: Float
128 , camUp :: Vector Float -- 3-vector
129 , camWorldToScreen :: Maybe (Matrix Float) -- 4×4
130 , camScreenToWorld :: Maybe (Matrix Float) -- 4×4
131 }
132
133camPos :: Camera -> Vector Float
134camPos c = camTarget c - scale (camDistance c) (camDirection c)
135
136initCamera :: Camera 122initCamera :: Camera
137initCamera = Camera 123initCamera = Camera
138 { camHeightAngle = pi/6 124 { camHeightAngle = pi/6
@@ -147,16 +133,6 @@ initCamera = Camera
147 } 133 }
148 where d = realToFrac $ norm_2 $ fromList [2::Float,2,10] 134 where d = realToFrac $ norm_2 $ fromList [2::Float,2,10]
149 135
150viewProjection :: Camera -> (Camera,(Matrix Float,Vector Float))
151viewProjection c
152 | Just m <- camWorldToScreen c = (c,(m,pos))
153 | otherwise = (c { camWorldToScreen = Just m' }, (m',pos))
154 where
155 m' = proj <> cam
156 cam = lookat pos (camTarget c) (camUp c)
157 pos = camPos c
158 proj = perspective 0.1 100 (camHeightAngle c) (camWidth c / camHeight c)
159
160realToFracVector :: ( Real a 136realToFracVector :: ( Real a
161 , Fractional b 137 , Fractional b
162 , Storable a 138 , Storable a
@@ -164,18 +140,6 @@ realToFracVector :: ( Real a
164 ) => Vector a -> Vector b 140 ) => Vector a -> Vector b
165realToFracVector v = Math.fromList $ map realToFrac $ Math.toList v 141realToFracVector v = Math.fromList $ map realToFrac $ Math.toList v
166 142
167realToFracMatrix :: (Real a, Fractional t, Element t, Element a) => Matrix a -> Matrix t
168realToFracMatrix m = fromLists $ map realToFrac <$> toLists m
169
170invFloat :: Matrix Float -> Matrix Float
171invFloat m = realToFracMatrix $ inv (realToFracMatrix m :: Matrix Double)
172
173projectionView :: Camera -> (Camera,Matrix Float)
174projectionView c
175 | Just m <- camScreenToWorld c = (c,m)
176 | Just w <- camWorldToScreen c = projectionView c{ camScreenToWorld = Just $ invFloat w }
177 | otherwise = projectionView $ fst $ viewProjection c
178
179addOBJToObjectArray :: GLStorage -> String -> [(GPUMesh, Maybe Text)] -> Map Text (ObjMaterial,TextureData) -> IO [LC.Object] 143addOBJToObjectArray :: GLStorage -> String -> [(GPUMesh, Maybe Text)] -> Map Text (ObjMaterial,TextureData) -> IO [LC.Object]
180addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \(mesh,mat) -> do 144addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \(mesh,mat) -> do
181 obj <- LC.addMeshToObjectArray storage slotName ["diffuseTexture","diffuseColor"] mesh 145 obj <- LC.addMeshToObjectArray storage slotName ["diffuseTexture","diffuseColor"] mesh
@@ -468,16 +432,6 @@ onResize glarea realized w h = do
468 } 432 }
469 LC.setScreenSize (stStorage realized) wd ht 433 LC.setScreenSize (stStorage realized) wd ht
470 434
471unit :: (Linear t c, Fractional t, Normed (c t)) => c t -> c t
472unit v = scale (1/realToFrac (norm_2 v)) v
473
474-- | Compute the height of a pixel at the given 3d point.
475pixelDelta :: Camera -> Vector Float -> Float
476pixelDelta cam x = realToFrac $ frustumHeight eyeToPoint / realToFrac (camHeight cam)
477 where
478 eyeToPoint = norm_2 (x - camPos cam)
479 frustumHeight d = 2 * d * tan (realToFrac $ camHeightAngle cam / 2)
480
481-- This computes a point in world coordinates on the view screen if 435-- This computes a point in world coordinates on the view screen if
482-- we assume the camera is located at the origin. 436-- we assume the camera is located at the origin.
483computeDirection :: Camera -> Double -> Double -> Vector Float 437computeDirection :: Camera -> Double -> Double -> Vector Float