Refact.
authorEugen Sawin <sawine@me73.com>
Fri, 15 Apr 2011 21:37:40 +0200
changeset 109c21841b3ef9
parent 9 610eb11054aa
child 11 9c0318d0f1c3
Refact.
scripts/machine.js
     1.1 --- a/scripts/machine.js	Sat Apr 02 15:17:52 2011 +0200
     1.2 +++ b/scripts/machine.js	Fri Apr 15 21:37:40 2011 +0200
     1.3 @@ -4,12 +4,10 @@
     1.4  function main()
     1.5  {
     1.6      var canvas = document.getElementById("machine");
     1.7 -    window.onresize = resize;
     1.8      var context = new Context(canvas);
     1.9 -    make_fullscreen(context);
    1.10 +    context.expand();
    1.11      var gl = context.gl;
    1.12      var object = new Cube(1, context);
    1.13 -    object.rotation = 0.01;
    1.14      gl.clearColor(0.0, 0.0, 0.0, 1.0);
    1.15      gl.enable(gl.DEPTH_TEST);
    1.16      machine = new Machine(object);
    1.17 @@ -20,30 +18,16 @@
    1.18  function update()
    1.19  {    
    1.20      requestAnimFrame(update);
    1.21 -    machine.scene.rotation += 0.0001;
    1.22 +    machine.scene.rotation.x += (random(0, 2) - 2) * 0.001; 
    1.23 +    machine.scene.rotation.y += (random(0, 3) - 1) * 0.001; 
    1.24 +    machine.scene.rotation.z += (random(0, 2) - 1) * 0.001;
    1.25      machine.update(new Date().getTime());
    1.26      renderer.update(machine.scene);
    1.27  }
    1.28  
    1.29 -function resize()
    1.30 -{
    1.31 -    make_fullscreen(renderer.context);
    1.32 -    update();
    1.33 -}
    1.34 -
    1.35 -function make_fullscreen(context)
    1.36 -{
    1.37 -    var width = window.innerWidth;
    1.38 -    var height = window.innerHeight;
    1.39 -    context.canvas.width = width;
    1.40 -    context.canvas.height = height;
    1.41 -    context.viewport.width = width;
    1.42 -    context.viewport.height = height;
    1.43 -}
    1.44 -
    1.45  function random(min, max)
    1.46  {
    1.47 -    return (min + Math.random() * (max - min + 1));
    1.48 +    return (min + Math.random() * (max - min));
    1.49  }
    1.50  
    1.51  function Renderer(context)
    1.52 @@ -69,7 +53,9 @@
    1.53  
    1.54      this.pushMatrix(mvMatrix);
    1.55  
    1.56 -    mat4.rotate(mvMatrix, scene.rotation, [random(0, 1), random(0, 1), random(0, 1)]);
    1.57 +    mat4.rotate(mvMatrix, scene.rotation.x, [1, 0, 0]);
    1.58 +    mat4.rotate(mvMatrix, scene.rotation.y, [0, 1, 0]);
    1.59 +    mat4.rotate(mvMatrix, scene.rotation.z, [0, 0, 1]);
    1.60  
    1.61      gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer);
    1.62      gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
    1.63 @@ -128,7 +114,8 @@
    1.64      if (!this.gl) alert("Failed: WebGL init.");
    1.65      this.mvMatrix = mat4.create();
    1.66      this.pMatrix = mat4.create();
    1.67 -    this.shader = new Shader(this);    
    1.68 +    this.shader = new Shader(this);  
    1.69 +    window.onresize = this.expand();
    1.70  }
    1.71  Context.prototype.updateMatrixUniforms = function()
    1.72  {
    1.73 @@ -140,6 +127,16 @@
    1.74      gl.uniformMatrix4fv(program.mvMatrixUniform, false, mvMatrix);
    1.75  }
    1.76  
    1.77 +Context.prototype.expand = function()
    1.78 +{ 
    1.79 +    var width = window.innerWidth;
    1.80 +    var height = window.innerHeight;
    1.81 +    this.canvas.width = width;
    1.82 +    this.canvas.height = height;
    1.83 +    this.viewport.width = width;
    1.84 +    this.viewport.height = height;
    1.85 +}
    1.86 +
    1.87  function Shader(context)
    1.88  {
    1.89      var gl = context.gl;
    1.90 @@ -202,7 +199,7 @@
    1.91  {
    1.92      var gl = context.gl;
    1.93      this.size = size || 1;
    1.94 -    this.rotation = 0;
    1.95 +    this.rotation = {'x': 0.0, 'y': 0.0, 'z': 0.0};
    1.96      
    1.97      this.positionBuffer = gl.createBuffer();
    1.98      gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);