Switched to per-fragment lighting.
1.1 --- a/machine.html Wed Apr 27 14:14:07 2011 +0200
1.2 +++ b/machine.html Wed Apr 27 14:41:04 2011 +0200
1.3 @@ -12,11 +12,29 @@
1.4 #endif
1.5
1.6 varying vec4 vColour;
1.7 - varying vec3 vLightWeighting;
1.8 + varying vec3 vTransformedNormal;
1.9 + varying vec4 vPosition;
1.10 +
1.11 + uniform bool uUseLighting;
1.12 +
1.13 + uniform vec3 uAmbientColour;
1.14 + uniform vec3 uPointLightingLocation;
1.15 + uniform vec3 uPointLightingColour;
1.16
1.17 - void main(void)
1.18 + void main(void)
1.19 {
1.20 - gl_FragColor = vec4(vColour.rgb * vLightWeighting, vColour.a);
1.21 + vec3 lightWeighting;
1.22 + if (!uUseLighting)
1.23 + {
1.24 + lightWeighting = vec3(1.0, 1.0, 1.0);
1.25 + }
1.26 + else
1.27 + {
1.28 + vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
1.29 + float directionalLightWeighting = max(dot(normalize(vTransformedNormal), lightDirection), 0.0);
1.30 + lightWeighting = uAmbientColour + uPointLightingColour * directionalLightWeighting;
1.31 + }
1.32 + gl_FragColor = vec4(vColour.rgb * lightWeighting, vColour.a);
1.33 }
1.34 </script>
1.35
1.36 @@ -29,38 +47,16 @@
1.37 uniform mat4 uPMatrix;
1.38 uniform mat3 uNMatrix;
1.39
1.40 - uniform vec3 uAmbientColour;
1.41 - uniform vec3 uLightingDirection;
1.42 - uniform vec3 uDirectionalColour;
1.43 - uniform vec3 uPointLightingLocation;
1.44 - uniform vec3 uPointLightingColour;
1.45 -
1.46 - uniform bool uUseLighting;
1.47 -
1.48 varying vec4 vColour;
1.49 - varying vec3 vLightWeighting;
1.50 + varying vec3 vTransformedNormal;
1.51 + varying vec4 vPosition;
1.52
1.53 void main(void)
1.54 {
1.55 - vec4 mvPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
1.56 - gl_Position = uPMatrix * mvPosition;
1.57 + vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
1.58 + gl_Position = uPMatrix * vPosition;
1.59 vColour = aVertexColour;
1.60 -
1.61 - if (!uUseLighting)
1.62 - {
1.63 - vLightWeighting = vec3(1.0, 1.0, 1.0);
1.64 - }
1.65 - else
1.66 - {
1.67 - //vec3 transformedNormal = uNMatrix * aVertexNormal;
1.68 - //float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0);
1.69 - //vLightWeighting = uAmbientColour + uDirectionalColour * directionalLightWeighting;
1.70 -
1.71 - vec3 lightDirection = normalize(uPointLightingLocation - mvPosition.xyz);
1.72 - vec3 transformedNormal = uNMatrix * aVertexNormal;
1.73 - float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0);
1.74 - vLightWeighting = uAmbientColour + uPointLightingColour * directionalLightWeighting;
1.75 - }
1.76 + vTransformedNormal = uNMatrix * aVertexNormal;
1.77 }
1.78 </script>
1.79 </head>
2.1 --- a/scripts/renderer.js Wed Apr 27 14:14:07 2011 +0200
2.2 +++ b/scripts/renderer.js Wed Apr 27 14:41:04 2011 +0200
2.3 @@ -117,7 +117,8 @@
2.4 }
2.5 Renderer.prototype.update = function(scene)
2.6 {
2.7 - var gl = this.context.gl;
2.8 + var gl = this.context.gl;
2.9 + var shader = this.context.shader;
2.10 var viewport = this.context.viewport;
2.11 var mvMatrix = this.context.mvMatrix;
2.12 var pMatrix = this.context.pMatrix;
2.13 @@ -127,6 +128,30 @@
2.14 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
2.15 mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix);
2.16
2.17 + var lighting = true;
2.18 + gl.uniform1i(shader.useLightingUniform, lighting);
2.19 +
2.20 + if (lighting)
2.21 + {
2.22 + var uni = 0.0;
2.23 + gl.uniform3f(shader.ambientColourUniform, uni, uni, uni);
2.24 +
2.25 + var pos = [0, 0, 0];
2.26 + gl.uniform3f(shader.pointLightingLocationUniform, pos[0], pos[1], pos[2]);
2.27 + uni = .3;
2.28 + gl.uniform3f(shader.pointLightingColourUniform, uni, uni, uni);
2.29 +
2.30 + // var lightingDir = vec3.create();
2.31 + // vec3.add(lightingDir, [0.5, -0.5, 0.0]);
2.32 + // vec3.normalize(lightingDir);
2.33 + // vec3.scale(lightingDir, -1);
2.34 + // gl.uniform3fv(shader.lightingDirectionUniform, lightingDir);
2.35 +
2.36 + // uni = 0.9;
2.37 + // var directional = {"r": uni, "g": uni, "b": uni};
2.38 + // gl.uniform3f(shader.directionalColourUniform, directional.r, directional.g, directional.b);
2.39 + }
2.40 +
2.41 mat4.identity(mvMatrix);
2.42 mat4.multiply(mvMatrix, camera.matrix);
2.43
2.44 @@ -157,30 +182,6 @@
2.45
2.46 gl.bindBuffer(gl.ARRAY_BUFFER, object.colourBuffer);
2.47 gl.vertexAttribPointer(shader.vertexColour, object.colourBuffer.itemSize, gl.FLOAT, false, 0, 0);
2.48 -
2.49 - var lighting = true;
2.50 - gl.uniform1i(shader.useLightingUniform, lighting);
2.51 -
2.52 - if (lighting)
2.53 - {
2.54 - var uni = 0.00;
2.55 - gl.uniform3f(shader.ambientColourUniform, uni, uni, uni);
2.56 -
2.57 - var pos = node.pos;
2.58 - gl.uniform3f(shader.pointLightingLocationUniform, pos[0], pos[1], pos[2]);
2.59 - uni = .1;
2.60 - gl.uniform3f(shader.pointLightingColourUniform, uni, uni, uni);
2.61 -
2.62 - // var lightingDir = vec3.create();
2.63 - // vec3.add(lightingDir, [0.5, -0.5, 0.0]);
2.64 - // vec3.normalize(lightingDir);
2.65 - // vec3.scale(lightingDir, -1);
2.66 - // gl.uniform3fv(shader.lightingDirectionUniform, lightingDir);
2.67 -
2.68 - // uni = 0.9;
2.69 - // var directional = {"r": uni, "g": uni, "b": uni};
2.70 - // gl.uniform3f(shader.directionalColourUniform, directional.r, directional.g, directional.b);
2.71 - }
2.72
2.73 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, object.indexBuffer);
2.74 this.context.updateMatrixUniforms();