-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcamera.c
More file actions
350 lines (297 loc) · 9.91 KB
/
Copy pathcamera.c
File metadata and controls
350 lines (297 loc) · 9.91 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "camera.h"
#include "draw.h"
#include "log.h"
#include "object.h"
#include "platform.h"
#include "shader.h"
#include "mat.h"
/* Animation to change something about the camera */
typedef struct {
enum {
CAMERA_ANIM_LENS
} type;
vec3_t start_pos;
vec3_t end_pos;
int64_t start_time;
int64_t total_time_ms;
} anim_t;
struct camera_ {
/* Window parameters */
int width;
int height;
/* Projection control (orthographic vs perspective) */
float lens;
/* Camera positioning */
float pitch;
float yaw;
vec3_t center;
float scale;
/* Calculated matrices */
mat4_t proj;
mat4_t view;
/* Matrix calculated in loader and stored in model */
mat4_t model;
/* Mouse position and state tracking */
enum { CAMERA_IDLE,
CAMERA_ROT,
CAMERA_PAN,
CAMERA_ANIM,
} state;
float mouse_pos[2];
float click_pos[2];
vec3_t start; /* Flexible drag data, depends on mode */
mat4_t drag_mat;
anim_t* anim;
};
////////////////////////////////////////////////////////////////////////////////
/* Updates the proj matrix from width and height */
static void camera_update_proj(camera_t* camera) {
camera->proj = mat4_identity();
const float aspect = (float)camera->width / (float)camera->height;
if (aspect > 1) {
camera->proj.m[0][0] = 1.0f / aspect;
} else {
camera->proj.m[1][1] = aspect;
}
camera->proj.m[2][2] = camera->scale / 2.0f;
camera->proj.m[2][3] = camera->lens;
}
/* Recalculates the view matrix */
static void camera_update_view(camera_t* camera) {
camera->view = mat4_identity();
/* Apply translation */
camera->view = mat4_mul(camera->view, mat4_translation(camera->center));
{ /* Apply the yaw rotation */
const float c = cos(camera->yaw);
const float s = sin(camera->yaw);
const mat4_t y = {{{ c, -s, 0.0f, 0.0f},
{ s, c, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f}}};
camera->view = mat4_mul(camera->view, y);
}
{ /* Apply the pitch rotation */
const float c = cos(camera->pitch);
const float s = sin(camera->pitch);
const mat4_t p = {{{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, c, -s, 0.0f},
{0.0f, s, c, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f}}};
camera->view = mat4_mul(camera->view, p);
}
{ /* Apply the scaling */
mat4_t s = mat4_scaling(1.0f / camera->scale);
s.m[1][1] *= -1.0f;
camera->view = mat4_mul(camera->view, s);
}
}
////////////////////////////////////////////////////////////////////////////////
camera_t* camera_new(float width, float height, camera_proj_t proj) {
OBJECT_ALLOC(camera);
camera->width = width;
camera->height = height;
/* Avoids a division by 0 during initial construction */
camera->scale = 1.0f;
switch (proj) {
case CAMERA_PROJ_ORTHOGRAPHIC: camera->lens = 0.0f; break;
case CAMERA_PROJ_PERSPECTIVE: camera->lens = 0.5f; break;
default: log_error_and_abort("Invalid projection %i", proj);
};
/* Initialize matrices to a sane state */
camera_update_proj(camera);
camera_update_view(camera);
return camera;
}
void camera_delete(camera_t* camera) {
free(camera->anim);
free(camera);
}
void camera_set_size(camera_t* camera, float width, float height) {
camera->width = width;
camera->height = height;
camera_update_proj(camera);
}
void camera_set_model(camera_t* camera, float* center, float scale) {
mat4_t t = mat4_translation(*(vec3_t*)center);
mat4_t s = mat4_scaling(1.0f / scale);
camera->model = mat4_mul(t, s);
}
void camera_set_mouse_pos(camera_t* camera, float x, float y) {
x = 2.0f * x / (camera->width) - 1.0f;
y = 1.0f - 2.0f * y / (camera->height);
camera->mouse_pos[0] = x;
camera->mouse_pos[1] = y;
const float dx = x - camera->click_pos[0];
const float dy = y - camera->click_pos[1];
switch (camera->state) {
case CAMERA_IDLE: /* Fallthrough */
case CAMERA_ANIM: break;
case CAMERA_PAN: {
vec3_t v = {{camera->click_pos[0], camera->click_pos[1], 0.0f}};
v = mat4_apply(camera->drag_mat, v);
vec3_t w = {{camera->mouse_pos[0], camera->mouse_pos[1], 0.0f}};
w = mat4_apply(camera->drag_mat, w);
for (unsigned i=0; i < 3; ++i) {
camera->center.v[i] = camera->start.v[i] + v.v[i] - w.v[i];
}
camera_update_view(camera);
break;
}
case CAMERA_ROT: {
const float start_pitch = camera->start.v[0];
const float start_yaw = camera->start.v[1];
/* Update pitch and clamp values */
camera->pitch = start_pitch + dy * 2.0f;
if (camera->pitch < -M_PI) {
camera->pitch = -M_PI;
} else if (camera->pitch > 0.0f) {
camera->pitch = 0.0f;
}
/* Update yaw and keep it under 360 degrees */
camera->yaw = start_yaw - dx * 2.0f;
while (camera->yaw < 0.0f) {
camera->yaw += 2.0f * M_PI;
}
while (camera->yaw > 2.0f * M_PI) {
camera->yaw -= 2.0f * M_PI;
}
/* Rebuild view matrix with new values */
camera_update_view(camera);
break;
}
}
}
void camera_rotate(camera_t* camera, float x, float y)
{
/* Update pitch and clamp values */
camera->pitch += y / 360.0f * M_PI;
if (camera->pitch < -M_PI) {
camera->pitch = -M_PI;
} else if (camera->pitch > 0.0f) {
camera->pitch = 0.0f;
}
/* Update yaw and keep it under 360 degrees */
camera->yaw += x / 360.0f * M_PI;
while (camera->yaw < 0.0f) {
camera->yaw += 2.0f * M_PI;
}
while (camera->yaw > 2.0f * M_PI) {
camera->yaw -= 2.0f * M_PI;
}
/* Rebuild view matrix with new values */
camera_update_view(camera);
}
static void camera_set_anim(camera_t* camera, anim_t* anim) {
if (camera->anim) {
log_warn("Triggered an animation while another was running; skipping");
free(anim);
} else {
anim->start_time = platform_get_time();
camera->anim = anim;
}
}
static void camera_anim_lens(camera_t* camera, float target, int time_ms) {
OBJECT_ALLOC(anim);
anim->type = CAMERA_ANIM_LENS;
anim->start_pos.v[0] = camera->lens;
anim->end_pos.v[0] = target;
anim->total_time_ms = time_ms;
camera_set_anim(camera, anim);
}
void camera_anim_proj_perspective(camera_t* camera) {
camera_anim_lens(camera, 0.5f, 100);
}
void camera_anim_proj_orthographic(camera_t* camera) {
camera_anim_lens(camera, 0.0f, 100);
}
/* Finds the inverse of the view + projection matrix. This
* turns normalized mouse coordinates (in the +/- 1 range)
* into world coordinates. */
static mat4_t camera_vpi_mat(camera_t* camera) {
mat4_t m = mat4_identity();
m = mat4_mul(camera->proj, m);
m = mat4_mul(camera->view, m);
return mat4_inv(m);
}
void camera_begin_pan(camera_t* camera) {
if (camera->state != CAMERA_IDLE) {
log_warn("Cannot start panning in state %i", camera->state);
return;
}
memcpy(camera->click_pos, camera->mouse_pos, sizeof(camera->mouse_pos));
camera->start = camera->center;
camera->drag_mat = camera_vpi_mat(camera);
camera->state = CAMERA_PAN;
}
void camera_begin_rot(camera_t* camera) {
if (camera->state != CAMERA_IDLE) {
log_warn("Cannot start rotating in state %i", camera->state);
return;
}
memcpy(camera->click_pos, camera->mouse_pos, sizeof(camera->mouse_pos));
camera->start.v[0] = camera->pitch;
camera->start.v[1] = camera->yaw;
camera->state = CAMERA_ROT;
}
void camera_end_drag(camera_t* camera) {
camera->state = CAMERA_IDLE;
}
void camera_zoom(camera_t* camera, float amount) {
const vec3_t mouse = {{camera->mouse_pos[0], camera->mouse_pos[1], 0.0f}};
mat4_t mat = camera_vpi_mat(camera);
vec3_t before = mat4_apply(mat, mouse);
camera->scale *= powf(1.01f, amount);
camera_update_view(camera);
camera_update_proj(camera);
mat = camera_vpi_mat(camera);
vec3_t after = mat4_apply(mat, mouse);
for (unsigned i=0; i < 3; ++i) {
camera->center.v[i] += before.v[i] - after.v[i];
}
camera_update_view(camera);
}
camera_uniforms_t camera_get_uniforms(GLuint prog) {
camera_uniforms_t u;
SHADER_GET_UNIFORM(proj);
SHADER_GET_UNIFORM(view);
SHADER_GET_UNIFORM(model);
return u;
}
void camera_bind(camera_t* camera, camera_uniforms_t u) {
glUniformMatrix4fv(u.proj, 1, GL_FALSE, (float*)&camera->proj);
glUniformMatrix4fv(u.view, 1, GL_FALSE, (float*)&camera->view);
glUniformMatrix4fv(u.model, 1, GL_FALSE, (float*)&camera->model);
}
bool camera_check_anim(camera_t* camera) {
if (!camera->anim) {
return false;
}
/* Get the current time, for use in interpolation */
const int64_t t = platform_get_time();
if (t < camera->anim->start_time) {
log_error_and_abort("Invalid time in animation");
}
/* Calculate an interpolated value */
float frac = (t - camera->anim->start_time) / camera->anim->total_time_ms
/ 1000.0f;
const bool done = (frac >= 1.0f);
if (done) {
frac = 1.0f;
}
vec3_t v;
for (unsigned i=0; i < 3; ++i) {
v.v[i] = camera->anim->start_pos.v[i] * (1.0f - frac) +
camera->anim->end_pos.v[i] * frac;
}
switch (camera->anim->type) {
case CAMERA_ANIM_LENS: {
camera->lens = v.v[0];
camera_update_proj(camera);
}
}
if (done) {
free(camera->anim);
camera->anim = NULL;
}
return !done;
}