Basic light.
authorEugen Sawin <sawine@me73.com>
Tue, 26 Apr 2011 03:06:56 +0200
changeset 253b66f92dbc4d
parent 24 8c2ee41d3727
child 26 84a998ac11c0
Basic light.
machine.html
scripts/cube.js
scripts/machine.js
scripts/renderer.js
     1.1 --- a/machine.html	Tue Apr 26 01:48:56 2011 +0200
     1.2 +++ b/machine.html	Tue Apr 26 03:06:56 2011 +0200
     1.3 @@ -19,15 +19,14 @@
     1.4   
     1.5  <script id="vertex-shader" type="x-shader/x-vertex"> 
     1.6      attribute vec3 aVertexPosition;
     1.7 -    attribute vec4 aVertexColour;
     1.8      attribute vec3 aVertexNormal;
     1.9 +    attribute vec4 aVertexColour;    
    1.10  
    1.11      uniform mat4 uMVMatrix;
    1.12      uniform mat4 uPMatrix;
    1.13      uniform mat3 uNMatrix;
    1.14  
    1.15      uniform vec3 uAmbientColour;
    1.16 -
    1.17      uniform vec3 uLightingDirection;
    1.18      uniform vec3 uDirectionalColour;
    1.19  
    1.20 @@ -40,6 +39,7 @@
    1.21      {
    1.22          gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
    1.23          vColour = aVertexColour;
    1.24 +
    1.25          if (!uUseLighting)
    1.26          {
    1.27              vLightWeighting = vec3(1.0, 1.0, 1.0);
     2.1 --- a/scripts/cube.js	Tue Apr 26 01:48:56 2011 +0200
     2.2 +++ b/scripts/cube.js	Tue Apr 26 03:06:56 2011 +0200
     2.3 @@ -46,19 +46,56 @@
     2.4      this.positionBuffer.itemSize = 3;
     2.5      this.positionBuffer.numItems = 24;
     2.6  
     2.7 +    this.normalBuffer = gl.createBuffer();
     2.8 +    gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
     2.9 +    var normals = [// font face
    2.10 +		   0.0, 0.0, 1.0,
    2.11 +		   0.0, 0.0, 1.0,
    2.12 +		   0.0, 0.0, 1.0,
    2.13 +		   0.0, 0.0, 1.0,
    2.14 +		   // back face
    2.15 +		   0.0, 0.0, -1.0,
    2.16 +		   0.0, 0.0, -1.0,
    2.17 +		   0.0, 0.0, -1.0,
    2.18 +		   0.0, 0.0, -1.0,
    2.19 +		   // top face
    2.20 +		   0.0, 1.0, 0.0,
    2.21 +		   0.0, 1.0, 0.0,
    2.22 +		   0.0, 1.0, 0.0, 
    2.23 +		   0.0, 1.0, 0.0,
    2.24 +		   // bottom face
    2.25 +		   0.0, -1.0, 0.0,
    2.26 +		   0.0, -1.0, 0.0,
    2.27 +		   0.0, -1.0, 0.0,
    2.28 +		   0.0, -1.0, 0.0,
    2.29 +		   // right face
    2.30 +		   1.0, 0.0, 0.0,
    2.31 +		   1.0, 0.0, 0.0,
    2.32 +		   1.0, 0.0, 0.0,
    2.33 +		   1.0, 0.0, 0.0,
    2.34 +		   // left face
    2.35 +		   -1.0, 0.0, 0.0,
    2.36 +		   -1.0, 0.0, 0.0,
    2.37 +		   -1.0, 0.0, 0.0,
    2.38 +		   -1.0, 0.0, 0.0];
    2.39 +    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
    2.40 +    this.normalBuffer.itemSize = 3;
    2.41 +    this.normalBuffer.numItems = 24;
    2.42 +
    2.43      this.colourBuffer = gl.createBuffer();
    2.44      gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
    2.45      var alpha = 1.0;
    2.46 -    var colours = [[1.0, 0.0, 0.0, alpha],
    2.47 -		   [0.0, 1.0, 0.0, alpha],
    2.48 -		   [0.0, 0.0, 1.0, alpha],
    2.49 -		   [1.0, 0.0, 1.0, alpha],
    2.50 -		   [1.0, 1.0, 0.0, alpha],
    2.51 -		   [0.0, 1.0, 1.0, alpha]];
    2.52 +    var uni = [1.0, 1.0, 1.0, alpha];
    2.53 +    var colours = [[0.0, 0.0, 0.0, alpha],
    2.54 +		   [0.0, 0.0, 0.0, alpha],
    2.55 +		   [0.0, 0.0, 0.0, alpha],
    2.56 +		   [0.0, 0.0, 0.0, alpha],
    2.57 +		   [0.0, 0.0, 0.0, alpha],
    2.58 +		   [0.0, 0.0, 0.0, alpha]];
    2.59      var unpackedColours = [];
    2.60      for (var i in colours)
    2.61      {
    2.62 -	var colour = colours[i];
    2.63 +	var colour = uni;//colours[i];
    2.64  	for (var j = 0; j < 4; j++)
    2.65  	{
    2.66  	    unpackedColours = unpackedColours.concat(colour);
    2.67 @@ -80,39 +117,4 @@
    2.68      gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
    2.69      this.indexBuffer.itemSize = 1;
    2.70      this.indexBuffer.numItems = 36;
    2.71 -
    2.72 -    this.normalBuffer = gl.createBuffer();
    2.73 -    gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
    2.74 -    var normals = [// font face
    2.75 -		   0.0, 0.0, 1.0,
    2.76 -		   0.0, 0.0, 1.0,
    2.77 -		   0.0, 0.0, 1.0,
    2.78 -		   // back face
    2.79 -		   0.0, 0.0, -1.0,
    2.80 -		   0.0, 0.0, -1.0,
    2.81 -		   0.0, 0.0, -1.0,
    2.82 -		   0.0, 0.0, -1.0,
    2.83 -		   // top face
    2.84 -		   0.0, 1.0, 0.0,
    2.85 -		   0.0, 1.0, 0.0,
    2.86 -		   0.0, 1.0, 0.0, 
    2.87 -		   0.0, 1.0, 0.0,
    2.88 -		   // bottom face
    2.89 -		   0.0, -1.0, 0.0,
    2.90 -		   0.0, -1.0, 0.0,
    2.91 -		   0.0, -1.0, 0.0,
    2.92 -		   0.0, -1.0, 0.0,
    2.93 -		   // right face
    2.94 -		   1.0, 0.0, 0.0,
    2.95 -		   1.0, 0.0, 0.0,
    2.96 -		   1.0, 0.0, 0.0,
    2.97 -		   1.0, 0.0, 0.0,
    2.98 -		   // left face
    2.99 -		   -1.0, 0.0, 0.0,
   2.100 -		   -1.0, 0.0, 0.0,
   2.101 -		   -1.0, 0.0, 0.0,
   2.102 -		   -1.0, 0.0, 0.0];
   2.103 -    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
   2.104 -    this.normalBuffer.itemSize = 3;
   2.105 -    this.normalBuffer.numItems = 24;
   2.106  }
   2.107 \ No newline at end of file
     3.1 --- a/scripts/machine.js	Tue Apr 26 01:48:56 2011 +0200
     3.2 +++ b/scripts/machine.js	Tue Apr 26 03:06:56 2011 +0200
     3.3 @@ -246,8 +246,6 @@
     3.4      }
     3.5  }
     3.6  
     3.7 -
     3.8 -
     3.9  function Machine(scene)
    3.10  {
    3.11      this.scene = scene;
    3.12 @@ -263,11 +261,6 @@
    3.13      this.lastUpdate = time;
    3.14  }
    3.15  
    3.16 -
    3.17 -
    3.18 -
    3.19 -
    3.20 -
    3.21  function read(file, handler)
    3.22  {
    3.23      var request = new XMLHttpRequest();
     4.1 --- a/scripts/renderer.js	Tue Apr 26 01:48:56 2011 +0200
     4.2 +++ b/scripts/renderer.js	Tue Apr 26 03:06:56 2011 +0200
     4.3 @@ -22,6 +22,7 @@
     4.4      var program = this.shader;
     4.5      var pMatrix = this.pMatrix;
     4.6      var mvMatrix = this.mvMatrix;
     4.7 +
     4.8      gl.uniformMatrix4fv(program.pMatrixUniform, false, pMatrix);
     4.9      gl.uniformMatrix4fv(program.mvMatrixUniform, false, mvMatrix);
    4.10      var normalMatrix = mat3.create();
    4.11 @@ -57,12 +58,12 @@
    4.12      gl.useProgram(this.program);
    4.13      this.vertexPosition = gl.getAttribLocation(this.program, "aVertexPosition");
    4.14      gl.enableVertexAttribArray(this.vertexPosition);
    4.15 -    
    4.16 -    this.vertexColour = gl.getAttribLocation(this.program, "aVertexColour");
    4.17 -    gl.enableVertexAttribArray(this.vertexColour);
    4.18  
    4.19      this.vertexNormal = gl.getAttribLocation(this.program, "aVertexNormal");
    4.20      gl.enableVertexAttribArray(this.vertexNormal);
    4.21 +    
    4.22 +    this.vertexColour = gl.getAttribLocation(this.program, "aVertexColour");
    4.23 +    gl.enableVertexAttribArray(this.vertexColour);    
    4.24  
    4.25      this.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix");
    4.26      this.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix");    
    4.27 @@ -145,21 +146,21 @@
    4.28     
    4.29      if (lighting)
    4.30      {   
    4.31 -	var ambient = {"r": 1.0, "g": 1.0, "b": 1.0};
    4.32 -    	gl.uniform3f(shader.ambientColourUniform, ambient.r, ambient.g, ambient.b);
    4.33 +	var uni = 0.02;
    4.34 +	var ambient = {"r": uni, "g": uni, "b": uni};
    4.35 +   	gl.uniform3f(shader.ambientColourUniform, ambient.r, ambient.g, ambient.b);
    4.36  
    4.37  	var lightingDir = vec3.create();
    4.38 -	vec3.add(lightingDir, [0.0, 0.0, -1.0]);
    4.39 +	vec3.add(lightingDir, [0.0, -1.0, 0.0]);
    4.40  	vec3.normalize(lightingDir);
    4.41  	vec3.scale(lightingDir, -1);     
    4.42  	gl.uniform3fv(shader.lightingDirectionUniform, lightingDir);
    4.43  	
    4.44 -	var directional = ambient;
    4.45 +	uni = 0.5;
    4.46 +	var directional =  {"r": uni, "g": uni, "b": uni};
    4.47  	gl.uniform3f(shader.directionalColourUniform, directional.r, directional.g, directional.b);
    4.48      }
    4.49     
    4.50 -
    4.51 -    
    4.52      gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, scene.indexBuffer);
    4.53      this.context.updateMatrixUniforms();
    4.54      gl.drawElements(gl.TRIANGLES, scene.indexBuffer.numItems, gl.UNSIGNED_SHORT, 0);