# HG changeset patch # User Eugen Sawin # Date 1303909842 -7200 # Node ID 2af408534f287276a55119ae5c84dfda7177ee06 # Parent 1b4c3fcff1b0a57c029349b48dec4497a8da62ad Added specular lighting. diff -r 1b4c3fcff1b0 -r 2af408534f28 machine.html --- a/machine.html Wed Apr 27 14:45:41 2011 +0200 +++ b/machine.html Wed Apr 27 15:10:42 2011 +0200 @@ -15,11 +15,14 @@ varying vec3 vTransformedNormal; varying vec4 vPosition; + uniform float uMaterialShininess; + uniform bool uUseLighting; uniform vec3 uAmbientColour; uniform vec3 uPointLightingLocation; - uniform vec3 uPointLightingColour; + uniform vec3 uPointLightingSpecularColour; + uniform vec3 uPointLightingDiffuseColour; void main(void) { @@ -31,8 +34,13 @@ else { vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz); - float directionalLightWeighting = max(dot(normalize(vTransformedNormal), lightDirection), 0.0); - lightWeighting = uAmbientColour + uPointLightingColour * directionalLightWeighting; + vec3 normal = normalize(vTransformedNormal); + vec3 eyeDirection = normalize(-vPosition.xyz); + vec3 reflectionDirection = reflect(-lightDirection, normal); + float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess); + float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0); + lightWeighting = uAmbientColour + uPointLightingSpecularColour * specularLightWeighting + + uPointLightingDiffuseColour * diffuseLightWeighting; } gl_FragColor = vec4(vColour.rgb * lightWeighting, vColour.a); } diff -r 1b4c3fcff1b0 -r 2af408534f28 scripts/machine.js --- a/scripts/machine.js Wed Apr 27 14:45:41 2011 +0200 +++ b/scripts/machine.js Wed Apr 27 15:10:42 2011 +0200 @@ -41,10 +41,11 @@ function createScene(context) { - var cube = new Cube(1, context); + var size = 1; + var cube = new Cube(size, context); var scene = new Node([0, 0, 0], [0, 0, 0], cube); var dim = 10; - var d = 4; + var d = size * 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) { diff -r 1b4c3fcff1b0 -r 2af408534f28 scripts/renderer.js --- a/scripts/renderer.js Wed Apr 27 14:45:41 2011 +0200 +++ b/scripts/renderer.js Wed Apr 27 15:10:42 2011 +0200 @@ -68,13 +68,14 @@ this.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix"); this.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix"); this.nMatrixUniform = gl.getUniformLocation(this.program, "uNMatrix"); - //this.samplerUniform = gl.getUniformLocation(this.program, "uSampler"); + this.materialShininessUniform = gl.getUniformLocation(this.program, "uMaterialShininess"); this.useLightingUniform = gl.getUniformLocation(this.program, "uUseLighting"); 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"); + this.pointLightingSpecularColourUniform = gl.getUniformLocation(this.program, "uPointLightingSpecularColour"); + this.pointLightingDiffuseColourUniform = gl.getUniformLocation(this.program, "uPointLightingDiffuseColour"); function loadShader(gl, id) { @@ -130,6 +131,7 @@ var lighting = true; gl.uniform1i(shader.useLightingUniform, lighting); + gl.uniform1f(shader.materialShininessUniform, 25.0); if (lighting) { @@ -138,8 +140,10 @@ var pos = [0, 0, 0]; gl.uniform3f(shader.pointLightingLocationUniform, pos[0], pos[1], pos[2]); - uni = .3; - gl.uniform3f(shader.pointLightingColourUniform, uni, uni, uni); + uni = .5; + gl.uniform3f(shader.pointLightingSpecularColourUniform, uni, uni, uni); + uni = .1; + gl.uniform3f(shader.pointLightingDiffuseColourUniform, uni, uni, uni); // var lightingDir = vec3.create(); // vec3.add(lightingDir, [0.5, -0.5, 0.0]);