scripts/camera.js
author Eugen Sawin <sawine@me73.com>
Sat, 30 Apr 2011 04:32:22 +0200
changeset 38 ce3efaacb700
permissions -rw-r--r--
Added test texture.
     1 function Camera(speed, pos, up, right, target)
     2 {
     3     this.speed = speed;
     4     this.pos = vec3.create(pos || [0, 0, 7]);
     5     this.up = vec3.create([0, 1, 0]);
     6     this.right = vec3.create([1, 0, 0]);
     7     this.target = vec3.create([0, 0, -1]);
     8     normalize([this.up, this.right, this.target]);
     9     this.matrix = mat4.create();
    10 
    11     this.pitchUp = vec3.create([0, 1, 0]);
    12     this.yawUp = vec3.create([0, 1, 0]);
    13     this.pitchRight = vec3.create([1, 0, 0]);
    14     this.yawRight = vec3.create([1, 0, 0]);
    15     this.pitchTarget = vec3.create([0, 0, -1]);
    16     this.yawTarget = vec3.create([0, 0, -1]);
    17     this.pitchMatrix = mat4.create();
    18     this.yawMatrix = mat4.create();
    19     mat4.identity(this.pitchMatrix);
    20     mat4.identity(this.yawMatrix);
    21 }
    22 Camera.prototype.moveLeft = function()
    23 {
    24     var dir = vec3.create(this.right);
    25     vec3.scale(dir, -this.speed.h);
    26     vec3.add(this.pos, dir);
    27 }
    28 Camera.prototype.moveRight = function()
    29 {
    30     var dir = vec3.create(this.right);
    31     vec3.scale(dir, this.speed.h);
    32     vec3.add(this.pos, dir);
    33 }
    34 Camera.prototype.moveUp = function()
    35 {  
    36     var dir = vec3.create(this.up);
    37     vec3.scale(dir, this.speed.v);
    38     vec3.add(this.pos, dir);
    39 }
    40 Camera.prototype.moveDown = function()
    41 { 
    42     var dir = vec3.create(this.up);
    43     vec3.scale(dir, -this.speed.v);
    44     vec3.add(this.pos, dir);
    45 }
    46 Camera.prototype.zoom = function(delta) 
    47 {
    48     var dir = vec3.create(this.target);
    49     vec3.scale(dir, delta * this.speed.zoom);
    50     vec3.add(this.pos, dir); 
    51 }
    52 Camera.prototype.pitch = function(delta)
    53 {
    54     delta *= -this.speed.pitch;
    55     vec3.scale(this.pitchTarget, Math.cos(delta));
    56     vec3.scale(this.pitchUp, Math.sin(delta));
    57     vec3.add(this.pitchTarget, this.pitchUp);
    58     vec3.normalize(this.pitchTarget);
    59     vec3.cross(this.pitchTarget, this.pitchRight, this.pitchUp);
    60     vec3.scale(this.pitchUp, -1);
    61 }
    62 Camera.prototype.yaw = function(delta)
    63 {
    64     delta *= -this.speed.yaw;
    65     vec3.scale(this.yawRight, Math.cos(delta));
    66     vec3.scale(this.yawTarget, Math.sin(delta));
    67     vec3.add(this.yawRight, this.yawTarget);
    68     vec3.normalize(this.yawRight);
    69     vec3.cross(this.yawRight, this.yawUp, this.yawTarget);
    70     vec3.scale(this.yawTarget, -1);
    71 }
    72 Camera.prototype.rotate = function(angles)
    73 {
    74     angles = [angles[0] * -this.speed.pitch, 
    75 	      angles[1] * -this.speed.yaw, 
    76 	      angles[2] * -this.speed.roll];
    77     vec3.scale(this.target, Math.cos(angles[1]) * Math.sin(angles[0]));
    78     vec3.scale(this.up, Math.sin(angles[1]));
    79     vec3.scale(this.right, Math.cos(angles[0]));
    80     vec3.add(this.right, this.target);
    81     vec3.add(this.target, this.up);
    82     
    83     vec3.normalize(this.target);
    84     vec3.normalize(this.right);
    85     vec3.cross(this.target, this.right, this.up);
    86     vec3.scale(this.up, -1);
    87     vec3.cross(this.right, this.up, this.target);
    88     vec3.scale(this.target, -1);
    89     
    90 }
    91 Camera.prototype.roll = function(delta)
    92 {
    93     
    94 }
    95 Camera.prototype.update = function()
    96 {
    97     // vec3.add(this.right, this.pitchRight);
    98     // vec3.add(this.up, this.pitchUp);
    99     // vec3.add(this.target, this.pitchTarget);
   100     // vec3.add(this.right, this.yawRight);
   101     // vec3.add(this.up, this.yawUp);
   102     // vec3.add(this.target, this.yawTarget);
   103     // vec3.normalize(this.right);
   104     // vec3.normalize(this.up);
   105     // vec3.normalize(this.target);
   106     var x = vec3.dot(this.right, this.pos);
   107     var y = vec3.dot(this.up, this.pos);
   108     var z = vec3.dot(this.target, this.pos);
   109     this.matrix = mat4.create([this.right[0], this.up[0], -this.target[0], 0,
   110      			       this.right[1], this.up[1], -this.target[1], 0,
   111      			       this.right[2], this.up[2], -this.target[2], 0,
   112      			       -x, -y, z, 1]);    
   113     //vec3.cross(this.yawRight, this.pitchRight);
   114     //vec3.cross(this.yawUp, this.pitchUp);
   115     //vec3.cross(this.yawTarget, this.pitchTarget);
   116     this.pitchMatrix = mat4.create([this.pitchRight[0], this.pitchUp[0], -this.pitchTarget[0], 0,
   117      			       this.pitchRight[1], this.pitchUp[1], -this.pitchTarget[1], 0,
   118      			       this.pitchRight[2], this.pitchUp[2], -this.pitchTarget[2], 0,
   119 			       0, 0, 0, 1]);
   120     this.yawMatrix = mat4.create([this.yawRight[0], this.yawUp[0], -this.yawTarget[0], 0,
   121      			       this.yawRight[1], this.yawUp[1], -this.yawTarget[1], 0,
   122      			       this.yawRight[2], this.yawUp[2], -this.yawTarget[2], 0,
   123 			       0, 0, 0, 1]);
   124     mat4.multiply(this.matrix, this.pitchMatrix);
   125     mat4.multiply(this.matrix, this.yawMatrix);
   126 }