scripts/machine.js
changeset 5 ac9ba687a05c
parent 4 3f61f8af55ff
child 6 cd14e5b5296c
     1.1 --- a/scripts/machine.js	Fri Apr 01 15:24:55 2011 +0200
     1.2 +++ b/scripts/machine.js	Fri Apr 01 23:28:15 2011 +0200
     1.3 @@ -1,24 +1,24 @@
     1.4  var machine;
     1.5 +var render;
     1.6  
     1.7  function main()
     1.8  {
     1.9      var canvas = document.getElementById("machine");
    1.10 -    window.onresize = update;
    1.11 +    window.onresize = resize;
    1.12      var context = new Context(canvas);
    1.13      make_fullscreen(context);
    1.14      var gl = context.gl;
    1.15      var object = new Box(1, context);
    1.16      gl.clearColor(0.0, 0.0, 0.0, 1.0);
    1.17      gl.enable(gl.DEPTH_TEST);
    1.18 -    //draw(context, object);
    1.19 -    machine = new Machine(context, object);
    1.20 -    machine.draw();
    1.21 +    machine = new Machine(object);
    1.22 +    render = new Render(context);
    1.23 +    render.draw(machine.scene);
    1.24  }
    1.25  
    1.26 -function update()
    1.27 +function resize()
    1.28  {
    1.29      make_fullscreen(machine.context);
    1.30 -    //draw(machine.context, machine.scene);
    1.31      machine.draw();
    1.32  }
    1.33  
    1.34 @@ -32,15 +32,44 @@
    1.35      context.viewport.height = height;
    1.36  }
    1.37  
    1.38 -function Machine(context, scene)
    1.39 +function Render(context)
    1.40  {
    1.41      this.context = context;
    1.42 -    this.scene = scene;
    1.43      this.gl = context.gl;
    1.44  }
    1.45 -Machine.prototype.draw = function()
    1.46 +Render.prototype.draw = function(scene)
    1.47 +{ 
    1.48 +    var gl = this.context.gl;
    1.49 +    var viewport = this.context.viewport;
    1.50 +    var shader = this.context.shader;
    1.51 +    var mvMatrix = this.context.mvMatrix;
    1.52 +    var pMatrix = this.context.pMatrix;
    1.53 +
    1.54 +    gl.viewport(0, 0, viewport.width, viewport.height);
    1.55 +    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    1.56 +    mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix);
    1.57 +
    1.58 +    mat4.identity(mvMatrix);
    1.59 +    mat4.translate(mvMatrix, [0.0, 0.0, -7.0]);
    1.60 +    gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer);
    1.61 +    gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
    1.62 +
    1.63 +    gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer);
    1.64 +    gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0);
    1.65 +
    1.66 +    this.context.updateMatrixUniforms();
    1.67 +    gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems);
    1.68 +
    1.69 +    if (this.next) this.next.draw(scene);
    1.70 +}
    1.71 +
    1.72 +function Machine(scene)
    1.73  {
    1.74 -    draw(this.context, this.scene);
    1.75 +    this.scene = scene;
    1.76 +}
    1.77 +Machine.prototype.draw = function(render)
    1.78 +{
    1.79 +    render.draw(this.scene);
    1.80  }
    1.81  
    1.82  function Context(canvas)
    1.83 @@ -158,28 +187,4 @@
    1.84      gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colours), gl.STATIC_DRAW);
    1.85      this.colourBuffer.itemSize = 4;
    1.86      this.colourBuffer.numItems = 4;
    1.87 -}
    1.88 -
    1.89 -function draw(context, scene)
    1.90 -{ 
    1.91 -    var gl = context.gl;
    1.92 -    var viewport = context.viewport;
    1.93 -    var shader = context.shader;
    1.94 -    var mvMatrix = context.mvMatrix;
    1.95 -    var pMatrix = context.pMatrix;
    1.96 -
    1.97 -    gl.viewport(0, 0, viewport.width, viewport.height);
    1.98 -    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    1.99 -    mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix);
   1.100 -
   1.101 -    mat4.identity(mvMatrix);
   1.102 -    mat4.translate(mvMatrix, [0.0, 0.0, -7.0]);
   1.103 -    gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer);
   1.104 -    gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
   1.105 -
   1.106 -    gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer);
   1.107 -    gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0);
   1.108 -
   1.109 -    context.updateMatrixUniforms();
   1.110 -    gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems);
   1.111  }
   1.112 \ No newline at end of file