Added test texture.
1 function Camera(speed, pos, up, right, target)
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();
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);
22 Camera.prototype.moveLeft = function()
24 var dir = vec3.create(this.right);
25 vec3.scale(dir, -this.speed.h);
26 vec3.add(this.pos, dir);
28 Camera.prototype.moveRight = function()
30 var dir = vec3.create(this.right);
31 vec3.scale(dir, this.speed.h);
32 vec3.add(this.pos, dir);
34 Camera.prototype.moveUp = function()
36 var dir = vec3.create(this.up);
37 vec3.scale(dir, this.speed.v);
38 vec3.add(this.pos, dir);
40 Camera.prototype.moveDown = function()
42 var dir = vec3.create(this.up);
43 vec3.scale(dir, -this.speed.v);
44 vec3.add(this.pos, dir);
46 Camera.prototype.zoom = function(delta)
48 var dir = vec3.create(this.target);
49 vec3.scale(dir, delta * this.speed.zoom);
50 vec3.add(this.pos, dir);
52 Camera.prototype.pitch = function(delta)
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);
62 Camera.prototype.yaw = function(delta)
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);
72 Camera.prototype.rotate = function(angles)
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);
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);
91 Camera.prototype.roll = function(delta)
95 Camera.prototype.update = function()
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,
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,
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,
124 mat4.multiply(this.matrix, this.pitchMatrix);
125 mat4.multiply(this.matrix, this.yawMatrix);