on master help sign in
fixef texture cache. updated crate from flat to spinning box
commit899c19002fec29516b6eb30d1da2abcde58fa49a authorJohn P Mayer Jr <john.p.mayer.jr@gmail.com> dateSun 2 Mar 2014, 14:31 (13 years ago) parent22001fa
2 files changed+56−18
libraries/Native/Graphics/WebGL.js+2 tests/test-files/good/Crate.elm+54−18
libraries/Native/Graphics/WebGL.js+2
@@ -235,6 +235,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
235235 }
236236 if (!tex) {
237237 tex = do_texture(gl, texture.img);
238+ model.cache.textures[texture.id] = tex;
238239 }
239240 var activeName = 'TEXTURE' + textureCounter;
240241 gl.activeTexture(gl[activeName]);
@@ -307,6 +308,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
307308 model.cache.shaders = [];
308309 model.cache.programs = {};
309310 model.cache.buffers = [];
311+ model.cache.textures = [];
310312
311313 update(div, model, model);
312314
tests/test-files/good/Crate.elm+54−18
@@ -5,44 +5,80 @@ import Http (..)
55import MJS (..)
66import Graphics.WebGL (..)
77
8−pul = v3 -1 1 0
9−pur = v3 1 1 0
10−pbl = v3 -1 -1 0
11−pbr = v3 1 -1 0
8+pul = {pos=v3 -1 1 0, coord=v3 0 1 0}
9+pur = {pos=v3 1 1 0, coord=v3 1 1 0}
10+pbl = {pos=v3 -1 -1 0, coord=v3 0 0 0}
11+pbr = {pos=v3 1 -1 0, coord=v3 1 0 0}
1212
13−mesh : [Triangle { pos : V3 }]
14−mesh = map (mapTriangle (\pos -> {pos=pos})) <|
15− [(pul,pur,pbl),(pbl,pur,pbr)]
13+face : [Triangle { pos : V3, coord : V3 }]
14+face = [(pul,pur,pbl),(pbl,pur,pbr)]
1615
17−vert : Shader { pos : V3 } u { coord : V2 }
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+
34+vert : Shader { pos : V3, coord : V3 } { u | view : M4x4 } { vcoord : V2 }
1835vert = [glShader|
1936attribute vec3 pos;
20−varying vec2 coord;
37+attribute vec3 coord;
38+uniform mat4 view;
39+varying vec2 vcoord;
2140void main () {
22− gl_Position = vec4(pos, 1.0);
23− coord = pos.xy;
41+ gl_Position = view * vec4(pos, 1.0);
42+ vcoord = coord.xy;
2443}
2544|]
2645
27−frag : Shader {} { u | crate : Texture } { coord : V2 }
46+frag : Shader {} { u | crate : Texture } { vcoord : V2 }
2847frag = [glShader|
2948precision mediump float;
3049uniform sampler2D crate;
31−varying vec2 coord;
50+varying vec2 vcoord;
3251void main () {
33− gl_FragColor = texture2D(crate, coord);
52+ gl_FragColor = texture2D(crate, vcoord);
3453}
3554|]
3655
3756crateTex : Signal (Response Texture)
3857crateTex = loadTex "woodCrate.JPG"
3958
40−content : Response Texture -> [Model]
41−content response = case response of
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+
74+content : Response Texture -> M4x4 -> [Model]
75+content response view = case response of
4276 Waiting -> []
4377 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))
4581
46−main = webgl (400,400) <~ (content <~ crateTex)
82+--main = asText (List.length mesh)
4783
4884