@@ -6,15 +6,14 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
6 elm.Native.Graphics.WebGL = elm.Native.Graphics.WebGL || {};
6 elm.Native.Graphics.WebGL = elm.Native.Graphics.WebGL || {};
7 if (elm.Native.Graphics.WebGL.values) return elm.Native.Graphics.WebGL.values;
7 if (elm.Native.Graphics.WebGL.values) return elm.Native.Graphics.WebGL.values;
9− //var Render = ElmRuntime.use(ElmRuntime.Render.Element);
10 var newNode = ElmRuntime.use(ElmRuntime.Render.Utils).newElement;
9 var newNode = ElmRuntime.use(ElmRuntime.Render.Utils).newElement;
12 var Signal = Elm.Signal.make(elm);
11 var Signal = Elm.Signal.make(elm);
13 var newElement = Elm.Graphics.Element.make(elm).newElement;
12 var newElement = Elm.Graphics.Element.make(elm).newElement;
14− //var JS = Elm.Native.JavaScript.make(elm);
15 var Utils = Elm.Native.Utils.make(elm);
13 var Utils = Elm.Native.Utils.make(elm);
16 var Tuple2 = Utils.Tuple2;
14 var Tuple2 = Utils.Tuple2;
17 var MJS = Elm.Native.MJS.make(elm);
15 var MJS = Elm.Native.MJS.make(elm);
16+ var List = Elm.Native.List.make(elm);
19 function createShader(gl, str, type) {
18 function createShader(gl, str, type) {
20 var shader = gl.createShader(type);
19 var shader = gl.createShader(type);
@@ -57,14 +56,12 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
57 var geoVert = "attribute vec3 aVertexPosition;\
56 var geoVert = "attribute vec3 aVertexPosition;\
58 attribute vec4 aVertexColor;\
57 attribute vec4 aVertexColor;\
60− uniform mat4 uMMatrix;\
59+ uniform mat4 uSceneMatrix;\
61− uniform mat4 uPMatrix;\
62− uniform mat4 uVMatrix;\
64 varying vec4 vVertexColor;\
61 varying vec4 vVertexColor;\
66 void main() {\
63 void main() {\
67− gl_Position = uPMatrix * uVMatrix * uMMatrix * vec4(aVertexPosition, 1.0);\
64+ gl_Position = uSceneMatrix * vec4(aVertexPosition, 1.0);\
68 vVertexColor = aVertexColor;\
65 vVertexColor = aVertexColor;\
@@ -79,22 +76,50 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
79 function drawGL(model) {
76 function drawGL(model) {
80 var aspect = model.aspect;
77 var aspect = model.aspect;
82− var projectionMatrix = MJS.M4x4.makePerspective(45, aspect, 0.01, 100);
79+ var gl = model.gl;
80+ gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
84− var eye = MJS.V3.$(0,0,1);
82+ var program = model.program;
85− var center = MJS.V3.$(0,0,0);
86− var up = MJS.V3.$(0,1,0);
88− var viewMatrix = MJS.M4x4.makeLookAt(eye,center,up);
84+ function drawMesh(mesh, sceneMatrix) {
89−
85+ gl.useProgram(program.programPtr);
90− var modelMatrixStack = [MJS.M4x4.identity];
86+
87+ gl.enableVertexAttribArray(program.vertexPositionAttribute);
88+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, mesh.indexBuffer);
89+ gl.bindBuffer(gl.ARRAY_BUFFER, mesh.vertexPosBuffer);
90+ gl.vertexAttribPointer(program.vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);
92− var gl = model.gl;
92+ gl.enableVertexAttribArray(program.vertexColorAttribute);
93− gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
93+ gl.bindBuffer(gl.ARRAY_BUFFER, mesh.vertexColorBuffer);
94+ gl.vertexAttribPointer(program.vertexColorAttribute, 4, gl.FLOAT, false, 0, 0);
96+ gl.uniformMatrix4fv(program.sceneMatrixUniform, false, sceneMatrix);
98+ gl.drawElements(gl.TRIANGLES, mesh.numIndices, gl.UNSIGNED_SHORT, 0);
101+ var sceneMatrixStack = [MJS.M4x4.identity];
103+ function drawScene(scene,n) {
104+ switch(scene.ctor) {
106+ drawMesh(scene._0, sceneMatrixStack[n]);
95− var scene = model.scene;
109+ var m = scene._0;
96− console.log(scene);
110+ sceneMatrixStack[n+1] = MJS.M4x4.mul(sceneMatrixStack[n],m)
111+ function drawChild(c) {
114+ var children = scene._1;
115+ List.each(drawChild, children);
120+ var scene = model.scene;
@@ -115,11 +140,20 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
115 function glContext(w,h) {
140 function glContext(w,h) {
116 var node = newNode('canvas');
141 var node = newNode('canvas');
117 var gl = node.getContext('webgl');
142 var gl = node.getContext('webgl');
118− var program = createProgram(gl, geoVert, geoFrag);
143+ var programPtr = createProgram(gl, geoVert, geoFrag);
144+ var vertexPositionAttribute = gl.getAttribLocation(programPtr,'aVertexPosition');
145+ var vertexColorAttribute = gl.getAttribLocation(programPtr,'aVertexColor');
146+ var sceneMatrixUniform = gl.getUniformLocation(programPtr, 'uSceneMatrix');
148+ programPtr: programPtr,
149+ vertexPositionAttribute: vertexPositionAttribute,
150+ vertexColorAttribute: vertexColorAttribute,
151+ sceneMatrixUniform: sceneMatrixUniform,
119 return Signal.constant({w:w,h:h,gl:gl,node:node,program:program});
153 return Signal.constant({w:w,h:h,gl:gl,node:node,program:program});
122− function makeMesh(triangles, context) {
156+ function bindMesh(triangles, context) {
123 var indices = [];
157 var indices = [];
124 var positions = [];
158 var positions = [];
125 var colors = [];
159 var colors = [];
@@ -134,9 +168,9 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
134 for (var j = 0; j < 3; j += 1) {
168 for (var j = 0; j < 3; j += 1) {
135 positions.push(vertex.pos[j]);
169 positions.push(vertex.pos[j]);
137− colors.push(vertex.color._0);
171+ colors.push(vertex.color._0 / 256);
138− colors.push(vertex.color._1);
172+ colors.push(vertex.color._1 / 256);
139− colors.push(vertex.color._2);
173+ colors.push(vertex.color._2 / 256);
140 colors.push(vertex.color._3);
174 colors.push(vertex.color._3);
142 pushTriangle(n+1,node._1);
176 pushTriangle(n+1,node._1);
@@ -154,14 +188,15 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
154 gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
188 gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
155 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
189 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
157− var vertexColors = gl.createBuffer();
191+ var vertexColorBuffer = gl.createBuffer();
158− gl.bindBuffer(gl.ARRAY_BUFFER, vertexColors);
192+ gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
159 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
193 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
162− indexBuffer:indexBuffer,
196+ indexBuffer: indexBuffer,
163 vertexPosBuffer: vertexPosBuffer,
197 vertexPosBuffer: vertexPosBuffer,
164− vertexColors: vertexColors
198+ vertexColorBuffer: vertexColorBuffer,
199+ numIndices: indices.length,
@@ -186,7 +221,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
187 return elm.Native.Graphics.WebGL.values = {
222 return elm.Native.Graphics.WebGL.values = {
188 glContext:F2(glContext),
223 glContext:F2(glContext),
189− makeMesh:F2(makeMesh),
224+ bindMesh:F2(bindMesh),
190 renderGL:F2(renderGL),
225 renderGL:F2(renderGL),