-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera.js
107 lines (93 loc) · 1.8 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const {vec3, mat4} = glMatrix;
export class Camera {
/**
* @public
* @type {number}
*/
x = 0.0;
/**
* @public
* @type {number}
*/
y = 0.0;
/**
* @public
* @type {number}
*/
z = 0.0;
/**
* @private
* @type {vec3}
*/
#cameraUP = undefined;
/**
* @private
* @type {vec3}
*/
#cameraPos = undefined;
/**
* @private
* @type {mat4}
*/
#projectionMatrix = undefined;
/**
* @private
* @type {mat4}
*/
#cameraMatrix = undefined;
/**
* @private
* @type {mat4}
*/
#lookMatrix = undefined;
/**
* @public
* @param {number} fov
* @param {number} aspect
* @param {number} zNear
* @param {number} zFar
*/
constructor(fov, aspect, zNear, zFar) {
this.#cameraUP = vec3.fromValues(0.0, 1.0, 0.0);
//
this.#cameraPos = vec3.fromValues(0.0, 0.0, 0.0);
this.#projectionMatrix = mat4.identity(mat4.create());
this.#cameraMatrix = mat4.identity(mat4.create());
this.#lookMatrix = mat4.identity(mat4.create());
//
this.x = this.#cameraPos[0];
this.y = this.#cameraPos[1];
this.z = this.#cameraPos[2];
mat4.perspective(this.#projectionMatrix, fov, aspect, zNear, zFar);
}
/**
* @public
* @return {mat4}
*/
getCameraMatrix = () => {
return this.#cameraMatrix;
}
/**
* @public
* @return {mat4}
*/
getProjectionMatrix = () => {
return this.#projectionMatrix;
}
/**
* @public
* @return {mat4}
*/
getLookMtx = () => {
return this.#lookMatrix;
}
/**
* @public
* @param {vec3} point
*/
lookAt = (point) => {
vec3.set(this.#cameraPos, this.x, this.y, this.z);
mat4.lookAt(this.#lookMatrix, this.#cameraPos, point, this.#cameraUP);
mat4.multiply(this.#cameraMatrix, this.#projectionMatrix, this.#lookMatrix);
}
}