@@ -1,30 +1,50 @@
11
2−import MJS (V3,v3,M4x4,m4x4makeRotate)
2+import MJS (V3,M4x4,v3,m4x4makeRotate,m4x4makePerspective,m4x4makeLookAt,m4x4mul)
33import Graphics.WebGL (Program, link, Triangle, zipTriangle, Buffer, bind, Model, encapsulate, webgl)
44
55-- Define what our geometry looks like
66
7−topleft = v3 -1 1 0
8−topright = v3 1 1 0
9−botleft = v3 -1 -1 0
10−botright = v3 1 -1 0
7+p0 = v3 1 1 1
8+p1 = v3 -1 1 1
9+p2 = v3 -1 -1 1
10+p3 = v3 1 -1 1
11+p4 = v3 1 -1 -1
12+p5 = v3 1 1 -1
13+p6 = v3 -1 1 -1
14+p7 = v3 -1 -1 -1
1115
12−red = v3 1 0 0
13−green = v3 0 1 0
14−blue = v3 0 0 1
16+front = [(p0,p1,p2),(p2,p3,p0)]
17+back = [(p5,p6,p7),(p7,p4,p5)]
18+right = [(p0,p3,p4),(p4,p5,p0)]
19+left = [(p1,p2,p7),(p7,p6,p1)]
20+top = [(p0,p5,p6),(p6,p1,p0)]
21+bottom = [(p3,p4,p7),(p7,p2,p3)]
1522
16−colors = [(red,green,blue),(red,green,blue)]
23+positions = front ++ back ++ right ++ left ++ top ++ bottom
24+
25+gray = v3 0.7 0.7 0.7
26+red = v3 1 0 0
27+green = v3 0 1 0
28+blue = v3 0 0 1
29+yellow = v3 1 1 0
30+purple = v3 0 1 1
31+
32+repeat n elem = map (\_ -> elem) [0..n-1]
33+
34+colors = concat . map (\c -> repeat 2 (c,c,c)) <| [gray,red,green,blue,yellow,purple]
1735
1836mesh : [Triangle {pos : V3, color : V3}]
19−mesh = zipWith (zipTriangle (\pos color -> { pos = pos, color = color })) [(topleft, topright, botleft), (topright, botleft, botright)] colors
37+mesh = zipWith (zipTriangle (\pos color -> { pos = pos, color = color })) positions colors
2038
2139vert = [glShader|
2240attribute vec3 pos;
2341attribute vec3 color;
42+uniform mat4 per;
43+uniform mat4 cam;
2444uniform mat4 rot;
2545varying vec3 vcolor;
2646void main () {
27− gl_Position = rot * vec4(pos, 1.0);
47+ gl_Position = per * cam * rot * vec4(pos, 1.0);
2848 vcolor = color;
2949}
3050|]
@@ -37,17 +57,21 @@ void main () {
3757}
3858|]
3959
40−prog : Program {pos : V3, color : V3} { rot : M4x4 }
4160prog = link vert frag
4261
4362buf : Buffer {pos : V3, color : V3}
4463buf = bind mesh
4564
65+per : M4x4
66+per = m4x4makePerspective 45 1 0.01 100
67+
68+cam : M4x4
69+cam = m4x4makeLookAt (v3 0 0 5) (v3 0 0 0) (v3 0 1 0)
70+
4671angle : Signal Float
47−angle = foldp (\_ n -> n + 0.1) 0 (fps 25)
72+angle = foldp (\_ n -> n + 0.02) 0 (fps 25)
4873
49−rot : Signal { rot : M4x4 }
50−rot = (\t -> { rot = m4x4makeRotate t (v3 0 1 0) }) <~ angle
74+rot = (\t -> { rot = m4x4mul (m4x4makeRotate (3*t) (v3 0 1 0)) (m4x4makeRotate (2*t) (v3 1 0 0)), per = per, cam = cam }) <~ angle
5175
5276draw : Signal [Model]
5377draw = combine [ (encapsulate prog buf) <~ rot ]