diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2016-02-26 16:20:42 +0100 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2016-02-26 16:20:42 +0100 |
commit | be123a709e6707a489c53650f71593aee3032c50 (patch) | |
tree | 8de97454d61b6c112056d4e19bcc00a6359467f5 /lc | |
parent | d253c70b504ec34712be9eece7ac5157dced5d52 (diff) |
add perspective to prelude
Diffstat (limited to 'lc')
-rw-r--r-- | lc/Prelude.lc | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/lc/Prelude.lc b/lc/Prelude.lc index 28c7fab8..1a1ae735 100644 --- a/lc/Prelude.lc +++ b/lc/Prelude.lc | |||
@@ -321,24 +321,24 @@ a %! b = PrimModS a b | |||
321 | ------------------ | 321 | ------------------ |
322 | -- common matrices | 322 | -- common matrices |
323 | ------------------ | 323 | ------------------ |
324 | {- | 324 | |
325 | -- | Perspective transformation matrix in row major order. | 325 | -- | Perspective transformation matrix in row major order. |
326 | perspective :: Float -- ^ Near plane clipping distance (always positive). | 326 | perspective :: Float -- ^ Near plane clipping distance (always positive). |
327 | -> Float -- ^ Far plane clipping distance (always positive). | 327 | -> Float -- ^ Far plane clipping distance (always positive). |
328 | -> Float -- ^ Field of view of the y axis, in radians. | 328 | -> Float -- ^ Field of view of the y axis, in radians. |
329 | -> Float -- ^ Aspect ratio, i.e. screen's width\/height. | 329 | -> Float -- ^ Aspect ratio, i.e. screen's width\/height. |
330 | -> Mat 4 4 Float | 330 | -> Mat 4 4 Float |
331 | perspective n f fovy aspect = --transpose $ | 331 | perspective n f fovy aspect = |
332 | M44F (V4F (2*n/(r-l)) 0 (-(r+l)/(r-l)) 0) | 332 | M44F (V4 (2*n/(r-l)) 0 (-(r+l)/(r-l)) 0) |
333 | (V4F 0 (2*n/(t-b)) ((t+b)/(t-b)) 0) | 333 | (V4 0 (2*n/(t-b)) ((t+b)/(t-b)) 0) |
334 | (V4F 0 0 (-(f+n)/(f-n)) (-2*f*n/(f-n))) | 334 | (V4 0 0 (-(f+n)/(f-n)) (-2*f*n/(f-n))) |
335 | (V4F 0 0 (-1) 0) | 335 | (V4 0 0 (-1) 0) |
336 | where | 336 | where |
337 | t = n*tan(fovy/2) | 337 | t = n*tan(fovy/2) |
338 | b = -t | 338 | b = -t |
339 | r = aspect*t | 339 | r = aspect*t |
340 | l = -r | 340 | l = -r |
341 | -} | 341 | |
342 | rotMatrixZ a = M44F (V4 c s 0 0) (V4 (-s) c 0 0) (V4 0 0 1 0) (V4 0 0 0 1) | 342 | rotMatrixZ a = M44F (V4 c s 0 0) (V4 (-s) c 0 0) (V4 0 0 1 0) (V4 0 0 0 1) |
343 | where | 343 | where |
344 | c = cos a | 344 | c = cos a |
@@ -356,19 +356,26 @@ rotMatrixX a = M44F (V4 1 0 0 0) (V4 0 c s 0) (V4 0 (-s) c 0) (V4 0 0 0 1) | |||
356 | 356 | ||
357 | rotationEuler a b c = rotMatrixY a .*. rotMatrixX b .*. rotMatrixZ c | 357 | rotationEuler a b c = rotMatrixY a .*. rotMatrixX b .*. rotMatrixZ c |
358 | 358 | ||
359 | {- | 359 | translateBefore4 :: Vec 3 Float -> Mat 4 4 Float -> Mat 4 4 Float |
360 | translateBefore4 v p4 = M44F r1 r2 r3 (ext0 u + r4) | ||
361 | where | ||
362 | ext0 a = V4 a%x a%y a%z 0 | ||
363 | (M44F r1 r2 r3 r4) = p4 | ||
364 | u = v .* M33F r1%xyz r2%xyz r3%xyz | ||
365 | |||
360 | -- | Camera transformation matrix. | 366 | -- | Camera transformation matrix. |
361 | lookat :: Vec 3 Float -- ^ Camera position. | 367 | lookat :: Vec 3 Float -- ^ Camera position. |
362 | -> Vec 3 Float -- ^ Target position. | 368 | -> Vec 3 Float -- ^ Target position. |
363 | -> Vec 3 Float -- ^ Upward direction. | 369 | -> Vec 3 Float -- ^ Upward direction. |
364 | -> M44F | 370 | -> Mat 4 4 Float |
365 | lookat pos target up = translateBefore4 (neg pos) (orthogonal $ toOrthoUnsafe r) | 371 | lookat pos target up = translateBefore4 (neg pos) r |
366 | where | 372 | where |
373 | ext0 a = V4 a%x a%y a%z 0 | ||
367 | w = normalize $ pos - target | 374 | w = normalize $ pos - target |
368 | u = normalize $ up `cross` w | 375 | u = normalize $ up `cross` w |
369 | v = w `cross` u | 376 | v = w `cross` u |
370 | r = transpose $ Mat3 u v w | 377 | r = transpose $ M44F (ext0 u) (ext0 v) (ext0 w) (V4 0 0 0 1) |
371 | -} | 378 | |
372 | 379 | ||
373 | scale t v = v * V4 t t t 1.0 | 380 | scale t v = v * V4 t t t 1.0 |
374 | 381 | ||