Refact: Render.
6 var canvas = document.getElementById("machine");
7 window.onresize = resize;
8 var context = new Context(canvas);
9 make_fullscreen(context);
11 var object = new Box(1, context);
12 gl.clearColor(0.0, 0.0, 0.0, 1.0);
13 gl.enable(gl.DEPTH_TEST);
14 machine = new Machine(object);
15 render = new Render(context);
16 render.draw(machine.scene);
21 make_fullscreen(machine.context);
25 function make_fullscreen(context)
27 var width = window.innerWidth;
28 var height = window.innerHeight;
29 context.canvas.width = width;
30 context.canvas.height = height;
31 context.viewport.width = width;
32 context.viewport.height = height;
35 function Render(context)
37 this.context = context;
40 Render.prototype.draw = function(scene)
42 var gl = this.context.gl;
43 var viewport = this.context.viewport;
44 var shader = this.context.shader;
45 var mvMatrix = this.context.mvMatrix;
46 var pMatrix = this.context.pMatrix;
48 gl.viewport(0, 0, viewport.width, viewport.height);
49 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
50 mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix);
52 mat4.identity(mvMatrix);
53 mat4.translate(mvMatrix, [0.0, 0.0, -7.0]);
54 gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer);
55 gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
57 gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer);
58 gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0);
60 this.context.updateMatrixUniforms();
61 gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems);
63 if (this.next) this.next.draw(scene);
66 function Machine(scene)
70 Machine.prototype.draw = function(render)
72 render.draw(this.scene);
75 function Context(canvas)
80 this.gl = canvas.getContext("experimental-webgl");
81 this.viewport = {'width': canvas.width,
82 'height': canvas.height};
88 if (!this.gl) alert("Failed: WebGL init.");
89 this.mvMatrix = mat4.create();
90 this.pMatrix = mat4.create();
91 this.shader = new Shader(this);
93 Context.prototype.updateMatrixUniforms = function()
96 var program = this.shader;
97 var pMatrix = this.pMatrix;
98 var mvMatrix = this.mvMatrix;
99 gl.uniformMatrix4fv(program.pMatrixUniform, false, pMatrix);
100 gl.uniformMatrix4fv(program.mvMatrixUniform, false, mvMatrix);
103 function Shader(context)
106 var fragment = load_shader(gl, "fragment-shader");
107 var vertex = load_shader(gl, "vertex-shader");
108 this.program = gl.createProgram();
109 gl.attachShader(this.program, vertex);
110 gl.attachShader(this.program, fragment);
111 gl.linkProgram(this.program);
113 if (!gl.getProgramParameter(this.program, gl.LINK_STATUS))
115 alert("Failed: Shader init.");
118 gl.useProgram(this.program);
119 this.vertexPosition = gl.getAttribLocation(this.program, "aVertexPosition");
120 gl.enableVertexAttribArray(this.vertexPosition);
122 this.vertexColour = gl.getAttribLocation(this.program, "aVertexColour");
123 gl.enableVertexAttribArray(this.vertexColour);
125 this.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix");
126 this.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix");
128 function load_shader(gl, id)
130 var script = document.getElementById(id);
131 if (!script) return null;
134 var child = script.firstChild;
137 if (child.nodeType == 3) str += child.textContent;
138 child = child.nextSibling;
142 var common = "x-shader/x-";
143 if (script.type == common + "fragment") shader = gl.createShader(gl.FRAGMENT_SHADER);
144 else if (script.type == common + "vertex") shader = gl.createShader(gl.VERTEX_SHADER);
147 gl.shaderSource(shader, str);
148 gl.compileShader(shader);
150 if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
152 alert(gl.getShaderInfoLog(shader));
161 function Box(size, context)
164 this.size = size || 1;
166 this.positionBuffer = gl.createBuffer();
167 gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
168 var vertices = [1.0, 1.0, 0.0,
172 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
173 this.positionBuffer.itemSize = 3;
174 this.positionBuffer.numItems = 4;
176 this.colourBuffer = gl.createBuffer();
177 gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
178 var colours = [1.0, 0.0, 0.0, .5,
183 for (var i = 0; i < 4; i++)
185 // colours = colours.concat([0.5, 0.5, 1.0, 1.0]);
187 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colours), gl.STATIC_DRAW);
188 this.colourBuffer.itemSize = 4;
189 this.colourBuffer.numItems = 4;