@@ -1,30 +1,50 @@
2−import MJS (V3,v3,M4x4,m4x4makeRotate)
2+import MJS (V3,M4x4,v3,m4x4makeRotate,m4x4makePerspective,m4x4makeLookAt,m4x4mul)
3import Graphics.WebGL (Program, link, Triangle, zipTriangle, Buffer, bind, Model, encapsulate, webgl)
3import Graphics.WebGL (Program, link, Triangle, zipTriangle, Buffer, bind, Model, encapsulate, webgl)
5-- Define what our geometry looks like
5-- Define what our geometry looks like
7−topleft = v3 -1 1 0
7+p0 = v3 1 1 1
8−topright = v3 1 1 0
8+p1 = v3 -1 1 1
9−botleft = v3 -1 -1 0
9+p2 = v3 -1 -1 1
10−botright = v3 1 -1 0
14+p7 = v3 -1 -1 -1
12−red = v3 1 0 0
16+front = [(p0,p1,p2),(p2,p3,p0)]
13−green = v3 0 1 0
17+back = [(p5,p6,p7),(p7,p4,p5)]
14−blue = v3 0 0 1
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)]
23+positions = front ++ back ++ right ++ left ++ top ++ bottom
32+repeat n elem = map (\_ -> elem) [0..n-1]
16−colors = [(red,green,blue),(red,green,blue)]
34+colors = concat . map (\c -> repeat 2 (c,c,c)) <| [gray,red,green,blue,yellow,purple]
18mesh : [Triangle {pos : V3, color : V3}]
36mesh : [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
21vert = [glShader|
39vert = [glShader|
22attribute vec3 pos;
40attribute vec3 pos;
23attribute vec3 color;
41attribute vec3 color;
24uniform mat4 rot;
44uniform mat4 rot;
25varying vec3 vcolor;
45varying vec3 vcolor;
26void main () {
46void main () {
27− gl_Position = rot * vec4(pos, 1.0);
47+ gl_Position = per * cam * rot * vec4(pos, 1.0);
28 vcolor = color;
48 vcolor = color;
@@ -37,17 +57,21 @@ void main () {
40−prog : Program {pos : V3, color : V3} { rot : M4x4 }
41prog = link vert frag
60prog = link vert frag
43buf : Buffer {pos : V3, color : V3}
62buf : Buffer {pos : V3, color : V3}
44buf = bind mesh
63buf = bind mesh
66+per = m4x4makePerspective 45 1 0.01 100
69+cam = m4x4makeLookAt (v3 0 0 5) (v3 0 0 0) (v3 0 1 0)
46angle : Signal Float
71angle : Signal Float
47−angle = foldp (\_ n -> n + 0.1) 0 (fps 25)
72+angle = foldp (\_ n -> n + 0.02) 0 (fps 25)
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
52draw : Signal [Model]
76draw : Signal [Model]
53draw = combine [ (encapsulate prog buf) <~ rot ]
77draw = combine [ (encapsulate prog buf) <~ rot ]