on master help sign in
finished makeMesh (creating buffers). TODO draw function!
commitaacd30a1d7c69d0af88efbb5e74927caaddbacaa authorJohn P Mayer Jr <john.p.mayer.jr@gmail.com> dateFri 15 Nov 2013, 05:22 (13 years ago) parentc8a14ff
3 files changed+107−24
libraries/Graphics/WebGL.elm+14−4 libraries/Native/Graphics/WebGL.js+90−19 libraries/Native/MJS.js+3−1
libraries/Graphics/WebGL.elm+14−4
@@ -5,17 +5,27 @@ module Graphics.WebGL where
55-}
66
77import Basics (Float)
8+import Signal (Signal)
89import MJS (V3,M4x4)
910import Color (Color)
1011import Graphics.Element (Element)
1112import Native.Graphics.WebGL
1213
14+data GLContext = Fake_GLContext
15+
16+glContext : Int -> Int -> Signal GLContext
17+glContext = Native.Graphics.WebGL.glContext
18+
1319type GLPoint = { pos: V3, color: Color }
1420type GLTriangle = { a: GLPoint, b: GLPoint, c: GLPoint }
15−type Model = [GLTriangle]
1621
17−data Scene = Node M4x4 [Scene] | Leaf Model
22+data Mesh = Fake_Mesh
1823
19−glContext : Int -> Int -> Scene -> Element
20−glContext = Native.Graphics.WebGL.glContext
24+makeMesh : [GLTriangle] -> GLContext -> Mesh
25+makeMesh = Native.Graphics.WebGL.makeMesh
26+
27+data Scene = Node M4x4 [Scene] | Leaf Mesh
28+
29+renderGL : GLContext -> Scene -> Element
30+renderGL = Native.Graphics.WebGL.renderGL
2131
libraries/Native/Graphics/WebGL.js+90−19
@@ -9,11 +9,12 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
99 //var Render = ElmRuntime.use(ElmRuntime.Render.Element);
1010 var newNode = ElmRuntime.use(ElmRuntime.Render.Utils).newElement;
1111
12− //var Signal = Elm.Signal.make(elm);
12+ var Signal = Elm.Signal.make(elm);
1313 var newElement = Elm.Graphics.Element.make(elm).newElement;
1414 //var JS = Elm.Native.JavaScript.make(elm);
15− //var Utils = Elm.Native.Utils.make(elm);
16− //var Tuple2 = Utils.Tuple2;
15+ var Utils = Elm.Native.Utils.make(elm);
16+ var Tuple2 = Utils.Tuple2;
17+ var MJS = Elm.Native.MJS.make(elm);
1718
1819 function createShader(gl, str, type) {
1920 var shader = gl.createShader(type);
@@ -76,47 +77,117 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
7677 }"
7778
7879 function drawGL(model) {
80+ var aspect = model.aspect;
81+
82+ var projectionMatrix = MJS.M4x4.makePerspective(45, aspect, 0.01, 100);
83+
84+ var eye = MJS.V3.$(0,0,1);
85+ var center = MJS.V3.$(0,0,0);
86+ var up = MJS.V3.$(0,1,0);
87+
88+ var viewMatrix = MJS.M4x4.makeLookAt(eye,center,up);
89+
90+ var modelMatrixStack = [MJS.M4x4.identity];
91+
92+ var gl = model.gl;
93+ gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
94+
7995 var scene = model.scene;
8096 console.log(scene);
97+
8198 }
8299
83100 /*
84101 * Called for new node, caches lots of stuff
85102 */
86103 function render(model) {
87− var canvas = newNode('canvas');
88− var gl = canvas.getContext("webgl");
89− var program = createProgram(gl, geoVert, geoFrag);
90− model.cache = {
91− gl: gl,
92− program: program,
93− };
94104 drawGL(model);
95− return canvas;
105+ return model.node;
96106 }
97107
98108 /*
99109 * Called at existing node
100110 */
101111 function update(canvasNode, oldModel, newModel) {
102− newModel.cache = oldModel.cache;
103112 drawGL(newModel)
104113 }
105114
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});
120+ }
121+
122+ function makeMesh(triangles, context) {
123+ var indices = [];
124+ var positions = [];
125+ var colors = [];
126+
127+ function pushTriangle(n, node) {
128+ if (node.ctor === "[]") return;
129+ var tri = node._0;
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]);
136+ }
137+ colors.push(vertex.color._0);
138+ colors.push(vertex.color._1);
139+ colors.push(vertex.color._2);
140+ colors.push(vertex.color._3);
141+ }
142+ pushTriangle(n+1,node._1);
143+ }
144+
145+ pushTriangle(0,triangles);
146+
147+ var gl = context.gl;
148+
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);
152+
153+ var vertexPosBuffer = gl.createBuffer();
154+ gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
155+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
156+
157+ var vertexColors = gl.createBuffer();
158+ gl.bindBuffer(gl.ARRAY_BUFFER, vertexColors);
159+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
160+
161+ return {
162+ indexBuffer:indexBuffer,
163+ vertexPosBuffer: vertexPosBuffer,
164+ vertexColors: vertexColors
165+ };
166+
167+ }
107168
108− return A3(newElement, w, h, {
169+ function renderGL(context, scene) {
170+
171+ return A3(newElement, context.w, context.h, {
109172 ctor: 'Custom',
110− type: 'WebGL',
111− render: render,
112− update: update,
113− model: {scene:scene},
173+ type: 'WebGL',
174+ render: render,
175+ update: update,
176+ model: {
177+ aspect: context.w/context.h,
178+ node: context.node,
179+ gl: context.gl,
180+ program: context.program,
181+ scene: scene,
182+ },
114183 });
115184
116185 }
117186
118187 return elm.Native.Graphics.WebGL.values = {
119− glContext:F3(glContext),
188+ glContext:F2(glContext),
189+ makeMesh:F2(makeMesh),
190+ renderGL:F2(renderGL),
120191 };
121192
122193};
libraries/Native/MJS.js+3−1
@@ -1845,6 +1845,7 @@ Elm.Native.MJS.make = function(elm) {
18451845 };
18461846
18471847 return {
1848+ V3: V3,
18481849 v3: F3(V3.$),
18491850 v3clone: V3.clone,
18501851 v3add: F2(V3.add),
@@ -1861,6 +1862,7 @@ Elm.Native.MJS.make = function(elm) {
18611862 /*
18621863 m4x4: F16(M4x4.$),
18631864 */
1865+ M4x4:M4x4,
18641866 m4x4identity: M4x4.identity,
18651867 m4x4clone: M4x4.clone,
18661868 m4x4topLeft3x3: M4x4.topLeft3x3,
@@ -1886,7 +1888,7 @@ Elm.Native.MJS.make = function(elm) {
18861888 m4x4translate3: F4(M4x4.translate3),
18871889 m4x4translate1: F2(M4x4.translate1),
18881890 m4x4translate: F2(M4x4.translate),
1889− m4x4makeLookFt: F3(M4x4.makeLookFt),
1891+ m4x4makeLookFt: F3(M4x4.makeLookAt),
18901892 m4x4transpose: M4x4.transpose,
18911893 m4x4transformPoint: F2(M4x4.transformPoint),
18921894 /*