# HG changeset patch # User Eugen Sawin # Date 1303906447 -7200 # Node ID 95688249c40c38600dfc1085b925206ace9b76d5 # Parent 99652d00489ff3bcd3cdd533a90f6b8878a5dec9 Added per-vertex positional lighting. diff -r 99652d00489f -r 95688249c40c machine.html --- a/machine.html Wed Apr 27 13:35:56 2011 +0200 +++ b/machine.html Wed Apr 27 14:14:07 2011 +0200 @@ -32,6 +32,8 @@ uniform vec3 uAmbientColour; uniform vec3 uLightingDirection; uniform vec3 uDirectionalColour; + uniform vec3 uPointLightingLocation; + uniform vec3 uPointLightingColour; uniform bool uUseLighting; @@ -40,7 +42,8 @@ void main(void) { - gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); + vec4 mvPosition = uMVMatrix * vec4(aVertexPosition, 1.0); + gl_Position = uPMatrix * mvPosition; vColour = aVertexColour; if (!uUseLighting) @@ -49,9 +52,14 @@ } else { + //vec3 transformedNormal = uNMatrix * aVertexNormal; + //float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0); + //vLightWeighting = uAmbientColour + uDirectionalColour * directionalLightWeighting; + + vec3 lightDirection = normalize(uPointLightingLocation - mvPosition.xyz); vec3 transformedNormal = uNMatrix * aVertexNormal; - float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0); - vLightWeighting = uAmbientColour + uDirectionalColour * directionalLightWeighting; + float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0); + vLightWeighting = uAmbientColour + uPointLightingColour * directionalLightWeighting; } } diff -r 99652d00489f -r 95688249c40c scripts/machine.js --- a/scripts/machine.js Wed Apr 27 13:35:56 2011 +0200 +++ b/scripts/machine.js Wed Apr 27 14:14:07 2011 +0200 @@ -22,7 +22,7 @@ context.expand(); var gl = context.gl; var scene = createScene(context); - gl.clearColor(0.0, 0.0, 0.0, 1.0); + gl.clearColor(0.05, 0.05, 0.05, 1.0); gl.enable(gl.DEPTH_TEST); machine = new Machine(scene); camera = new Camera(cameraSpeed, [0, 5, 20]); @@ -41,14 +41,15 @@ function createScene(context) { - var scene = new Node([0, 0, 0], [0, 0, 0], new Cube(1, context)); - var dim = 100; + var cube = new Cube(1, context); + var scene = new Node([0, 0, 0], [0, 0, 0], cube); + var dim = 10; var d = 4; for (var x = 0; x < dim; x += 1) { for (var y = 0; y < dim; y += 1) { for (var z = 0; z < dim; z += 1) { var pos = [x * d, y * d, -z * d]; - scene.children.push(new Node(pos, [0, 0, 0], new Cube(1, context))); + scene.children.push(new Node(pos, [0, 0, 0], cube)); } } } diff -r 99652d00489f -r 95688249c40c scripts/renderer.js --- a/scripts/renderer.js Wed Apr 27 13:35:56 2011 +0200 +++ b/scripts/renderer.js Wed Apr 27 14:14:07 2011 +0200 @@ -73,6 +73,8 @@ this.ambientColourUniform = gl.getUniformLocation(this.program, "uAmbientColour"); this.lightingDirectionUniform = gl.getUniformLocation(this.program, "uLightingDirection"); this.directionalColourUniform = gl.getUniformLocation(this.program, "uDirectionalColour"); + this.pointLightingLocationUniform = gl.getUniformLocation(this.program, "uPointLightingLocation"); + this.pointLightingColourUniform = gl.getUniformLocation(this.program, "uPointLightingColour"); function loadShader(gl, id) { @@ -161,19 +163,23 @@ if (lighting) { - var uni = 0.02; - var ambient = {"r": uni, "g": uni, "b": uni}; - gl.uniform3f(shader.ambientColourUniform, ambient.r, ambient.g, ambient.b); + var uni = 0.00; + gl.uniform3f(shader.ambientColourUniform, uni, uni, uni); - var lightingDir = vec3.create(); - vec3.add(lightingDir, [0.5, -0.5, 0.0]); - vec3.normalize(lightingDir); - vec3.scale(lightingDir, -1); - gl.uniform3fv(shader.lightingDirectionUniform, lightingDir); + var pos = node.pos; + gl.uniform3f(shader.pointLightingLocationUniform, pos[0], pos[1], pos[2]); + uni = .1; + gl.uniform3f(shader.pointLightingColourUniform, uni, uni, uni); + + // var lightingDir = vec3.create(); + // vec3.add(lightingDir, [0.5, -0.5, 0.0]); + // vec3.normalize(lightingDir); + // vec3.scale(lightingDir, -1); + // gl.uniform3fv(shader.lightingDirectionUniform, lightingDir); - uni = 0.9; - var directional = {"r": uni, "g": uni, "b": uni}; - gl.uniform3f(shader.directionalColourUniform, directional.r, directional.g, directional.b); + // uni = 0.9; + // var directional = {"r": uni, "g": uni, "b": uni}; + // gl.uniform3f(shader.directionalColourUniform, directional.r, directional.g, directional.b); } gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, object.indexBuffer);