-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
451 lines (369 loc) · 10.3 KB
/
common.cpp
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "common.h"
Context global_context = {};
Temporary_Storage __default_temporary_storage = {};
void *heap_allocator(Allocator_Mode mode, i64 nbytes, i64 old_nbytes,
void *old_memory_pointer, void *allocator_data)
{
// printf("Using heap allocator, mode %d, size %ld, data_ptr: %p\n",
// static_cast<i32>(mode), nbytes, old_memory_pointer);
if (allocator_data != NULL) logprint("heap_allocator", "??? Allocating on the heap does not use 'allocator_data'...\n");
if (mode == Allocator_Mode::ALLOCATE)
{
auto memory = malloc(nbytes);
return memory;
}
else if (mode == Allocator_Mode::RESIZE)
{
return realloc(old_memory_pointer, nbytes);
}
else if (mode == Allocator_Mode::FREE)
{
free(old_memory_pointer);
return NULL;
}
else if (mode == Allocator_Mode::FREE_ALL)
{
// Heap allocator does not have free all method
free(old_memory_pointer);
return NULL;
}
assert(0);
}
const Allocator_Proc __default_allocator = heap_allocator;
i64 get_temporary_storage_mark()
{
return global_context.temporary_storage->occupied;
}
void reset_temporary_storage()
{
set_temporary_storage_mark(0);
global_context.temporary_storage->high_water_mark = 0;
}
void set_temporary_storage_mark(i64 mark)
{
assert(mark >= 0);
assert(mark <= global_context.temporary_storage->size);
global_context.temporary_storage->occupied = mark;
}
void log_ts_usage()
{
auto ts = global_context.temporary_storage;
printf("in TS, occupied %ld, size %ld, water mark %ld, pointer %p \n",
ts->occupied, ts->size, ts->high_water_mark, ts->data);
}
void *__temporary_allocator(Allocator_Mode mode,
i64 size, i64 old_size,
void *old_memory, void *allocator_data)
{
auto ts = static_cast<Temporary_Storage*>(allocator_data);
if (mode == Allocator_Mode::ALLOCATE)
{
if (ts->data == NULL) // Therefore this is the first time calling
{
logprint("temporary_storage", "First time using TS, setting the storage of the TS....\n");
ts->data = static_cast<u8*>(my_alloc(ts->size, {NULL, __default_allocator}));
assert(ts->data != NULL);
}
if ((ts->occupied + size) > ts->size)
{
logprint("temporary_storage", "TS is too small to allocate an extra %ld bytes....\n", size);
logprint("temporary_storage", "occupied: %ld, size: %ld, high_water_mark: %ld\n", ts->occupied, ts->size, ts->high_water_mark);
assert(0);
}
void *memory_start = ts->data + ts->occupied;
// @Todo: Aligns allocated temp memory to 8 bytes
size = (size + 7) & ~7;
ts->occupied += size;
ts->high_water_mark = std::max(ts->high_water_mark, ts->occupied);
return memory_start;
}
else if (mode == Allocator_Mode::RESIZE)
{
// Bump allocator does not resize stuff, instead
// it allocates a new chunk of memory to be the same
// as the new size
ts->data = reinterpret_cast<u8*>(__temporary_allocator(Allocator_Mode::ALLOCATE, size, 0, 0, allocator_data));
assert(ts->data != NULL);
if (old_memory && (old_size > 0)) memcpy(ts->data, old_memory, std::min(old_size, size));
return ts->data;
}
else if (mode == Allocator_Mode::FREE)
{
// Bump allocator does not free individual elements
// The strategy is ``fire and forget'', which is
// allocate all the stuff and freeing everything at the end
logprint("temporary_storage", "[WARNING]: Attempting to free individual elements with TS.......\n");
return NULL;
}
else if (mode == Allocator_Mode::FREE_ALL)
{
my_free(ts->data, {NULL, __default_allocator});
ts->size = 0;
ts->occupied = 0;
ts->high_water_mark = 0;
return NULL;
}
assert(0);
}
#include <stdarg.h>
void __logprint(const char *func, long line, const char *agent, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
printf("[%s]: ", agent);
//printf("[%s] Function '%s', line %ld: ", agent, func, line);
// Ignore the security warning....
vprintf(fmt, args);
va_end(args);
}
void __logprint(const char *func, long line, u8 *agent, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
printf("[%s]: ", agent);
//printf("[%s] Function '%s', line %ld: ", agent, func, line);
// Ignore the security warning....
vprintf(fmt, args);
va_end(args);
}
void *my_alloc(i64 size, Allocator allocator)
{
auto a = allocator;
if (!a) a = global_context.allocator;
return a.proc(Allocator_Mode::ALLOCATE, size, 0, NULL, a.data);
}
void my_free(void *memory, Allocator allocator)
{
auto a = allocator;
if (!a) a = global_context.allocator;
a.proc(Allocator_Mode::FREE, 0, 0, memory, a.data);
}
// Math stuff
f32 lerp(f32 x, f32 y, f32 t)
{
return x * (1.f - t) + y * t;
}
Vector2 lerp(Vector2 x, Vector2 y, f32 t)
{
return x * (1.f - t) + y * t;
}
Vector3 lerp(Vector3 x, Vector3 y, f32 t)
{
return x * (1.f - t) + y * t;
}
Vector4 lerp(Vector4 x, Vector4 y, f32 t)
{
return x * (1.f - t) + y * t;
}
Quaternion lerp(Quaternion a, Quaternion b, f32 t)
{
Quaternion r;
r.x = a.x + t * (b.x - a.x);
r.y = a.y + t * (b.y - a.y);
r.z = a.z + t * (b.z - a.z);
r.w = a.w + t * (b.w - a.w);
return r;
}
void normalize_or_identity(Quaternion *q)
{
auto sq = sqrtf(q->x*q->x + q->y*q->y + q->z*q->z + q->w*q->w);
if (sq == 0)
{
q->x = 0;
q->y = 0;
q->z = 0;
q->w = 1;
return;
}
auto factor = 1.0f / sq;
q->x *= factor;
q->y *= factor;
q->z *= factor;
q->w *= factor;
}
void normalize_or_z_axis(Vector3 *v)
{
auto sq = sqrtf(v->x*v->x + v->y*v->y + v->z*v->z);
if (sq == 0)
{
v->x = 0;
v->y = 0;
v->z = 1;
return;
}
auto factor = 1.0f / sq;
v->x *= factor;
v->y *= factor;
v->z *= factor;
}
Quaternion nlerp(Quaternion a, Quaternion b, f32 t)
{
auto r = lerp(a, b, t);
normalize_or_identity(&r);
return r;
}
Quaternion negate(Quaternion q)
{
Quaternion r;
r.x = -q.x;
r.y = -q.y;
r.z = -q.z;
r.w = -q.w;
return r;
}
Vector2 rotate(Vector2 v, f32 theta)
{
auto ct = cosf(theta);
auto st = sinf(theta);
auto x = v.x*ct + v.y*-st;
auto y = v.x*st + v.y* ct;
v.x = x;
v.y = y;
return v;
}
Vector2 unit_vector(Vector2 v)
{
auto sq = sqrtf(v.x*v.x + v.y*v.y);
if (sq == 0) return v;
Vector2 result;
auto factor = 1.0f / sq;
result.x = v.x * factor;
result.y = v.y * factor;
return result;
}
Vector3 unit_vector(Vector3 v)
{
auto sq = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
if (sq == 0) return v;
Vector3 result;
auto factor = 1.0f / sq;
result.x = v.x * factor;
result.y = v.y * factor;
result.z = v.z * factor;
return result;
}
Vector4 unit_vector(Vector4 v)
{
auto sq = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
if (sq == 0) return v;
Vector4 result;
auto factor = 1.0f / sq;
result.x = v.x * factor;
result.y = v.y * factor;
result.z = v.z * factor;
result.w = v.w * factor;
return result;
}
void print_cmaj_as_rmaj(Matrix4 mat)
{
mat = glm::transpose(mat);
printf("%f %f %f %f\n", mat[0][0], mat[0][1], mat[0][2], mat[0][3]);
printf("%f %f %f %f\n", mat[1][0], mat[1][1], mat[1][2], mat[1][3]);
printf("%f %f %f %f\n", mat[2][0], mat[2][1], mat[2][2], mat[2][3]);
printf("%f %f %f %f\n", mat[3][0], mat[3][1], mat[3][2], mat[3][3]);
}
void set_rotation(Matrix4 *rotation_matrix, Quaternion orientation)
{
*rotation_matrix = glm::toMat4(orientation);
}
void get_ori_from_rot(Quaternion *ori, Vector3 axis_of_rot, f32 theta)
{
f32 sin_of_half_theta = sinf(theta / 2.0f);
ori->x = axis_of_rot.x * sin_of_half_theta;
ori->y = axis_of_rot.y * sin_of_half_theta;
ori->z = axis_of_rot.z * sin_of_half_theta;
ori->w = cosf(theta / 2.0f);
}
void get_rot_mat(Matrix4 *mat, Vector3 axis_of_rot, f32 theta)
{
Matrix4 ident(1.0f);
*mat = glm::rotate(ident, theta, axis_of_rot);
}
Vector3 rotate(Vector3 v, Quaternion ori)
{
Vector3 qv = Vector3(ori.x, ori.y, ori.z);
f32 s = ori.w;
return
static_cast<f32>(2.0*glm::dot(v, qv))*qv
+ (s*s - glm::dot(qv, qv))*v
+ 2.0f*s*glm::cross(qv, v);
}
f32 normalize_or_zero(Vector3 *dir)
{
f32 length = glm::length(*dir);
if (length == 0)
{
*dir = Vector3(0, 0, 0);
return length;
}
f32 inversed_length = 1 / length;
dir->x *= inversed_length;
dir->y *= inversed_length;
dir->z *= inversed_length;
return length;
}
f32 sign_float(f32 x)
{
return (0.0f < x) - (x < 0.0f);
}
f32 move_toward(f32 a, f32 b, f32 amount)
{
if (a > b)
{
a -= amount;
if (a < b) a = b;
}
else
{
a += amount;
if (a > b) a = b;
}
return a;
}
Vector3 move_toward(Vector3 a, Vector3 b, f32 amount)
{
Vector3 result;
result.x = move_toward(a.x, b.x, amount);
result.y = move_toward(a.y, b.y, amount);
result.z = move_toward(a.z, b.z, amount);
return result;
}
my_pair<Vector3 /*y_axis*/, Vector3 /*z_axis*/> make_an_orthonormal_basis(Vector3 x_axis)
{
auto cross = Vector3(1, 1, 1); // A seed vector.
if (x_axis.x > x_axis.y)
{
if (x_axis.y > x_axis.z)
{
cross.x = 0;
}
else
{
cross.z = 0;
}
}
else
{
if (x_axis.y > x_axis.z)
{
cross.y = 0;
}
else
{
cross.z = 0;
}
}
auto y_axis = glm::cross(cross, x_axis);
normalize_or_z_axis(&y_axis);
auto z_axis = glm::cross(x_axis, y_axis);
normalize_or_z_axis(&z_axis);
return {y_axis, z_axis};
}
my_pair<Vector3 /*y_axis*/, Vector3 /*z_axis*/> make_an_orthonormal_basis(Vector3 x_axis, Vector3 approximate_axis)
{
auto y_axis = glm::cross(approximate_axis, x_axis);
normalize_or_z_axis(&y_axis);
auto z_axis = glm::cross(x_axis, y_axis);
normalize_or_z_axis(&z_axis);
return {y_axis, z_axis};
}