-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.js
75 lines (59 loc) · 1.71 KB
/
camera.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Basic camera
function Camera(position,orientation,aspect_ratio,fov)
{
this.position = position;
this.orientation = orientation;
var translation = mat4.create();
var rotation = mat4.create();
this.aspect_ratio = aspect_ratio;
//convert to radians for internal calculations
this.fov = (fov * Math.PI)/180.0;
this.near = 0.001;
this.far = 1000.0;
}
Camera.prototype.translate = function(tVec)
{
vec3.add(this.position,this.position,tVec);
}
/**
* Rotate camera
* @param {float} angle Rotation angle given in radian
* @param {vec3} axis Rotation axis given in world coordinates
*/
Camera.prototype.rotate = function (angle, axis)
{
var tmp = quat.create();
quat.setAxisAngle(tmp, axis, angle)
quat.multiply(this.orientation,tmp,this.orientation);
}
Camera.prototype.getFrontVector = function()
{
var front_vector = vec3.create();
front_vector = [0.0,0.0,-1.0];
vec3.transformQuat(front_vector,front_vector,this.orientation);
return front_vector;
}
Camera.prototype.getUpVector = function()
{
var up_vector = vec3.create();
up_vector = [0.0,1.0,0.0];
vec3.transformQuat(up_vector,up_vector,this.orientation);
return up_vector;
}
Camera.prototype.getRightVector = function()
{
var right_vector = vec3.create();
right_vector = [1.0,0.0,0.0];
vec3.transformQuat(right_vector,right_vector,this.orientation);
return right_vector;
}
Camera.prototype.computeViewMatrix = function(view_matrix)
{
mat4.identity(view_matrix);
mat4.fromRotationTranslation(view_matrix, this.orientation, this.position);
mat4.invert(view_matrix,view_matrix);
}
Camera.prototype.computeProjectionMatrix = function(projection_matrix)
{
mat4.perspective(projection_matrix,this.fov,this.aspect_ratio,this.near,this.far);
}