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