Added keyboard camera.
authorEugen Sawin <sawine@me73.com>
Fri, 15 Apr 2011 23:02:28 +0200
changeset 119c0318d0f1c3
parent 10 9c21841b3ef9
child 12 c942ba4d4bbf
Added keyboard camera.
scripts/machine.js
     1.1 --- a/scripts/machine.js	Fri Apr 15 21:37:40 2011 +0200
     1.2 +++ b/scripts/machine.js	Fri Apr 15 23:02:28 2011 +0200
     1.3 @@ -1,5 +1,8 @@
     1.4  var machine;
     1.5  var renderer;
     1.6 +var controller;
     1.7 +var camera;
     1.8 +
     1.9  
    1.10  function main()
    1.11  {
    1.12 @@ -10,14 +13,17 @@
    1.13      var object = new Cube(1, context);
    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 -    renderer = new Renderer(context);    
    1.18 +    machine = new Machine(object);       
    1.19 +    camera = new Camera();
    1.20 +    renderer = new Renderer(camera, context);
    1.21 +    controller = new Controller(camera, machine, renderer);
    1.22      update();
    1.23  }
    1.24  
    1.25  function update()
    1.26  {    
    1.27      requestAnimFrame(update);
    1.28 +    controller.update();
    1.29      machine.scene.rotation.x += (random(0, 2) - 2) * 0.001; 
    1.30      machine.scene.rotation.y += (random(0, 3) - 1) * 0.001; 
    1.31      machine.scene.rotation.z += (random(0, 2) - 1) * 0.001;
    1.32 @@ -30,8 +36,92 @@
    1.33      return (min + Math.random() * (max - min));
    1.34  }
    1.35  
    1.36 -function Renderer(context)
    1.37 +function Camera()
    1.38  {
    1.39 +    this.x = 0.0;
    1.40 +    this.y = 0.0;
    1.41 +}
    1.42 +Camera.prototype.moveLeft = function()
    1.43 +{
    1.44 +    camera.x += 0.1;
    1.45 +}
    1.46 +Camera.prototype.moveRight = function()
    1.47 +{
    1.48 +    camera.x -= 0.1;
    1.49 +}
    1.50 +Camera.prototype.moveUp = function()
    1.51 +{
    1.52 +    camera.y -= 0.1;
    1.53 +}
    1.54 +Camera.prototype.moveDown = function()
    1.55 +{
    1.56 +    camera.y += 0.1;
    1.57 +}
    1.58 +
    1.59 +function ucode(char)
    1.60 +{
    1.61 +    return char.charCodeAt(0);
    1.62 +}
    1.63 +     
    1.64 +function Controller(camera, machine, renderer)
    1.65 +{ 
    1.66 +    this.actionMap = {'A': camera.moveLeft,
    1.67 +                      'D': camera.moveRight,
    1.68 +                      'W': camera.moveUp,
    1.69 +                      'S': camera.moveDown};
    1.70 +    this.keyboard = new Keyboard(this.actionMap);
    1.71 +    this.mouse = new Mouse();
    1.72 +   
    1.73 +}
    1.74 +Controller.prototype.update = function()
    1.75 +{
    1.76 +    this.keyboard.handle();
    1.77 +}
    1.78 +
    1.79 +function Mouse()
    1.80 +{
    1.81 +}
    1.82 +function Keyboard(actionMap)
    1.83 +{
    1.84 +    this.actionMap = actionMap;
    1.85 +    this.pressed = {};
    1.86 +    document.onkeydown = handleKeyDown;
    1.87 +    document.onkeyup = handleKeyUp;
    1.88 +}
    1.89 +Keyboard.prototype.keyDown = function(event)
    1.90 +{
    1.91 +    this.pressed[event.keyCode] = true;    
    1.92 +}
    1.93 +Keyboard.prototype.keyUp = function(event)
    1.94 +{
    1.95 +    this.pressed[event.keyCode] = false;
    1.96 +}
    1.97 +Keyboard.prototype.handle = function()
    1.98 +{
    1.99 +    for (key in this.pressed)
   1.100 +    {
   1.101 +	if (this.pressed[key])
   1.102 +	{
   1.103 +	    //alert(this.pressed[key]);
   1.104 +	    key = String.fromCharCode(key);
   1.105 +	    if (this.actionMap[key]) this.actionMap[key](); 
   1.106 +	}
   1.107 +    }
   1.108 +}
   1.109 +
   1.110 +function handleKeyDown(event)
   1.111 +{
   1.112 +    controller.keyboard.keyDown(event);
   1.113 +}
   1.114 +
   1.115 +function handleKeyUp(event)
   1.116 +{
   1.117 +    controller.keyboard.keyUp(event);
   1.118 +}
   1.119 +
   1.120 +function Renderer(camera, context)
   1.121 +{
   1.122 +    this.camera = camera;
   1.123      this.context = context;
   1.124      this.gl = context.gl;
   1.125      this.matrixStack = [];
   1.126 @@ -49,7 +139,7 @@
   1.127      mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix);
   1.128  
   1.129      mat4.identity(mvMatrix);
   1.130 -    mat4.translate(mvMatrix, [0.0, 0.0, -7.0]);
   1.131 +    mat4.translate(mvMatrix, [this.camera.x, this.camera.y, -7.0]);
   1.132  
   1.133      this.pushMatrix(mvMatrix);
   1.134  
   1.135 @@ -126,7 +216,6 @@
   1.136      gl.uniformMatrix4fv(program.pMatrixUniform, false, pMatrix);
   1.137      gl.uniformMatrix4fv(program.mvMatrixUniform, false, mvMatrix);
   1.138  }
   1.139 -
   1.140  Context.prototype.expand = function()
   1.141  { 
   1.142      var width = window.innerWidth;