on master help sign in
API nicety changes
commit7ff90c8fcdf0cc3c1589d1a5f5ebd8bb8b210b4d authorJohn P Mayer Jr <john.p.mayer.jr@gmail.com> dateThu 21 Nov 2013, 03:08 (13 years ago) parent517b17a
5 files changed+64−35
libraries/Graphics/WebGL.elm+17−11 libraries/MJS.elm+6 libraries/Native/Graphics/WebGL.js+8−9 libraries/Native/MJS.js+1−1 tests/good/Box.elm+32−14
libraries/Graphics/WebGL.elm+17−11
@@ -1,4 +1,4 @@
1−module Graphics.WebGL where
1+module Graphics.WebGL (makeGL, Vertex, bufferMesh, transform, mesh, webgl) where
22
33{-| WebGL
44
@@ -11,21 +11,27 @@ import Color (Color)
1111import Graphics.Element (Element)
1212import Native.Graphics.WebGL
1313
14−data GLContext = Fake_GLContext
14+data GLContext = Fake_Context
1515
16−glContext : Int -> Int -> Signal GLContext
17−glContext = Native.Graphics.WebGL.glContext
16+makeGL : Int -> Int -> Signal GLContext
17+makeGL = Native.Graphics.WebGL.makeGL
1818
19−type GLPoint = { pos: V3, color: Color }
20−type GLTriangle = { a: GLPoint, b: GLPoint, c: GLPoint }
19+type Vertex = { pos: V3, color: Color }
20+type Triangle = (Vertex,Vertex,Vertex)
2121
2222data Mesh = Fake_Mesh
2323
24−bindMesh : [GLTriangle] -> GLContext -> Mesh
25−bindMesh = Native.Graphics.WebGL.bindMesh
24+bufferMesh : [Triangle] -> GLContext -> Mesh
25+bufferMesh = Native.Graphics.WebGL.bufferMesh
2626
27−data Scene = Node M4x4 [Scene] | Leaf Mesh
27+data Scene = SceneNode M4x4 [Scene] | SceneLeaf Mesh
2828
29−renderGL : GLContext -> Scene -> Element
30−renderGL = Native.Graphics.WebGL.renderGL
29+transform : M4x4 -> [Scene] -> Scene
30+transform = SceneNode
31+
32+mesh : Mesh -> Scene
33+mesh = SceneLeaf
34+
35+webgl : GLContext -> Scene -> Element
36+webgl = Native.Graphics.WebGL.webgl
3137
libraries/MJS.elm+6
@@ -17,3 +17,9 @@ m4x4identity = Native.MJS.m4x4identity
1717
1818m4x4makeRotate : Float -> V3 -> M4x4
1919m4x4makeRotate = Native.MJS.m4x4makeRotate
20+
21+m4x4makeLookAt : V3 -> V3 -> V3 -> M4x4
22+m4x4makeLookAt = Native.MJS.m4x4makeLookAt
23+
24+m4x4makePerspective : Float -> Float -> Float -> Float -> M4x4
25+m4x4makePerspective = Native.MJS.m4x4makePerspective
libraries/Native/Graphics/WebGL.js+8−9
@@ -102,7 +102,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
102102
103103 function drawScene(scene,n) {
104104 switch(scene.ctor) {
105− case 'Leaf':
105+ case 'SceneLeaf':
106106 drawMesh(scene._0, sceneMatrixStack[n]);
107107 break;
108108 default:
@@ -137,7 +137,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
137137 drawGL(newModel)
138138 }
139139
140− function glContext(w,h) {
140+ function makeGL(w,h) {
141141 var node = newNode('canvas');
142142 var gl = node.getContext('webgl');
143143 var programPtr = createProgram(gl, geoVert, geoFrag);
@@ -153,7 +153,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
153153 return Signal.constant({w:w,h:h,gl:gl,node:node,program:program});
154154 }
155155
156− function bindMesh(triangles, context) {
156+ function bufferMesh(triangles, context) {
157157 var indices = [];
158158 var positions = [];
159159 var colors = [];
@@ -161,10 +161,9 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
161161 function pushTriangle(n, node) {
162162 if (node.ctor === "[]") return;
163163 var tri = node._0;
164− var names = ['a','b','c'];
165164 for (var i = 0; i < 3; i+= 1) {
166165 indices.push(3 * n + i);
167− var vertex = tri[names[i]];
166+ var vertex = tri['_' + i];
168167 for (var j = 0; j < 3; j += 1) {
169168 positions.push(vertex.pos[j]);
170169 }
@@ -201,7 +200,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
201200
202201 }
203202
204− function renderGL(context, scene) {
203+ function webgl(context, scene) {
205204
206205 return A3(newElement, context.w, context.h, {
207206 ctor: 'Custom',
@@ -220,9 +219,9 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
220219 }
221220
222221 return elm.Native.Graphics.WebGL.values = {
223− glContext:F2(glContext),
224− bindMesh:F2(bindMesh),
225− renderGL:F2(renderGL),
222+ makeGL:F2(makeGL),
223+ bufferMesh:F2(bufferMesh),
224+ webgl:F2(webgl),
226225 };
227226
228227};
libraries/Native/MJS.js+1−1
@@ -1888,7 +1888,7 @@ Elm.Native.MJS.make = function(elm) {
18881888 m4x4translate3: F4(M4x4.translate3),
18891889 m4x4translate1: F2(M4x4.translate1),
18901890 m4x4translate: F2(M4x4.translate),
1891− m4x4makeLookFt: F3(M4x4.makeLookAt),
1891+ m4x4makeLookAt: F3(M4x4.makeLookAt),
18921892 m4x4transpose: M4x4.transpose,
18931893 m4x4transformPoint: F2(M4x4.transformPoint),
18941894 /*
tests/good/Box.elm+32−14
@@ -1,36 +1,53 @@
11
22import open Color
3−import open Graphics.WebGL
43import open MJS
54
6−-- Setup the GL context
5+import Graphics.WebGL (GLContext, Vertex, Triangle, Mesh, Scene, makeGL, bufferMesh, transform, mesh, webgl)
76
8−context : Signal GLContext
9−context = glContext 400 400
7+-- Setup the GLContext
108
11−-- Define what our picture looks like
9+gl : Signal GLContext
10+gl = makeGL 400 400
11+
12+-- Define what our geometry looks like
1213
1314top = v3 0 1 0
1415left = v3 -1 -1 0
1516right = v3 1 -1 0
1617
17−triangle : GLTriangle
18−triangle = GLTriangle (GLPoint top red) (GLPoint left blue) (GLPoint right green)
18+geometry : Triangle
19+geometry = (Vertex top red, Vertex left blue, Vertex right green)
20+
21+-- Bind our geometry to a mesh in our GL gl
22+
23+geoMesh : Signal Mesh
24+geoMesh = lift (bufferMesh [geometry]) gl
1925
20−-- Bind our picture to a mesh in our GL context
26+-- Build two helper transforms
2127
22−triMesh : Signal Mesh
23−triMesh = lift (bindMesh [triangle]) context
28+perspective : M4x4
29+perspective = m4x4makePerspective 45 1 0.01 100
2430
25−-- Given an angle and a Mesh, what does the scene look like?
31+camera : Float -> M4x4
32+camera r =
33+ let eye = v3 0 0 r
34+ center = v3 0 0 0
35+ up = v3 0 1 0
36+ in m4x4makeLookAt eye center up
37+
38+-- Build the scene with extra model rotate transforms
2639
2740scene : Float -> Mesh -> Scene
28−scene angle mesh =
41+scene angle m =
2942 let xAxis = v3 1 0 0
3043 yAxis = v3 0 1 0
3144 rotationX = m4x4makeRotate (angle / 2) xAxis
3245 rotationY = m4x4makeRotate (angle / 3) yAxis
33− in Node rotationX [Node rotationY [Leaf mesh]]
46+ in transform perspective [
47+ transform (camera <| 5 + 3 * sin angle) [
48+ transform rotationX [
49+ transform rotationY [
50+ mesh m ]]]]
3451
3552-- Standard accumulator and wiring it up with main
3653
@@ -38,4 +55,5 @@ angle : Signal Float
3855angle = foldp (\_ a -> a + 0.1) 0 (fps 25)
3956
4057main : Signal Element
41−main = lift2 renderGL context (lift2 scene angle triMesh)
58+main = lift2 webgl gl (lift2 scene angle geoMesh)
59+