Rotation.
1.1 --- a/scripts/machine.js Fri Apr 01 23:38:58 2011 +0200
1.2 +++ b/scripts/machine.js Sat Apr 02 00:31:54 2011 +0200
1.3 @@ -8,18 +8,19 @@
1.4 var context = new Context(canvas);
1.5 make_fullscreen(context);
1.6 var gl = context.gl;
1.7 - var object = new Box(1, context);
1.8 + var object = new Cube(1, context);
1.9 + object.rotation = 0.01;
1.10 gl.clearColor(0.0, 0.0, 0.0, 1.0);
1.11 gl.enable(gl.DEPTH_TEST);
1.12 machine = new Machine(object);
1.13 - render = new Render(context);
1.14 - //render.draw(machine.scene);
1.15 - requestAnimFrame(update);
1.16 + render = new Render(context);
1.17 update();
1.18 }
1.19
1.20 function update()
1.21 {
1.22 + requestAnimFrame(update);
1.23 + //machine.scene.rotation *= 1.003;
1.24 render.draw(machine.scene);
1.25 }
1.26
1.27 @@ -43,6 +44,7 @@
1.28 {
1.29 this.context = context;
1.30 this.gl = context.gl;
1.31 + this.matrixStack = [];
1.32 }
1.33 Render.prototype.draw = function(scene)
1.34 {
1.35 @@ -58,6 +60,12 @@
1.36
1.37 mat4.identity(mvMatrix);
1.38 mat4.translate(mvMatrix, [0.0, 0.0, -7.0]);
1.39 +
1.40 + this.pushMatrix(mvMatrix);
1.41 +
1.42 + mat4.rotate(mvMatrix, scene.rotation, [1, 1, 1]);
1.43 + //mat4.rotate(mvMatrix, scene.rotation, [1, 0, 0]);
1.44 +
1.45 gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer);
1.46 gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
1.47
1.48 @@ -66,9 +74,21 @@
1.49
1.50 this.context.updateMatrixUniforms();
1.51 gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems);
1.52 +
1.53 + mvMatrix = this.popMatrix();
1.54
1.55 if (this.next) this.next.draw(scene);
1.56 }
1.57 +Render.prototype.pushMatrix = function(matrix)
1.58 +{
1.59 + var copy = mat4.create();
1.60 + mat4.set(matrix, copy);
1.61 + this.matrixStack.push(copy);
1.62 +}
1.63 +Render.prototype.popMatrix = function()
1.64 +{
1.65 + if (this.matrixStack.length > 0) return this.matrixStack.pop();
1.66 +}
1.67
1.68 function Machine(scene)
1.69 {
1.70 @@ -165,10 +185,11 @@
1.71 }
1.72
1.73
1.74 -function Box(size, context)
1.75 +function Cube(size, context)
1.76 {
1.77 var gl = context.gl;
1.78 this.size = size || 1;
1.79 + this.rotation = 0;
1.80
1.81 this.positionBuffer = gl.createBuffer();
1.82 gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);