-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.js
191 lines (157 loc) · 5.26 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Represents a camera.
* Contains functions for movement and rotation.
* Note: This class is taken from the OOP sample project provided by Farid R. Ahmadov and modified for this project.
*/
class Camera {
SENSITIVITY = 0.3
SPEED = 0.08
TRACKBALL_SPEED = 2
yaw = -134.0
pitch = -27.0
constructor(program, position, target, up) {
this.program = program;
this.position = position;
this.initPosition = position;
this.target = target;
this.initTarget = target;
this.up = up;
this.front = normalize(subtract(this.target, this.position))
}
render() {
this.front = normalize(subtract(this.target, this.position))
var pos = gl.getUniformLocation(this.program, "v_Camera");
gl.uniform4fv(pos, flatten(vec4(this.position, 1.0)));
var view = gl.getUniformLocation(this.program, "m_View");
var matView = lookAt(this.position, this.target, this.up);
gl.uniformMatrix4fv(view, false, flatten(matView));
var proj = gl.getUniformLocation(this.program, "m_Proj");
var matProj = perspective(90, 1.0, 0.01, 1000);
gl.uniformMatrix4fv(proj, false, flatten(matProj));
}
/**
* Rotates the camera based on the offsets of mouse coordinates
* @param {number} xOffset
* @param {number} yOffset
*/
rotate(xOffset, yOffset)
{
xOffset *= this.SENSITIVITY;
yOffset *= this.SENSITIVITY;
this.yaw += xOffset
this.pitch += yOffset
if(this.pitch > 89.0) this.pitch = 89.0
if(this.pitch < -89.0) this.pitch = -89.0
var directionX = Math.cos(radians(this.yaw)) * Math.cos(radians(this.pitch))
var directionY = Math.sin(radians(this.pitch));
var directionZ = Math.sin(radians(this.yaw)) * Math.cos(radians(this.pitch))
var direction = vec3(directionX, directionY, directionZ)
this.target = add(this.position, normalize(direction))
}
/**
* Rotates the camera around the given axis by the specified angle value
* @param {number} angle
* @param {vec3} vector3
*/
rotateAround(angle, vector3)
{
this.position = vec3(mult_v(rotate(angle, vector3), vec4(this.position)));
}
/**
* Translates the camera by the x, y, and z values given as a vec3.
* @param {vec3} dVector – vec3, indicating the dx, dy, and dz
*/
translateBy(x, y, z)
{
this.position = vec3(mult_v(translate(x, y, z), vec4(this.position)));
}
/**
* Resets the position and target of the camera.
*/
reset()
{
this.position = this.initPosition
this.target = this.initTarget
}
moveForward()
{
var delta = scale(this.SPEED, this.front)
this.translateBy(delta)
this.target = add(this.position, delta)
}
moveBackward()
{
var delta = scale(this.SPEED, this.front)
this.translateBy(negate(delta))
this.target = add(this.position, delta)
}
moveRight()
{
var cameraRight = normalize(cross(this.front, this.up))
this.translateBy(scale(this.SPEED, cameraRight))
this.target = add(this.position, scale(this.SPEED, this.front))
}
moveRightWithTrackBall()
{
this.target = vec3(-0.5, -0.5, -0.5)
this.translateBy(0.4, 0, 0.4)
this.rotateAround(this.TRACKBALL_SPEED, vec3(0, 1, 0))
this.translateBy(-0.4, 0, -0.4)
}
moveLeft()
{
var cameraLeft = normalize(cross(this.up, this.front))
this.translateBy(scale(this.SPEED, cameraLeft))
this.target = add(this.position, scale(this.SPEED, this.front))
}
moveLeftWithTrackBall()
{
this.target = vec3(-0.5, -0.5, -0.5)
this.translateBy(0.4, 0, 0.4)
this.rotateAround(-this.TRACKBALL_SPEED, vec3(0, 1, 0))
this.translateBy(-0.4, 0, -0.4)
}
moveUp()
{
this.translateBy(scale(this.SPEED, this.up))
this.target = add(this.position, scale(this.SPEED, this.front))
}
moveUpWithTrackBall()
{
this.target = vec3(-0.5, -0.5, -0.5)
if(this.position[1] <= 5)
{
this.translateBy(scale(this.SPEED, this.up)) // up
}
}
moveDown()
{
this.translateBy(negate(scale(this.SPEED, this.up)))
this.target = subtract(this.position, negate(scale(this.SPEED, this.front)))
}
moveDownWithTrackBall()
{
this.target = vec3(-0.5, -0.5, -0.5)
if(this.position[1] >= -5)
{
this.translateBy(negate(scale(this.SPEED, this.up))) // down
}
}
}
function mult_v(m, v) {
if (!m.matrix) {
return "trying to multiply by non matrix";
}
var result;
if (v.length == 2) result = vec2();
else if (v.length == 3) result = vec3();
else if (v.length == 4) result = vec4();
for (var i = 0; i < m.length; i++) {
if (m[i].length != v.length)
return "dimensions do not match";
result[i] = 0;
for (var j = 0; j < m[i].length; j++)
result[i] += m[i][j] * v[j];
}
return result;
}