-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmsh_camera.h
385 lines (307 loc) · 11.9 KB
/
msh_camera.h
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
==============================================================================
Licensing information can be found at the end of the file.
==============================================================================
MSH_CAMERA.H - v0.65
A single header library for camera manipulation
To use the library you simply add:
#define MSH_CAMERA_IMPLEMENTATION
#include "msh_cam.h"
==============================================================================
API DOCUMENTATION
==============================================================================
TODOs:
[x] Implement camera speeds
[ ] Euler angle alternative?
[ ] API docs
[ ] Custom inverse function for projection and view matrices
[ ] First person camera controls
==============================================================================
AUTHORS
Maciej Halber ([email protected])
==============================================================================
DEPENDENCES
This library depends on following standard headers:
<float.h>
<math.h>
<stdbool.h>
By default this library does not import these headers. Please see
docs/no_headers.md for explanation. Importing heades is enabled by:
#define MSH_CAMERA_INCLUDE_LIBC_HEADERS
Additionally, currently it also depends on on msh_vec_math.h for vector math
==============================================================================
REFERENCES:
[1] nlguillemot/arcball_camera.h https://github.com/nlguillemot/arcball_camera/blob/master/arcball_camera.h
[2] nlguillemot/flythrough_camera.h https://github.com/nlguillemot/flythrough_camera/blob/master/flythrough_camera.h
[3] vurtun/camera.c https://gist.github.com/vurtun/d41914c00b6608da3f6a73373b9533e5
[4] Euler/Quaternion https://gamedev.stackexchange.com/questions/13436/glm-euler-angles-to-quaternion
*/
#ifndef MSH_CAMERA
#define MSH_CAMERA
#ifndef MSH_VEC_MATH
#error "Please include msh_vec_math first!"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef MSH_CAMERA_INCLUDE_LIBC_HEADERS
#include <float.h>
#include <math.h>
#include <stdbool.h>
#endif
#ifndef MSH_PI
#define MSH_PI \
3.14159265358979323846264338327950288419716939937510582097494459231
#endif
#ifdef MSH_CAMERA_STATIC
#define MSHCAMDEF static
#else
#define MSHCAMDEF extern
#endif
typedef struct msh_camera_desc
{
msh_vec3_t eye;
msh_vec3_t center;
msh_vec3_t up;
msh_vec4_t viewport;
float fovy;
float znear;
float zfar;
unsigned char use_ortho;
float pan_speed;
float zoom_speed;
float rot_speed;
} msh_camera_desc_t;
typedef struct msh_camera
{
/* View Matrix Params */
msh_vec3_t origin;
msh_vec3_t offset;
msh_quat_t orientation;
/* Projection Matrix Params */
msh_vec4_t viewport;
float znear, zfar;
float fovy;
bool use_ortho;
/* Options */
float pan_speed;
float zoom_speed;
float rot_speed;
/* Generated */
msh_vec3_t location;
msh_mat4_t view;
msh_mat4_t proj;
} msh_camera_t;
void msh_camera_init(msh_camera_t* cam, msh_camera_desc_t* desc);
void
msh_camera_rotate(msh_camera_t* cam, msh_vec2_t prev_pos, msh_vec2_t curr_pos);
void
msh_camera_pan(msh_camera_t* cam, msh_vec2_t prev_pos, msh_vec2_t curr_pos);
void msh_camera_zoom(msh_camera_t* cam, float zoom_amount);
void msh_camera_move(msh_camera_t* cam, msh_vec3_t translation);
void msh_camera_update_view(msh_camera_t* cam);
void msh_camera_update_proj(msh_camera_t* cam);
void msh_camera_update(msh_camera_t* cam);
void msh_camera_ray_through_pixel(msh_camera_t* camera,
msh_vec2_t p,
msh_vec3_t* origin,
msh_vec3_t* direction);
#ifdef __cplusplus
}
#endif
#endif /*MSH_CAMERA_H*/
#ifdef MSH_CAMERA_IMPLEMENTATION
void
msh_camera_pan(msh_camera_t* cam, msh_vec2_t prev_pos, msh_vec2_t curr_pos)
{
float h = cam->viewport.w - cam->viewport.y;
float x0 = prev_pos.x;
float y0 = h - prev_pos.y;
float x1 = curr_pos.x;
float y1 = h - curr_pos.y;
float dx = (x0 - x1) * cam->pan_speed;
float dy = (y0 - y1) * cam->pan_speed;
if (fabs(dx) < 1 && fabs(dy) < 1) { return; }
msh_mat3_t orient = msh_quat_to_mat3(cam->orientation);
msh_vec3_t u = msh_vec3_scalar_mul(orient.col[0], dx * 0.005f);
msh_vec3_t v = msh_vec3_scalar_mul(orient.col[1], dy * 0.005f);
cam->origin = msh_vec3_add(cam->origin, u);
cam->origin = msh_vec3_add(cam->origin, v);
}
void
msh_camera_zoom(msh_camera_t* cam, float zoom_amount)
{
float norm = msh_vec3_norm(cam->offset);
float zoom_factor = (norm <= 2.0) ? 0.5f * norm : 1.0f;
msh_vec3_t zoom_dir = msh_vec3_scalar_div(cam->offset, norm);
float zoom_mult = cam->zoom_speed * zoom_amount * zoom_factor * zoom_factor;
msh_vec3_t zoom_vec = msh_vec3_scalar_mul(zoom_dir, zoom_mult);
cam->offset = msh_vec3_add(cam->offset, zoom_vec);
}
void
msh_camera_move(msh_camera_t* cam, msh_vec3_t translation)
{
cam->origin = msh_vec3_add(cam->origin, translation);
}
void
msh_camera_rotate(msh_camera_t* cam, msh_vec2_t prev_pos, msh_vec2_t curr_pos)
{
float w = cam->viewport.z - cam->viewport.x;
float h = cam->viewport.w - cam->viewport.y;
float r = (w > h) ? h : w;
float x0 = prev_pos.x;
float y0 = h - prev_pos.y;
float x1 = curr_pos.x;
float y1 = h - curr_pos.y;
float dx = (x1 - x0);
float dy = (y1 - y0);
if (fabs(dx) < 1 && fabs(dy) < 1) { return; }
msh_vec3_t p0 = msh_vec3((x0 - w * 0.5f) / r, (y0 - h * 0.5f) / r, 0.0f);
p0 = msh_vec3_scalar_mul(p0, cam->rot_speed);
float l0_sq = p0.x * p0.x + p0.y * p0.y;
if (l0_sq > (0.5 * cam->rot_speed))
{
p0.z = (0.5f * cam->rot_speed) / sqrtf(l0_sq);
}
else
{
p0.z = sqrtf((1.0f * cam->rot_speed) - l0_sq);
}
p0 = msh_vec3_normalize(p0);
msh_vec3_t p1 = msh_vec3((x1 - w * 0.5f) / r, (y1 - h * 0.5f) / r, 0.0f);
p1 = msh_vec3_scalar_mul(p1, cam->rot_speed);
float l1_sq = p1.x * p1.x + p1.y * p1.y;
if (l1_sq > (0.5f * cam->rot_speed))
{
p1.z = (0.5f * cam->rot_speed) / sqrtf(l1_sq);
}
else
{
p1.z = sqrtf((1.0f * cam->rot_speed) - l1_sq);
}
p1 = msh_vec3_normalize(p1);
// From Shoemake
msh_quat_t rot;
rot.im = msh_vec3_cross(p0, p1);
rot.re = msh_vec3_dot(p0, p1);
cam->orientation = msh_quat_mul(cam->orientation, msh_quat_conjugate(rot));
}
void
msh_camera_update_view(msh_camera_t* cam)
{
msh_mat3_t orientation = msh_quat_to_mat3(cam->orientation);
msh_vec3_t rot_offset = msh_mat3_vec3_mul(orientation, cam->offset);
cam->location = msh_vec3_add(cam->origin, rot_offset);
msh_mat4_t inv_view = msh_mat3_to_mat4(orientation);
inv_view.col[3] =
msh_vec4(cam->location.x, cam->location.y, cam->location.z, 1.0f);
cam->view = msh_mat4_se3_inverse(inv_view);
}
void
msh_camera_update_proj(msh_camera_t* cam)
{
float w = cam->viewport.z - cam->viewport.x;
float h = cam->viewport.w - cam->viewport.y;
float aspect_ratio = w / h;
if (cam->use_ortho)
{
float top = 0.85f * cam->zoom_speed * msh_vec3_norm(cam->offset);
float left = -aspect_ratio * top;
cam->proj = msh_ortho(left, -left, -top, top, cam->znear, cam->zfar);
}
else
{
cam->proj = msh_perspective(cam->fovy, aspect_ratio, cam->znear, cam->zfar);
}
}
void
msh_camera_update(msh_camera_t* cam)
{
msh_camera_update_view(cam);
msh_camera_update_proj(cam);
}
void
msh_camera_init(msh_camera_t* cam, msh_camera_desc_t* desc)
{
cam->fovy = (desc->fovy > 0) ? desc->fovy : (2.0f / 3.0f) * (float)MSH_PI;
cam->viewport.x = desc->viewport.x;
cam->viewport.y = desc->viewport.y;
cam->viewport.z = desc->viewport.z;
cam->viewport.w = desc->viewport.w;
cam->znear = desc->znear;
cam->zfar = desc->zfar;
cam->use_ortho = desc->use_ortho;
cam->origin = desc->center;
cam->rot_speed = (desc->rot_speed > 0) ? desc->rot_speed : 1.0f;
cam->pan_speed = (desc->pan_speed > 0) ? desc->pan_speed : 1.0f;
cam->zoom_speed = (desc->zoom_speed > 0) ? desc->zoom_speed : 1.0f;
msh_vec3_t rot_offset = msh_vec3_sub(desc->eye, desc->center);
msh_mat3_t R;
R.col[2] = msh_vec3_normalize(rot_offset);
R.col[0] = msh_vec3_normalize(msh_vec3_cross(desc->up, R.col[2]));
R.col[1] = msh_vec3_normalize(msh_vec3_cross(R.col[2], R.col[0]));
cam->offset = msh_mat3_vec3_mul(msh_mat3_transpose(R), rot_offset);
cam->orientation = msh_mat3_to_quat(R);
msh_camera_update(cam);
}
void
msh_camera_ray_through_pixel(msh_camera_t* camera,
msh_vec2_t p,
msh_vec3_t* origin,
msh_vec3_t* direction)
{
*origin = camera->location;
msh_mat4_t inv_v = msh_mat4_se3_inverse(camera->view);
msh_mat4_t inv_p = msh_mat4_inverse(camera->proj);
float clip_x = (2.0f * p.x) / camera->viewport.z - 1.0f;
float clip_y = 1.0f - (2.0f * p.y) / camera->viewport.w;
msh_vec4_t clip_coords = msh_vec4(clip_x, clip_y, 0.0f, 1.0f);
msh_vec4_t eye_ray_dir = msh_mat4_vec4_mul(inv_p, clip_coords);
eye_ray_dir.z = -1.0f;
eye_ray_dir.w = 0.0f;
msh_vec3_t world_ray_dir =
msh_vec4_to_vec3(msh_mat4_vec4_mul(inv_v, eye_ray_dir));
*direction = msh_vec3_normalize(world_ray_dir);
}
#endif /* MSH_CAMERA_IMPLEMENTATION*/
/*
------------------------------------------------------------------------------
This software is available under 2 licenses - you may choose the one you like.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2019-2020 Maciej Halber
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/