@@ -9,11 +9,12 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
9 //var Render = ElmRuntime.use(ElmRuntime.Render.Element);
9 //var Render = ElmRuntime.use(ElmRuntime.Render.Element);
10 var newNode = ElmRuntime.use(ElmRuntime.Render.Utils).newElement;
10 var newNode = ElmRuntime.use(ElmRuntime.Render.Utils).newElement;
12− //var Signal = Elm.Signal.make(elm);
12+ var Signal = Elm.Signal.make(elm);
13 var newElement = Elm.Graphics.Element.make(elm).newElement;
13 var newElement = Elm.Graphics.Element.make(elm).newElement;
14 //var JS = Elm.Native.JavaScript.make(elm);
14 //var JS = Elm.Native.JavaScript.make(elm);
15− //var Utils = Elm.Native.Utils.make(elm);
15+ var Utils = Elm.Native.Utils.make(elm);
16− //var Tuple2 = Utils.Tuple2;
16+ var Tuple2 = Utils.Tuple2;
17+ var MJS = Elm.Native.MJS.make(elm);
18 function createShader(gl, str, type) {
19 function createShader(gl, str, type) {
19 var shader = gl.createShader(type);
20 var shader = gl.createShader(type);
@@ -76,47 +77,117 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
78 function drawGL(model) {
79 function drawGL(model) {
80+ var aspect = model.aspect;
82+ var projectionMatrix = MJS.M4x4.makePerspective(45, aspect, 0.01, 100);
84+ var eye = MJS.V3.$(0,0,1);
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);
90+ var modelMatrixStack = [MJS.M4x4.identity];
93+ gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
79 var scene = model.scene;
95 var scene = model.scene;
80 console.log(scene);
96 console.log(scene);
84 * Called for new node, caches lots of stuff
101 * Called for new node, caches lots of stuff
86 function render(model) {
103 function render(model) {
87− var canvas = newNode('canvas');
88− var gl = canvas.getContext("webgl");
89− var program = createProgram(gl, geoVert, geoFrag);
94 drawGL(model);
104 drawGL(model);
95− return canvas;
105+ return model.node;
99 * Called at existing node
109 * Called at existing node
101 function update(canvasNode, oldModel, newModel) {
111 function update(canvasNode, oldModel, newModel) {
102− newModel.cache = oldModel.cache;
103 drawGL(newModel)
112 drawGL(newModel)
106− function glContext(w,h,scene) {
115+ function glContext(w,h) {
116+ var node = newNode('canvas');
117+ var gl = node.getContext('webgl');
118+ var program = createProgram(gl, geoVert, geoFrag);
119+ return Signal.constant({w:w,h:h,gl:gl,node:node,program:program});
122+ function makeMesh(triangles, context) {
127+ function pushTriangle(n, node) {
128+ if (node.ctor === "[]") return;
130+ var names = ['a','b','c'];
131+ for (var i = 0; i < 3; i+= 1) {
132+ indices.push(3 * n + i);
133+ var vertex = tri[names[i]];
134+ for (var j = 0; j < 3; j += 1) {
135+ positions.push(vertex.pos[j]);
137+ colors.push(vertex.color._0);
138+ colors.push(vertex.color._1);
139+ colors.push(vertex.color._2);
140+ colors.push(vertex.color._3);
142+ pushTriangle(n+1,node._1);
145+ pushTriangle(0,triangles);
147+ var gl = context.gl;
149+ var indexBuffer = gl.createBuffer();
150+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
151+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
153+ var vertexPosBuffer = gl.createBuffer();
154+ gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
155+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
157+ var vertexColors = gl.createBuffer();
158+ gl.bindBuffer(gl.ARRAY_BUFFER, vertexColors);
159+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
162+ indexBuffer:indexBuffer,
163+ vertexPosBuffer: vertexPosBuffer,
164+ vertexColors: vertexColors
169+ function renderGL(context, scene) {
108− return A3(newElement, w, h, {
171+ return A3(newElement, context.w, context.h, {
109 ctor: 'Custom',
172 ctor: 'Custom',
110− type: 'WebGL',
173+ type: 'WebGL',
111− render: render,
174+ render: render,
112− update: update,
175+ update: update,
177+ aspect: context.w/context.h,
180+ program: context.program,
113− model: {scene:scene},
181+ scene: scene,
118 return elm.Native.Graphics.WebGL.values = {
187 return elm.Native.Graphics.WebGL.values = {
119− glContext:F3(glContext),
188+ glContext:F2(glContext),
189+ makeMesh:F2(makeMesh),
190+ renderGL:F2(renderGL),