Added per-vertex positional lighting.
authorEugen Sawin <sawine@me73.com>
Wed, 27 Apr 2011 14:14:07 +0200
changeset 3095688249c40c
parent 29 99652d00489f
child 31 744b427d1379
Added per-vertex positional lighting.
machine.html
scripts/machine.js
scripts/renderer.js
     1.1 --- a/machine.html	Wed Apr 27 13:35:56 2011 +0200
     1.2 +++ b/machine.html	Wed Apr 27 14:14:07 2011 +0200
     1.3 @@ -32,6 +32,8 @@
     1.4      uniform vec3 uAmbientColour;
     1.5      uniform vec3 uLightingDirection;
     1.6      uniform vec3 uDirectionalColour;
     1.7 +    uniform vec3 uPointLightingLocation;
     1.8 +    uniform vec3 uPointLightingColour;
     1.9  
    1.10      uniform bool uUseLighting;
    1.11  
    1.12 @@ -40,7 +42,8 @@
    1.13   
    1.14      void main(void) 
    1.15      {
    1.16 -        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
    1.17 +        vec4 mvPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
    1.18 +        gl_Position = uPMatrix * mvPosition;
    1.19          vColour = aVertexColour;
    1.20  
    1.21          if (!uUseLighting)
    1.22 @@ -49,9 +52,14 @@
    1.23          }
    1.24          else
    1.25          {
    1.26 +            //vec3 transformedNormal = uNMatrix * aVertexNormal;
    1.27 +            //float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0);
    1.28 +            //vLightWeighting = uAmbientColour + uDirectionalColour * directionalLightWeighting;
    1.29 +
    1.30 +            vec3 lightDirection = normalize(uPointLightingLocation - mvPosition.xyz);
    1.31              vec3 transformedNormal = uNMatrix * aVertexNormal;
    1.32 -            float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0);
    1.33 -            vLightWeighting = uAmbientColour + uDirectionalColour * directionalLightWeighting;
    1.34 +            float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0);
    1.35 +            vLightWeighting = uAmbientColour + uPointLightingColour * directionalLightWeighting;
    1.36          }
    1.37      }
    1.38  </script>
     2.1 --- a/scripts/machine.js	Wed Apr 27 13:35:56 2011 +0200
     2.2 +++ b/scripts/machine.js	Wed Apr 27 14:14:07 2011 +0200
     2.3 @@ -22,7 +22,7 @@
     2.4      context.expand();
     2.5      var gl = context.gl;
     2.6      var scene = createScene(context);   
     2.7 -    gl.clearColor(0.0, 0.0, 0.0, 1.0);
     2.8 +    gl.clearColor(0.05, 0.05, 0.05, 1.0);
     2.9      gl.enable(gl.DEPTH_TEST);
    2.10      machine = new Machine(scene);       
    2.11      camera = new Camera(cameraSpeed, [0, 5, 20]);
    2.12 @@ -41,14 +41,15 @@
    2.13  
    2.14  function createScene(context)
    2.15  {
    2.16 -    var scene = new Node([0, 0, 0], [0, 0, 0], new Cube(1, context));
    2.17 -    var dim = 100;
    2.18 +    var cube = new Cube(1, context);
    2.19 +    var scene = new Node([0, 0, 0], [0, 0, 0], cube);
    2.20 +    var dim = 10;
    2.21      var d = 4;
    2.22      for (var x = 0; x < dim; x += 1) {
    2.23  	for (var y = 0; y < dim; y += 1) {
    2.24  	    for (var z = 0; z < dim; z += 1) {		
    2.25  		var pos = [x * d, y * d, -z * d];
    2.26 -		scene.children.push(new Node(pos, [0, 0, 0], new Cube(1, context)));
    2.27 +		scene.children.push(new Node(pos, [0, 0, 0], cube));
    2.28  	    }
    2.29  	}
    2.30      }
     3.1 --- a/scripts/renderer.js	Wed Apr 27 13:35:56 2011 +0200
     3.2 +++ b/scripts/renderer.js	Wed Apr 27 14:14:07 2011 +0200
     3.3 @@ -73,6 +73,8 @@
     3.4      this.ambientColourUniform = gl.getUniformLocation(this.program, "uAmbientColour");
     3.5      this.lightingDirectionUniform = gl.getUniformLocation(this.program, "uLightingDirection");
     3.6      this.directionalColourUniform = gl.getUniformLocation(this.program, "uDirectionalColour");
     3.7 +    this.pointLightingLocationUniform = gl.getUniformLocation(this.program, "uPointLightingLocation");
     3.8 +    this.pointLightingColourUniform = gl.getUniformLocation(this.program, "uPointLightingColour");
     3.9      
    3.10      function loadShader(gl, id)
    3.11      {
    3.12 @@ -161,19 +163,23 @@
    3.13     
    3.14  	    if (lighting)
    3.15  	    {   
    3.16 -		var uni = 0.02;
    3.17 -		var ambient = {"r": uni, "g": uni, "b": uni};
    3.18 -		gl.uniform3f(shader.ambientColourUniform, ambient.r, ambient.g, ambient.b);
    3.19 +		var uni = 0.00;
    3.20 +		gl.uniform3f(shader.ambientColourUniform, uni, uni, uni);
    3.21  
    3.22 -		var lightingDir = vec3.create();
    3.23 -		vec3.add(lightingDir, [0.5, -0.5, 0.0]);
    3.24 -		vec3.normalize(lightingDir);
    3.25 -		vec3.scale(lightingDir, -1);     
    3.26 -		gl.uniform3fv(shader.lightingDirectionUniform, lightingDir);
    3.27 +		var pos = node.pos;
    3.28 +		gl.uniform3f(shader.pointLightingLocationUniform, pos[0], pos[1], pos[2]);
    3.29 +		uni = .1;
    3.30 +		gl.uniform3f(shader.pointLightingColourUniform, uni, uni, uni);
    3.31 +
    3.32 +		// var lightingDir = vec3.create();
    3.33 +		// vec3.add(lightingDir, [0.5, -0.5, 0.0]);
    3.34 +		// vec3.normalize(lightingDir);
    3.35 +		// vec3.scale(lightingDir, -1);     
    3.36 +		// gl.uniform3fv(shader.lightingDirectionUniform, lightingDir);
    3.37  	
    3.38 -		uni = 0.9;
    3.39 -		var directional =  {"r": uni, "g": uni, "b": uni};
    3.40 -		gl.uniform3f(shader.directionalColourUniform, directional.r, directional.g, directional.b);
    3.41 +		// uni = 0.9;
    3.42 +		// var directional =  {"r": uni, "g": uni, "b": uni};
    3.43 +		// gl.uniform3f(shader.directionalColourUniform, directional.r, directional.g, directional.b);
    3.44  	    }
    3.45     
    3.46  	    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, object.indexBuffer);