summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-05-02 03:24:53 -0400
committerJoe Crayne <joe@jerkface.net>2019-05-02 03:24:53 -0400
commit5abe70c7457ebedc5e4e348e3dc5a7b830fab897 (patch)
tree6dcf8cd85e330be12631e319bd8ffaf70533195d
parent7853a4b2f025d6368564bc9ea6cbf72c36b6fcd9 (diff)
Rotate camera-up direction too.
-rw-r--r--MeshSketch.hs17
1 files changed, 15 insertions, 2 deletions
diff --git a/MeshSketch.hs b/MeshSketch.hs
index c88da0c..5bf6fea 100644
--- a/MeshSketch.hs
+++ b/MeshSketch.hs
@@ -170,7 +170,7 @@ whirlingCamera st = Animation $ \_ t -> do
170 let tf = realToFrac t :: Float 170 let tf = realToFrac t :: Float
171 rot = rotMatrixZ (-tf/2) <> rotMatrixX (-tf/pi) 171 rot = rotMatrixZ (-tf/2) <> rotMatrixX (-tf/pi)
172 modifyIORef (stCamera st) $ \cam -> cam 172 modifyIORef (stCamera st) $ \cam -> cam
173 { camUp = rot #> fromList [0,1,0] 173 { camUp = fromList [0,1,0] <# rot
174 , camDirection = (scale (1/camDistance cam) $ fromList [-2,-2,-10]) <# rot 174 , camDirection = (scale (1/camDistance cam) $ fromList [-2,-2,-10]) <# rot
175 , camWorldToScreen = Nothing 175 , camWorldToScreen = Nothing
176 , camScreenToWorld = Nothing 176 , camScreenToWorld = Nothing
@@ -335,9 +335,15 @@ updateCameraRotation w st h k = do
335 -- tu = dt `dot` û 335 -- tu = dt `dot` û
336 -- td = dt `dot` d̂ 336 -- td = dt `dot` d̂
337 cosθ = dot df dt / realToFrac (norm_2 df) / realToFrac (norm_2 dt) 337 cosθ = dot df dt / realToFrac (norm_2 df) / realToFrac (norm_2 dt)
338 axis = df `cross` dt 338 axis0 = df `cross` dt
339 small x = abs x < 0.00001
340 axis = let xs = toList axis0
341 in if any isNaN xs || all small xs
342 then fromList [0,1,0]
343 else axis0
339 cam' = cam 344 cam' = cam
340 { camDirection = d̂ <# rotate cosθ axis 345 { camDirection = d̂ <# rotate cosθ axis
346 , camUp = û <# rotate cosθ axis
341 , camWorldToScreen = Nothing 347 , camWorldToScreen = Nothing
342 , camScreenToWorld = Nothing 348 , camScreenToWorld = Nothing
343 } 349 }
@@ -349,13 +355,20 @@ updateCameraRotation w st h k = do
349sanitizeCamera st = do 355sanitizeCamera st = do
350 modifyIORef (stCamera st) $ \cam -> 356 modifyIORef (stCamera st) $ \cam ->
351 let d = camDirection cam 357 let d = camDirection cam
358 u = camUp cam
352 dd = norm_2 d 359 dd = norm_2 d
360 uu = norm_2 u
353 e = scale (realToFrac $ 1/dd) d 361 e = scale (realToFrac $ 1/dd) d
354 d̂ = if any isNaN (toList e) 362 d̂ = if any isNaN (toList e)
355 then fromList [0,0,-1] 363 then fromList [0,0,-1]
356 else e 364 else e
365 f = scale (realToFrac $ 1/uu) u
366 û = if any isNaN (toList f)
367 then fromList [0,1,0]
368 else f
357 in cam 369 in cam
358 { camDirection = d̂ 370 { camDirection = d̂
371 , camUp = û
359 , camWorldToScreen = Nothing 372 , camWorldToScreen = Nothing
360 , camScreenToWorld = Nothing 373 , camScreenToWorld = Nothing
361 } 374 }