on master help sign in
fixef texture cache. updated crate from flat to spinning box
899c190John P Mayer Jr
libraries/Native/Graphics/WebGL.js+2
@@ -235,6 +235,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
235 }
235 }
236 if (!tex) {
236 if (!tex) {
237 tex = do_texture(gl, texture.img);
237 tex = do_texture(gl, texture.img);
238+ model.cache.textures[texture.id] = tex;
238 }
239 }
239 var activeName = 'TEXTURE' + textureCounter;
240 var activeName = 'TEXTURE' + textureCounter;
240 gl.activeTexture(gl[activeName]);
241 gl.activeTexture(gl[activeName]);
@@ -307,6 +308,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
307 model.cache.shaders = [];
308 model.cache.shaders = [];
308 model.cache.programs = {};
309 model.cache.programs = {};
309 model.cache.buffers = [];
310 model.cache.buffers = [];
311+ model.cache.textures = [];
310
312
311 update(div, model, model);
313 update(div, model, model);
312
314
tests/test-files/good/Crate.elm+54−18
@@ -5,44 +5,80 @@ import Http (..)
5import MJS (..)
5import MJS (..)
6import Graphics.WebGL (..)
6import Graphics.WebGL (..)
7
7
8−pul = v3 -1 1 0
8+pul = {pos=v3 -1 1 0, coord=v3 0 1 0}
9−pur = v3 1 1 0
9+pur = {pos=v3 1 1 0, coord=v3 1 1 0}
10−pbl = v3 -1 -1 0
10+pbl = {pos=v3 -1 -1 0, coord=v3 0 0 0}
11−pbr = v3 1 -1 0
11+pbr = {pos=v3 1 -1 0, coord=v3 1 0 0}
12
12
13−mesh : [Triangle { pos : V3 }]
13+face : [Triangle { pos : V3, coord : V3 }]
14−mesh = map (mapTriangle (\pos -> {pos=pos})) <|
15− [(pul,pur,pbl),(pbl,pur,pbr)]
14+face = [(pul,pur,pbl),(pbl,pur,pbr)]
16
15
16+rotFace : Float -> Float -> [Triangle { pos : V3, coord : V3 }]
17+rotFace turnH turnV =
18+ let x = makeRotate (turnV * pi / 2) (v3 1 0 0)
19+ y = makeRotate (turnH * pi / 2) (v3 0 1 0)
20+ s = makeTranslate (v3 0 0 1)
21+ t = mul (mul x y) s
22+ in map (mapTriangle (\x -> {x | pos <- mul4x4 t x.pos })) face
23+
24+mesh : [Triangle { pos : V3, coord : V3 }]
25+mesh = concat . map (uncurry rotFace) <|
26+ [ (0,0)
27+ , (1,0)
28+ , (2,0)
29+ , (3,0)
30+ , (0,1)
31+ , (0,-1)
32+ ]
33+
17−vert : Shader { pos : V3 } u { coord : V2 }
34+vert : Shader { pos : V3, coord : V3 } { u | view : M4x4 } { vcoord : V2 }
18vert = [glShader|
35vert = [glShader|
19attribute vec3 pos;
36attribute vec3 pos;
37+attribute vec3 coord;
38+uniform mat4 view;
20−varying vec2 coord;
39+varying vec2 vcoord;
21void main () {
40void main () {
22− gl_Position = vec4(pos, 1.0);
41+ gl_Position = view * vec4(pos, 1.0);
23− coord = pos.xy;
42+ vcoord = coord.xy;
24}
43}
25|]
44|]
26
45
27−frag : Shader {} { u | crate : Texture } { coord : V2 }
46+frag : Shader {} { u | crate : Texture } { vcoord : V2 }
28frag = [glShader|
47frag = [glShader|
29precision mediump float;
48precision mediump float;
30uniform sampler2D crate;
49uniform sampler2D crate;
31−varying vec2 coord;
50+varying vec2 vcoord;
32void main () {
51void main () {
33− gl_FragColor = texture2D(crate, coord);
52+ gl_FragColor = texture2D(crate, vcoord);
34}
53}
35|]
54|]
36
55
37crateTex : Signal (Response Texture)
56crateTex : Signal (Response Texture)
38crateTex = loadTex "woodCrate.JPG"
57crateTex = loadTex "woodCrate.JPG"
39
58
59+per : M4x4
60+per = makePerspective 45 1 0.01 100
61+
62+cam : M4x4
63+cam = makeLookAt (v3 0 0 5) (v3 0 0 0) (v3 0 1 0)
64+
65+angle : Signal Float
66+angle = foldp (\_ n -> n + 0.02) 0 (fps 25)
67+
68+view : Float -> M4x4
69+view t =
70+ let x = makeRotate (2*t) (v3 1 0 0)
71+ y = makeRotate (3*t) (v3 0 1 0)
72+ in foldr mul identity [per,cam,y,x]
73+
40−content : Response Texture -> [Model]
74+content : Response Texture -> M4x4 -> [Model]
41−content response = case response of
75+content response view = case response of
42 Waiting -> []
76 Waiting -> []
43 Failure i s -> []
77 Failure i s -> []
44− Success tex -> [model vert frag mesh { crate = tex }]
78+ Success tex -> [model vert frag mesh { crate = tex, view = view }]
79+
80+main = webgl (400,400) <~ (content <~ crateTex ~ (view <~ angle))
45
81
46−main = webgl (400,400) <~ (content <~ crateTex)
82+--main = asText (List.length mesh)
47
83
48
84