Added specular lighting.
authorEugen Sawin <sawine@me73.com>
Wed, 27 Apr 2011 15:10:42 +0200
changeset 332af408534f28
parent 32 1b4c3fcff1b0
child 34 f471c01dca99
Added specular lighting.
machine.html
scripts/machine.js
scripts/renderer.js
     1.1 --- a/machine.html	Wed Apr 27 14:45:41 2011 +0200
     1.2 +++ b/machine.html	Wed Apr 27 15:10:42 2011 +0200
     1.3 @@ -15,11 +15,14 @@
     1.4      varying vec3 vTransformedNormal;
     1.5      varying vec4 vPosition;
     1.6  
     1.7 +    uniform float uMaterialShininess;
     1.8 +
     1.9      uniform bool uUseLighting;
    1.10     
    1.11      uniform vec3 uAmbientColour;
    1.12      uniform vec3 uPointLightingLocation;
    1.13 -    uniform vec3 uPointLightingColour;
    1.14 +    uniform vec3 uPointLightingSpecularColour;
    1.15 +    uniform vec3 uPointLightingDiffuseColour;
    1.16   
    1.17      void main(void)
    1.18      {
    1.19 @@ -31,8 +34,13 @@
    1.20          else
    1.21          {
    1.22              vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
    1.23 -            float directionalLightWeighting = max(dot(normalize(vTransformedNormal), lightDirection), 0.0);
    1.24 -            lightWeighting = uAmbientColour + uPointLightingColour * directionalLightWeighting;
    1.25 +            vec3 normal = normalize(vTransformedNormal);
    1.26 +            vec3 eyeDirection = normalize(-vPosition.xyz);
    1.27 +            vec3 reflectionDirection = reflect(-lightDirection, normal);
    1.28 +            float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);
    1.29 +            float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
    1.30 +            lightWeighting = uAmbientColour + uPointLightingSpecularColour * specularLightWeighting
    1.31 +                             + uPointLightingDiffuseColour * diffuseLightWeighting;
    1.32          }     
    1.33          gl_FragColor = vec4(vColour.rgb * lightWeighting, vColour.a);
    1.34      }
     2.1 --- a/scripts/machine.js	Wed Apr 27 14:45:41 2011 +0200
     2.2 +++ b/scripts/machine.js	Wed Apr 27 15:10:42 2011 +0200
     2.3 @@ -41,10 +41,11 @@
     2.4  
     2.5  function createScene(context)
     2.6  {
     2.7 -    var cube = new Cube(1, context);
     2.8 +    var size = 1;
     2.9 +    var cube = new Cube(size, context);
    2.10      var scene = new Node([0, 0, 0], [0, 0, 0], cube);
    2.11      var dim = 10;
    2.12 -    var d = 4;
    2.13 +    var d = size * 4;
    2.14      for (var x = 0; x < dim; x += 1) {
    2.15  	for (var y = 0; y < dim; y += 1) {
    2.16  	    for (var z = 0; z < dim; z += 1) {		
     3.1 --- a/scripts/renderer.js	Wed Apr 27 14:45:41 2011 +0200
     3.2 +++ b/scripts/renderer.js	Wed Apr 27 15:10:42 2011 +0200
     3.3 @@ -68,13 +68,14 @@
     3.4      this.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix");
     3.5      this.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix");    
     3.6      this.nMatrixUniform = gl.getUniformLocation(this.program, "uNMatrix");
     3.7 -    //this.samplerUniform = gl.getUniformLocation(this.program, "uSampler");
     3.8 +    this.materialShininessUniform = gl.getUniformLocation(this.program, "uMaterialShininess");
     3.9      this.useLightingUniform = gl.getUniformLocation(this.program, "uUseLighting");
    3.10      this.ambientColourUniform = gl.getUniformLocation(this.program, "uAmbientColour");
    3.11      this.lightingDirectionUniform = gl.getUniformLocation(this.program, "uLightingDirection");
    3.12      this.directionalColourUniform = gl.getUniformLocation(this.program, "uDirectionalColour");
    3.13      this.pointLightingLocationUniform = gl.getUniformLocation(this.program, "uPointLightingLocation");
    3.14 -    this.pointLightingColourUniform = gl.getUniformLocation(this.program, "uPointLightingColour");
    3.15 +    this.pointLightingSpecularColourUniform = gl.getUniformLocation(this.program, "uPointLightingSpecularColour");
    3.16 +    this.pointLightingDiffuseColourUniform = gl.getUniformLocation(this.program, "uPointLightingDiffuseColour");
    3.17      
    3.18      function loadShader(gl, id)
    3.19      {
    3.20 @@ -130,6 +131,7 @@
    3.21  
    3.22      var lighting = true;
    3.23      gl.uniform1i(shader.useLightingUniform, lighting);
    3.24 +    gl.uniform1f(shader.materialShininessUniform, 25.0);
    3.25     
    3.26      if (lighting)
    3.27      {   
    3.28 @@ -138,8 +140,10 @@
    3.29  
    3.30  	var pos = [0, 0, 0];
    3.31  	gl.uniform3f(shader.pointLightingLocationUniform, pos[0], pos[1], pos[2]);
    3.32 -	uni = .3;
    3.33 -	gl.uniform3f(shader.pointLightingColourUniform, uni, uni, uni);
    3.34 +	uni = .5;
    3.35 +	gl.uniform3f(shader.pointLightingSpecularColourUniform, uni, uni, uni);
    3.36 +	uni = .1;
    3.37 +	gl.uniform3f(shader.pointLightingDiffuseColourUniform, uni, uni, uni);
    3.38  
    3.39  	// var lightingDir = vec3.create();
    3.40  	// vec3.add(lightingDir, [0.5, -0.5, 0.0]);