aboutsummaryrefslogtreecommitdiff
path: root/Spear/Scene/Light.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Spear/Scene/Light.hs')
-rw-r--r--Spear/Scene/Light.hs82
1 files changed, 82 insertions, 0 deletions
diff --git a/Spear/Scene/Light.hs b/Spear/Scene/Light.hs
new file mode 100644
index 0000000..76ff074
--- /dev/null
+++ b/Spear/Scene/Light.hs
@@ -0,0 +1,82 @@
1module Spear.Scene.Light
2(
3 Light(..)
4)
5where
6
7
8import qualified Spear.Math.Matrix4 as M
9import qualified Spear.Math.Spatial as S
10import Spear.Math.Vector3
11import qualified Spear.Math.Vector4 as V4
12
13
14data Light
15 = PointLight
16 { ambient :: Vector3
17 , diffuse :: Vector3
18 , specular :: Vector3
19 , transform :: M.Matrix4
20 }
21 | DirectionalLight
22 { ambient :: Vector3
23 , diffuse :: Vector3
24 , specular :: Vector3
25 , direction :: Vector3
26 }
27 | SpotLight
28 { ambient :: Vector3
29 , diffuse :: Vector3
30 , specular :: Vector3
31 , transform :: M.Matrix4
32 }
33
34
35instance S.Spatial Light where
36 move _ l@DirectionalLight {} = l
37 move v l = l { transform = M.translv v * transform l}
38
39 moveFwd _ l@DirectionalLight {} = l
40 moveFwd f l = l { transform = M.translv (scale f $ S.fwd l) * transform l }
41
42 moveBack _ l@DirectionalLight {} = l
43 moveBack f l = l { transform = M.translv (scale (-f) $ S.fwd l) * transform l }
44
45 strafeLeft _ l@DirectionalLight {} = l
46 strafeLeft f l = l { transform = M.translv (scale (-f) $ S.right l) * transform l }
47
48 strafeRight _ l@DirectionalLight {} = l
49 strafeRight f l = l { transform = M.translv (scale f $ S.right l) * transform l }
50
51 pitch _ l@DirectionalLight {} = l
52 pitch a l = l { transform = transform l * M.axisAngle (S.right l) a }
53
54 yaw _ l@DirectionalLight {} = l
55 yaw a l = l { transform = transform l * M.axisAngle (S.up l) a }
56
57 roll _ l@DirectionalLight {} = l
58 roll a l = l { transform = transform l * M.axisAngle (S.fwd l) a }
59
60 pos l@DirectionalLight {} = vec3 0 0 0
61 pos l = M.position . transform $ l
62
63 fwd (DirectionalLight _ _ _ f) = f
64 fwd l = M.forward . transform $ l
65
66 up l@DirectionalLight {} = vec3 0 1 0
67 up l = M.up . transform $ l
68
69 right l@DirectionalLight {} = vec3 1 0 0
70 right l = M.right . transform $ l
71
72 transform (PointLight _ _ _ transf) = transf
73 transform (DirectionalLight _ _ _ fwd) =
74 let up' = vec3 0 1 0
75 right = up `cross` fwd
76 up = fwd `cross` right
77 in
78 M.transform up right fwd (vec3 0 0 0)
79 transform (SpotLight _ _ _ transf) = transf
80
81 setTransform _ l@DirectionalLight {} = l
82 setTransform t l = l { Spear.Scene.Light.transform = t }