scripts/machine.js
changeset 4 3f61f8af55ff
parent 3 a768f6393f94
child 5 ac9ba687a05c
     1.1 --- a/scripts/machine.js	Fri Apr 01 14:49:58 2011 +0200
     1.2 +++ b/scripts/machine.js	Fri Apr 01 15:24:55 2011 +0200
     1.3 @@ -1,15 +1,3 @@
     1.4 -function Machine(canvas, context, scene)
     1.5 -{
     1.6 -    this.canvas = canvas;
     1.7 -    this.context = context;
     1.8 -    this.scene = scene;
     1.9 -    this.gl = context.gl;
    1.10 -}
    1.11 -Machine.prototype.draw = function()
    1.12 -{
    1.13 -    draw(this.context, this.scene);
    1.14 -}
    1.15 -
    1.16  var machine;
    1.17  
    1.18  function main()
    1.19 @@ -23,7 +11,7 @@
    1.20      gl.clearColor(0.0, 0.0, 0.0, 1.0);
    1.21      gl.enable(gl.DEPTH_TEST);
    1.22      //draw(context, object);
    1.23 -    machine = new Machine(canvas, context, object);
    1.24 +    machine = new Machine(context, object);
    1.25      machine.draw();
    1.26  }
    1.27  
    1.28 @@ -44,6 +32,17 @@
    1.29      context.viewport.height = height;
    1.30  }
    1.31  
    1.32 +function Machine(context, scene)
    1.33 +{
    1.34 +    this.context = context;
    1.35 +    this.scene = scene;
    1.36 +    this.gl = context.gl;
    1.37 +}
    1.38 +Machine.prototype.draw = function()
    1.39 +{
    1.40 +    draw(this.context, this.scene);
    1.41 +}
    1.42 +
    1.43  function Context(canvas)
    1.44  {
    1.45      this.canvas = canvas;
    1.46 @@ -90,6 +89,10 @@
    1.47      gl.useProgram(this.program);
    1.48      this.vertexPosition = gl.getAttribLocation(this.program, "aVertexPosition");
    1.49      gl.enableVertexAttribArray(this.vertexPosition);
    1.50 +    
    1.51 +    this.vertexColour = gl.getAttribLocation(this.program, "aVertexColour");
    1.52 +    gl.enableVertexAttribArray(this.vertexColour);
    1.53 +
    1.54      this.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix");
    1.55      this.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix");
    1.56  
    1.57 @@ -130,15 +133,31 @@
    1.58  {
    1.59      var gl = context.gl;
    1.60      this.size = size || 1;
    1.61 -    this.buffer = gl.createBuffer();
    1.62 -    gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
    1.63 +    
    1.64 +    this.positionBuffer = gl.createBuffer();
    1.65 +    gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
    1.66      var vertices = [1.0, 1.0, 0.0,
    1.67  		    -1.0, 1.0, 0.0,
    1.68  		    1.0, -1.0, 0.0,
    1.69  		    -1.0, -1.0, 0.0];
    1.70      gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
    1.71 -    this.itemSize = 3;
    1.72 -    this.numItems = 4;
    1.73 +    this.positionBuffer.itemSize = 3;
    1.74 +    this.positionBuffer.numItems = 4;
    1.75 +
    1.76 +    this.colourBuffer = gl.createBuffer();
    1.77 +    gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
    1.78 +    var colours = [1.0, 0.0, 0.0, .5,
    1.79 +		   0.0, 1.0, 0.0, .5,
    1.80 +		   0.0, 0.0, 1.0, .5,
    1.81 +		   1.0, 0.0, 1.0, .5];
    1.82 +    //colours = [];
    1.83 +    for (var i = 0; i < 4; i++)
    1.84 +    {
    1.85 +	//	colours = colours.concat([0.5, 0.5, 1.0, 1.0]);
    1.86 +    }
    1.87 +    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colours), gl.STATIC_DRAW);
    1.88 +    this.colourBuffer.itemSize = 4;
    1.89 +    this.colourBuffer.numItems = 4;
    1.90  }
    1.91  
    1.92  function draw(context, scene)
    1.93 @@ -148,13 +167,19 @@
    1.94      var shader = context.shader;
    1.95      var mvMatrix = context.mvMatrix;
    1.96      var pMatrix = context.pMatrix;
    1.97 +
    1.98      gl.viewport(0, 0, viewport.width, viewport.height);
    1.99      gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.100      mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix);
   1.101 +
   1.102      mat4.identity(mvMatrix);
   1.103      mat4.translate(mvMatrix, [0.0, 0.0, -7.0]);
   1.104 -    gl.bindBuffer(gl.ARRAY_BUFFER, scene.buffer);
   1.105 -    gl.vertexAttribPointer(shader.vertexPosition, scene.itemSize, gl.FLOAT, false, 0, 0);
   1.106 +    gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer);
   1.107 +    gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
   1.108 +
   1.109 +    gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer);
   1.110 +    gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0);
   1.111 +
   1.112      context.updateMatrixUniforms();
   1.113 -    gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.numItems);
   1.114 +    gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems);
   1.115  }
   1.116 \ No newline at end of file