Broken light.
1.1 --- a/machine.html Mon Apr 25 23:30:26 2011 +0200
1.2 +++ b/machine.html Tue Apr 26 01:48:56 2011 +0200
1.3 @@ -9,26 +9,47 @@
1.4 #endif
1.5
1.6 varying vec4 vColour;
1.7 + varying vec3 vLightWeighting;
1.8
1.9 void main(void)
1.10 {
1.11 - gl_FragColor = vColour;
1.12 + gl_FragColor = vec4(vColour.rgb * vLightWeighting, vColour.a);
1.13 }
1.14 </script>
1.15
1.16 <script id="vertex-shader" type="x-shader/x-vertex">
1.17 attribute vec3 aVertexPosition;
1.18 attribute vec4 aVertexColour;
1.19 + attribute vec3 aVertexNormal;
1.20
1.21 uniform mat4 uMVMatrix;
1.22 uniform mat4 uPMatrix;
1.23 + uniform mat3 uNMatrix;
1.24 +
1.25 + uniform vec3 uAmbientColour;
1.26 +
1.27 + uniform vec3 uLightingDirection;
1.28 + uniform vec3 uDirectionalColour;
1.29 +
1.30 + uniform bool uUseLighting;
1.31
1.32 varying vec4 vColour;
1.33 + varying vec3 vLightWeighting;
1.34
1.35 void main(void)
1.36 {
1.37 gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
1.38 vColour = aVertexColour;
1.39 + if (!uUseLighting)
1.40 + {
1.41 + vLightWeighting = vec3(1.0, 1.0, 1.0);
1.42 + }
1.43 + else
1.44 + {
1.45 + vec3 transformedNormal = uNMatrix * aVertexNormal;
1.46 + float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0);
1.47 + vLightWeighting = uAmbientColour + uDirectionalColour * directionalLightWeighting;
1.48 + }
1.49 }
1.50 </script>
1.51 </head>
2.1 --- a/scripts/boot.js Mon Apr 25 23:30:26 2011 +0200
2.2 +++ b/scripts/boot.js Tue Apr 26 01:48:56 2011 +0200
2.3 @@ -10,7 +10,6 @@
2.4 element.setAttribute("href", url);
2.5 break;
2.6 case "js":
2.7 - alert(url);
2.8 element = document.createElement("script");
2.9 element.setAttribute("language", "javascript");
2.10 element.setAttribute("src", url);
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/scripts/cube.js Tue Apr 26 01:48:56 2011 +0200
3.3 @@ -0,0 +1,118 @@
3.4 +function Cube(size, context)
3.5 +{
3.6 + var gl = context.gl;
3.7 + this.size = size || 1;
3.8 + this.rotation = {'x': 0.0, 'y': 0.0, 'z': 0.0};
3.9 +
3.10 + this.positionBuffer = gl.createBuffer();
3.11 + gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
3.12 + var vertices = [
3.13 + // Front face
3.14 + -1.0, -1.0, 1.0,
3.15 + 1.0, -1.0, 1.0,
3.16 + 1.0, 1.0, 1.0,
3.17 + -1.0, 1.0, 1.0,
3.18 +
3.19 + // Back face
3.20 + -1.0, -1.0, -1.0,
3.21 + -1.0, 1.0, -1.0,
3.22 + 1.0, 1.0, -1.0,
3.23 + 1.0, -1.0, -1.0,
3.24 +
3.25 + // Top face
3.26 + -1.0, 1.0, -1.0,
3.27 + -1.0, 1.0, 1.0,
3.28 + 1.0, 1.0, 1.0,
3.29 + 1.0, 1.0, -1.0,
3.30 +
3.31 + // Bottom face
3.32 + -1.0, -1.0, -1.0,
3.33 + 1.0, -1.0, -1.0,
3.34 + 1.0, -1.0, 1.0,
3.35 + -1.0, -1.0, 1.0,
3.36 +
3.37 + // Right face
3.38 + 1.0, -1.0, -1.0,
3.39 + 1.0, 1.0, -1.0,
3.40 + 1.0, 1.0, 1.0,
3.41 + 1.0, -1.0, 1.0,
3.42 +
3.43 + // Left face
3.44 + -1.0, -1.0, -1.0,
3.45 + -1.0, -1.0, 1.0,
3.46 + -1.0, 1.0, 1.0,
3.47 + -1.0, 1.0, -1.0];
3.48 + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
3.49 + this.positionBuffer.itemSize = 3;
3.50 + this.positionBuffer.numItems = 24;
3.51 +
3.52 + this.colourBuffer = gl.createBuffer();
3.53 + gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
3.54 + var alpha = 1.0;
3.55 + var colours = [[1.0, 0.0, 0.0, alpha],
3.56 + [0.0, 1.0, 0.0, alpha],
3.57 + [0.0, 0.0, 1.0, alpha],
3.58 + [1.0, 0.0, 1.0, alpha],
3.59 + [1.0, 1.0, 0.0, alpha],
3.60 + [0.0, 1.0, 1.0, alpha]];
3.61 + var unpackedColours = [];
3.62 + for (var i in colours)
3.63 + {
3.64 + var colour = colours[i];
3.65 + for (var j = 0; j < 4; j++)
3.66 + {
3.67 + unpackedColours = unpackedColours.concat(colour);
3.68 + }
3.69 + //colours = colours.concat([0.5, 0.5, 1.0, 1.0]);
3.70 + }
3.71 + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColours), gl.STATIC_DRAW);
3.72 + this.colourBuffer.itemSize = 4;
3.73 + this.colourBuffer.numItems = 24;
3.74 +
3.75 + this.indexBuffer = gl.createBuffer();
3.76 + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
3.77 + var indices = [0, 1, 2, 0, 2, 3,
3.78 + 4, 5, 6, 4, 6, 7,
3.79 + 8, 9, 10, 8, 10, 11,
3.80 + 12, 13, 14, 12, 14, 15,
3.81 + 16, 17, 18, 16, 18, 19,
3.82 + 20, 21, 22, 20, 22, 23];
3.83 + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
3.84 + this.indexBuffer.itemSize = 1;
3.85 + this.indexBuffer.numItems = 36;
3.86 +
3.87 + this.normalBuffer = gl.createBuffer();
3.88 + gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
3.89 + var normals = [// font face
3.90 + 0.0, 0.0, 1.0,
3.91 + 0.0, 0.0, 1.0,
3.92 + 0.0, 0.0, 1.0,
3.93 + // back face
3.94 + 0.0, 0.0, -1.0,
3.95 + 0.0, 0.0, -1.0,
3.96 + 0.0, 0.0, -1.0,
3.97 + 0.0, 0.0, -1.0,
3.98 + // top face
3.99 + 0.0, 1.0, 0.0,
3.100 + 0.0, 1.0, 0.0,
3.101 + 0.0, 1.0, 0.0,
3.102 + 0.0, 1.0, 0.0,
3.103 + // bottom face
3.104 + 0.0, -1.0, 0.0,
3.105 + 0.0, -1.0, 0.0,
3.106 + 0.0, -1.0, 0.0,
3.107 + 0.0, -1.0, 0.0,
3.108 + // right face
3.109 + 1.0, 0.0, 0.0,
3.110 + 1.0, 0.0, 0.0,
3.111 + 1.0, 0.0, 0.0,
3.112 + 1.0, 0.0, 0.0,
3.113 + // left face
3.114 + -1.0, 0.0, 0.0,
3.115 + -1.0, 0.0, 0.0,
3.116 + -1.0, 0.0, 0.0,
3.117 + -1.0, 0.0, 0.0];
3.118 + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
3.119 + this.normalBuffer.itemSize = 3;
3.120 + this.normalBuffer.numItems = 24;
3.121 +}
3.122 \ No newline at end of file
4.1 --- a/scripts/machine.js Mon Apr 25 23:30:26 2011 +0200
4.2 +++ b/scripts/machine.js Tue Apr 26 01:48:56 2011 +0200
4.3 @@ -10,6 +10,8 @@
4.4 var mouseActionMap = {"pitch": [[true, false, false], pitchCamera],
4.5 "yaw": [[true, false, false], yawCamera],
4.6 "wheel": zoomCamera};
4.7 +include("scripts/cube.js");
4.8 +include("scripts/renderer.js");
4.9
4.10 function main()
4.11 {
4.12 @@ -244,58 +246,7 @@
4.13 }
4.14 }
4.15
4.16 -function Renderer(camera, context)
4.17 -{
4.18 - this.camera = camera;
4.19 - this.context = context;
4.20 - this.gl = context.gl;
4.21 - this.matrixStack = [];
4.22 -}
4.23 -Renderer.prototype.update = function(scene)
4.24 -{
4.25 - var gl = this.context.gl;
4.26 - var viewport = this.context.viewport;
4.27 - var shader = this.context.shader;
4.28 - var mvMatrix = this.context.mvMatrix;
4.29 - var pMatrix = this.context.pMatrix;
4.30 - var camera = this.camera;
4.31 - camera.update();
4.32
4.33 - gl.viewport(0, 0, viewport.width, viewport.height);
4.34 - gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
4.35 - mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix);
4.36 -
4.37 - mat4.identity(mvMatrix);
4.38 - mat4.multiply(mvMatrix, camera.matrix);
4.39 -
4.40 - //this.pushMatrix(mvMatrix);
4.41 -
4.42 - gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer);
4.43 - gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
4.44 -
4.45 - gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer);
4.46 - gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0);
4.47 -
4.48 - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, scene.indexBuffer);
4.49 -
4.50 - this.context.updateMatrixUniforms();
4.51 - gl.drawElements(gl.TRIANGLES, scene.indexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
4.52 - //gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems);
4.53 -
4.54 - //mvMatrix = this.popMatrix();
4.55 -
4.56 - if (this.next) this.next.draw(scene);
4.57 -}
4.58 -Renderer.prototype.pushMatrix = function(matrix)
4.59 -{
4.60 - var copy = mat4.create();
4.61 - mat4.set(matrix, copy);
4.62 - this.matrixStack.push(copy);
4.63 -}
4.64 -Renderer.prototype.popMatrix = function()
4.65 -{
4.66 - if (this.matrixStack.length > 0) return this.matrixStack.pop();
4.67 -}
4.68
4.69 function Machine(scene)
4.70 {
4.71 @@ -312,184 +263,10 @@
4.72 this.lastUpdate = time;
4.73 }
4.74
4.75 -function Context(canvas)
4.76 -{
4.77 - this.canvas = canvas;
4.78 - try
4.79 - {
4.80 - this.gl = canvas.getContext("experimental-webgl");
4.81 - this.viewport = {'width': canvas.width,
4.82 - 'height': canvas.height};
4.83 - }
4.84 - catch(e)
4.85 - {
4.86 - alert(e);
4.87 - }
4.88 - if (!this.gl) alert("Failed: WebGL init.");
4.89 - this.mvMatrix = mat4.create();
4.90 - this.pMatrix = mat4.create();
4.91 - this.shader = new Shader(this);
4.92 -}
4.93 -Context.prototype.updateMatrixUniforms = function()
4.94 -{
4.95 - var gl = this.gl;
4.96 - var program = this.shader;
4.97 - var pMatrix = this.pMatrix;
4.98 - var mvMatrix = this.mvMatrix;
4.99 - gl.uniformMatrix4fv(program.pMatrixUniform, false, pMatrix);
4.100 - gl.uniformMatrix4fv(program.mvMatrixUniform, false, mvMatrix);
4.101 -}
4.102 -Context.prototype.expand = function()
4.103 -{
4.104 - var width = window.innerWidth;
4.105 - var height = window.innerHeight;
4.106 - this.canvas.width = width;
4.107 - this.canvas.height = height;
4.108 - this.viewport.width = width;
4.109 - this.viewport.height = height;
4.110 -}
4.111
4.112 -function Shader(context)
4.113 -{
4.114 - var gl = context.gl;
4.115 - var fragment = loadShader(gl, "fragment-shader");
4.116 - var vertex = loadShader(gl, "vertex-shader");
4.117 - this.program = gl.createProgram();
4.118 - gl.attachShader(this.program, vertex);
4.119 - gl.attachShader(this.program, fragment);
4.120 - gl.linkProgram(this.program);
4.121
4.122 - if (!gl.getProgramParameter(this.program, gl.LINK_STATUS))
4.123 - {
4.124 - alert("Failed: Shader init.");
4.125 - }
4.126
4.127 - gl.useProgram(this.program);
4.128 - this.vertexPosition = gl.getAttribLocation(this.program, "aVertexPosition");
4.129 - gl.enableVertexAttribArray(this.vertexPosition);
4.130 -
4.131 - this.vertexColour = gl.getAttribLocation(this.program, "aVertexColour");
4.132 - gl.enableVertexAttribArray(this.vertexColour);
4.133
4.134 - this.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix");
4.135 - this.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix");
4.136 -
4.137 - function loadShader(gl, id)
4.138 - {
4.139 - var script = document.getElementById(id);
4.140 - if (!script) return null;
4.141 -
4.142 - var str = "";
4.143 - var child = script.firstChild;
4.144 - while (child)
4.145 - {
4.146 - if (child.nodeType == 3) str += child.textContent;
4.147 - child = child.nextSibling;
4.148 - }
4.149 -
4.150 - var shader;
4.151 - var common = "x-shader/x-";
4.152 - if (script.type == common + "fragment") shader = gl.createShader(gl.FRAGMENT_SHADER);
4.153 - else if (script.type == common + "vertex") shader = gl.createShader(gl.VERTEX_SHADER);
4.154 - else return null;
4.155 -
4.156 - gl.shaderSource(shader, str);
4.157 - gl.compileShader(shader);
4.158 -
4.159 - if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
4.160 - {
4.161 - alert(gl.getShaderInfoLog(shader));
4.162 - return null;
4.163 - }
4.164 -
4.165 - return shader;
4.166 - }
4.167 -}
4.168 -
4.169 -
4.170 -function Cube(size, context)
4.171 -{
4.172 - var gl = context.gl;
4.173 - this.size = size || 1;
4.174 - this.rotation = {'x': 0.0, 'y': 0.0, 'z': 0.0};
4.175 -
4.176 - this.positionBuffer = gl.createBuffer();
4.177 - gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
4.178 - var vertices = [
4.179 - // Front face
4.180 - -1.0, -1.0, 1.0,
4.181 - 1.0, -1.0, 1.0,
4.182 - 1.0, 1.0, 1.0,
4.183 - -1.0, 1.0, 1.0,
4.184 -
4.185 - // Back face
4.186 - -1.0, -1.0, -1.0,
4.187 - -1.0, 1.0, -1.0,
4.188 - 1.0, 1.0, -1.0,
4.189 - 1.0, -1.0, -1.0,
4.190 -
4.191 - // Top face
4.192 - -1.0, 1.0, -1.0,
4.193 - -1.0, 1.0, 1.0,
4.194 - 1.0, 1.0, 1.0,
4.195 - 1.0, 1.0, -1.0,
4.196 -
4.197 - // Bottom face
4.198 - -1.0, -1.0, -1.0,
4.199 - 1.0, -1.0, -1.0,
4.200 - 1.0, -1.0, 1.0,
4.201 - -1.0, -1.0, 1.0,
4.202 -
4.203 - // Right face
4.204 - 1.0, -1.0, -1.0,
4.205 - 1.0, 1.0, -1.0,
4.206 - 1.0, 1.0, 1.0,
4.207 - 1.0, -1.0, 1.0,
4.208 -
4.209 - // Left face
4.210 - -1.0, -1.0, -1.0,
4.211 - -1.0, -1.0, 1.0,
4.212 - -1.0, 1.0, 1.0,
4.213 - -1.0, 1.0, -1.0];
4.214 - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
4.215 - this.positionBuffer.itemSize = 3;
4.216 - this.positionBuffer.numItems = 24;
4.217 -
4.218 - this.colourBuffer = gl.createBuffer();
4.219 - gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
4.220 - var alpha = 1.0;
4.221 - var colours = [[1.0, 0.0, 0.0, alpha],
4.222 - [0.0, 1.0, 0.0, alpha],
4.223 - [0.0, 0.0, 1.0, alpha],
4.224 - [1.0, 0.0, 1.0, alpha],
4.225 - [1.0, 1.0, 0.0, alpha],
4.226 - [0.0, 1.0, 1.0, alpha]];
4.227 - var unpackedColours = [];
4.228 - for (var i in colours)
4.229 - {
4.230 - var colour = colours[i];
4.231 - for (var j = 0; j < 4; j++)
4.232 - {
4.233 - unpackedColours = unpackedColours.concat(colour);
4.234 - }
4.235 - //colours = colours.concat([0.5, 0.5, 1.0, 1.0]);
4.236 - }
4.237 - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColours), gl.STATIC_DRAW);
4.238 - this.colourBuffer.itemSize = 4;
4.239 - this.colourBuffer.numItems = 24;
4.240 -
4.241 - this.indexBuffer = gl.createBuffer();
4.242 - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
4.243 - var indices = [0, 1, 2, 0, 2, 3,
4.244 - 4, 5, 6, 4, 6, 7,
4.245 - 8, 9, 10, 8, 10, 11,
4.246 - 12, 13, 14, 12, 14, 15,
4.247 - 16, 17, 18, 16, 18, 19,
4.248 - 20, 21, 22, 20, 22, 23];
4.249 - gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
4.250 - this.indexBuffer.itemSize = 1;
4.251 - this.indexBuffer.numItems = 36;
4.252 -}
4.253
4.254 function read(file, handler)
4.255 {
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/scripts/renderer.js Tue Apr 26 01:48:56 2011 +0200
5.3 @@ -0,0 +1,181 @@
5.4 +function Context(canvas)
5.5 +{
5.6 + this.canvas = canvas;
5.7 + try
5.8 + {
5.9 + this.gl = canvas.getContext("experimental-webgl");
5.10 + this.viewport = {'width': canvas.width,
5.11 + 'height': canvas.height};
5.12 + }
5.13 + catch(e)
5.14 + {
5.15 + alert(e);
5.16 + }
5.17 + if (!this.gl) alert("Failed: WebGL init.");
5.18 + this.mvMatrix = mat4.create();
5.19 + this.pMatrix = mat4.create();
5.20 + this.shader = new Shader(this);
5.21 +}
5.22 +Context.prototype.updateMatrixUniforms = function()
5.23 +{
5.24 + var gl = this.gl;
5.25 + var program = this.shader;
5.26 + var pMatrix = this.pMatrix;
5.27 + var mvMatrix = this.mvMatrix;
5.28 + gl.uniformMatrix4fv(program.pMatrixUniform, false, pMatrix);
5.29 + gl.uniformMatrix4fv(program.mvMatrixUniform, false, mvMatrix);
5.30 + var normalMatrix = mat3.create();
5.31 + mat4.toInverseMat3(mvMatrix, normalMatrix);
5.32 + mat3.transpose(normalMatrix);
5.33 + gl.uniformMatrix3fv(program.nMatrixUniform, false, normalMatrix);
5.34 +}
5.35 +Context.prototype.expand = function()
5.36 +{
5.37 + var width = window.innerWidth;
5.38 + var height = window.innerHeight;
5.39 + this.canvas.width = width;
5.40 + this.canvas.height = height;
5.41 + this.viewport.width = width;
5.42 + this.viewport.height = height;
5.43 +}
5.44 +
5.45 +function Shader(context)
5.46 +{
5.47 + var gl = context.gl;
5.48 + var fragment = loadShader(gl, "fragment-shader");
5.49 + var vertex = loadShader(gl, "vertex-shader");
5.50 + this.program = gl.createProgram();
5.51 + gl.attachShader(this.program, vertex);
5.52 + gl.attachShader(this.program, fragment);
5.53 + gl.linkProgram(this.program);
5.54 +
5.55 + if (!gl.getProgramParameter(this.program, gl.LINK_STATUS))
5.56 + {
5.57 + alert("Failed: Shader init.");
5.58 + }
5.59 +
5.60 + gl.useProgram(this.program);
5.61 + this.vertexPosition = gl.getAttribLocation(this.program, "aVertexPosition");
5.62 + gl.enableVertexAttribArray(this.vertexPosition);
5.63 +
5.64 + this.vertexColour = gl.getAttribLocation(this.program, "aVertexColour");
5.65 + gl.enableVertexAttribArray(this.vertexColour);
5.66 +
5.67 + this.vertexNormal = gl.getAttribLocation(this.program, "aVertexNormal");
5.68 + gl.enableVertexAttribArray(this.vertexNormal);
5.69 +
5.70 + this.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix");
5.71 + this.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix");
5.72 + this.nMatrixUniform = gl.getUniformLocation(this.program, "uNMatrix");
5.73 + //this.samplerUniform = gl.getUniformLocation(this.program, "uSampler");
5.74 + this.useLightingUniform = gl.getUniformLocation(this.program, "uUseLighting");
5.75 + this.ambientColourUniform = gl.getUniformLocation(this.program, "uAmbientColour");
5.76 + this.lightingDirectionUniform = gl.getUniformLocation(this.program, "uLightingDirection");
5.77 + this.directionalColourUniform = gl.getUniformLocation(this.program, "uDirectionalColour");
5.78 +
5.79 + function loadShader(gl, id)
5.80 + {
5.81 + var script = document.getElementById(id);
5.82 + if (!script) return null;
5.83 +
5.84 + var str = "";
5.85 + var child = script.firstChild;
5.86 + while (child)
5.87 + {
5.88 + if (child.nodeType == 3) str += child.textContent;
5.89 + child = child.nextSibling;
5.90 + }
5.91 +
5.92 + var shader;
5.93 + var common = "x-shader/x-";
5.94 + if (script.type == common + "fragment") shader = gl.createShader(gl.FRAGMENT_SHADER);
5.95 + else if (script.type == common + "vertex") shader = gl.createShader(gl.VERTEX_SHADER);
5.96 + else return null;
5.97 +
5.98 + gl.shaderSource(shader, str);
5.99 + gl.compileShader(shader);
5.100 +
5.101 + if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
5.102 + {
5.103 + alert(gl.getShaderInfoLog(shader));
5.104 + return null;
5.105 + }
5.106 +
5.107 + return shader;
5.108 + }
5.109 +}
5.110 +
5.111 +function Renderer(camera, context)
5.112 +{
5.113 + this.camera = camera;
5.114 + this.context = context;
5.115 + this.gl = context.gl;
5.116 + this.matrixStack = [];
5.117 +}
5.118 +Renderer.prototype.update = function(scene)
5.119 +{
5.120 + var gl = this.context.gl;
5.121 + var viewport = this.context.viewport;
5.122 + var shader = this.context.shader;
5.123 + var mvMatrix = this.context.mvMatrix;
5.124 + var pMatrix = this.context.pMatrix;
5.125 + var camera = this.camera;
5.126 + camera.update();
5.127 +
5.128 + gl.viewport(0, 0, viewport.width, viewport.height);
5.129 + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
5.130 + mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix);
5.131 +
5.132 + mat4.identity(mvMatrix);
5.133 + mat4.multiply(mvMatrix, camera.matrix);
5.134 +
5.135 + //this.pushMatrix(mvMatrix);
5.136 +
5.137 + gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer);
5.138 + gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
5.139 +
5.140 + gl.bindBuffer(gl.ARRAY_BUFFER, scene.normalBuffer);
5.141 + gl.vertexAttribPointer(shader.vertexNormal, scene.normalBuffer.itemSize, gl.FLOAT, false, 0, 0);
5.142 +
5.143 + gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer);
5.144 + gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0);
5.145 +
5.146 + var lighting = true;
5.147 + gl.uniform1i(shader.useLightingUniform, lighting);
5.148 +
5.149 + if (lighting)
5.150 + {
5.151 + var ambient = {"r": 1.0, "g": 1.0, "b": 1.0};
5.152 + gl.uniform3f(shader.ambientColourUniform, ambient.r, ambient.g, ambient.b);
5.153 +
5.154 + var lightingDir = vec3.create();
5.155 + vec3.add(lightingDir, [0.0, 0.0, -1.0]);
5.156 + vec3.normalize(lightingDir);
5.157 + vec3.scale(lightingDir, -1);
5.158 + gl.uniform3fv(shader.lightingDirectionUniform, lightingDir);
5.159 +
5.160 + var directional = ambient;
5.161 + gl.uniform3f(shader.directionalColourUniform, directional.r, directional.g, directional.b);
5.162 + }
5.163 +
5.164 +
5.165 +
5.166 + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, scene.indexBuffer);
5.167 + this.context.updateMatrixUniforms();
5.168 + gl.drawElements(gl.TRIANGLES, scene.indexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
5.169 + //gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems);
5.170 +
5.171 + //mvMatrix = this.popMatrix();
5.172 +
5.173 + if (this.next) this.next.draw(scene);
5.174 +}
5.175 +Renderer.prototype.pushMatrix = function(matrix)
5.176 +{
5.177 + var copy = mat4.create();
5.178 + mat4.set(matrix, copy);
5.179 + this.matrixStack.push(copy);
5.180 +}
5.181 +Renderer.prototype.popMatrix = function()
5.182 +{
5.183 + if (this.matrixStack.length > 0) return this.matrixStack.pop();
5.184 +}
5.185 \ No newline at end of file