-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcamera.cpp
More file actions
290 lines (259 loc) · 6.61 KB
/
Copy pathcamera.cpp
File metadata and controls
290 lines (259 loc) · 6.61 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <iostream>
#include "helper.h"
#include "camera.h"
#include "global.h"
#include "world.h"
#include "bullet.h"
#include "sphere.h"
//#define CAMERA_FOLLOW
#define CAMERA_MOVEMENT 0.02f
#define CAMERA_ROTATE (2.f * PI / 180.f)
#define CAMERA_ELEV (2.f * PI / 180.f)
#define CAMERA_ACCEL 0.2f
#define CAMERA_SIZE 0.015f
#ifdef CAMERA_FOLLOW
#define CAMERA_HEIGHT (CAMERA_SIZE * 1.f)
#else
#define CAMERA_HEIGHT (CAMERA_SIZE * 5.f)
#endif
#define CAMERA_INIT_POS glm::vec3(0.f, 0.16f + CAMERA_HEIGHT, 1.f)
using namespace std;
using namespace glm;
Camera camera;
Camera::Camera()
{
pos = CAMERA_INIT_POS;
rot = quat();
speed = 0.f;
input.cursor = vec2(-1.f, -1.f);
input.pressed = false;
movement = CAMERA_MOVEMENT;
backup();
updateCalc();
}
void Camera::keyCB(int key)
{
switch (key) {
case GLFW_KEY_LEFT:
// Turn camera to the left
rotate(CAMERA_ROTATE);
break;
case GLFW_KEY_RIGHT:
// Turn camera to the right
rotate(-CAMERA_ROTATE);
break;
case GLFW_KEY_UP:
// Increase the forward speed of the camera
accelerate(CAMERA_ACCEL);
break;
case GLFW_KEY_DOWN:
// Decrease the forward speed of the camera (minimum 0, stays)
accelerate(-CAMERA_ACCEL);
break;
case GLFW_KEY_PAGE_UP:
// Increase the elevation of the camera (optional)
elevate(CAMERA_ELEV);
break;
case GLFW_KEY_PAGE_DOWN:
// Decrease the elevation of the camera (optional)
elevate(-CAMERA_ELEV);
break;
#ifndef SUBMISSION
case GLFW_KEY_W:
pos += forward() * movement;
break;
case GLFW_KEY_S:
pos += forward() * -movement;
break;
case GLFW_KEY_D:
pos += right() * movement;
break;
case GLFW_KEY_A:
pos += right() * -movement;
break;
case GLFW_KEY_K:
pos += upward() * movement;
break;
case GLFW_KEY_J:
pos += upward() * -movement;
break;
case GLFW_KEY_COMMA:
rot = glm::rotate(quat(), movement, forward()) * rot;
break;
case GLFW_KEY_PERIOD:
rot = glm::rotate(quat(), -movement, forward()) * rot;
break;
#endif
}
updateCalc();
}
void Camera::mouseCB(int button, int action)
{
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if (action == GLFW_PRESS) {
input.pressed = true;
} else {
input.pressed = false;
input.cursor = vec2(-1.f, -1.f);
}
}
}
void Camera::scrollCB(double yoffset)
{
if (yoffset > 0)
movement *= 2.f;
else
movement /= 2.f;
}
void Camera::cursorCB(double xpos, double ypos)
{
if (!input.pressed)
return;
vec2 pos(xpos, ypos);
if (input.cursor == pos)
return;
if (input.cursor.x >= 0 && input.cursor.y >= 0) {
vec2 move(vec2(1.f, -1.f) * (input.cursor - pos));
vec3 axis(cross(vec3(move, 0.f), vec3(0.f, 0.f, 1.f)));
rot = glm::rotate(rot, 1.f / 512.f * glm::distance(input.cursor, pos), axis);
}
input.cursor = pos;
updateCalc();
}
void Camera::updateCB(float time)
{
if (status.mode == status_t::TourMode) {
stop();
modelMatrix = bulletGetMatrix(rigidBody);
goto update;
}
if (status.run) {
pos = from_btVector3(bulletGetOrigin(rigidBody)) + vec3(0.f, CAMERA_HEIGHT, 0.f);
modelMatrix = bulletGetMatrix(rigidBody);
rigidBody->activate(true);
btScalar vy = rigidBody->getLinearVelocity().y();
vec3 speed(forward() * this->speed);
rigidBody->setLinearVelocity(btVector3(speed.x, vy, speed.z));
} else
setPosition(pos + forward() * speed * time);
update:
lights[LIGHT_CAMERA].position = position() + (forward() + right() * 0.5f + upward() * -0.5f) * CAMERA_SIZE;
updateCalc();
}
void Camera::setup()
{
sphere = new Sphere(16);
btTransform t = btTransform(btQuaternion(0, 0, 0, 1), to_btVector3(CAMERA_INIT_POS));
rigidBody = sphere->createRigidBody(60.f, CAMERA_SIZE, t);
rigidBody->setLinearVelocity(btVector3(0.f, 0.f, 0.f));
bulletAddRigidBody(rigidBody, BULLET_CAMERA);
light_t &light = lights[LIGHT_CAMERA];
light.daytime = false;
light.attenuation = 0.9f;
light.colour = vec3(1.f);
light.ambient = vec3(0.f);
light.shadow = 0;
}
void Camera::reset()
{
rigidBody->getWorldTransform().setIdentity();
rigidBody->getWorldTransform().setOrigin(to_btVector3(CAMERA_INIT_POS));
stop();
pos = vec3(0.f);
rot = quat();
}
void Camera::stop()
{
rigidBody->activate(true);
rigidBody->setLinearVelocity(btVector3(0.f, 0.f, 0.f));
rigidBody->setAngularVelocity(btVector3(0.f, 0.f, 0.f));
speed = 0.f;
}
void Camera::render()
{
#ifndef SUBMISSION
glEnable(GL_CULL_FACE);
if (status.shadow) {
shadowMatrix.model = modelMatrix;
shadowMatrix.model = scale(shadowMatrix.model, vec3(CAMERA_SIZE));
shadowMatrix.update();
uniformMap &uniforms = programs[PROGRAM_SHADOW].uniforms;
glUniformMatrix4fv(uniforms[UNIFORM_MAT_MVP], 1, GL_FALSE, (GLfloat *)&shadowMatrix.mvp);
sphere->bind();
sphere->render();
return;
}
glUseProgram(programs[PROGRAM_TEXTURE_LIGHTING].id);
uniformMap &uniforms = programs[PROGRAM_TEXTURE_LIGHTING].uniforms;
glUniform3fv(uniforms[UNIFORM_VIEWER], 1, (GLfloat *)&position());
setLights(uniforms);
matrix.model = modelMatrix;
matrix.model = scale(matrix.model, vec3(CAMERA_SIZE));
matrix.update();
glUniformMatrix4fv(uniforms[UNIFORM_MAT_MVP], 1, GL_FALSE, (GLfloat *)&matrix.mvp);
glUniformMatrix4fv(uniforms[UNIFORM_MAT_MODEL], 1, GL_FALSE, (GLfloat *)&matrix.model);
glUniformMatrix3fv(uniforms[UNIFORM_MAT_NORMAL], 1, GL_FALSE, (GLfloat *)&matrix.normal);
glUniform3f(uniforms[UNIFORM_AMBIENT], 0.2f, 0.4f, 1.f);
glUniform3f(uniforms[UNIFORM_DIFFUSE], 0.2f, 0.4f, 1.f);
glUniform3f(uniforms[UNIFORM_EMISSION], 0.f, 0.f, 0.f);
glUniform3f(uniforms[UNIFORM_SPECULAR], 0.2f, 0.4f, 1.f);
glUniform1f(uniforms[UNIFORM_SHININESS], 10.f);
glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_SPHERE].id);
sphere->bind();
sphere->render();
#endif
}
void Camera::backup()
{
bak.pos = pos;
bak.rot = rot;
}
void Camera::restore()
{
pos = bak.pos;
rot = bak.rot;
rigidBody->getWorldTransform().setIdentity();
rigidBody->getWorldTransform().setOrigin(to_btVector3(pos));
stop();
}
void Camera::rotate(float angle)
{
rot = glm::rotate(rot, angle, vec3(0.f, 1.f, 0.f));
}
void Camera::accelerate(float v)
{
speed += speed * v;
if (v > 0) {
if (speed < v)
speed = v;
} else {
if (speed < -v)
speed = 0.f;
}
}
void Camera::elevate(float angle)
{
rot = glm::rotate(rot, angle, vec3(1.f, 0.f, 0.f));
}
void Camera::setPosition(const vec3 pos)
{
this->pos = pos;
rigidBody->getWorldTransform().setIdentity();
rigidBody->getWorldTransform().setOrigin(to_btVector3(pos));
}
glm::mat4 Camera::view() const
{
#ifdef CAMERA_FOLLOW
return lookAt(pos - forward() * CAMERA_SIZE * 2.f + upward() * CAMERA_SIZE * 1.f, pos + forward(), upward());
#else
return lookAt(pos, pos + forward(), upward());
#endif
}
void Camera::print()
{
clog << "Camera\t@" << pos << ",\t" << rot << endl;
}
void Camera::updateCalc()
{
matrix.view = view();
}