diff -r 3f61f8af55ff -r ac9ba687a05c scripts/machine.js --- a/scripts/machine.js Fri Apr 01 15:24:55 2011 +0200 +++ b/scripts/machine.js Fri Apr 01 23:28:15 2011 +0200 @@ -1,24 +1,24 @@ var machine; +var render; function main() { var canvas = document.getElementById("machine"); - window.onresize = update; + window.onresize = resize; var context = new Context(canvas); make_fullscreen(context); var gl = context.gl; var object = new Box(1, context); gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.enable(gl.DEPTH_TEST); - //draw(context, object); - machine = new Machine(context, object); - machine.draw(); + machine = new Machine(object); + render = new Render(context); + render.draw(machine.scene); } -function update() +function resize() { make_fullscreen(machine.context); - //draw(machine.context, machine.scene); machine.draw(); } @@ -32,15 +32,44 @@ context.viewport.height = height; } -function Machine(context, scene) +function Render(context) { this.context = context; - this.scene = scene; this.gl = context.gl; } -Machine.prototype.draw = function() +Render.prototype.draw = function(scene) +{ + var gl = this.context.gl; + var viewport = this.context.viewport; + var shader = this.context.shader; + var mvMatrix = this.context.mvMatrix; + var pMatrix = this.context.pMatrix; + + gl.viewport(0, 0, viewport.width, viewport.height); + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); + mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix); + + mat4.identity(mvMatrix); + mat4.translate(mvMatrix, [0.0, 0.0, -7.0]); + gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer); + gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0); + + gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer); + gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0); + + this.context.updateMatrixUniforms(); + gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems); + + if (this.next) this.next.draw(scene); +} + +function Machine(scene) { - draw(this.context, this.scene); + this.scene = scene; +} +Machine.prototype.draw = function(render) +{ + render.draw(this.scene); } function Context(canvas) @@ -158,28 +187,4 @@ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colours), gl.STATIC_DRAW); this.colourBuffer.itemSize = 4; this.colourBuffer.numItems = 4; -} - -function draw(context, scene) -{ - var gl = context.gl; - var viewport = context.viewport; - var shader = context.shader; - var mvMatrix = context.mvMatrix; - var pMatrix = context.pMatrix; - - gl.viewport(0, 0, viewport.width, viewport.height); - gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); - mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix); - - mat4.identity(mvMatrix); - mat4.translate(mvMatrix, [0.0, 0.0, -7.0]); - gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer); - gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0); - - gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer); - gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0); - - context.updateMatrixUniforms(); - gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems); } \ No newline at end of file