Untitled
Loading...
Searching...
No Matches
init.c
Go to the documentation of this file.
1#include <SDL2/SDL.h>
2#include <GL/glew.h>
3#include <SDL2/SDL_events.h>
4#include <SDL2/SDL_mouse.h>
5#include <SDL2/SDL_video.h>
6#include "SDL2/SDL_atomic.h"
7#include "SDL2/SDL_mutex.h"
8#include "SDL2/SDL_opengl.h"
9#include "editor/editor.h"
10#include "fftw3.h"
11#include "utilities/mathf.h"
12#include <complex.h>
13#include <math.h>
14#include <stdatomic.h>
15#include <stdbool.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
20#include "init.h"
21#include "core/mesh.h"
22#include "render/render.h"
23#include "core/engine_types.h"
24#include "ui/ui.h"
25#include "core/shaders.h"
26#include "stb/stb_image.h"
27#include "utilities/utilities.h"
28#include "core/acceleration.h"
29#include "ECS/entity.h"
30#include "load/load.h"
31#include "ui/fonts.h"
32#include "nuklear/nuklear.h"
33#include "nuklear/nuklear_sdl_gl3.h"
34#include "audio/audio.h"
35#include "audio/audio_ir.h"
36#include "ui/ui_callbacks.h"
37
38
39
40
41Material * init_new_material(const char * filename)
42{
43
45 if (!mat) return NULL;
46 mat->ka = (vec4){0.5f, 0.5f, 0.5f, 0.5f};
47 mat->kd = (vec4){1.0f, 1.0f, 1.0f, 1.0f};
48 mat->ks = (vec4){0.0f, 0.0f, 0.0f, 1.0f};
49 mat->ke = (vec4){0.0f, 0.0f, 0.0f, 1.0f};
50 mat->diffuse_tex = -1;
51 mat->arm_tex = -1;
52 mat->normal_tex = -1;
53 mat->emissive_tex = -1;
54 mat->ao_tex = -1;
55 mat->mr_tex = -1;
56 mat->metallic = 0.0;
57 mat->roughness = 0.5;
58 mat->normal_scale = 1.0;
59 mat->alpha_cutoff = 0;
60 mat->alpha_mode = 0;
61 mat->double_sided = 0;
62 mat->ao_strength = 1.0;
63 char *str = NULL;
64 strncpy(mat->name, filename, 126);
65 mat->name[127] = '\0';
66 return mat;
67}
68void init_mesh(Mesh * mesh)
69{
70
71
72 init_vector(&mesh->hulls, 0, sizeof(ConvexHull));
73 mesh->tri_adjacency = NULL;
74 mesh->nodes.buffer = NULL;
75 mesh->tri_aabbs.buffer = NULL;
76 mesh->bvh_indices = NULL;
77 ConvexHull * hull = vector_expand(&mesh->hulls, 1);
78 hull->vertices.buffer = NULL;
79 hull->faces.buffer = NULL;
80 hull->hull_faces.buffer = NULL;
81 hull->aabb = mesh->aabb;
82 init_vector(&hull->vertices, 0, sizeof(vec3));
83
84 Point * pts = CAST_VEC(mesh->vertices, Point);
85 for (int i = 0; i < mesh->vertices.count; i++)
86 {
87 vec3 * p = vector_expand(&hull->vertices, 1);
88 *p = pts[i].pos;
89
90 }
91
92 init_vector(&hull->faces, 0, sizeof(MeshFace));
93 MeshFace * ts = CAST_VEC(mesh->tris, MeshFace);
94
95 for (int i = 0; i < mesh->tris.count; i++)
96 {
97 MeshFace * p = vector_expand(&hull->faces, 1);
98 *p = ts[i];
99
100 }
101 init_vector(&hull->hull_faces, 0, sizeof(HullFace));
102 if (mesh->static_mesh)
103 {
104
105 init_mesh_tri_bvh(mesh);
106 mesh->bvh_prim = true;
107 }
108 else
109 {
110 incremental_hull(hull);
111 compute_mesh_hulls(hull);
112
113 mesh->bvh_prim = false;
114
115 }
116
118
119}
120
121#define MN_STARTING_SIZE 10000
123{
124
126
128 for (int i = 0; i < MN_STARTING_SIZE; i++)
129 {
130 MeshNode * mn = &mns[i];
131 mn->free = true;
132 mn->id = i;
133 }
134
135}
136
137#define SKIN_STARTING_SIZE 10000
139{
140
142
144 for (int i = 0; i < SKIN_STARTING_SIZE; i++)
145 {
146 GLTFSkin * s = &skins[i];
147 s->free = true;
148 s->id = i;
149 }
150
151}
152
154{
155
157 for (int i = 0; i < GLOBALS.mesh_nodes.capacity; i++)
158 {
159
160 MeshNode * mn = &mns[i];
161 if (mn->free)
162 {
163 mn->free = false;
164 return mn;
165 }
166
167 }
168
169 size_t count_before = GLOBALS.mesh_nodes.capacity;
172 for (int i = count_before; i < GLOBALS.mesh_nodes.capacity; i++)
173 {
174 mns[i].free = true;
175 mns[i].id = i;
176 }
177
178 mn->free = false;
179 return mn;
180
181}
182
183void * find_free_in_vector(Vector * v, size_t byte_size, size_t free_byte_offset, size_t id_byte_offset)
184{
185 uint8_t * p = v->buffer;
186 for (int i = 0; i < v->capacity; i++)
187 {
188
189 uint8_t * pt = p + byte_size * i;
190 bool * f = (bool*)(pt + free_byte_offset);
191 if (*f)
192 {
193 *f = false;
194 return pt;
195 }
196
197 }
198
199 size_t count_before = v->capacity;
200 void * mn = vector_expand(v, 1);
201
202 p = v->buffer;
203 for (int i = count_before; i < v->capacity; i++)
204 {
205 uint8_t * pt = p + byte_size * i;
206 bool * f = (bool*)(pt + free_byte_offset);
207 size_t * id = (size_t*)(pt + id_byte_offset);
208 *f = true;
209 *id = i;
210 }
211
212 bool * f = (bool*)(p + free_byte_offset);
213 *f = false;
214 return mn;
215
216
217}
218
219
220Asset * retrieve_asset_by_name(const char * asset_name)
221{
222 Asset * assets = CAST_VEC(GLOBALS.assets, Asset);
223 int aidx = -1;
224 for (int i = 0; i < GLOBALS.assets.count; i++)
225 {
226 Asset * a = &assets[i];
227 if (strncmp(a->path, asset_name, 127) == 0)
228 {
229 aidx = i;
230 break;
231 }
232
233 }
234 if (aidx == -1)
235 {
236 printf("Asset %s not found.\n", asset_name);
237 return NULL;
238 }
239 return &assets[aidx];
240}
241
242Mesh * retrieve_mesh_by_name(const char * asset_name, const char * mesh_name)
243{
244 Asset * assets = CAST_VEC(GLOBALS.assets, Asset);
245 int aidx = -1;
246 for (int i = 0; i < GLOBALS.assets.count; i++)
247 {
248 Asset * a = &assets[i];
249 if (strncmp(a->path, asset_name, 127) == 0)
250 {
251 aidx = i;
252 break;
253 }
254
255 }
256 if (aidx == -1)
257 {
258 printf("Asset %s not found.\n", asset_name);
259 return NULL;
260 }
261 Asset * asset = &assets[aidx];
262
263 int * mesh_indices = CAST_VEC(asset->mesh_indices, int);
264 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
265 int idx = -1;
266 for (int i = 0; i < asset->mesh_indices.count; i++)
267 {
268
269 Mesh * mesh = &meshes[mesh_indices[i]];
270
271 if (strncmp(mesh->name, mesh_name, 127) == 0)
272 {
273 idx = mesh_indices[i];
274 break;
275 }
276
277 }
278
279 if (idx == -1)
280 {
281 printf("Mesh %s not found.\n", mesh_name);
282 return NULL;
283 }
284 Mesh * mesh = &meshes[idx];
285
286 return mesh;
287
288
289}
290
292{
293 mn->ent = parent->ent;
294 mn->pc = parent->pc;
295
296 for (int i = 0; i < mn->child_count; i++)
297 {
298 set_mn_components_to_children(mns, &mns[mn->children[i]], mn);
299 }
300
301}
302
303
304AssetInstance * init_new_asset_instance(const char * asset_name, vec3 tr, vec4 rot, vec3 scale)
305{
306 Asset * assets = CAST_VEC(GLOBALS.assets, Asset);
307
308 Asset * asset = retrieve_asset_by_name(asset_name);
309 if (!asset) return NULL;
310 mat4 mat = compose_trs_from_vectors(tr, rot, scale);
311
312 AssetInstance * instance = NULL;
313
314 for (int i = 0; i < MAX_ASSET_INSTANCES; i++)
315 {
316 if (ASSET_INSTANCES[i].free)
317 {
318 instance = &ASSET_INSTANCES[i];
319 instance->free = false;
320 instance->node_count = asset->node_count;
321 instance->skin_count = asset->skin_count;
322 break;
323 }
324
325 }
326 if (instance == NULL) return NULL;
327
328 instance->first_skin = GLOBALS.skins.count;
329 instance->aabb = asset->aabb;
330 instance->node_indices = malloc(sizeof(size_t) * instance->node_count);
331 instance->tr = tr;
332 instance->rot = rot;
333 instance->scale = scale;
334 instance->model = compose_trs_from_vectors(tr, rot, scale);
335
336 for (int i = 0; i < asset->node_count; i++)
337 {
338
339 MeshNode * mn = find_free_in_vector(&GLOBALS.mesh_nodes, sizeof(MeshNode), offsetof(MeshNode, free), offsetof(MeshNode, id));
340 size_t mn_id = mn->id;
341 *mn = asset->nodes[i];
342 mn->free = false;
343 mn->id = mn_id;
344 mn->local_id = asset->nodes[i].id;
345 instance->node_indices[i] = mn->id;
346
347 mn->ent = NULL;
348 mn->pc = NULL;
349 mn->asset_instance = instance->id;
350 mn->uv_map = NULL;
352 strcpy(mn->name, asset->nodes[i].name);
353 if (mn->skin != -1)
354 {
355
356
357 mn->skin += instance->first_skin;
358 }
359
360 memcpy(mn->children, asset->nodes[i].children, sizeof(int) * asset->nodes[i].child_count);
361
362 }
364 for (int i = 0; i < asset->node_count; i++)
365 {
366
367 MeshNode * mn = &mns[instance->node_indices[i]];
368 Entity * ent = NULL;
369 if (mn->physics_mode >= 1)
370 {
371 ent = create_entity(instance, mn->id, true, false);
372 }
373 if (mn->parent != -1)
374 {
375 mn->parent = instance->node_indices[mn->parent];
376 }
377 for (int j = 0; j < mn->child_count; j++)
378 {
379 if (mn->children[j] != -1)
380 {
381 mn->children[j] = instance->node_indices[mn->children[j]];
382
383 set_mn_components_to_children(mns, &mns[mn->children[j]], mn);
384 }
385
386 }
387 }
388
389 instance->skin_indices = malloc(sizeof(size_t) * instance->skin_count);
390
391 for (int i = 0; i < asset->skin_count; i++)
392 {
393 GLTFSkin * skin = find_free_in_vector(&GLOBALS.skins, sizeof(GLTFSkin), offsetof(GLTFSkin, free), offsetof(GLTFSkin, id));
394 size_t skin_index = skin->id;
395 *skin = asset->skins[i];
396 skin->id = skin_index;
397 skin->free = false;
398 instance->skin_indices[i] = skin->id;
399
400 skin->joint_count = asset->skins[i].joint_count;
401 skin->joints = malloc(sizeof(int) * skin->joint_count);
402 memcpy(skin->joints, asset->skins[i].joints, sizeof(int) * skin->joint_count);
403 skin->inverse_bind = malloc(sizeof(mat4) * skin->joint_count);
404 memcpy(skin->inverse_bind, asset->skins[i].inverse_bind, sizeof(mat4) * skin->joint_count);
405 skin->final_mat = calloc(asset->skins[i].joint_count, sizeof(mat4));
406 strcpy(skin->name, asset->skins[i].name);
407 memcpy(skin->animation_indices, asset->skins[i].animation_indices, sizeof(int) * MAX_ANIMATION_INDICES);
408 }
409
410 instance->asset_id = asset->asset_id;
411
412 return instance;
413
414}
415
416
417// this only works if asset instance is newest in the array, for undos
418void destroy_asset_instance(uint32_t id)
419{
420
422
424 for (int i = 0; i < a->node_count; i++)
425 {
426
427 MeshNode * mn = &mns[a->node_indices[i]];
428 mn->free = true;
429
430 }
431
433 for (int i = 0; i < a->skin_count; i++)
434 {
435
436 GLTFSkin * s = &skins[a->skin_indices[i]];
437 if (s->joints) free(s->joints);
438 if (s->inverse_bind) free(s->inverse_bind);
439 if (s->final_mat) free(s->final_mat);
440 s->free = true;
441
442 }
443
444 if (a->node_indices) free(a->node_indices);
445 if (a->skin_indices) free(a->skin_indices);
446 a->free = true;
447
448}
449
450
451
452
453
455{
456
457 pc->center_of_mass = (vec3){0,0,player->height/2};
458 pc->vel = (vec3){0,0,0};
459 pc->angular_velocity = (vec3){0,0,0};
460 float mass = 100.0f;
461 pc->mass = mass;
462 pc->restitution = 0.0f;
463 pc->mu = 0.5f;
464 pc->player_pc = true;
465
466 float w = player->radius * 2;
467 float d = player->radius * 2;
468 float h = player->height;
469 float w2 = w*w;
470 float d2 = d*d;
471 float h2 = h*h;
472
473 pc->inverse_inertia.a0 = 12.0f / (mass * (h2 + d2));
474 pc->inverse_inertia.a1 = 0;
475 pc->inverse_inertia.a2 = 0;
476 pc->inverse_inertia.b0 = 0;
477 pc->inverse_inertia.b1 = 12.0f / (mass * (w2 + d2));
478 pc->inverse_inertia.b2 = 0;
479 pc->inverse_inertia.c0 = 0;
480 pc->inverse_inertia.c1 = 0;
481 pc->inverse_inertia.c2 = 12.0f / (mass * (w2 + h2));
482
483
484}
486{
487 player.radius = 0.4f;
488 player.height = 0.8f;
489 player.cam = &cam;
490
495
496 Skeleton * s = &player.skeleton;
497
498 s->hip_height = 0.42f;
499 s->neck_height = 0.75f;
500 s->pelvis_hip_diff = 0.02f;
503 s->upper_leg_length = 0.2f;
504 s->lower_leg_length = 0.28f;
505 s->pelvis.damp = 11;
506 s->pelvis.stiff = 120;
508
509 s->pelvis.pos.z += s->pelvis_height;
510 s->pelvis.velocity = (vec3){0,0,0};
511 s->pelvis.target = (vec3){0,0,0};
512
513 s->neck.damp = 12;
514 s->neck.stiff = 90;
515 s->neck.damp_rot = 24;
516 s->neck.stiff_rot = 144;
517 s->neck.pos = spawn_point;
518 s->neck.pos.z += s->neck_height;
519 s->neck.velocity = (vec3){0,0,0};
520 s->neck.target = (vec3){0,0,0};
521
524
525 init_vector(&player.mesh.hulls, 1, sizeof(ConvexHull));
527 ConvexHull * hull = &hulls[0];
528 Vector * faces = &hull->faces;
529 Vector * vertices = &hull->vertices;
530 init_vector(faces, 0, sizeof(MeshFace));
531 init_vector(vertices, 0, sizeof(vec3));
532 vec3 * v = vector_expand(vertices, 8);
533 PhysicsComponent * pc = NULL;
534 for (int i = 0; i < MAX_PHYSICS_COMPONENTS; i++)
535 {
536 if (PHYSICS_COMPONENTS[i].free)
537 {
538 pc = &PHYSICS_COMPONENTS[i];
539 PHYSICS_COMPONENTS[i].free = false;
540 PHYSICS_COMPONENTS[i].ent = NULL;
541
542 break;
543
544 }
545 }
546 if (!pc)
547 {
548
549 printf("NO PC SLOT FOUND\n");
550 player.pc = NULL;
551 }
552 else
553 {
554 player.pc = pc;
556
557 }
558
559
560
561
562 vec3 extents = {player.radius / 2, player.radius / 2, player.height / 2 + 0.1};
563 vec3 center = (vec3){0, 0, player.height / 2 - 0.1};
564 hull->centroid = center;
565 hull->aabb.center = center;
566
567
568 int count = 0;
569 for (int z = -1; z <= 1; z += 2){
570 for (int y = -1; y <= 1; y += 2){
571 for (int x = -1; x <= 1; x += 2){
572 v[count++] = vec3_add(center, vec3_mul((vec3){x, y, z}, extents));
573 }
574 }
575 }
576
577 incremental_hull(hull);
578 compute_mesh_hulls(hull);
579
580 player.mesh.aabb.center = center;
581 player.mesh.aabb.extents = extents;
583
584
585
586}
587
588
589void GPU_unwrap_mesh_to_buffers(Mesh * m, Vector * vertices, Vector * tris, Vector * nodes)
590{
591
592 size_t vertex_offset = vertices->count;
593 size_t tri_offset = tris->count;
594 size_t node_offset = nodes->count;
595 VertexGPU * vs = vector_expand(vertices, m->vertices.count);
596 TriGPU * ts = vector_expand(tris, m->tris.count);
597 BVHNodeGPU * ns = vector_expand(nodes, m->nodes.count);
598
599 Point * pts = CAST_VEC(m->vertices, Point);
600 MeshFace * mfs = CAST_VEC(m->tris, MeshFace);
601 BVHNode * bvh_nodes = CAST_VEC(m->nodes, BVHNode);
602
603 for (int i = 0; i < m->vertices.count; i++)
604 {
605
606 Point * pt = &pts[i];
607 VertexGPU * v = &vs[i];
608 v->normal = pt->normal;
609 v->pos = pt->pos;
610 v->uv = pt->uv;
611
612 }
613
614 for (int i = 0; i < m->tris.count; i++)
615 {
616 MeshFace * mf = &mfs[m->bvh_indices[i]];
617 TriGPU * t = &ts[i];
618 t->mesh_id = m->id;
619 t->indices.x = (uint32_t)(mf->indices[0] + vertex_offset);
620 t->indices.y = (uint32_t)(mf->indices[1] + vertex_offset);
621 t->indices.z = (uint32_t)(mf->indices[2] + vertex_offset);
622 t->normal = mf->normal;
623 t->material_id = mf->material_id;
624 t->bvh_index = m->bvh_indices[i] + tri_offset;
625 t->tri_idx = m->bvh_indices[i];
626 t->v0.pos = pts[mf->indices[0]].pos;
627 t->v0.normal = pts[mf->indices[0]].normal;
628 t->v0.uv = pts[mf->indices[0]].uv;
629 t->v1.pos = pts[mf->indices[1]].pos;
630 t->v1.normal = pts[mf->indices[1]].normal;
631 t->v1.uv = pts[mf->indices[1]].uv;
632 t->v2.pos = pts[mf->indices[2]].pos;
633 t->v2.normal = pts[mf->indices[2]].normal;
634 t->v2.uv = pts[mf->indices[2]].uv;
635
636 }
637
638 for (int i = 0; i < m->nodes.count; i++)
639 {
640
641 BVHNode * node = &bvh_nodes[i];
642 BVHNodeGPU * n = &ns[i];
643 n->parent = (node->parent == -1) ? -1 : node->parent + node_offset;
644 n->left = (node->left == -1) ? -1 : node->left + node_offset;
645 n->right = (node->right == -1) ? -1 : node->right + node_offset;
646 n->next = (node->next == -1) ? -1 : node->next + node_offset;
647 n->index = node->index + node_offset;
648 n->first_index = node->first_index + tri_offset;
649 n->count = node->count;
650 n->center = node->aabb.center;
651 n->extents = node->aabb.extents;
652 n->min = (vec3){node->aabb.min_x, node->aabb.min_y, node->aabb.min_z};
653 n->max = (vec3){node->aabb.max_x, node->aabb.max_y, node->aabb.max_z};
654
655 }
656
657
658}
659
660
662{
663
664
665 MeshGPU * mesh_data = calloc(GLOBALS.mesh_nodes.count, sizeof(MeshGPU));
666 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
668
669 Vector vertices;
670 init_vector(&vertices, 0, sizeof(VertexGPU));
671 Vector tris;
672 init_vector(&tris, 0, sizeof(TriGPU));
673 Vector nodes;
674 init_vector(&nodes, 0, sizeof(BVHNodeGPU));
675
676 uint32_t vertex_offset = 0;
677 uint32_t vertex_count = 0;
678 uint32_t face_offset = 0;
679 uint32_t face_count = 0;
680 uint32_t node_offset = 0;
681 uint32_t node_count = 0;
682
683 bool * mesh_processed = calloc(GLOBALS.meshes.count, sizeof(bool));
684 for (int j = 0; j < GLOBALS.scene_data.bvh_indices_count; j++)
685 {
686 MeshNode * mn = &mns[GLOBALS.scene_data.bvh_indices[j]];
687 if (mn->mesh == -1 || mn->free) continue;
688 mesh_data[j].bvh_index = GLOBALS.scene_data.bvh_indices[j];
689 if (!meshes[mn->mesh].static_mesh) continue;
690 if (!mesh_processed[mn->mesh])
691 {
692 vertex_offset = vertices.count;
693 face_offset = tris.count;
694 node_offset = nodes.count;
695
696 GPU_unwrap_mesh_to_buffers(&meshes[mn->mesh], &vertices, &tris, &nodes);
697
698 vertex_count = vertices.count - vertex_offset;
699 face_count = tris.count - face_offset;
700 node_count = nodes.count - node_offset;
701
702
703 mesh_processed[mn->mesh] = true;
704 for (int i = 0; i < GLOBALS.scene_data.bvh_indices_count; i++)
705 {
706 size_t idx = GLOBALS.scene_data.bvh_indices[i];
707 if (mns[idx].mesh != mn->mesh || mns[idx].free) continue;
708 mesh_data[i].vertex_offset = vertex_offset;
709 mesh_data[i].vertex_count = vertex_count;
710 mesh_data[i].tri_offset = face_offset;
711 mesh_data[i].tri_count = face_count;
712 mesh_data[i].node_offset = node_offset;
713 mesh_data[i].node_count = node_count;
714
715 }
716 }
717
718 mesh_data[j].mesh_node_id = j;
719 mesh_data[j].mesh_id = mn->mesh;
720 mesh_data[j].is_static = 1;
721 mesh_data[j].global_matrix = mn->global_matrix;
723 m4_inverse((float*)&mesh_data[j].inverse_global_matrix, (float*)&mn->global_matrix);
724
725 }
726
727
729 BVHNodeGPU * snodes = calloc(GLOBALS.scene_data.bvh_nodes.count, sizeof(BVHNodeGPU));
730 for (int i = 0; i < GLOBALS.scene_data.bvh_nodes.count; i++)
731 {
732
733 BVHNode * node = &scene_nodes[i];
734 BVHNodeGPU * n = &snodes[i];
735 n->parent = node->parent;
736 n->left = node->left;
737 n->right = node->right;
738 n->next = node->next;
739
740 n->first_index = node->first_index;
741 n->count = node->count;
742 n->center = node->aabb.center;
743 n->extents = node->aabb.extents;
744 n->min = (vec3){node->aabb.min_x, node->aabb.min_y, node->aabb.min_z};
745 n->max = (vec3){node->aabb.max_x, node->aabb.max_y, node->aabb.max_z};
746
747 }
748
749
750 glBindBuffer(GL_SHADER_STORAGE_BUFFER, VERTEX_SSBO);
751 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(VertexGPU) * vertices.count, vertices.buffer, GL_STATIC_DRAW);
752
753 glBindBuffer(GL_SHADER_STORAGE_BUFFER, TRI_SSBO);
754 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(TriGPU) * tris.count, tris.buffer, GL_STATIC_DRAW);
755
756 glBindBuffer(GL_SHADER_STORAGE_BUFFER, MESH_SSBO);
757 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(MeshGPU) * GLOBALS.mesh_nodes.count, mesh_data, GL_STATIC_DRAW);
758
759 glBindBuffer(GL_SHADER_STORAGE_BUFFER, MESH_BVH_SSBO);
760 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(BVHNodeGPU) * nodes.count, nodes.buffer, GL_STATIC_DRAW);
761
762 glBindBuffer(GL_SHADER_STORAGE_BUFFER, SCENE_BVH_SSBO);
763 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(BVHNodeGPU) * GLOBALS.scene_data.bvh_nodes.count, snodes, GL_STATIC_DRAW);
764
765 if (MESH_DATA.meshes) free(MESH_DATA.meshes);
766 MESH_DATA.meshes = mesh_data;
768 VERTEX_DATA.vertices = vertices.buffer;
769 if (TRI_DATA.tris) free(TRI_DATA.tris);
770 TRI_DATA.tris = tris.buffer;
772 MESH_BVH_DATA.nodes = nodes.buffer;
774 SCENE_BVH_DATA.nodes = snodes;
775
776}
777
779{
780
781
782 glUseProgram(INIT_CLUSTER_AABBS_SHADER);
783 glUniform3ui(glGetUniformLocation(INIT_CLUSTER_AABBS_SHADER, "uGridSize"), GRID_X, GRID_Y, GRID_Z);
784 glUniform1f(glGetUniformLocation(INIT_CLUSTER_AABBS_SHADER, "uNear"), VIEW_NEAR);
785 glUniform1f(glGetUniformLocation(INIT_CLUSTER_AABBS_SHADER, "uFar"), VIEW_FAR);
786 glUniform2ui(glGetUniformLocation(INIT_CLUSTER_AABBS_SHADER, "uScreenDimensions"), RENDER_WIDTH, RENDER_HEIGHT);
787
788 glDispatchCompute(GRID_X, GRID_Y, GRID_Z);
789 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
790
791}
792
793
795{
796
797 Image * DD = vector_expand(&GLOBALS.images, 1);
799 sprintf(DD->path, "DD");
800 uint8_t dd[3] = {255,255,255};
801 DD->buf = malloc(sizeof(uint8_t) * 3);
802 DD->buf[0] = 255;
803 DD->buf[1] = 255;
804 DD->buf[2] = 255;
805 DD->width = 1;
806 DD->height = 1;
807 DD->channels = 3;
808
809 glGenTextures(1, &DD->i);
810 glBindTexture(GL_TEXTURE_2D, DD->i);
811 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, &dd);
812 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
813 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
814 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
815 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
816
817 GLuint64 dd_handle = glGetTextureHandleARB(DD->i);
818 glMakeTextureHandleResidentARB(dd_handle);
819 DD->handle = dd_handle;
820
821 Image * DN = vector_expand(&GLOBALS.images, 1);
823 sprintf(DN->path, "DN");
824 DN->channels = 3;
825 DN->buf = NULL;
826 uint8_t dn[3] = {128,128,255};
827 DN->buf = malloc(sizeof(uint8_t) * 3);
828 DN->buf[0] = 128;
829 DN->buf[1] = 128;
830 DN->buf[2] = 255;
831 DN->width = 1;
832 DN->height = 1;
833
834 glGenTextures(1, &DN->i);
835 glBindTexture(GL_TEXTURE_2D, DN->i);
836 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, &dn);
837 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
838 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
839 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
840 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
841
842 GLuint64 dn_handle = glGetTextureHandleARB(DN->i);
843 glMakeTextureHandleResidentARB(dn_handle);
844 DN->handle = dn_handle;
845
846 Image * DA = vector_expand(&GLOBALS.images, 1);
848 sprintf(DA->path, "DA");
849 DA->channels = 3;
850 DA->buf = NULL;
851 uint8_t darm[3] = {255,255,255};
852 DA->buf = malloc(sizeof(uint8_t) * 3);
853 DA->buf[0] = 255;
854 DA->buf[1] = 255;
855 DA->buf[2] = 255;
856 DA->width = 1;
857 DA->height = 1;
858
859 glGenTextures(1, &DA->i);
860 glBindTexture(GL_TEXTURE_2D, DA->i);
861 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, &darm);
862 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
863 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
864 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
865 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
866 GLuint64 da_handle = glGetTextureHandleARB(DA->i);
867 glMakeTextureHandleResidentARB(da_handle);
868 DA->handle = da_handle;
869
870 Image * EM = vector_expand(&GLOBALS.images, 1);
872 sprintf(EM->path, "EM");
873 EM->channels = 3;
874 EM->buf = NULL;
875 uint8_t dem[3] = {255,255,255};
876 EM->buf = malloc(sizeof(uint8_t) * 3);
877 EM->buf[0] = 255;
878 EM->buf[1] = 255;
879 EM->buf[2] = 255;
880 EM->width = 1;
881 EM->height = 1;
882
883 glGenTextures(1, &EM->i);
884 glBindTexture(GL_TEXTURE_2D, EM->i);
885 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, &dem);
886 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
887 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
888 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
889 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
890
891 GLuint64 em_handle = glGetTextureHandleARB(EM->i);
892 glMakeTextureHandleResidentARB(em_handle);
893 EM->handle = em_handle;
894
895 Image * DL = vector_expand(&GLOBALS.images, 1);
897 sprintf(DL->path, "DL ");
898 DL->channels = 4;
899 DL->buf = NULL;
900 vec4 dlight = (vec4){0.5f,0.5f,0.5f, 1.0};
901 DL->buf = NULL;
902 DL->width = 1;
903 DL->height = 1;
904
905 glGenTextures(1, &DL->i);
906 glBindTexture(GL_TEXTURE_2D, DL->i);
907 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 1, 1, 0, GL_RGBA, GL_FLOAT, &dlight);
908 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
909 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
910 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
911 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
912
913
914 GLuint64 dl_handle = glGetTextureHandleARB(DL->i);
915 glMakeTextureHandleResidentARB(dl_handle);
916 DL->handle = dl_handle;
917
918}
919
921{
922
923
924 init_vector(&GLOBALS.meshes, 0, sizeof(Mesh));
925 MN_INIT();
926 SKIN_INIT();
927 init_vector(&GLOBALS.assets, 0, sizeof(Asset));
930 init_vector(&GLOBALS.images, 0, sizeof(Image));
931 init_vector(&GLOBALS.lights, 0, sizeof(Light));
932
934
935 init_vector(&AW.min_heap, 1000, sizeof(OpenNode));
936 AW.path = malloc(sizeof(int) * MAX_PORTALS);
937 AW.path_count = 0;
938 AW.stack = calloc(MAX_PORTALS, sizeof(AStarStateStack));
939
940 // init_vector(&GLOBALS.audio_emitters, 0, sizeof(AudioEmitter));
941
942 GLOBALS.audio_probes = NULL;
944 LIGHTMAP_DATA.uvs = NULL;
946
948
954 GLOBALS.lights_dirty = true;
962 memset(GLOBALS.scene_data.LOADED_LIGHTING_PV, 0, sizeof(LPV) * LPV_SLOTS);
963 memset(GLOBALS.scene_data.LOADED_LIGHTING_PV_FLAG, 0, sizeof(bool) * LPV_SLOTS);
964
967
968
972 EDITOR_SETTINGS.mfactor = 0.001f;
982 EDITOR_SETTINGS.SSAO = false;
983 EDITOR_SETTINGS.TAA = false;
984 EDITOR_SETTINGS.MBLUR = true;
985 EDITOR_SETTINGS.DOF = true;
986 EDITOR_SETTINGS.SSR = false;
987 EDITOR_SETTINGS.BLOOM = false;
988 EDITOR_SETTINGS.MSAA = true;
989 EDITOR_SETTINGS.LUT = true;
990 EDITOR_SETTINGS.ACES = false;
991 EDITOR_SETTINGS.NOISE = true;
992 EDITOR_SETTINGS.UI = true;
1003
1004 GLOBALS.probe_spacing = 0.5f;
1005
1006 sprintf(asset_dir, "./assets/");
1007 sprintf(rprobe_dir, "./rprobes/");
1008
1009
1010
1017 for (int i = 0; i < UBERSHADER_VERSIONS; i++)
1018 {
1019 DYNAMIC_SHADER[i].program = 0;
1020
1021 }
1024 for (int i = 0; i < 64; i++)
1025 {
1027
1028 }
1029 SKY_SHADER.program = 0;
1031 for (int i = 0; i < GBUFFER_VERSIONS; i++)
1032 {
1034
1035 }
1037 SSAO_SHADER.program = 0;
1039 SSR_SHADER.program = 0;
1041 TEXT_SHADER.program = 0;
1042
1043
1044 MESH_DATA.meshes = NULL;
1045 VERTEX_DATA.vertices = NULL;
1046 TRI_DATA.tris = NULL;
1047 MESH_BVH_DATA.nodes = NULL;
1048 SCENE_BVH_DATA.nodes = NULL;
1049
1050 for (int i = 0; i < MAX_ENTITIES; i++)
1051 {
1052 ENTITIES[i].id = i;
1053 ENTITIES[i].free = true;
1054 }
1055 for (int i = 0; i < MAX_ASSET_INSTANCES; i++)
1056 {
1057 ASSET_INSTANCES[i].id = i;
1058 ASSET_INSTANCES[i].free = true;
1059
1060 }
1061
1062 for (int i = 0; i < MAX_PHYSICS_COMPONENTS; i++)
1063 {
1064 PHYSICS_COMPONENTS[i].id = i;
1065 PHYSICS_COMPONENTS[i].free = true;
1066 PHYSICS_COMPONENTS[i].at_rest = false;
1067 }
1068 for (int i = 0; i < MAX_ANIMATION_COMPONENTS; i++)
1069 {
1070 ANIMATION_COMPONENTS[i].free = true;
1072 for (int j = 0; j < MAX_TRACKS; j++)
1073 {
1074
1075 ANIMATION_COMPONENTS[i].tracks[j].data = NULL;
1077 }
1078 }
1079 for (int i = 0; i < MAX_MANIFOLDS; i++)
1080 {
1081 MANIFOLDS[i].a = NULL;
1082 MANIFOLDS[i].b = NULL;
1083 MANIFOLDS[i].free = true;
1084 }
1085 for (int i = 0; i < MAX_AUDIO_EMITTERS; i++)
1086 {
1087 AUDIO_EMITTERS[i].free = true;
1088 AUDIO_EMITTERS[i].id = i;
1089 }
1090
1091 EW[0] = malloc(sizeof(EPAWorkspace));
1092 GLOBALS.mesh_light_indices = malloc(sizeof(uint32_t) * MAX_MESH_NODES * MAX_LIGHTS_PER_MESH);
1093
1094
1095
1096
1097 Light * lights = vector_expand(&GLOBALS.lights, 2 );
1098
1099
1100 lights[0].light_type = DIRECTIONAL_LIGHT;
1101 lights[0].rgb = (vec3){0.7, 0.6, 0.6};
1102 lights[0].pos = (vec3){0, 4, 2};
1103 lights[0].dir = (vec3){0.0, 0.0, 1.0};
1104 lights[0].dir = vec3_normalize(lights[0].dir);
1105 lights[0].intensity = 1;
1106 Entity * l1 = create_entity(NULL, -1, false, true);
1107 lights[0].entity_id = l1->id;
1108 lights[0].threshold = 0.005f;
1109
1110
1111
1112
1113 lights[1].light_type = POINT_LIGHT;
1114 lights[1].rgb = (vec3){0.77, 0.66, 0.45};
1115 lights[1].pos = (vec3){0, 0, 2.0};
1116 lights[1].intensity = 20;
1117 Entity * l3 = create_entity(NULL, -1, false, true);
1118 lights[1].entity_id = l3->id;
1119 lights[1].threshold = 0.001f;
1120 lights[1].sm_dirty = true;
1121
1122
1123
1124 Font * font = read_json_msdf("../engine-assets/fonts/MxPlus_IBM_VGA_8x16.json", "../engine-assets/fonts/MyFont.png");
1125 if (font)
1126 {
1127
1129 font->name,
1130 GL_LINEAR,
1131 GL_LINEAR,
1132 GL_CLAMP_TO_EDGE,
1133 GL_CLAMP_TO_EDGE,
1134 false);
1135 }
1136
1137
1138 Material * default_mat = init_new_material("default_mat");
1139 default_mat->diffuse_tex = DEFAULT_DIFFUSE;
1140 default_mat->normal_tex = DEFAULT_NORMAL;
1141 default_mat->arm_tex = DEFAULT_ARM;
1142 default_mat->emissive_tex = DEFAULT_EMISSIVE;
1143 default_mat->fwd_shader = 0;
1144 default_mat->gbuffer_shader = 0;
1145 default_mat->mr_tex = -1;
1146 for (int i = 0; i < AUDIO_NUM_BANDS; i++)
1147 {
1148 default_mat->absorption_coefficients[i] = 0.05f;
1149 default_mat->scattering_coefficients[i] = 0.05f;
1150 }
1151
1152
1153 return true;
1154
1155}
1156
1157
1158
1160{
1161
1162
1164
1165 vl->current_offset = 0;
1166 vl->current_capacity = 0;
1167 vl->current_index_offset = 0;
1168 vl->current_index_capacity = 0;
1169 init_vector(&vl->meshes, 0, sizeof(size_t));
1170 vl->vao = MAIN_VAO;
1171 vl->vbo = MAIN_VBO;
1172 vl->ebo = MAIN_EBO;
1173
1174
1175 return true;
1176
1177}
1178
1179
1180vec3 * fbm_tex = NULL;
1181
1182
1183void
1185
1186{
1187
1188 fbm_tex = calloc(FBM_TEXTURE_SIZE * FBM_TEXTURE_SIZE, sizeof(vec3));
1189
1190 for (int y = 0; y < FBM_TEXTURE_SIZE; y++)
1191 {
1192
1193 for (int x = 0; x < FBM_TEXTURE_SIZE; x++)
1194 {
1195 vec3 p = {(float)x, (float)y, 0};
1196 float d = fbm(p);
1197
1198 fbm_tex[y*FBM_TEXTURE_SIZE+x].x = (d + 1) * 0.5f;
1199
1200 float d1 = fbm(vec3_scale(p, 0.1f));
1201 fbm_tex[y*FBM_TEXTURE_SIZE+x].y = (d1 + 1) * 0.5f;
1202
1203 float d2 = fbm(vec3_scale(p, 0.05f));
1204 fbm_tex[y*FBM_TEXTURE_SIZE+x].z = (d2 + 1) * 0.5f;
1205
1206
1207 }
1208 }
1209
1210 glBindTexture(GL_TEXTURE_2D, FBM_TEXTURE);
1211 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, FBM_TEXTURE_SIZE, FBM_TEXTURE_SIZE, GL_RGB, GL_FLOAT, fbm_tex);
1212 free(fbm_tex);
1213 fbm_tex = NULL;
1214
1215}
1216
1217
1218
1220{
1221
1222
1223 for (int i = 0; i < 16; i++)
1224 {
1225 vec3 sample =
1226 {
1227 ((float)rand() / RAND_MAX) * 2.0f - 1.0f,
1228 ((float)rand() / RAND_MAX) * 2.0f - 1.0f,
1229 ((float)rand() / RAND_MAX)
1230 };
1231
1232 sample = vec3_normalize(sample);
1233
1234 float r = (float)rand() / RAND_MAX;
1235 sample = vec3_scale(sample, r);
1236
1237 float scale = (float)i / 16;
1238 scale = lerp(0.1, 1.0f, scale * scale);
1239 sample = vec3_scale(sample, scale);
1240 SSAO_KERNEL[i] = vec3_to_vec4(sample);
1241 }
1242}
1244{
1245 GLuint i = 0;
1246 glGenTextures(1, &i);
1247 glBindTexture(tex.target, i);
1248 if (tex.target == GL_TEXTURE_CUBE_MAP_ARRAY)
1249 {
1250 glTexStorage3D(tex.target, tex.levels, tex.internal_format, tex.w, tex.h, tex.d);
1251 }
1252 else if (tex.target == GL_TEXTURE_2D_ARRAY)
1253 {
1254
1255 glTexImage3D(tex.target, 0, tex.internal_format, tex.w, tex.h, tex.d, 0, tex.format, tex.type, NULL);
1256 }
1257 else if (tex.target == GL_TEXTURE_2D_MULTISAMPLE)
1258 {
1259
1260 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, tex.samples, tex.internal_format, tex.w, tex.h, tex.fixed_sample_locations);
1261 }
1262 else
1263 {
1264
1265 glTexImage2D(tex.target, 0, tex.internal_format, tex.w, tex.h, 0, tex.format, tex.type, NULL);
1266 }
1267 glTexParameteri(tex.target, GL_TEXTURE_MIN_FILTER, tex.min_filter);
1268 glTexParameteri(tex.target, GL_TEXTURE_MAG_FILTER, tex.mag_filter);
1269 glGenerateMipmap(tex.target);
1270 glTexParameteri(tex.target, GL_TEXTURE_WRAP_S, tex.wrap_s);
1271 glTexParameteri(tex.target, GL_TEXTURE_WRAP_T, tex.wrap_t);
1272 glTexParameteri(tex.target, GL_TEXTURE_WRAP_R, tex.wrap_r);
1273
1274 switch(tex.dm)
1275 {
1276 case DEPTH_MODE_NONE:
1277 break;
1278 case DEPTH_MODE_NO_HW:
1279 glTexParameteri(tex.target, GL_TEXTURE_COMPARE_MODE, GL_NONE);
1280 break;
1281 case DEPTH_MODE_HW:
1282 glTexParameteri(tex.target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
1283 glTexParameteri(tex.target, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
1284 break;
1285 }
1286 return i;
1287}
1288GLTexture init_gl_texture(GLenum target, GLint internal_format, GLenum format, GLenum type, GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t, GLint wrap_r, int w, int h, int d, int levels, DepthMode dm)
1289{
1290
1291 GLTexture tex;
1292 tex.target = target;
1293 tex.internal_format = internal_format;
1294 tex.format = format;
1295 tex.type = type;
1296 tex.min_filter = min_filter;
1297 tex.mag_filter = mag_filter;
1298 tex.wrap_s = wrap_s;
1299 tex.wrap_t = wrap_t;
1300 tex.wrap_r = wrap_r;
1301 tex.w = w;
1302 tex.h = h;
1303 tex.d = d;
1304 tex.levels = levels;
1305 tex.dm = dm;
1306 if (tex.target == GL_TEXTURE_2D_MULTISAMPLE)
1307 {
1308 tex.multisampled = true;
1309 tex.fixed_sample_locations = GL_TRUE;
1310 tex.samples = 4;
1311 }
1312
1313 tex.i = generate_gl_texture(tex);
1314
1315 return tex;
1316}
1317
1319{
1320
1321 glGenFramebuffers(1, &fbo->fbo);
1322 glBindFramebuffer(GL_FRAMEBUFFER, fbo->fbo);
1323 GLenum drawBuffers[6] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5};
1324 for (int i = 0; i < fbo->texture_count; i++)
1325 {
1326
1327 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, fbo->texture[i].target, fbo->texture[i].i, 0);
1328 }
1329 for (int i = fbo->texture_count; i < fbo->rbo_count; i++)
1330 {
1331 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_RENDERBUFFER, fbo->renderbuffers[i - fbo->texture_count].i);
1332 }
1333 glDrawBuffers(fbo->texture_count + fbo->rbo_count, drawBuffers);
1334 if (fbo->depth_rbo.i != 0)
1335 {
1336
1337 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo->depth_rbo.i);
1338 }
1339 else
1340 {
1341 if (fbo->depth_texture.target == GL_TEXTURE_CUBE_MAP_ARRAY)
1342 {
1343
1344 glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, fbo->depth_texture.i, 0);
1345 }
1346 else
1347 {
1348
1349 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, fbo->depth_texture.target, fbo->depth_texture.i, 0);
1350 }
1351
1352 }
1353 if (fbo->texture_count == 0 && fbo->rbo_count == 0)
1354 {
1355
1356 glDrawBuffer(GL_NONE);
1357 glReadBuffer(GL_NONE);
1358 }
1359}
1360
1361void change_fbo_resolution(Framebuffer * fbo, int w, int h, bool skip_depth)
1362{
1363 glDeleteFramebuffers(1, &fbo->fbo);
1364 fbo->w = w;
1365 fbo->h = h;
1366 for (int i = 0; i < fbo->texture_count; i++)
1367 {
1368 glDeleteTextures(1, &fbo->texture[i].i);
1369 fbo->texture[i].w = w;
1370 fbo->texture[i].h = h;
1371 fbo->texture[i].i = generate_gl_texture(fbo->texture[i]);
1372 }
1373
1374 if (!skip_depth && fbo->depth_texture.i > 0)
1375 {
1376 glDeleteTextures(1, &fbo->depth_texture.i);
1377 fbo->depth_texture.w = w;
1378 fbo->depth_texture.h = h;
1380 }
1381
1382 init_fbo(fbo);
1383
1384}
1385
1388{
1389
1390 SDL_AudioSpec desired, obtained;
1391 SDL_zero(desired);
1392 desired.freq = 48000;
1393 desired.format = AUDIO_F32SYS;
1394 desired.channels = 2;
1395 desired.samples = 512;
1396 desired.callback = SDLAudioCallback;
1397 desired.userdata = &AUDIO_ENGINE;
1398
1399 SDL_AUDIO_DEVICE = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0);
1400 estimated_frame_count = obtained.samples;
1401
1402 SDL_PauseAudioDevice(SDL_AUDIO_DEVICE, 0);
1403}
1404
1405#define CROSSFADE_FRAMES 512
1406
1407// we found out that the aliasing bug is actually due to the hdmi cable probably since it sounds clean plugged in raw
1408// write ma_mutex
1409static void convolver_node_process_pcm_frames(ma_node* pNode, const float** ppFramesIn, ma_uint32* pFrameCountIn, float** ppFramesOut, ma_uint32* pFrameCountOut)
1410{
1411
1412 ConvolverNode * node = (ConvolverNode *)pNode;
1413
1414 const float* pFramesIn_0 = ppFramesIn[0];
1415 const float* pFramesIn_1 = ppFramesIn[1];
1416 float* pFramesOut_0 = ppFramesOut[0];
1417 int frame_count = *pFrameCountOut;
1418
1419 PartitionTable * front = NULL;
1420 bool crossfade_initialized = false;
1421 SDL_AtomicLock(&HRTF_TD.ir_swap_lock);
1422 {
1423 front = HRTF_TD.FRONT;
1424 if (CONVOLVER_NODE.prev_ir == NULL)
1425 {
1426 CONVOLVER_NODE.prev_ir = front;
1427 CONVOLVER_NODE.ir = NULL;
1429 }
1430 if ((front != CONVOLVER_NODE.prev_ir) && front != NULL && !CONVOLVER_NODE.crossfading)
1431 {
1432 crossfade_initialized = true;
1433 CONVOLVER_NODE.ir = front;
1434 }
1435 }
1436 SDL_AtomicUnlock(&HRTF_TD.ir_swap_lock);
1437
1438 if (crossfade_initialized)
1439 {
1440
1443
1444
1445 memset(CONVOLVER_NODE.SUM_L, 0, sizeof(fftwf_complex) * CONVOLVER_NODE.complex_len);
1446 memset(CONVOLVER_NODE.SUM_R, 0, sizeof(fftwf_complex) * CONVOLVER_NODE.complex_len);
1447 memset(CONVOLVER_NODE.LEFT_OUT, 0, sizeof(float) * CONVOLVER_NODE.padded_len);
1448 memset(CONVOLVER_NODE.RIGHT_OUT, 0, sizeof(float) * CONVOLVER_NODE.padded_len);
1449
1450
1451 for (int i = 0; i < NUM_PARTITIONS; i++)
1452 {
1453
1454 int history_idx = (node->history_head - i + NUM_PARTITIONS_MAX) % NUM_PARTITIONS_MAX;
1455
1456 fftwf_complex * X = node->AUDIO_HISTORY[history_idx];
1457 fftwf_complex * prev_ir_left = (*node->ir)[0][i];
1458 fftwf_complex * prev_ir_right = (*node->ir)[1][i];
1459
1460 for (int j = 0; j < node->complex_len; j++)
1461 {
1462
1463 node->SUM_L[j][0] += X[j][0] * prev_ir_left[j][0] - X[j][1] * prev_ir_left[j][1];
1464 node->SUM_L[j][1] += X[j][0] * prev_ir_left[j][1] + X[j][1] * prev_ir_left[j][0];
1465
1466 node->SUM_R[j][0] += X[j][0] * prev_ir_right[j][0] - X[j][1] * prev_ir_right[j][1];
1467 node->SUM_R[j][1] += X[j][0] * prev_ir_right[j][1] + X[j][1] * prev_ir_right[j][0];
1468
1469 }
1470
1471 }
1472
1473
1474 fftwf_execute_dft_c2r(node->bwd_plan, node->SUM_L, node->LEFT_OUT);
1475 fftwf_execute_dft_c2r(node->bwd_plan, node->SUM_R, node->RIGHT_OUT);
1476
1477
1478
1479
1480 memcpy(CONVOLVER_NODE.TAIL_LEFT, &node->LEFT_OUT[node->block_size], node->tail_len * sizeof(float));
1481 memcpy(CONVOLVER_NODE.TAIL_RIGHT, &node->RIGHT_OUT[node->block_size], node->tail_len * sizeof(float));
1482 float scale = 1.0f / node->padded_len;
1483 for (int i = 0; i < node->tail_len; i++)
1484 {
1485 CONVOLVER_NODE.TAIL_LEFT[i] *= scale;
1486 CONVOLVER_NODE.TAIL_RIGHT[i] *= scale;
1487 }
1488 }
1489
1490 if (CONVOLVER_NODE.prev_ir == NULL)
1491 {
1492
1493 for (int i = 0; i < node->block_size; i++)
1494 {
1495 pFramesOut_0[i * 2 + 0] = pFramesIn_0[i * 2 + 0] * EDITOR_SETTINGS.volume;
1496 pFramesOut_0[i * 2 + 1] = pFramesIn_0[i * 2 + 1] * EDITOR_SETTINGS.volume;
1497 }
1498 return;
1499
1500 }
1501
1502
1503 for (int i = 0; i < node->block_size; i++)
1504 {
1505
1506 node->LEFT_OUT[i] = (pFramesIn_0[i * 2 + 0] + pFramesIn_0[i * 2 + 1]) * 0.5f;
1507
1508 }
1509 memset(&CONVOLVER_NODE.LEFT_OUT[node->block_size], 0, sizeof(float) * (CONVOLVER_NODE.padded_len - node->block_size));
1510
1511 memset(node->PREV_SUM_L, 0, sizeof(fftwf_complex) * node->complex_len);
1512 memset(node->PREV_SUM_R, 0, sizeof(fftwf_complex) * node->complex_len);
1513
1515 {
1516
1517 memset(node->SUM_L, 0, sizeof(fftwf_complex) * node->complex_len);
1518 memset(node->SUM_R, 0, sizeof(fftwf_complex) * node->complex_len);
1519
1520 }
1521
1523
1524 float norm = 1.0f / node->padded_len;
1525 for (int i = 0; i < node->block_size; i++)
1526 {
1527
1528 float alpha = (float)CONVOLVER_NODE.crossfade_pos / CROSSFADE_FRAMES;
1529 if (alpha > 1.0f) alpha = 1.0f;
1530 float gain_cur = 1.0f-alpha;
1531 float gain_target = alpha;
1532
1533 float interp_l = 0.0f;
1534 float interp_r = 0.0f;
1536 {
1537
1538
1539 interp_l = ((node->LEFT_OUT[i] * norm + node->TAIL_LEFT[i]) * gain_target + (node->PREV_LEFT_OUT[i] * norm + node->PREV_TAIL_LEFT[i]) * gain_cur);
1540 interp_r = ((node->RIGHT_OUT[i] * norm + node->TAIL_RIGHT[i]) * gain_target + (node->PREV_RIGHT_OUT[i] * norm + node->PREV_TAIL_RIGHT[i]) * gain_cur);
1541
1542 }
1543 else
1544 {
1545
1546 interp_l = (node->PREV_LEFT_OUT[i] * norm + node->PREV_TAIL_LEFT[i]);
1547 interp_r = (node->PREV_RIGHT_OUT[i] * norm + node->PREV_TAIL_RIGHT[i]);
1548
1549 }
1550
1551 pFramesOut_0[i * 2 + 0] = pFramesIn_0[i * 2 + 0] * EDITOR_SETTINGS.volume + interp_l * EDITOR_SETTINGS.reverb_wet;
1552 pFramesOut_0[i * 2 + 1] = pFramesIn_0[i * 2 + 1] * EDITOR_SETTINGS.volume + interp_r * EDITOR_SETTINGS.reverb_wet;
1553
1555 {
1556
1557 node->TAIL_LEFT[i] = (node->LEFT_OUT[i + node->block_size] * norm);
1558 node->TAIL_RIGHT[i] = (node->RIGHT_OUT[i + node->block_size] * norm);
1559
1560 }
1561 // since we only crossfade one frame this is fine to optimize since we will always be swapping to tail left / right instead of keeping this one anyway
1562 node->PREV_TAIL_LEFT[i] = (node->PREV_LEFT_OUT[i + node->block_size] * norm);
1563 node->PREV_TAIL_RIGHT[i] = (node->PREV_RIGHT_OUT[i + node->block_size] * norm);
1564
1565
1567 }
1568
1569
1571 {
1572
1573 SDL_AtomicLock(&HRTF_TD.ir_swap_lock);
1574 {
1576 CONVOLVER_NODE.ir = NULL;
1577 }
1578 SDL_AtomicUnlock(&HRTF_TD.ir_swap_lock);
1579 memcpy(node->PREV_TAIL_LEFT, node->TAIL_LEFT, sizeof(float) * node->tail_len);
1580 memcpy(node->PREV_TAIL_RIGHT, node->TAIL_RIGHT, sizeof(float) * node->tail_len);
1583
1584 }
1585
1586}
1587
1588
1589
1590
1591
1592static ma_node_vtable my_custom_node_vtable =
1593{
1595 NULL,
1596 2,
1597 1,
1598 0
1599};
1600
1602{
1603
1604
1605 int tail_len = 512;
1606 int padded_size = 1024;
1607 int complex_len = (padded_size / 2) + 1;
1608
1609 AUDIO_SCRATCH.aprobe_mutex = SDL_CreateMutex();
1610
1611 AUDIO_SCRATCH.block_size = tail_len;
1612 AUDIO_SCRATCH.padded_len = padded_size;
1613 AUDIO_SCRATCH.complex_len = complex_len;
1614
1615 AUDIO_SCRATCH.fwd_plan = fftwf_plan_dft_r2c_1d(padded_size, NULL, NULL, FFTW_ESTIMATE);
1616 AUDIO_SCRATCH.bwd_plan = fftwf_plan_dft_c2r_1d(padded_size, NULL, NULL, FFTW_ESTIMATE);
1617
1618 AUDIO_SCRATCH.HRTF_IR_IN = fftwf_malloc(sizeof(float) * padded_size);
1619 memset(AUDIO_SCRATCH.HRTF_IR_IN, 0, sizeof(float) * padded_size);
1620
1621 CONVOLVER_NODE.tail_len = tail_len;
1622 CONVOLVER_NODE.PREV_TAIL_LEFT = calloc(tail_len, sizeof(float));
1623 CONVOLVER_NODE.PREV_TAIL_RIGHT = calloc(tail_len, sizeof(float));
1624 CONVOLVER_NODE.TAIL_LEFT = calloc(tail_len, sizeof(float));
1625 CONVOLVER_NODE.TAIL_RIGHT = calloc(tail_len, sizeof(float));
1626
1627 CONVOLVER_NODE.block_size = tail_len;
1628 CONVOLVER_NODE.padded_len = padded_size;
1629 CONVOLVER_NODE.complex_len = complex_len;
1630
1631
1632 CONVOLVER_NODE.fwd_plan = fftwf_plan_dft_r2c_1d(padded_size, NULL, NULL, FFTW_ESTIMATE);
1633 CONVOLVER_NODE.bwd_plan = fftwf_plan_dft_c2r_1d(padded_size, NULL, NULL, FFTW_ESTIMATE);
1634
1635 CONVOLVER_NODE.MONO_FWD = fftwf_malloc(sizeof(fftwf_complex) * complex_len);
1636 CONVOLVER_NODE.PREV_SUM_L = fftwf_malloc(sizeof(fftwf_complex) * complex_len);
1637 CONVOLVER_NODE.PREV_SUM_R = fftwf_malloc(sizeof(fftwf_complex) * complex_len);
1638 CONVOLVER_NODE.SUM_L = fftwf_malloc(sizeof(fftwf_complex) * complex_len);
1639 CONVOLVER_NODE.SUM_R = fftwf_malloc(sizeof(fftwf_complex) * complex_len);
1640 for (int i = 0; i < NUM_PARTITIONS_MAX; i++)
1641 {
1642
1643 CONVOLVER_NODE.AUDIO_HISTORY[i] = fftwf_malloc(sizeof(fftwf_complex) * complex_len);
1644 memset(CONVOLVER_NODE.AUDIO_HISTORY[i], 0, sizeof(fftwf_complex) * complex_len);
1645 }
1646
1647 CONVOLVER_NODE.PREV_LEFT_OUT = fftwf_malloc(sizeof(float) * padded_size);
1648 CONVOLVER_NODE.PREV_RIGHT_OUT = fftwf_malloc(sizeof(float) * padded_size);
1649 CONVOLVER_NODE.LEFT_OUT = fftwf_malloc(sizeof(float) * padded_size);
1650 CONVOLVER_NODE.RIGHT_OUT = fftwf_malloc(sizeof(float) * padded_size);
1651 CONVOLVER_NODE.ir = NULL;
1652 CONVOLVER_NODE.prev_ir = NULL;
1653
1654 memset(CONVOLVER_NODE.MONO_FWD, 0, sizeof(fftwf_complex) * complex_len);
1655 memset(CONVOLVER_NODE.PREV_SUM_L, 0, sizeof(fftwf_complex) * complex_len);
1656 memset(CONVOLVER_NODE.PREV_SUM_R, 0, sizeof(fftwf_complex) * complex_len);
1657 memset(CONVOLVER_NODE.SUM_L, 0, sizeof(fftwf_complex) * complex_len);
1658 memset(CONVOLVER_NODE.SUM_R, 0, sizeof(fftwf_complex) * complex_len);
1659 memset(CONVOLVER_NODE.PREV_LEFT_OUT, 0, sizeof(float) * padded_size);
1660 memset(CONVOLVER_NODE.PREV_RIGHT_OUT, 0, sizeof(float) * padded_size);
1661 memset(CONVOLVER_NODE.LEFT_OUT, 0, sizeof(float) * padded_size);
1662 memset(CONVOLVER_NODE.RIGHT_OUT, 0, sizeof(float) * padded_size);
1663
1665
1667
1668
1669}
1670
1672{
1673
1674 for (int i = 0; i < fbo->texture_count; i++)
1675 {
1676
1677 glDeleteTextures(1, &fbo->texture[i].i);
1678
1679 }
1680 for (int i = 0; i < fbo->rbo_count; i++)
1681 {
1682
1683 glDeleteRenderbuffers(1, &fbo->renderbuffers[i].i);
1684
1685 }
1686 glDeleteTextures(1, &fbo->depth_texture.i);
1687 glDeleteRenderbuffers(1, &fbo->depth_rbo.i);
1688 glDeleteFramebuffers(1, &fbo->fbo);
1689
1690 memset(fbo, 0, sizeof(Framebuffer));
1691
1692}
1693
1694void init_gl_renderbuffer(GLRenderbuffer * rbo, GLint internal_format, GLsizei w, GLsizei h, bool multisampled, int samples)
1695{
1696
1697
1698 glGenRenderbuffers(1, &rbo->i);
1699 glBindRenderbuffer(GL_RENDERBUFFER, rbo->i);
1700 rbo->multisampled = multisampled;
1701 rbo->samples = samples;
1702 rbo->w = w;
1703 rbo->h = h;
1704 rbo->internal_format = internal_format;
1705 if (rbo->multisampled)
1706 {
1707 glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, internal_format, w, h);
1708 }
1709}
1710
1712{
1713
1716
1717
1720 MAIN_FBO.rbo_count = 1;
1722 (
1724 GL_RGBA16F,
1725 MAIN_FBO.w,
1726 MAIN_FBO.h,
1727 GL_TRUE,
1729 );
1731 (
1733 GL_DEPTH_COMPONENT24,
1734 MAIN_FBO.w,
1735 MAIN_FBO.h,
1736 GL_TRUE,
1738 );
1739
1741
1742
1746
1749 (
1750 GL_TEXTURE_2D,
1751 GL_RGBA16F,
1752 GL_RGBA,
1753 GL_FLOAT,
1754 GL_LINEAR,
1755 GL_LINEAR,
1756 GL_CLAMP_TO_EDGE,
1757 GL_CLAMP_TO_EDGE,
1758 0,
1761 0,
1762 0,
1764 );
1766
1767}
1768
1770{
1771
1774
1778
1780 (
1782 GL_RG32F,
1783 GBUFFER_FBO.w,
1784 GBUFFER_FBO.h,
1785 GL_TRUE,
1787 );
1789 (
1791 GL_DEPTH_COMPONENT24,
1792 GBUFFER_FBO.w,
1793 GBUFFER_FBO.h,
1794 GL_TRUE,
1796 );
1797
1799
1805 (
1806 GL_TEXTURE_2D,
1807 GL_RG32F,
1808 GL_RG,
1809 GL_FLOAT,
1810 GL_NEAREST,
1811 GL_NEAREST,
1812 GL_CLAMP_TO_EDGE,
1813 GL_CLAMP_TO_EDGE,
1814 0,
1817 0,
1818 0,
1820 );
1821
1824 (
1825 GL_TEXTURE_2D,
1826 GL_DEPTH_COMPONENT24,
1827 GL_DEPTH_COMPONENT,
1828 GL_FLOAT,
1829 GL_NEAREST,
1830 GL_NEAREST,
1831 GL_CLAMP_TO_BORDER,
1832 GL_CLAMP_TO_BORDER,
1833 0,
1836 0,
1837 0,
1839 );
1841
1842}
1843
1845{
1846
1849
1855 (
1856 GL_TEXTURE_2D,
1857 GL_RGBA16F,
1858 GL_RGBA,
1859 GL_FLOAT,
1860 GL_NEAREST,
1861 GL_NEAREST,
1862 GL_CLAMP_TO_EDGE,
1863 GL_CLAMP_TO_EDGE,
1864 0,
1867 0,
1868 0,
1870 );
1873 (
1874 GL_TEXTURE_2D,
1875 GL_RGBA8,
1876 GL_RGBA,
1877 GL_UNSIGNED_BYTE,
1878 GL_NEAREST,
1879 GL_NEAREST,
1880 GL_CLAMP_TO_EDGE,
1881 GL_CLAMP_TO_EDGE,
1882 0,
1885 0,
1886 0,
1888 );
1891 (
1892 GL_TEXTURE_2D,
1893 GL_RGBA8,
1894 GL_RGBA,
1895 GL_UNSIGNED_BYTE,
1896 GL_NEAREST,
1897 GL_NEAREST,
1898 GL_CLAMP_TO_EDGE,
1899 GL_CLAMP_TO_EDGE,
1900 0,
1903 0,
1904 0,
1906 );
1909 (
1910 GL_TEXTURE_2D,
1911 GL_RG32F,
1912 GL_RG,
1913 GL_FLOAT,
1914 GL_NEAREST,
1915 GL_NEAREST,
1916 GL_CLAMP_TO_EDGE,
1917 GL_CLAMP_TO_EDGE,
1918 0,
1921 0,
1922 0,
1924 );
1927 (
1928 GL_TEXTURE_2D,
1929 GL_R32F,
1930 GL_RED,
1931 GL_FLOAT,
1932 GL_NEAREST,
1933 GL_NEAREST,
1934 GL_CLAMP_TO_EDGE,
1935 GL_CLAMP_TO_EDGE,
1936 0,
1939 0,
1940 0,
1942 );
1945 (
1946 GL_TEXTURE_2D,
1947 GL_RG16F,
1948 GL_RG,
1949 GL_FLOAT,
1950 GL_NEAREST,
1951 GL_NEAREST,
1952 GL_CLAMP_TO_EDGE,
1953 GL_CLAMP_TO_EDGE,
1954 0,
1957 0,
1958 0,
1960 );
1961
1962
1965 (
1966 GL_TEXTURE_2D,
1967 GL_DEPTH_COMPONENT24,
1968 GL_DEPTH_COMPONENT,
1969 GL_FLOAT,
1970 GL_NEAREST,
1971 GL_NEAREST,
1972 GL_CLAMP_TO_BORDER,
1973 GL_CLAMP_TO_BORDER,
1974 0,
1977 0,
1978 0,
1980 );
1981
1983}
1984
1985
1986bool
1988 (
1989 SDL_Window ** window,
1990 SDL_GLContext * context
1991 )
1992
1993{
1994
1995 printf("Initializing\n");
1996 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
1997 printf("SDL Init failed: %s\n", SDL_GetError());
1998 return false;
1999 }
2000
2001 #ifdef INCLUDE_CONFIGURATOR
2002 static struct nk_color color_table[NK_COLOR_COUNT];
2003 memcpy(color_table, nk_default_color_style, sizeof(color_table));
2004 #endif
2005
2006 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
2007 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
2008 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
2009 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2010 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
2011 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
2012
2013
2014 *window = SDL_CreateWindow("Untitled", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
2015 SDL_SetWindowGrab(*window, SDL_TRUE);
2016 SDL_SetRelativeMouseMode(SDL_TRUE);
2017 if (!*window) {
2018 printf("Window creation failed: %s\n", SDL_GetError());
2019 return false;
2020 }
2021
2022 *context = SDL_GL_CreateContext(*window);
2023 if (!*context) {
2024 printf("GL Context failed: %s\n", SDL_GetError());
2025 return false;
2026 }
2027
2028 glewExperimental = GL_TRUE;
2029 if (glewInit() != GLEW_OK) {
2030 printf("GLEW Init failed\n");
2031 return false;
2032 }
2033
2034 NK_CTX = nk_sdl_init(*window);
2035 if (!NK_CTX)
2036 {
2037 printf("NUKLEAR INIT FAILED\n");
2038 return false;
2039 }
2040
2041 glEnable(GL_MULTISAMPLE);
2042 SDL_GL_SetSwapInterval(0);
2044
2045 UNDO_INIT();
2046 UI_INIT();
2047 FONTS_INIT();
2048 STATES_INIT();
2049
2050 init_band_kernels(48000, 125.0f, 250.0f, 500.0f, 1000.0f, 2000.0f, BAND_IMPULSES);
2051
2052 ma_engine_config ENGINE_CONFIG;
2053
2054 ENGINE_CONFIG = ma_engine_config_init();
2055
2056 ENGINE_CONFIG.noDevice = MA_TRUE;
2057
2058 ENGINE_CONFIG.channels = 2;
2059 ENGINE_CONFIG.sampleRate = 48000;
2060 ENGINE_CONFIG.periodSizeInFrames = 512;
2061 ma_result result = ma_engine_init(&ENGINE_CONFIG, &AUDIO_ENGINE);
2062 if (result != MA_SUCCESS) {
2063 printf("AUDIO ENGINE INIT FAILED\n");
2064 return false;
2065 }
2066
2067 // order is important here. make sure to init bufferrs before attaching the node?
2068
2070
2071 ma_uint32 input_channels[1] = {2};
2072 ma_uint32 output_channels[1] = {2};
2073
2074
2076
2077 load_hrtf_from_binary("./HRTF.bin");
2078
2079 ma_node_config node_config = ma_node_config_init();
2080
2081 node_config.vtable = &my_custom_node_vtable;
2082 node_config.pInputChannels = input_channels;
2083 node_config.pOutputChannels = output_channels;
2084
2085 ma_result init_res = ma_node_init(ma_engine_get_node_graph(&AUDIO_ENGINE), &node_config, NULL, &CONVOLVER_NODE.base);
2086 if (init_res != MA_SUCCESS)
2087 {
2088 printf("INIT FAILED\n");
2089 }
2090
2091 ma_result at_res = ma_node_attach_output_bus(&CONVOLVER_NODE, 0, ma_engine_get_endpoint(&AUDIO_ENGINE), 0);
2092 if (at_res != MA_SUCCESS)
2093 {
2094 printf("AT FAILED\n");
2095 }
2096
2097 ma_result mut_res = ma_mutex_init(&CONVOLVER_NODE.lock);
2098 if (mut_res != MA_SUCCESS)
2099 {
2100 printf("AUDIO MUTEX FAILED\n");
2101 }
2102
2103
2104 if (!init_globals())
2105 {
2106 printf("GLOBALS FAILED TO INIT\n");
2107 return false;
2108
2109 }
2112
2113 init_player();
2114
2116
2117
2118 SDL_GL_SetSwapInterval(0);
2119 {struct nk_font_atlas *atlas;
2120 nk_sdl_font_stash_begin(&atlas);
2121 nk_sdl_font_stash_end();
2122 }
2123
2124 #ifdef INCLUDE_STYLE
2125 #ifdef STYLE_WHITE
2126 set_style(ctx, THEME_WHITE);
2127 #elif defined(STYLE_RED)
2128 set_style(ctx, THEME_RED);
2129 #elif defined(STYLE_BLUE)
2130 set_style(ctx, THEME_BLUE);
2131 #elif defined(STYLE_DARK)
2132 set_style(ctx, THEME_DARK);
2133 #endif
2134 #endif
2135
2136 // glEnable(GL_MULTISAMPLE);
2137
2138 struct nk_color transparent = nk_rgba(30, 30, 30, 90);
2139 struct nk_color border = nk_rgba(60, 60, 60, 150);
2140 struct nk_color text = nk_rgba(255, 255, 255, 255);
2141
2142 NK_CTX->style.window.fixed_background = nk_style_item_color(transparent);
2143 NK_CTX->style.window.border_color = border;
2144 NK_CTX->style.window.padding = nk_vec2(10, 10);
2145 NK_CTX->style.window.border = 1;
2146
2147 NK_CTX->style.text.color = text;
2148
2149
2150
2151 SDL_LockAudioDevice(SDL_AUDIO_DEVICE);
2152
2153 AudioEmitter * emitter = init_emitter("./sounds/byzantine columns 5.mp3", true, true, 0.5);
2154 play_emitter(emitter, (vec3){3,5,2});
2155 AudioEmitter * fs_emitter = init_emitter("./sounds/footstep_01.wav", false, false, 0.5);
2156 FOOTSTEP_EMITTER = fs_emitter;
2157
2158 SDL_UnlockAudioDevice(SDL_AUDIO_DEVICE);
2159
2160
2161
2163
2167 TEXT_FBO.texture[0] =
2169 (
2170 GL_TEXTURE_2D,
2171 GL_RGBA8,
2172 GL_RGBA,
2173 GL_UNSIGNED_BYTE,
2174 GL_LINEAR,
2175 GL_LINEAR,
2176 GL_CLAMP_TO_EDGE,
2177 GL_CLAMP_TO_EDGE,
2178 0,
2179 TEXT_FBO.w,
2180 TEXT_FBO.h,
2181 0,
2182 0,
2184 );
2185
2187
2188 glGenTextures(1, &LIGHTMAP_POS_TEXTURE);
2189 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_POS_TEXTURE);
2190 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, LIGHTMAP_WIDTH, LIGHTMAP_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
2191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2193 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2195 glGenTextures(1, &LIGHTMAP_NORM_TEXTURE);
2196 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_NORM_TEXTURE);
2197 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, LIGHTMAP_WIDTH, LIGHTMAP_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
2198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2202 glGenTextures(1, &LIGHTMAP_ID_TEXTURE);
2203 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_ID_TEXTURE);
2204 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32I, LIGHTMAP_WIDTH, LIGHTMAP_HEIGHT, 0, GL_RGB_INTEGER, GL_INT, NULL);
2205 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2206 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2209
2210
2211 glGenFramebuffers(1, &LIGHTMAP_FBO);
2212 glBindFramebuffer(GL_FRAMEBUFFER, LIGHTMAP_FBO);
2213 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, LIGHTMAP_POS_TEXTURE, 0);
2214 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, LIGHTMAP_NORM_TEXTURE, 0);
2215 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, LIGHTMAP_ID_TEXTURE, 0);
2216
2217 GLenum draw_buffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2};
2218 glDrawBuffers(3, draw_buffers);
2219
2220
2221 glGenTextures(1, &BRDF_LUT);
2222 glBindTexture(GL_TEXTURE_2D, BRDF_LUT);
2223
2224 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16F, 512, 512, 0, GL_RG, GL_FLOAT, NULL);
2225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2226 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2229 glGenFramebuffers(1, &BRDF_FBO);
2230 glBindFramebuffer(GL_FRAMEBUFFER, BRDF_FBO);
2231 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, BRDF_LUT, 0);
2232
2233
2234
2235 glGenTextures(1, &LPV_TEXTURE);
2236 glBindTexture(GL_TEXTURE_3D, LPV_TEXTURE);
2237 glTexStorage3D(GL_TEXTURE_3D, 1, GL_RGB32F, PROBE_VOLUME_WIDTH, PROBE_VOLUME_HEIGHT, PROBE_VOLUME_DEPTH_PADDED * SH_MAX * LPV_SLOTS);
2238 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2239 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2240 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2241 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2242 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2243
2244
2245 glGenTextures(1, &RPROBE_CUBE_MAP_ARRAY);
2246 glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, RPROBE_CUBE_MAP_ARRAY);
2247 glTexStorage3D(GL_TEXTURE_CUBE_MAP_ARRAY, MAX_RPROBE_MIPS, GL_RGBA16F, RPROBE_WIDTH, RPROBE_HEIGHT, 6);
2248 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
2249 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2250 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2251 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2252 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2253
2254 glGenTextures(1, &RPROBE_DEPTH_TEXTURE);
2255 glBindTexture(GL_TEXTURE_2D, RPROBE_DEPTH_TEXTURE);
2256
2257 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, RPROBE_WIDTH, RPROBE_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
2258 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
2263
2264 glGenFramebuffers(1, &RPROBE_FBO);
2265 glBindFramebuffer(GL_FRAMEBUFFER, RPROBE_FBO);
2266 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_ARRAY, RPROBE_CUBE_MAP_ARRAY, 0, 0);
2267 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, RPROBE_DEPTH_TEXTURE, 0);
2268
2269
2270 glGenTextures(1, &TEMP_RPROBE_CUBE_MAP);
2271 glBindTexture(GL_TEXTURE_CUBE_MAP, TEMP_RPROBE_CUBE_MAP);
2272
2273 glTexStorage2D(GL_TEXTURE_CUBE_MAP, 1, GL_RGBA16F, RPROBE_WIDTH, RPROBE_HEIGHT);
2274 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2275 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2276 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2277 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2278 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2279
2280 glGenTextures(1, &BLENDED_RPROBE_CUBE_MAP);
2281 glBindTexture(GL_TEXTURE_CUBE_MAP, BLENDED_RPROBE_CUBE_MAP);
2282
2283 glTexStorage2D(GL_TEXTURE_CUBE_MAP, MAX_RPROBE_MIPS, GL_RGBA8, RPROBE_WIDTH, RPROBE_HEIGHT);
2284 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
2285 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2286 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2287 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2288 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2289
2290 glGenTextures(1, &RPROBE_BACK_TEXTURE);
2291 glBindTexture(GL_TEXTURE_2D, RPROBE_BACK_TEXTURE);
2292
2293 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, RPROBE_WIDTH, RPROBE_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
2295 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2296 glGenerateMipmap(GL_TEXTURE_2D);
2297 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2298 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2299
2300 glGenFramebuffers(1, &RPROBE_BACK_FBO);
2301 glBindFramebuffer(GL_FRAMEBUFFER, RPROBE_BACK_FBO);
2302 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, RPROBE_BACK_TEXTURE, 0);
2303
2304
2305
2306
2307
2308 float border_color[] = {1.0f, 1.0f, 1.0f, 1.0f};
2309
2315 (
2316 GL_TEXTURE_CUBE_MAP_ARRAY,
2317 GL_DEPTH_COMPONENT16,
2318 GL_DEPTH_COMPONENT,
2319 GL_FLOAT,
2320 GL_LINEAR,
2321 GL_LINEAR,
2322 GL_CLAMP_TO_EDGE,
2323 GL_CLAMP_TO_EDGE,
2324 GL_CLAMP_TO_EDGE,
2328 1,
2330 );
2331
2333
2338
2341 (
2342 GL_TEXTURE_2D_ARRAY,
2343 GL_DEPTH_COMPONENT24,
2344 GL_DEPTH_COMPONENT,
2345 GL_FLOAT,
2346 GL_LINEAR,
2347 GL_LINEAR,
2348 GL_CLAMP_TO_BORDER,
2349 GL_CLAMP_TO_BORDER,
2350 0,
2354 0,
2356 );
2357 // we are leaving this here because i don't want to deal with the abstraction it probably doesnt fucking matter anyway
2358 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);
2360
2361
2362
2363
2364
2365
2366
2372 (
2373 GL_TEXTURE_2D,
2374 GL_R32F,
2375 GL_RED,
2376 GL_FLOAT,
2377 GL_NEAREST,
2378 GL_NEAREST,
2379 GL_CLAMP_TO_BORDER,
2380 GL_CLAMP_TO_BORDER,
2381 0,
2384 0,
2385 0,
2387 );
2388
2389
2391
2395 SSR_FBO.texture[0] =
2397 (
2398 GL_TEXTURE_2D,
2399 GL_RGBA16F,
2400 GL_RGBA,
2401 GL_FLOAT,
2402 GL_LINEAR,
2403 GL_LINEAR,
2404 GL_CLAMP_TO_BORDER,
2405 GL_CLAMP_TO_BORDER,
2406 0,
2407 SSR_FBO.w,
2408 SSR_FBO.h,
2409 0,
2410 0,
2412 );
2413 init_fbo(&SSR_FBO);
2419 (
2420 GL_TEXTURE_2D,
2421 GL_RGBA16F,
2422 GL_RGBA,
2423 GL_FLOAT,
2424 GL_LINEAR,
2425 GL_LINEAR,
2426 GL_CLAMP_TO_BORDER,
2427 GL_CLAMP_TO_BORDER,
2428 0,
2431 0,
2432 0,
2434 );
2436
2437
2438 TAA_FBO[0].w = RENDER_WIDTH;
2439 TAA_FBO[0].h = RENDER_HEIGHT;
2440 TAA_FBO[0].texture_count = 1;
2441 TAA_FBO[0].texture[0] =
2443 (
2444 GL_TEXTURE_2D,
2445 GL_RGBA16F,
2446 GL_RGBA,
2447 GL_FLOAT,
2448 GL_LINEAR_MIPMAP_LINEAR,
2449 GL_LINEAR,
2450 GL_CLAMP_TO_EDGE,
2451 GL_CLAMP_TO_EDGE,
2452 0,
2453 GBUFFER_FBO.w,
2454 GBUFFER_FBO.h,
2455 0,
2456 0,
2458 );
2459 init_fbo(&TAA_FBO[0]);
2460 TAA_FBO[1].w = RENDER_WIDTH;
2461 TAA_FBO[1].h = RENDER_HEIGHT;
2462 TAA_FBO[1].texture_count = 1;
2463 TAA_FBO[1].texture[0] =
2465 (
2466 GL_TEXTURE_2D,
2467 GL_RGBA16F,
2468 GL_RGBA,
2469 GL_FLOAT,
2470 GL_LINEAR,
2471 GL_LINEAR,
2472 GL_CLAMP_TO_EDGE,
2473 GL_CLAMP_TO_EDGE,
2474 0,
2475 GBUFFER_FBO.w,
2476 GBUFFER_FBO.h,
2477 0,
2478 0,
2480 );
2481 init_fbo(&TAA_FBO[1]);
2482
2483
2484
2488 COC_FBO.texture[0] =
2490 (
2491 GL_TEXTURE_2D,
2492 GL_RGBA16F,
2493 GL_RGBA,
2494 GL_FLOAT,
2495 GL_LINEAR,
2496 GL_LINEAR,
2497 GL_CLAMP_TO_EDGE,
2498 GL_CLAMP_TO_EDGE,
2499 0,
2500 COC_FBO.w,
2501 COC_FBO.h,
2502 0,
2503 0,
2505 );
2506
2507 init_fbo(&COC_FBO);
2508
2512 BOKEH_FBO.texture[0] =
2514 (
2515 GL_TEXTURE_2D,
2516 GL_RGBA16F,
2517 GL_RGBA,
2518 GL_FLOAT,
2519 GL_LINEAR,
2520 GL_LINEAR,
2521 GL_CLAMP_TO_EDGE,
2522 GL_CLAMP_TO_EDGE,
2523 0,
2524 BOKEH_FBO.w,
2525 BOKEH_FBO.h,
2526 0,
2527 0,
2529 );
2530
2532
2533 int current_width = BLOOM_WIDTH;
2534 int current_height = BLOOM_HEIGHT;
2535 for (int i = 0; i < 5; i++)
2536 {
2537
2538 Framebuffer * fbo = &BLOOM_PYRAMID[i];
2539 fbo->w = current_width;
2540 fbo->h = current_height;
2541 fbo->texture_count = 1;
2542 fbo->texture[0] =
2544 (
2545 GL_TEXTURE_2D,
2546 GL_RGBA16F,
2547 GL_RGBA,
2548 GL_FLOAT,
2549 GL_LINEAR,
2550 GL_LINEAR,
2551 GL_CLAMP_TO_EDGE,
2552 GL_CLAMP_TO_EDGE,
2553 0,
2554 fbo->w,
2555 fbo->h,
2556 0,
2557 0,
2559 );
2560
2561 init_fbo(fbo);
2562
2563
2564 current_width /= 2;
2565 current_height /= 2;
2566
2567 }
2568
2569
2571 {
2573 }
2574 else
2575 {
2576
2578 }
2579
2580
2581
2582
2584
2585
2589 SSAO_FBO.texture[0] =
2591 (
2592 GL_TEXTURE_2D,
2593 GL_RED,
2594 GL_RED,
2595 GL_FLOAT,
2596 GL_LINEAR,
2597 GL_LINEAR,
2598 GL_CLAMP_TO_EDGE,
2599 GL_CLAMP_TO_EDGE,
2600 0,
2601 SSAO_FBO.w,
2602 SSAO_FBO.h,
2603 0,
2604 0,
2606 );
2607
2609
2615 (
2616 GL_TEXTURE_2D,
2617 GL_RED,
2618 GL_RED,
2619 GL_FLOAT,
2620 GL_LINEAR,
2621 GL_LINEAR,
2622 GL_CLAMP_TO_EDGE,
2623 GL_CLAMP_TO_EDGE,
2624 0,
2627 0,
2628 0,
2630 );
2631
2633
2635 {
2636 glBindFramebuffer(GL_FRAMEBUFFER, MAIN_FBO.fbo);
2637 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, GBUFFER_FBO.depth_rbo.i);
2638
2639 }
2640 else
2641 {
2642
2643 glBindFramebuffer(GL_FRAMEBUFFER, MAIN_RESOLVE_FBO.fbo);
2644 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, GBUFFER_RESOLVE_FBO.depth_texture.i, 0);
2645 }
2646
2647
2648 float noise[16][4];
2649 for (int i = 0; i < 16; i++) {
2650 noise[i][0] = ((float)rand() / RAND_MAX) * 2.0f - 1.0f;
2651 noise[i][1] = ((float)rand() / RAND_MAX) * 2.0f - 1.0f;
2652 noise[i][2] = ((float)rand() / RAND_MAX) * 2.0f - 1.0f;
2653 noise[i][3] = 1.0f;
2654 }
2655 glGenTextures(1, &SSAO_NOISE_TEXTURE);
2656 glBindTexture(GL_TEXTURE_2D, SSAO_NOISE_TEXTURE);
2657 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, 4, 4, 0, GL_RGBA, GL_FLOAT, noise);
2658 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2659 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2660 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2661 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2662
2663
2664
2665
2666 glGenTextures(1, &SOBEL_TEXTURE[0]);
2667 glBindTexture(GL_TEXTURE_2D, SOBEL_TEXTURE[0]);
2668 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, RENDER_WIDTH, RENDER_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
2669 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2670 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2671 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2672 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2673
2674 glGenFramebuffers(1, &SOBEL_FBO[0]);
2675 glBindFramebuffer(GL_FRAMEBUFFER, SOBEL_FBO[0]);
2676 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, SOBEL_TEXTURE[0], 0);
2677
2678 glGenTextures(1, &SOBEL_TEXTURE[1]);
2679 glBindTexture(GL_TEXTURE_2D, SOBEL_TEXTURE[1]);
2680 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, RENDER_WIDTH, RENDER_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
2681 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2682 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2683 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2684 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2685
2686 glGenFramebuffers(1, &SOBEL_FBO[1]);
2687 glBindFramebuffer(GL_FRAMEBUFFER, SOBEL_FBO[1]);
2688 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, SOBEL_TEXTURE[1], 0);
2689
2690 glGenTextures(1, &HISTORY_TEXTURE[0]);
2691 glBindTexture(GL_TEXTURE_2D, HISTORY_TEXTURE[0]);
2692 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, RENDER_WIDTH, RENDER_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
2693 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2694 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2695 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2696 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2697
2698 glGenFramebuffers(1, &HISTORY_FBO[0]);
2699 glBindFramebuffer(GL_FRAMEBUFFER, HISTORY_FBO[0]);
2700 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, HISTORY_TEXTURE[0], 0);
2701
2702 glGenTextures(1, &HISTORY_TEXTURE[1]);
2703 glBindTexture(GL_TEXTURE_2D, HISTORY_TEXTURE[1]);
2704 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, RENDER_WIDTH, RENDER_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
2705 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2706 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2707 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2708 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2709
2710 glGenFramebuffers(1, &HISTORY_FBO[1]);
2711 glBindFramebuffer(GL_FRAMEBUFFER, HISTORY_FBO[1]);
2712 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, HISTORY_TEXTURE[1], 0);
2713
2714
2715
2716 glGenTextures(1, &FBM_TEXTURE);
2717 glBindTexture(GL_TEXTURE_2D, FBM_TEXTURE);
2718
2719 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, FBM_TEXTURE_SIZE, FBM_TEXTURE_SIZE, 0, GL_RGB, GL_FLOAT, NULL);
2720 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2721 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2722 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2723 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2724
2726
2727 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2728
2729 GLbitfield ubo_flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
2730
2731 GLint ubo_alignment;
2732 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &ubo_alignment);
2733 printf("SSBO\n");
2734
2735
2736 glGenBuffers(1, &CAM_UBO);
2737 glBindBuffer(GL_UNIFORM_BUFFER, CAM_UBO);
2738 cam_ubo_padded = (sizeof(CamData) + ubo_alignment - 1) / ubo_alignment * ubo_alignment;
2739 glBufferStorage(GL_UNIFORM_BUFFER, cam_ubo_padded * BUFFER_FRAMES, NULL, ubo_flags);
2740 cam_ubo_ptr = (uint8_t*)glMapBufferRange(GL_UNIFORM_BUFFER, 0, cam_ubo_padded * BUFFER_FRAMES, ubo_flags);
2741
2742
2743
2744 glGenBuffers(1, &RENDER_UBO);
2745 glBindBuffer(GL_UNIFORM_BUFFER, RENDER_UBO);
2746 ivec4 res = (ivec4){RENDER_WIDTH, RENDER_HEIGHT, 0, 0};
2747 glBufferData(GL_UNIFORM_BUFFER, sizeof(RenderData), &res, GL_DYNAMIC_DRAW);
2748 glBindBufferBase(GL_UNIFORM_BUFFER, 1, RENDER_UBO);
2749
2750 light_cam_ubo_padded = (sizeof(ShadowmapUBO) + ubo_alignment - 1) / ubo_alignment * ubo_alignment;
2751 glGenBuffers(1, &LIGHT_CAM_UBO);
2752 glBindBuffer(GL_UNIFORM_BUFFER, LIGHT_CAM_UBO);
2753 glBufferStorage(GL_UNIFORM_BUFFER, light_cam_ubo_padded * BUFFER_FRAMES, NULL, ubo_flags);
2754 light_cam_ubo_ptr = (uint8_t*)glMapBufferRange(GL_UNIFORM_BUFFER, 0, light_cam_ubo_padded * BUFFER_FRAMES, ubo_flags);
2755 glBindBufferBase(GL_UNIFORM_BUFFER, 2, LIGHT_CAM_UBO);
2756
2757 rprobe_ubo_padded = (sizeof(RProbeUBO) + ubo_alignment - 1) / ubo_alignment * ubo_alignment;
2758 glGenBuffers(1, &RPROBE_UBO);
2759 glBindBuffer(GL_UNIFORM_BUFFER, RPROBE_UBO);
2760 glBufferStorage(GL_UNIFORM_BUFFER, rprobe_ubo_padded * BUFFER_FRAMES, NULL, ubo_flags);
2761 rprobe_ubo_ptr = (uint8_t*)glMapBufferRange(GL_UNIFORM_BUFFER, 0, rprobe_ubo_padded * BUFFER_FRAMES, ubo_flags);
2762 glBindBufferBase(GL_UNIFORM_BUFFER, 3, RPROBE_UBO);
2763
2764 glGenBuffers(1, &SSAO_KERNEL_UBO);
2765 glBindBuffer(GL_UNIFORM_BUFFER, SSAO_KERNEL_UBO);
2766 glBufferStorage(GL_UNIFORM_BUFFER, SSAO_KERNEL_SIZE * sizeof(vec4), SSAO_KERNEL, 0);
2767 glBindBufferBase(GL_UNIFORM_BUFFER, 5, SSAO_KERNEL_UBO);
2768
2769 probe_volume_ubo_padded = (sizeof(ProbeVolumeUBO) + ubo_alignment - 1) / ubo_alignment * ubo_alignment;
2770 glGenBuffers(1, &PROBE_VOLUME_UBO);
2771 glBindBuffer(GL_UNIFORM_BUFFER, PROBE_VOLUME_UBO);
2772 glBufferStorage(GL_UNIFORM_BUFFER, probe_volume_ubo_padded * BUFFER_FRAMES, NULL, ubo_flags);
2773 probe_volume_ubo_ptr = (uint8_t*)glMapBufferRange(GL_UNIFORM_BUFFER, 0, probe_volume_ubo_padded * BUFFER_FRAMES, ubo_flags);
2774 glBindBufferBase(GL_UNIFORM_BUFFER, 6, PROBE_VOLUME_UBO);
2775
2776 glGenBuffers(1, &LIGHT_SSBO);
2777 glBindBuffer(GL_SHADER_STORAGE_BUFFER, LIGHT_SSBO);
2779 glBufferStorage(GL_SHADER_STORAGE_BUFFER, light_ssbo_size * BUFFER_FRAMES, NULL, ubo_flags);
2780 light_ssbo_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, light_ssbo_size * BUFFER_FRAMES, ubo_flags);
2781 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, LIGHT_SSBO);
2782
2783 glGenBuffers(1, &MESH_LIGHT_SSBO);
2784 glBindBuffer(GL_SHADER_STORAGE_BUFFER, MESH_LIGHT_SSBO);
2786 glBufferStorage(GL_SHADER_STORAGE_BUFFER, mesh_light_ssbo_size * BUFFER_FRAMES, NULL, ubo_flags);
2787 mesh_light_ssbo_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, mesh_light_ssbo_size * BUFFER_FRAMES, ubo_flags);
2788
2789 glGenBuffers(1, &MATERIAL_SSBO);
2790 glBindBuffer(GL_SHADER_STORAGE_BUFFER, MATERIAL_SSBO);
2791 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(MaterialData), NULL, GL_STATIC_DRAW);
2792
2793 glGenBuffers(1, &LIGHTMAP_SSBO);
2794 glGenBuffers(1, &VERTEX_SSBO);
2795 glGenBuffers(1, &TRI_SSBO);
2796 glGenBuffers(1, &MESH_SSBO);
2797 glGenBuffers(1, &MESH_BVH_SSBO);
2798 glGenBuffers(1, &SCENE_BVH_SSBO);
2799 glGenBuffers(1, &PROBE_SSBO);
2800 glGenBuffers(1, &RPROBE_SSBO);
2801
2802 glGenBuffers(1, &JOINT_SSBO);
2803 glBindBuffer(GL_SHADER_STORAGE_BUFFER, JOINT_SSBO);
2805 glBufferStorage(GL_SHADER_STORAGE_BUFFER, joint_ssbo_size * BUFFER_FRAMES, NULL, ubo_flags);
2806 joint_ssbo_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, joint_ssbo_size * BUFFER_FRAMES, ubo_flags);
2807
2808 glGenBuffers(1, &CLUSTER_AABB_SSBO);
2809 glBindBuffer(GL_SHADER_STORAGE_BUFFER, CLUSTER_AABB_SSBO);
2810 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(ClusterAABBGPU) * TOTAL_CLUSTERS, NULL, 0);
2811
2812 glGenBuffers(1, &GLOBAL_LIGHT_INDICES_SSBO);
2813 glBindBuffer(GL_SHADER_STORAGE_BUFFER, GLOBAL_LIGHT_INDICES_SSBO);
2814 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(uint32_t) * TOTAL_CLUSTERS * MAX_LIGHTS_PER_CLUSTER, NULL, 0);
2815
2816 glGenBuffers(1, &CLUSTER_SSBO);
2817 glBindBuffer(GL_SHADER_STORAGE_BUFFER, CLUSTER_SSBO);
2818 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(ClusterGPU) * TOTAL_CLUSTERS * MAX_LIGHTS_PER_CLUSTER, NULL, 0);
2819
2820 glGenBuffers(1, &MESH_NODE_SSBO);
2821 glBindBuffer(GL_SHADER_STORAGE_BUFFER, MESH_NODE_SSBO);
2823 glBufferStorage(GL_SHADER_STORAGE_BUFFER, mesh_node_ssbo_size * BUFFER_FRAMES, NULL, ubo_flags);
2824 mesh_node_ssbo_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, mesh_node_ssbo_size * BUFFER_FRAMES, ubo_flags);
2825
2826 glGenBuffers(1, &GLOBAL_INSTANCE_SSBO);
2827 glBindBuffer(GL_SHADER_STORAGE_BUFFER, GLOBAL_INSTANCE_SSBO);
2828 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(GlobalInstanceGPU) * 6, NULL, GL_MAP_READ_BIT);
2829
2830 glGenBuffers(1, &INDIRECT_ARRAYS_COMMAND_BUFFER);
2831 glBindBuffer(GL_SHADER_STORAGE_BUFFER, INDIRECT_ARRAYS_COMMAND_BUFFER);
2832 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(DrawArraysIndirectCommand) * MAX_MESH_NODES, NULL, ubo_flags);
2833 indirect_arrays_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(DrawArraysIndirectCommand) * MAX_MESH_NODES, ubo_flags);
2834
2835 glGenBuffers(1, &INDIRECT_ARRAYS_COMMAND_BUFFER_OUT);
2836 glBindBuffer(GL_SHADER_STORAGE_BUFFER, INDIRECT_ARRAYS_COMMAND_BUFFER_OUT);
2837 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(DrawArraysIndirectCommand) * MAX_MESH_NODES, NULL, 0);
2838 indirect_arrays_out_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(DrawArraysIndirectCommand) * MAX_MESH_NODES, GL_MAP_PERSISTENT_BIT);
2839
2840 glGenBuffers(1, &INDIRECT_ELEMENTS_COMMAND_BUFFER);
2841 glBindBuffer(GL_SHADER_STORAGE_BUFFER, INDIRECT_ELEMENTS_COMMAND_BUFFER);
2842 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(DrawElementsIndirectCommand) * MAX_MESH_NODES, NULL, ubo_flags);
2843 indirect_elements_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(DrawElementsIndirectCommand) * MAX_MESH_NODES, ubo_flags);
2844
2845 glGenBuffers(1, &INDIRECT_ELEMENTS_COMMAND_BUFFER_OUT);
2846 glBindBuffer(GL_SHADER_STORAGE_BUFFER, INDIRECT_ELEMENTS_COMMAND_BUFFER_OUT);
2847 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(DrawElementsIndirectCommand) * MAX_MESH_NODES, NULL, 0);
2848 indirect_elements_out_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(DrawElementsIndirectCommand) * MAX_MESH_NODES, GL_MAP_PERSISTENT_BIT);
2849
2850
2851 glGenBuffers(1, &INSTANCE_SSBO);
2852 glBindBuffer(GL_SHADER_STORAGE_BUFFER, INSTANCE_SSBO);
2854 glBufferStorage(GL_SHADER_STORAGE_BUFFER, instance_ssbo_size * BUFFER_FRAMES, NULL, ubo_flags);
2855 instance_ssbo_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, instance_ssbo_size * BUFFER_FRAMES, ubo_flags);
2856
2857 glGenBuffers(1, &TEXT_BOX_SSBO);
2858 glBindBuffer(GL_SHADER_STORAGE_BUFFER, TEXT_BOX_SSBO);
2860 glBufferStorage(GL_SHADER_STORAGE_BUFFER, text_box_ssbo_size * BUFFER_FRAMES, NULL, ubo_flags);
2861 text_box_ssbo_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, text_box_ssbo_size * BUFFER_FRAMES, ubo_flags);
2862
2863 glGenBuffers(1, &UI_SSBO);
2864 glBindBuffer(GL_SHADER_STORAGE_BUFFER, UI_SSBO);
2866 glBufferStorage(GL_SHADER_STORAGE_BUFFER, ui_ssbo_size * BUFFER_FRAMES, NULL, ubo_flags);
2867 ui_ssbo_ptr = (uint8_t*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, ui_ssbo_size * BUFFER_FRAMES, ubo_flags);
2868
2870
2871 printf("Shaders\n");
2872 SHADERS_INIT();
2873
2874 printf("Vertices\n");
2875
2876
2877
2878
2879 glGenVertexArrays(1, &MAIN_VAO);
2880 glBindVertexArray(MAIN_VAO);
2881 glGenBuffers(1, &MAIN_VBO);
2882 glBindBuffer(GL_ARRAY_BUFFER, MAIN_VBO);
2883 glGenBuffers(1, &MAIN_EBO);
2884 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, MAIN_EBO);
2885
2886 size_t main_stride = sizeof(float) * 16 + sizeof(int) * 4;
2887 // pos
2888 glEnableVertexAttribArray(0);
2889 glVertexAttribPointer(0, 3, GL_FLOAT, 0, main_stride, (void *)(uintptr_t)0);
2890 // normal
2891 glEnableVertexAttribArray(1);
2892 glVertexAttribPointer(1, 3, GL_FLOAT, 0, main_stride, (void *)(uintptr_t)(sizeof(float) * 3));
2893 // tangent
2894 glEnableVertexAttribArray(2);
2895 glVertexAttribPointer(2, 4, GL_FLOAT, 0, main_stride, (void *)(uintptr_t)(sizeof(float) * 6));
2896
2897 // joint indices
2898 glEnableVertexAttribArray(3);
2899 glVertexAttribIPointer(3, 4, GL_INT, main_stride, (void *)(uintptr_t)(sizeof(float) * 10));
2900
2901 // weights
2902 glEnableVertexAttribArray(4);
2903 glVertexAttribPointer(4, 4, GL_FLOAT, 0, main_stride, (void *)(uintptr_t)(sizeof(float) * 10) + sizeof(int) * 4);
2904 // uv
2905 glEnableVertexAttribArray(5);
2906 glVertexAttribPointer(5, 2, GL_FLOAT, 0, main_stride, (void *)(uintptr_t)(sizeof(float) * 14) + sizeof(int) * 4);
2907
2908 // can bind a second vbo to use just for instance matrices
2910 glGenBuffers(1, &INSTANCE_VBO);
2911 glBindBuffer(GL_ARRAY_BUFFER, INSTANCE_VBO);
2912 glBufferStorage(GL_ARRAY_BUFFER, instance_vbo_size * BUFFER_FRAMES, NULL, ubo_flags);
2913 instance_vbo_ptr = (uint8_t*)glMapBufferRange(GL_ARRAY_BUFFER, 0, instance_vbo_size * BUFFER_FRAMES, ubo_flags);
2914
2915 for (int i = 0; i < 4; i++) {
2916
2917 GLuint location = 6 + i;
2918 glEnableVertexAttribArray(location);
2919
2920 glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, sizeof(InstanceData), (void*)(i * sizeof(float) * 4));
2921
2922 glVertexAttribDivisor(location, 1);
2923 }
2924
2925 glEnableVertexAttribArray(10);
2926 glVertexAttribIPointer(10, 2, GL_INT, sizeof(InstanceData), (void *)(uintptr_t)(4 * sizeof(float) * 4));
2927 glVertexAttribDivisor(10, 1);
2928 glEnableVertexAttribArray(11);
2929 glVertexAttribIPointer(11, 2, GL_INT, sizeof(InstanceData), (void *)(uintptr_t)(4 * sizeof(float) * 4 + sizeof(int) * 2));
2930 glVertexAttribDivisor(11, 1);
2931 glEnableVertexAttribArray(12);
2932 glVertexAttribIPointer(12, 2, GL_INT, sizeof(InstanceData), (void *)(uintptr_t)(4 * sizeof(float) * 4 + sizeof(int) * 4));
2933 glVertexAttribDivisor(12, 1);
2934 glEnableVertexAttribArray(13);
2935 glVertexAttribIPointer(13, 1, GL_INT, sizeof(InstanceData), (void *)(uintptr_t)(4 * sizeof(float) * 4 + sizeof(int) * 6));
2936 glVertexAttribDivisor(13, 1);
2937
2938
2939 glGenVertexArrays(1, &LIGHTMAP_VAO);
2940 glBindVertexArray(LIGHTMAP_VAO);
2941 glGenBuffers(1, &LIGHTMAP_VBO);
2942 glBindBuffer(GL_ARRAY_BUFFER, LIGHTMAP_VBO);
2943
2944 size_t lightmap_stride = sizeof(float) * 10 + sizeof(int) * 2;
2945 // pos
2946 glEnableVertexAttribArray(0);
2947 glVertexAttribPointer(0, 3, GL_FLOAT, 0, lightmap_stride, (void *)(uintptr_t)0);
2948 // normal
2949 glEnableVertexAttribArray(1);
2950 glVertexAttribPointer(1, 3, GL_FLOAT, 0, lightmap_stride, (void *)(uintptr_t)(sizeof(float) * 3));
2951 // uv
2952 glEnableVertexAttribArray(2);
2953 glVertexAttribPointer(2, 2, GL_FLOAT, 0, lightmap_stride, (void *)(uintptr_t)(sizeof(float) * 6));
2954 // lm_uv
2955 glEnableVertexAttribArray(3);
2956 glVertexAttribPointer(3, 2, GL_FLOAT, 0, lightmap_stride, (void *)(uintptr_t)(sizeof(float) * 8));
2957 // material id
2958 glEnableVertexAttribArray(4);
2959 glVertexAttribIPointer(4, 1, GL_INT, lightmap_stride, (void *)(uintptr_t)(sizeof(float) * 10));
2960 glEnableVertexAttribArray(5);
2961 glVertexAttribIPointer(5, 1, GL_INT, lightmap_stride, (void *)(uintptr_t)(sizeof(float) * 10 + sizeof(int)));
2962
2963
2964 glGenVertexArrays(1, &WIREFRAME_VAO);
2965 glBindVertexArray(WIREFRAME_VAO);
2966 glGenBuffers(1, &WIREFRAME_VBO);
2967 glBindBuffer(GL_ARRAY_BUFFER, WIREFRAME_VBO);
2968 glBufferData(GL_ARRAY_BUFFER, DEBUG_LINES_MAX * 3 * sizeof(DebugVertex), NULL, GL_STREAM_DRAW);
2969
2970 size_t wireframe_stride = sizeof(float) * 6;
2971 // pos
2972 glEnableVertexAttribArray(0);
2973 glVertexAttribPointer(0, 3, GL_FLOAT, 0, wireframe_stride, (void *)(uintptr_t)0);
2974 glEnableVertexAttribArray(1);
2975 glVertexAttribPointer(1, 3, GL_FLOAT, 0, wireframe_stride, (void *)(uintptr_t)(sizeof(float) * 3));
2976
2977
2978
2979 glGenVertexArrays(1, &TEXT_VAO);
2980 glBindVertexArray(TEXT_VAO);
2981 glGenBuffers(1, &TEXT_VBO);
2982 glBindBuffer(GL_ARRAY_BUFFER, TEXT_VBO);
2983
2985 glBufferData(GL_ARRAY_BUFFER, text_vbo_size, NULL, GL_STREAM_DRAW);
2986 // text_vbo_ptr = (uint8_t*)glMapBufferRange(GL_ARRAY_BUFFER, 0, text_vbo_size, ubo_flags);
2987
2988 glGenBuffers(1, &TEXT_EBO);
2989 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, TEXT_EBO);
2990
2991 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_TEXT_BOXES * MAX_TEXT_BOX_CHARACTERS * 4 * sizeof(uint32_t), NULL, GL_STREAM_DRAW);
2992
2993 size_t text_stride = sizeof(float) * 14 + sizeof(uint32_t) * 2;
2994 // pos
2995 glEnableVertexAttribArray(0);
2996 glVertexAttribPointer(0, 2, GL_FLOAT, 0, text_stride, (void *)(uintptr_t)0);
2997 // uv
2998 glEnableVertexAttribArray(1);
2999 glVertexAttribPointer(1, 2, GL_FLOAT, 0, text_stride, (void *)(uintptr_t)(sizeof(float) * 2));
3000 // center of character
3001 glEnableVertexAttribArray(2);
3002 glVertexAttribPointer(2, 2, GL_FLOAT, 0, text_stride, (void *)(uintptr_t)(sizeof(float) * 4));
3003 // color
3004 glEnableVertexAttribArray(3);
3005 glVertexAttribPointer(3, 4, GL_FLOAT, 0, text_stride, (void *)(uintptr_t)(sizeof(float) * 6));
3006 // fill
3007 glEnableVertexAttribArray(4);
3008 glVertexAttribIPointer(4, 4, GL_FLOAT, text_stride, (void *)(uintptr_t)(sizeof(float) * 10));
3009 // state
3010 glEnableVertexAttribArray(5);
3011 glVertexAttribIPointer(5, 1, GL_UNSIGNED_INT, text_stride, (void *)(uintptr_t)(sizeof(float) * 14));
3012 // uID
3013 glEnableVertexAttribArray(6);
3014 glVertexAttribIPointer(6, 1, GL_UNSIGNED_INT, text_stride, (void *)(uintptr_t)(sizeof(float) * 14) + sizeof(uint32_t) * 1);
3015
3016
3017 glGenVertexArrays(1, &UI_VAO);
3018 glBindVertexArray(UI_VAO);
3019 glGenBuffers(1, &UI_VBO);
3020 glBindBuffer(GL_ARRAY_BUFFER, UI_VBO);
3021
3022 ui_vbo_size = sizeof(vec2) * MAX_UI_ELEMENTS * 6;
3023 glBufferStorage(GL_ARRAY_BUFFER, ui_vbo_size, NULL, ubo_flags);
3024
3025 ui_vbo_ptr = (uint8_t*)glMapBufferRange(GL_ARRAY_BUFFER, 0, ui_vbo_size, ubo_flags);
3026 int ui_vbo_index = 0;
3027 for (int i = 0; i < MAX_UI_ELEMENTS; i++)
3028 {
3029
3030 vec2 * uvbo = &(((vec2*)(ui_vbo_ptr))[ui_vbo_index]);
3031 uvbo[0] = (vec2){-1, -1};
3032 uvbo[1] = (vec2){-1, 1};
3033 uvbo[2] = (vec2){1, -1};
3034 uvbo[3] = (vec2){1, -1};
3035 uvbo[4] = (vec2){-1, 1};
3036 uvbo[5] = (vec2){1, 1};
3037
3038 ui_vbo_index += 6;
3039 }
3040
3041 size_t ui_stride = sizeof(vec2);
3042 glEnableVertexAttribArray(0);
3043 glVertexAttribPointer(0, 2, GL_FLOAT, 0, ui_stride, (void *)(uintptr_t)0);
3044
3045 // can bind a second vbo to use just for instance matrices
3046 ui_instance_vbo_size = MAX_UI_ELEMENTS * sizeof(uint32_t);
3047
3048 glGenBuffers(1, &UI_INSTANCE_VBO);
3049 glBindBuffer(GL_ARRAY_BUFFER, UI_INSTANCE_VBO);
3050
3051 glBufferStorage(GL_ARRAY_BUFFER, ui_instance_vbo_size * BUFFER_FRAMES, NULL, ubo_flags);
3052 ui_instance_vbo_ptr = (uint8_t*)glMapBufferRange(GL_ARRAY_BUFFER, 0, ui_instance_vbo_size * BUFFER_FRAMES, ubo_flags);
3053
3054
3055 glEnableVertexAttribArray(1);
3056 glVertexAttribIPointer(1, 1, GL_UNSIGNED_INT, sizeof(uint32_t), (void *)(uintptr_t)(0));
3057 glVertexAttribDivisor(1, 1);
3058
3059 COLOR_GRADE_LUT = load_3d_lut("../engine-assets/LUTs/main_LUT.cube");
3060
3061 SKYBOX = load_cubemap("../engine-assets/cubemap3");
3062 glGenVertexArrays(1, &SKY_VAO);
3063 glBindVertexArray(SKY_VAO);
3064 glGenBuffers(1, &SKY_VBO);
3065 glBindBuffer(GL_ARRAY_BUFFER, SKY_VBO);
3066
3067 const float skyboxVertices[] = {
3068
3069 -1.0f, 1.0f, -1.0f,
3070 -1.0f, -1.0f, -1.0f,
3071 1.0f, -1.0f, -1.0f,
3072 1.0f, -1.0f, -1.0f,
3073 1.0f, 1.0f, -1.0f,
3074 -1.0f, 1.0f, -1.0f,
3075
3076 -1.0f, -1.0f, 1.0f,
3077 -1.0f, -1.0f, -1.0f,
3078 -1.0f, 1.0f, -1.0f,
3079 -1.0f, 1.0f, -1.0f,
3080 -1.0f, 1.0f, 1.0f,
3081 -1.0f, -1.0f, 1.0f,
3082
3083 1.0f, -1.0f, -1.0f,
3084 1.0f, -1.0f, 1.0f,
3085 1.0f, 1.0f, 1.0f,
3086 1.0f, 1.0f, 1.0f,
3087 1.0f, 1.0f, -1.0f,
3088 1.0f, -1.0f, -1.0f,
3089
3090 -1.0f, -1.0f, 1.0f,
3091 -1.0f, 1.0f, 1.0f,
3092 1.0f, 1.0f, 1.0f,
3093 1.0f, 1.0f, 1.0f,
3094 1.0f, -1.0f, 1.0f,
3095 -1.0f, -1.0f, 1.0f,
3096
3097 -1.0f, 1.0f, -1.0f,
3098 1.0f, 1.0f, -1.0f,
3099 1.0f, 1.0f, 1.0f,
3100 1.0f, 1.0f, 1.0f,
3101 -1.0f, 1.0f, 1.0f,
3102 -1.0f, 1.0f, -1.0f,
3103
3104 -1.0f, -1.0f, -1.0f,
3105 -1.0f, -1.0f, 1.0f,
3106 1.0f, -1.0f, -1.0f,
3107 1.0f, -1.0f, -1.0f,
3108 -1.0f, -1.0f, 1.0f,
3109 1.0f, -1.0f, 1.0f
3110
3111 };
3112
3113 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 36 * 3, skyboxVertices, GL_STATIC_DRAW);
3114
3115
3116 // Skybox
3117 size_t sky_stride = sizeof(float) * 3;
3118 // pos
3119 glEnableVertexAttribArray(0);
3120 glVertexAttribPointer(0, 3, GL_FLOAT, 0, sky_stride, (void *)(uintptr_t)0);
3121
3122
3123
3124 glGenVertexArrays(1, &CONVOLVE_TRIANGLE_VAO);
3125 glBindVertexArray(CONVOLVE_TRIANGLE_VAO);
3126 glGenBuffers(1, &CONVOLVE_TRIANGLE_VBO);
3127 glBindBuffer(GL_ARRAY_BUFFER, CONVOLVE_TRIANGLE_VBO);
3128 float triangleVertices[] = {
3129 -1.0f, -1.0f,
3130 3.0f, -1.0f,
3131 -1.0f, 3.0f
3132 };
3133 glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
3134 glEnableVertexAttribArray(0);
3135 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (void*)0);
3136
3137
3138 glGenVertexArrays(1, &FS_VAO);
3139
3141
3142
3143
3144 glBindFramebuffer(GL_FRAMEBUFFER, BRDF_FBO);
3145 glViewport(0,0,512,512);
3146 glClearColor(0, 0, 0, 1.0f);
3147 glClearDepth(1.0);
3148 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3149 glUseProgram(BRDF_SHADER.program);
3150
3151 glDrawArrays(GL_TRIANGLES, 0, 3);
3152
3153 glBindFramebuffer(GL_FRAMEBUFFER, 0);
3154
3155 return true;
3156}
void init_mesh_tri_bvh(Mesh *mesh)
Initializes the bvh for a mesh's triangles.
Acceleration structure generation and how we traverse them.
void init_band_kernels(float sample_rate, float f1_hz, float f2_hz, float f3_hz, float f4_hz, float f5_hz, float out_kernels[6][31])
Definition audio.c:1881
float BAND_IMPULSES[6][31]
Definition audio.c:28
Audio probe baking, portal creation, probe volumes, and emitter functions.
Audio IR processing, hrtf loading and parsing, and audio threading.
float rand()
vec3 rot(vec3 M[3], vec3 a)
Refer to Ken Perlin's 2 joint IK implmentation.
Definition camera.c:1004
Camera cam
This is currently the player cam.
Definition main.c:322
The debug editor UI for the engine.
uniform uint n
DrawArraysIndirect commands are used for non-static meshes.
Engine specific types and defines.
GLenum type
#define LIGHTMAP_WIDTH
#define SSAO_KERNEL_SIZE
SSAO init kernel size.
LightBVH light_bvh
The LightBVH, required for baking lighting.
Spring neck
Neck Spring (target based on pelvis).
VertexGPU v2
vec3 pos
Currently anchors the Skeleton.
bool fixed_sample_locations
This is set to true if multisampled and is standard for MSAA.
#define PROBE_VOLUME_DEPTH
int joint_count
The amount of joints.
int bvh_indices_count
bool lights_dirty
If lights need to be reuploaded.
FILE * LPV_FILE
FILE pointer to the opened binary file.
FILE * APV_FILE
FILE pointer to the opened binary file.
#define GBUFFER_VERSIONS
Maxmimum gbuffer shader branches.
Vector mesh_nodes
MeshNodes.
bool free
Indicates an empty slot in ASSET_INSTANCES.
GLsizei d
float height
The height of the Player body.
#define MAX_LIGHTS_PER_CLUSTER
float stiff
Stiffness.
uint32_t tri_idx
Local: The bvh index for this MeshFace.
@ POINT_LIGHT
@ DIRECTIONAL_LIGHT
#define PROBE_VOLUME_HEIGHT
#define MAX_PORTALS
The maximum amount of portals that are allocated to AStarWorkspace.path.
int rbo_count
The amount of GLRenderbuffers bound.
Vector lights
The Lights that were created (emissive triangles become AREA_LIGHTS).
SceneData scene_data
SceneData that is more-so scene specific.
MeshGPU * meshes
DepthMode
The depth modes for initializing depth textures.
@ DEPTH_MODE_NO_HW
@ DEPTH_MODE_NONE
@ DEPTH_MODE_HW
#define GRID_Z
LightmapGPU * uvs
#define MAX_AUDIO_PROBE_VOLUMES
size_t id
The ID of this skin in Globals.skins.
uint32_t tri_offset
Where the TriGPUs for this Mesh start in the SSBO.
Vector bvh_nodes
GLuint i
The image handle for this texture.
ivec3 probe_tex_size
The texture size for lighting probes.
VertexGPU v1
#define PROBE_VOLUME_WIDTH
GLint internal_format
GLRenderbuffer renderbuffers[6]
The renderbuffers bound to this fbo.
uint32_t skip_directional_light
Skip directional lights so that they go through this Mesh.
uint32_t node_offset
Where the BVHNodes start in the SSBO.
size_t skin_count
The amount of GLTFSkins in this instance.
int asset_id
The ID into Globals.assets.
mat4 global_matrix
vec3 velocity
Velocity of this spring.
Skeleton skeleton
Player Skeleton.
size_t skin_count
float neck_pelvis_diff
Height from pelvis to neck.
#define FBM_TEXTURE_SIZE
Vector vertex_layouts
VertexLayouts.
PhysicsComponent * pc
PhysicsComponent in PHYSICS_COMPONENTS.
float lower_leg_length
Lower leg length.
#define MAX_TRACKS
Maximum AnimationTracks in an AnimationComponent.
GLenum format
Mesh mesh
The Mesh for the ConvexHull used for collisions (an AABB).
DepthMode dm
If this is a depth texture and what mode it is.
bool LOADED_AUDIO_PV_FLAG[4]
Flags that indicate if the slot is loaded.
#define LPV_SLOTS
Vector audio_portals
AudioPortals.
#define DEFAULT_POINT_LIGHT_SHADOWS_SIZE
float stiff_rot
Stiffness for rotation.
LegIK right_leg
Right leg.
uint32_t material_id
Vector meshes
Meshes.
GLuint program
Program handle.
int animation_indices[256]
Animation indices into Asset.
AudioProbe * audio_probes
A scratch buffer used when baking AudioProbes.
float neck_height
Height from ground to neck.
int h
Dimensions.
#define RPROBE_WIDTH
#define MAX_DIRECTIONAL_LIGHTS
Maxmimum directional Lights at one time (used for shadowmap sizing).
AnimationTrack tracks[64]
The AnimationTracks with individual values that occur simultaneously.
bool multisampled
If this is a multisampled texture.
#define VIEW_NEAR
Near plane for player.
int asset_id
The Asset ID in Globals.assets.
AABB aabb
The AABB over each MeshNode via Asset.
@ SELMODE_LIGHT
Light.
mat4 * inverse_bind
The inverse_bind that is computed on load_gltf().
int channels
The amount of channels for an Image (RGBA).
#define MAX_LIGHTS
Maximum amount of Lights to be pushed per frame (used for SSBO sizing).
#define MAX_RPROBE_MIPS
The amount of mips to allocate for rprobes.
GLenum target
AABB aabb
AABB over each MeshNode for this Asset.
Globals GLOBALS
Engine Globals.
Definition main.c:310
size_t * node_indices
The indices of MeshNodes spawned from this instance inside of Globals.mesh_nodes.
bool free
If the component is empty.
uint32_t bvh_index
Global: The bvh index + the offset for the current Mesh.
int * joints
The joints for skeletal animating.
size_t current_offset
GLTexture depth_texture
The normal depth texture, if there is one.
LPV LOADED_LIGHTING_PV[6]
Load slots for lighting ProbeVolumes.
float damp_rot
Dampening for rotation.
GLint min_filter
float damp
Dampening.
Vector reflection_probes
ReflectionProbes in the scene.
Vector lights
Lights.
vec3 pos
Position of the foot.
float probe_spacing
The spacing between lighting probes.
APV LOADED_AUDIO_PV[4]
Load slots for Audio ProbeVolumes.
uint32_t * mesh_light_indices
Buffer used when calling determine_light_visibility_per_mesh.
#define TOTAL_CLUSTERS
void * data
Stored pointer to data to change during animation.
uint32_t is_static
If the mesh is not static it is not considered for path tracing.
int path_count
The final path count.
Vector lighting_probe_volumes
All of the lighting probe volumes, regardless of if they have been loaded yet.
char name[256]
The name of the skin from load.
float upper_leg_length
Upper leg length.
bool free
If the Entity is empty.
float radius
The radius of the Player body.
TriGPU * tris
#define MAX_LIGHTS_PER_MESH
Max directional + spot Lights to push at one time for a Mesh.
GLTFSkin * skins
#define UBERSHADER_VERSIONS
Maxmimum main shader brances.
int texture_count
The amount of GLTextures bound.
uint32_t mesh_node_id
GLsizei levels
Vector audio_probe_volumes
All of the AudioProbe% volumes, regardless of if they have been loaded yet.
Vector mesh_indices
int - Indices into Globals.meshes which indicate what Meshes are within this Asset.
uint32_t mesh_id
The Mesh id.
size_t node_count
The amount of MeshNodes in this instance.
GLTexture texture[6]
The textures bound to this fbo.
GLint mag_filter
Vector images
Images.
#define LIGHTMAP_HEIGHT
size_t id
The ID in ASSET_INSTANCES.
MeshNode * nodes
Vector min_heap
OpenNode - the heap of nodes that are pushed and popped until there are none left or it reaches the e...
#define RPROBE_HEIGHT
size_t first_skin
Refer to todo.
unsigned char * buf
If available, the raw bytes of the Image.
Vector materials
Materials.
#define MAX_ANIMATION_INDICES
The maximum amount of animation indices per GLTFSkin.
BVHNodeGPU * nodes
mat4 * final_mat
The transformation after update_skin_matrices().
#define VIEW_FAR
Far plane for player.
Vector assets
Assets.
GLuint64 handle
The handle for the bindless texture.
int track_count
The amount of AnimationTracks.
uint32_t vertex_offset
Where the VertexGPUs for this Mesh start in the SSBO.
int * path
The path indices that are returned.
uint32_t vertex_count
How many VertexGPUs this Mesh has.
vec3 normal
size_t current_index_offset
GLuint i
The handle for the texture.
float hip_height
Height from ground to hip.
VertexGPU v0
#define MAX_JOINT_MATRICES
Maximum joints in a scene (used for SSBO sizing).
size_t node_count
size_t * indices
The indices sorted when initialized.
#define BUFFER_FRAMES
Triple buffer count.
float pelvis_height
Height from ground to pelvis.
size_t id
ID inside of ENTITIES.
Vector nodes
Each node of the BVH.
GLsizei w
VertexGPU * vertices
bool LOADED_LIGHTING_PV_FLAG[6]
Flags that indicate if the slot is loaded.
#define DEFAULT_SHADOW_MAP_SIZE
GLsizei h
LegIK left_leg
Left leg.
Vector skins
GLTFSkins.
uint32_t mesh_id
char path[256]
The path to the Asset.
vec3 pos
Position of the spring.
GLuint fbo
The handle for this fbo.
bool free
If this skin is free.
#define MAX_INSTANCES
Maximum amount of TransformGPU instances to push in one frame (used for SSBO sizing).
#define GRID_Y
@ GNORMAL
@ GPOMDEPTH
@ GVEL
@ GUV
@ GALBEDO
@ GEXTRA
#define MAX_MESH_NODES
Maximum MeshNodes in a scene (used for SSBO sizing).
uvec3 indices
Unused - the indices of each point.
Camera * cam
Player Camera.
#define PROBE_VOLUME_DEPTH_PADDED
Padding to avoid leaking on edges in Z.
CamData cam_data
char path[256]
The path to the Image.
vec3 target
Where the spring wants to move to based on Skeleton positions, before velocity is integrated.
#define MAX_POINT_LIGHT_SHADOWS_CMA
Maximum amount of point light shadows in texture depth.
LightmapData lightmap_data
The loaded lightmap and lightmap configuration for the scene.
void * completion_data
Pointer to the data that is modified on completion.
size_t * bvh_indices
uint32_t bvh_index
The BVH index for this Mesh in the scene BVH.
#define GRID_X
size_t current_index_capacity
int samples
If multisampled, how many samples.
Vector islands
The UVIslands on the current lightmap.
int msaa_samples
How many samples our multisampled textures are using.
vec4 cam_pos
The position of the Camera (padded).
AStarStateStack * stack
The stack that tracks where each node connects so that it can be backtracked.
size_t current_capacity
Spring pelvis
Pelvis Spring (target based on leg positions).
GLRenderbuffer depth_rbo
The renderbuffer depth texture, if there is one.
float pelvis_hip_diff
Height from pelvis to hip.
uint32_t tri_count
How many TriGPUs there are for this Mesh.
size_t * skin_indices
The indices of GLTFSkins spawned from this instance inside of Globals.skins.
uint32_t node_count
How many BVHNodes are in this Mesh.
A running stack of nodes in a path that tracks where they came from so they can be retraced at the en...
An Asset after it is loaded in from file into Globals.assets.
A instance of an Asset that retains its node structure but can be duplicated into multiple instances ...
The GPU side structure of a BVHNode, used for path traced baking.
The structure uploaded to the GPU for providing render data for a Camera.
The structure for cluster AABBs for clustered shading.
The structure that we push clusters to when we cull them in clustered shading.
The structure used for debug wireframes.
A workspace used for EPA during collision detection.
Entity structure that holds components and references to the data it owns.
The abstraction for a framebuffer in OpenGL.
The abstraction for a renderbuffer in OpenGL.
The structure of an Skeletatal Animation loaded in from GLTFAnimation.
Texture abstraction for init.
The global visibility buffer that holds all indices to instances per frame after they are pushed duri...
Where Material textures are saved upon loading from file.
Simply the size we use to allocate the INSTANCE_VBO (unused).
A ProbeVolume for indirect lighting probes.
The GPU side structure for Lights padded for GPU.
The GPU side SSBO structure.
The GPU side structure of a Mesh, used for path traced baking.
MeshNode buffer structure for MESH_NODE_SSBO.
A node inside of the min_heap of an AStarWorkspace.
The Player.
GPU side structure for an OBB UBO.
The UBO structure for pushing the closest ReflectionProbes to the main shader.
ReflectionProbes used for parallax corrected cubemaps.
The structure uploaded to the GPU for providing the current resolution (fairly unused).
The structure used for uploading matrices for directional Lights used when rendering shadowmaps.
The controller for leg IKs for Skeletons.
The structure used for rendering TextBoxes.
TransformGPU buffer structure for INSTANCE_SSBO.
The GPU side structure of a MeshFace, used for path traced baking.
The UVIslands that are generated from contiguous MeshFaces with similar planes for UV lightmapping.
The GPU side structure of a Point, used for path traced baking.
An unused abstraction for VertexLayouts.
void STATES_INIT()
Initializes entity states, which are a registry of function pointers used for entity events and behav...
Definition entity.c:23
Entity * create_entity(AssetInstance *asset_instance, int mn_id, bool physics_component, bool animation_component)
Creates an entity using the free list of ENTITIES.
Definition entity.c:153
ECS creation and maintenance.
Font * read_json_msdf(const char *json_path, const char *png_path)
Reads the JSON for an MSDF Font, and creates a Font from it.
Definition fonts.c:33
Functions that modify TextBoxes and load Fonts.
void FONTS_INIT()
Simply clears the FONTS buffer and re-initializes the Vector.
Definition fonts.h:21
fftwf_plan bwd_plan
Inverse plan used when convolving IR with current audio stream.
float * PREV_LEFT_OUT
Contains the output of the inverse FFT of the previous IR with current audio stream.
fftwf_complex * PREV_SUM_L
The complex multiply of the previous IR with the partitions.
int tail_len
Currently: = block_size.
fftwf_plan fwd_plan
Forward plan used when convolving IR with current audio stream.
PartitionTable * FRONT
The current partition presented to the audio thread.
ma_node_base base
miniaudio node base.
bool crossfading
Indicates if it is still crossfading between an old and new IR.
PartitionTable * ir
The pointer to the new IR to convolve with.
float * PREV_RIGHT_OUT
Contains the output of the inverse FFT of the previous IR with current audio stream.
ma_mutex lock
Unused mutex for the audio thread.
fftwf_complex * PREV_SUM_R
The complex multiply of the previous IR with the partitions.
fftwf_complex * MONO_FWD
Contains the output of the forward FFT of IR with current audio stream.
float * HRTF_IR_IN
Buffer used when splitting IR into partitions.
float * RIGHT_OUT
Contains the output of the inverse FFT of the new IR with current audio stream.
fftwf_plan bwd_plan
Unusued - was used when parsing in sofas.
int padded_len
Currently 1024.
int complex_len
(padded_len / 2) + 1
float * PREV_TAIL_RIGHT
Contains the tail of the previous IR.
fftwf_complex * SUM_R
The complex multiply of the new IR with the partitions.
float * TAIL_RIGHT
Contains the tail of the new IR.
int block_size
Currently 512.
SDL_mutex * aprobe_mutex
Used when loading new APVs to avoid concurrency with the HRTF thread.
fftwf_complex * SUM_L
The complex multiply of the new IR with the partitions.
float * PREV_TAIL_LEFT
Contains the tail of the previous IR.
int complex_len
(padded_len / 2) + 1
PartitionTable * prev_ir
The pointer to the previous IR we were convolving with.
float * TAIL_LEFT
Contains the tail of the new IR.
fftwf_complex * AUDIO_HISTORY[96]
Contains the history of MONO_FWD up to NUM_PARTITIONS_MAX.
SDL_SpinLock ir_swap_lock
The spinlock used when presenting an IR to the audio thread.
int block_size
Currently 512.
fftwf_plan fwd_plan
The plan used when splitting IR into partitions.
int padded_len
Currently 1024.
int crossfade_pos
The amount of samples crossfaded so far.
int history_head
Contains the current position in the audio history buffer.
float * LEFT_OUT
Contains the output of the inverse FFT of the new IR with current audio stream.
#define NUM_PARTITIONS
Amount of max partitions to be used for overlap-add.
#define SH_MAX
void fft_channels(ConvolverNode *node, bool crossfading)
Applies an impulse response filter via discrete convolution.
Definition audio_ir.c:138
PartitionTableChannel PartitionTable[2]
Type for two channels (stereo) for overlap-add.
void SDLAudioCallback(void *pUserData, Uint8 *pStream, int len)
Passes frames to miniaudio in the audio thread.
Definition audio.c:908
void load_hrtf_from_binary(const char *filename)
Loads a stored HRTF from engine binary.
Definition audio_ir.c:111
#define AUDIO_NUM_BANDS
The amount of bands to use for audio probe baking.
#define NUM_PARTITIONS_MAX
Amount of max partitions to be used for overlap-add for audio probes + 1 for the tail trick.
void init_hrtf_worker_thread(HRTFThreadData *td)
Initializes the worker thread that convolves the audio probes with the HRTF.
Definition audio_ir.c:512
A ProbeVolume for AudioProbes.
The IR CONVOLVER_NODE used to process frames in the audio thread.
The ProbeVolumes for APVs and LPVs, controlled by OBBs.
uint32_t id
The static ID of the AudioEmitter inside of AUDIO_EMITTERS.
bool free
Indicates if this AudioEmitter is empty or can be replaced.
AudioEmitter * init_emitter(const char *filepath, bool is_loop, bool is_occludable, float base_volume)
Initialized an audio emitter and sets up its audio object in miniaudio.
Definition audio.c:814
void play_emitter(AudioEmitter *emitter, vec3 pos)
Starts playing an emitter.
Definition audio.c:853
#define MAX_AUDIO_EMITTERS
The maximum amount of AudioEmitters.
An AudioEmitter that plays audio possibily from a certain position or at a certain time.
AudioPortals used for determining paths to transmit sound when emitter and listener are occluded.
int ao_iterations
Controls how many iterations per lightmap iteration perform the AO check.
float ao_threshold
Controls the distance threshold for AO detection.
int probe_iterations
The amount of iterations to bake each probe.
AABB lm_aabb
The AABB that sizes the current lightmap in world space.
int audio_probe_iterations
The amount of iterations (sample rays) to bake per AudioProbe.
int lm_iterations
The amount of iterations to bake a lightmap.
float texels_per_uv_unit
Controls the texel density of the uv lightmapper.
float DOF_sensor_height
float reverb_wet
Controls the volume of the reverb signal.
int selected_audio_emitter_id
float DOF_max_blur_radius
float volume
Controls the engine volume.
float render_framerate_target
The target framerate for rendering and processing ui events (a cap).
bool wireframe_toggled
Shows wireframes around many structures: Lights, ReflectionProbes, AudioProbes, AudioPortals,...
bool mfactor_enabled
Controls how many iterations per lightmap iteration perform the AO check.
SelectionMode SELMODE
size_t undo_stack_max_byte_size
The maximum memory value for the Undo stack.
int audio_probe_bounces
The amount of bounces to accept per AudioProbe iteration.
float mfactor
Controls how many iterations per lightmap iteration perform the AO check.
void UNDO_INIT()
Initializes Undos inside of UNDOS to free state.
Definition editor.c:155
void FILE_DIR_REGISTRY_INIT()
Initializes each FileDirectoryRegistry based on initial paths.
Definition editor.c:1858
Vector hull_faces
HullFaces that are generated after incremental_hull() that combine contiguous triangles into singular...
Vector vertices
Points that initially copy the Mesh vertices but then are deduplicated and modified in incremental_hu...
size_t id
The ID in PHYSICS_COMPONENTS.
Vector faces
MeshFaces that initially copy the Mesh faces but then are modified in incremental_hull().
bool player_pc
If this is the Player PhysicsComponent.
vec3 vel
Velocity.
vec3 centroid
The center of the hull.
mat3 inverse_inertia
IIA (currently just a calculated box).
AABB aabb
The bounding box of the hull.
bool free
Tracks if a Manifold can be replaced.
PhysicsComponent * a
float mass
Mass of the component.
PhysicsComponent * b
vec3 angular_velocity
Rotational velocity.
bool at_rest
If this component is at rest it does not update.
float mu
The friction constant for the component.
Entity * ent
Which Entity this component belongs to.
bool free
If this is an empty component.
vec3 center_of_mass
COM based on the AABB.
float restitution
The restitution (bounce).
Convex Hulls that are generated via incremental hull for non-static meshes.
Entity Component used for physics resolution.
float intensity
The intensity of the Light.
int fwd_shader
The corresponding forward rendering shader branch in DYNAMIC_SHADER.
size_t id
The ID inside of Globals.mesh_nodes.
vec3 pos
Position of light.
int skin
The GLTFSkin it controls.
LightType light_type
The type of Light.
float normal_scale
Unused - Used to determine how much of the texture normal to use.
sampler2D emissive_tex
char name[256]
The Material path.
Entity * ent
The Entity this MeshNode refers to (all children point to the same Entity as parent).
PhysicsComponent * pc
The physics controller initialized based on data from this MeshNode.
size_t local_id
The ID local to the original file structure.
vec3 dir
Direction for lights that are not POINT_LIGHTs.
char name[256]
The name generated on loading from file.
bool sm_dirty
If a point light needs a new shadowmap.
bool skip_directional_uv_lighting
Skips directional Lights and rays go through them.
vec4 ka
Ambient.
bool free
If the MeshNode can be replaced.
int children[128]
The children to this node, a global index into Globals.mesh_nodes.
vec4 ke
Emissive.
int mesh
The Mesh it references.
int parent
The parent to this node, a global index into Globals.mesh_nodes.
PhysicsType physics_mode
The type of physics to apply for the PhysicsComponent that may not have been created yet.
int child_count
The amount of children.
sampler2D arm_tex
int asset_instance
The AssetInstance it belongs to.
sampler2D normal_tex
vec3 rgb
Color of the light.
UVMapping * uv_map
The mapping between MeshNodes and UVIslands for uv lightmapping.
vec4 ks
Specular (unused).
float threshold
The threshold (how far a light reaches).
int gbuffer_shader
The corresponding gbuffer rendering shader branch in DEPTH_PRE_PASS_SHADER or DEPTH_PRE_PASS_SHADER_M...
uint32_t entity_id
Entity ID into ENTITIES.
float alpha_cutoff
Used to discard transparent Materials.
float absorption_coefficients[6]
How much audio is absorbed when hitting this Material.
vec4 kd
Diffuse.
float scattering_coefficients[6]
How much audio specularly reflects when hitting this Material.
DrawArraysIndirect commands are used for static meshes (eg.
The CPU side structure for Lights.
The Materials parsed in from scene files that determine rendering pipeline setup, uploads,...
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
char name[256]
The name of the file path.
Definition ui.h:84
size_t texture
The Image ID.
Definition ui.h:77
#define MAX_UI_ELEMENTS
Definition ui.h:33
#define MAX_TEXT_BOXES
Definition ui.h:37
#define MAX_TEXT_BOX_CHARACTERS
Definition ui.h:38
int TEXT_HEIGHT
Definition ui.c:12
void UI_INIT()
Simply frees the MENUS buffer and reinitializes the Vector.
Definition ui.h:864
int TEXT_WIDTH
Definition ui.c:11
A Font and its Glyphs used for rendering TextBoxes.
Definition ui.h:74
The GPU-side representation of a TextBox.
Definition ui.h:95
The GPU-side representation of a UIElement.
Definition ui.h:121
void * find_free_in_vector(Vector *v, size_t byte_size, size_t free_byte_offset, size_t id_byte_offset)
Definition init.c:183
MeshNode * find_free_mn()
Definition init.c:153
bool init_vertex_layouts()
Definition init.c:1159
int estimated_frame_count
Definition init.c:1386
int generate_gl_texture(GLTexture tex)
Definition init.c:1243
void deallocate_fbo(Framebuffer *fbo)
Definition init.c:1671
void MN_INIT()
Definition init.c:122
void SKIN_INIT()
Definition init.c:138
void GPU_init_cluster_aabbs()
Initializes AABBs on the GPU for clustered forward shading.
Definition init.c:778
void destroy_asset_instance(uint32_t id)
Destroys and frees an AssetInstance.
Definition init.c:418
vec3 * fbm_tex
Definition init.c:1180
Asset * retrieve_asset_by_name(const char *asset_name)
Definition init.c:220
bool init_globals()
Definition init.c:920
void init_player()
Definition init.c:485
void set_mn_components_to_children(MeshNode *mns, MeshNode *mn, MeshNode *parent)
Definition init.c:291
Mesh * retrieve_mesh_by_name(const char *asset_name, const char *mesh_name)
Searches Globals.assets for a certain Mesh.
Definition init.c:242
void GPU_upload_scene_data()
Uploads current Globals and SceneData structures to the GPU for baking SSBOs.
Definition init.c:661
void precompute_fbm_texture()
Definition init.c:1184
void init_audio_scratch_buffers()
Definition init.c:1601
void allocate_gbuffer_msaa()
Allocated a gbuffer with multisampled textures.
Definition init.c:1769
void GPU_unwrap_mesh_to_buffers(Mesh *m, Vector *vertices, Vector *tris, Vector *nodes)
Definition init.c:589
void init_default_textures()
Initializes dummy default textures inside of Globals.images that are used as fallback for empty Mater...
Definition init.c:794
ma_node_vtable my_custom_node_vtable
Definition init.c:1592
GLTexture init_gl_texture(GLenum target, GLint internal_format, GLenum format, GLenum type, GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t, GLint wrap_r, int w, int h, int d, int levels, DepthMode dm)
Definition init.c:1288
void init_sdl_audio()
Definition init.c:1387
bool init(SDL_Window **window, SDL_GLContext *context)
Initializes the engine, including SDL, OpenGL, GLEW, Nuklear, and engine-specific systems.
Definition init.c:1988
void init_gl_renderbuffer(GLRenderbuffer *rbo, GLint internal_format, GLsizei w, GLsizei h, bool multisampled, int samples)
Definition init.c:1694
void allocate_gbuffer()
Allocates a gbuffer without multisampled textures.
Definition init.c:1844
#define MN_STARTING_SIZE
Definition init.c:121
AssetInstance * init_new_asset_instance(const char *asset_name, vec3 tr, vec4 rot, vec3 scale)
Initializes and spawns a new AssetInstance based on an Asset name, and places the AssetInstance....
Definition init.c:304
void init_player_PC(PhysicsComponent *pc, Player *player)
Definition init.c:454
void convolver_node_process_pcm_frames(ma_node *pNode, const float **ppFramesIn, ma_uint32 *pFrameCountIn, float **ppFramesOut, ma_uint32 *pFrameCountOut)
Definition init.c:1409
void change_fbo_resolution(Framebuffer *fbo, int w, int h, bool skip_depth)
Changes Framebuffer's resolution, and deletes the old textures.
Definition init.c:1361
void init_mesh(Mesh *mesh)
Initializes a static or dynamic Mesh.
Definition init.c:68
void precompute_ssao_kernel()
Definition init.c:1219
void allocate_main_fbo()
Allocates the main FBO, changes if MSAA is enabled.
Definition init.c:1711
void init_fbo(Framebuffer *fbo)
Definition init.c:1318
#define SKIN_STARTING_SIZE
Definition init.c:137
Material * init_new_material(const char *filename)
Expands Globals.materials and initializes a material to default.
Definition init.c:41
#define CROSSFADE_FRAMES
Definition init.c:1405
Important globals for the entire engine and initialization functions that are mainly called on engine...
#define MAX_ENTITIES
Definition init.h:439
#define MAX_ASSET_INSTANCES
Definition init.h:442
#define DEBUG_LINES_MAX
Definition init.h:347
#define MAX_PHYSICS_COMPONENTS
Definition init.h:445
#define MAX_ANIMATION_COMPONENTS
Definition init.h:451
#define MAX_MANIFOLDS
The maximum amount of Manifolds in MANIFOLDS.
Definition init.h:448
Vector hulls
The ConvexHulls generated if this Mesh is dynamic.
TriangleAdjacency * tri_adjacency
Adjacent triangles allocated per MeshFace.
size_t id
The ID into Globals.meshes.
bool static_mesh
This Mesh is static and must create a BVH or if not it must perform incremental_hull.
size_t * bvh_indices
Indices for the BVH, sorted as the BVH is generated.
AABB aabb
The AABB of the mesh.
Vector tri_aabbs
AABBs for each triangle inside of this Mesh.
char name[256]
The Mesh name assigned on load.
bool bvh_prim
This Mesh has a BVH computed.
Vector nodes
BVHNodes.
Vector tris
The MeshFaces.
Vector vertices
The Points.
A Mesh with geometry data that is either initialized as static or dynamic and split into SubMeshes.
GLuint load_cubemap(const char *path)
Loads a cubemap in OpenGL naming convention (must be HDR).
Definition load.c:161
int add_image_to_registry(char *filename, GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t, bool sRGB)
Loads an adds an image to Globals.images.
Definition load.c:292
GLuint load_3d_lut(const char *filepath)
Loads a 3D LUT for postprocessing.
Definition load.c:221
Saves and loads various files that store engine data.
BVHGPU MESH_BVH_DATA
Definition main.c:190
EPAWorkspace * EW[8]
Definition main.c:291
GLuint FS_VAO
Definition main.c:244
GLuint INIT_CLUSTER_AABBS_SHADER
render/init_cluster_aabbs.comp
Definition main.c:274
GLuint INDIRECT_ARRAYS_COMMAND_BUFFER
Definition main.c:167
GLuint SOBEL_TEXTURE[2]
Definition main.c:211
GLuint PROBE_SSBO
Definition main.c:159
ShaderProgram SSR_BLUR_SHADER
Definition main.c:108
uint8_t * rprobe_ubo_ptr
Definition main.c:339
uint8_t * light_ssbo_ptr
Definition main.c:345
uint8_t * indirect_elements_ptr
Definition main.c:367
GLuint LPV_TEXTURE
Definition main.c:268
AudioScratchBuffers AUDIO_SCRATCH
Definition main.c:73
PhysicsComponent PHYSICS_COMPONENTS[MAX_PHYSICS_COMPONENTS]
Definition main.c:314
GLuint LIGHTMAP_SSBO
Definition main.c:153
Framebuffer MAIN_FBO
Definition main.c:90
ShaderProgram TEXT_SHADER
ui/text.vert ui/text.frag
Definition main.c:131
LightmapSSBO LIGHTMAP_DATA
Definition main.c:193
ShaderProgram SSAO_BLUR_SHADER
render/ssao_blur.frag
Definition main.c:106
GLuint MESH_SSBO
Instances (Transforms): Needs the Model Matrix, Inverse Model Matrix (crucial for transforming rays i...
Definition main.c:156
char asset_dir[256]
Definition main.c:284
ma_engine AUDIO_ENGINE
Definition main.c:66
Framebuffer GBUFFER_RESOLVE_FBO
Definition main.c:94
GLuint INDIRECT_ARRAYS_COMMAND_BUFFER_OUT
Definition main.c:168
GLuint INSTANCE_VBO
Definition main.c:239
uint8_t * ui_ssbo_ptr
Definition main.c:380
int DEFAULT_NORMAL
Definition main.c:197
size_t mesh_light_ssbo_size
Definition main.c:347
int SSAO_HEIGHT
Definition main.c:88
GLuint CLUSTER_AABB_SSBO
Definition main.c:162
int RENDER_HEIGHT
Definition main.c:80
GLuint GLOBAL_INSTANCE_SSBO
Definition main.c:166
GLuint RPROBE_FBO
Definition main.c:306
Entity ENTITIES[MAX_ENTITIES]
Definition main.c:312
GLuint BLENDED_RPROBE_CUBE_MAP
Definition main.c:145
uint8_t * indirect_arrays_out_ptr
Definition main.c:365
uint8_t * probe_volume_ubo_ptr
Definition main.c:342
int DUMMY_LIGHTMAP
Definition main.c:201
size_t instance_ssbo_size
Definition main.c:374
uint8_t * mesh_light_ssbo_ptr
Definition main.c:348
AudioEmitter AUDIO_EMITTERS[1024]
Definition main.c:67
GLuint MAIN_EBO
Definition main.c:237
GLuint UI_SSBO
Definition main.c:173
Framebuffer GBUFFER_FBO
Definition main.c:93
uint8_t * ui_vbo_ptr
Definition main.c:383
Framebuffer SSAO_FBO
Definition main.c:97
GLuint SOBEL_FBO[2]
Definition main.c:210
GLuint TEMP_RPROBE_CUBE_MAP
Definition main.c:232
GLuint RPROBE_CUBE_MAP_ARRAY
Definition main.c:305
uint8_t * cam_ubo_ptr
Definition main.c:333
GLuint CONVOLVE_TRIANGLE_VBO
Definition main.c:233
size_t ui_instance_vbo_size
Definition main.c:385
GLuint CLUSTER_SSBO
Definition main.c:164
struct nk_context * NK_CTX
Nuklear context handle used for Editor UI.
Definition main.c:327
BVHGPU SCENE_BVH_DATA
Definition main.c:191
GLuint COLOR_GRADE_LUT
Definition main.c:146
GLuint MESH_NODE_SSBO
Definition main.c:165
size_t ui_ssbo_size
Definition main.c:379
GLuint HISTORY_TEXTURE[2]
Definition main.c:209
GLuint LIGHTMAP_BOUNCE_SHADER
light/lightmap_bounce.comp
Definition main.c:225
GLuint MAIN_VBO
Definition main.c:236
GLuint RPROBE_UBO
Definition main.c:138
Framebuffer SSR_BLUR_FBO
Definition main.c:96
GLuint BRDF_LUT
Definition main.c:203
int RENDER_WIDTH
Definition main.c:79
uint8_t * indirect_elements_out_ptr
Definition main.c:369
int SSR_WIDTH
Definition main.c:85
GLuint MATERIAL_SSBO
Material Data: Roughness, emission multipliers, albedo colors for each instance.
Definition main.c:152
ShaderProgram DEPTH_PRE_PASS_SHADER[1]
render/depth_pre_pass.vert render/depth_pre_pass.frag
Definition main.c:116
GLuint GLOBAL_LIGHT_INDICES_SSBO
Definition main.c:163
GLuint MAIN_VAO
Definition main.c:238
GLuint RPROBE_SSBO
Definition main.c:160
GLuint SSAO_KERNEL_UBO
Definition main.c:139
GLuint INDIRECT_ELEMENTS_COMMAND_BUFFER
Definition main.c:169
AStarWorkspace AW
Definition main.c:75
size_t probe_volume_ubo_padded
Definition main.c:341
AnimationComponent ANIMATION_COMPONENTS[MAX_ANIMATION_COMPONENTS]
Definition main.c:315
ShaderProgram POSTPROCESS_SHADER[128]
render/postprocess.vert render/postprocess.frag
Definition main.c:119
ConvolverNode CONVOLVER_NODE
Definition main.c:72
int SCREEN_WIDTH
Definition main.c:77
GLuint LIGHTMAP_VAO
Definition main.c:217
GLuint MESH_BVH_SSBO
Mesh BVH Nodes: Flattened array of nodes.
Definition main.c:157
size_t ui_vbo_size
Definition main.c:382
size_t mesh_node_ssbo_size
Definition main.c:358
GLuint CONVOLVE_TRIANGLE_VAO
Definition main.c:234
int BLOOM_WIDTH
Definition main.c:81
char rprobe_dir[256]
Definition main.c:285
uint8_t * ui_instance_vbo_ptr
Definition main.c:386
size_t text_box_ssbo_size
Definition main.c:371
EditorSettings EDITOR_SETTINGS
Definition main.c:330
AudioEmitter * FOOTSTEP_EMITTER
Definition main.c:149
int DEFAULT_DIFFUSE
Definition main.c:196
GLuint LIGHTMAP_VBO
Definition main.c:218
int SSAO_WIDTH
Definition main.c:87
GLuint WIREFRAME_VBO
Definition main.c:242
ShaderProgram LIGHTMAP_SHADER
light/lightmap.vert light/lightmap.frag
Definition main.c:128
size_t rprobe_ubo_padded
Definition main.c:338
GLuint TEXT_VAO
Definition main.c:250
GLuint HISTORY_FBO[2]
Definition main.c:208
uint8_t * text_box_ssbo_ptr
Definition main.c:372
uint8_t * joint_ssbo_ptr
Definition main.c:351
GLuint WIREFRAME_VAO
Definition main.c:243
size_t light_ssbo_size
Definition main.c:344
size_t instance_vbo_size
Definition main.c:353
GLuint LIGHTMAP_DILATE_SHADER
light/lightmap_dilate.comp
Definition main.c:227
ShaderProgram SKY_SHADER
render/sky.vert render/sky.frag
Definition main.c:127
GLuint TEXT_BOX_SSBO
Definition main.c:172
int DEFAULT_ARM
Definition main.c:198
GLuint INSTANCE_SSBO
Definition main.c:171
Framebuffer SSR_FBO
Definition main.c:95
size_t joint_ssbo_size
Definition main.c:350
GLuint PROBE_UNPACK_SHADER
light/probe_unpack.comp
Definition main.c:230
GLuint RPROBE_BACK_FBO
Definition main.c:142
SDL_AudioDeviceID SDL_AUDIO_DEVICE
Definition main.c:263
GLuint SKY_VBO
Definition main.c:304
GLuint LIGHT_CAM_UBO
Definition main.c:137
GLuint SKY_VAO
Definition main.c:303
int BLOOM_HEIGHT
Definition main.c:82
MeshData MESH_DATA
Definition main.c:189
GLuint LIGHTMAP_DENOISE_SHADER
light/lightmap_denoise.comp
Definition main.c:226
VertexData VERTEX_DATA
Definition main.c:187
GLuint JOINT_SSBO
Definition main.c:161
GLuint DIRECTIONAL_MAP_NORMALIZE_SHADER
light/directional_map_normalize.comp
Definition main.c:231
vec3 spawn_point
Spawn point.
Definition main.c:289
int DEFAULT_EMISSIVE
Definition main.c:199
GLuint UI_VBO
Definition main.c:255
GLuint RPROBE_DEPTH_TEXTURE
Definition main.c:143
GLuint FBM_TEXTURE
Definition main.c:177
GLuint LIGHTMAP_NORM_TEXTURE
Definition main.c:220
SDL_Window * window
Definition main.c:519
uint8_t * indirect_arrays_ptr
Definition main.c:361
Framebuffer BLOOM_PYRAMID[6]
Definition main.c:102
GLuint PROBE_VOLUME_UBO
Definition main.c:140
GLuint MESH_LIGHT_SSBO
Definition main.c:151
ShaderProgram SHADOW_MAP_SHADER
render/shadow.vert render/shadow.frag
Definition main.c:115
GLuint TRI_SSBO
Indices (Tris): uvec3 for triangle lookups.
Definition main.c:155
vec4 SSAO_KERNEL[16]
Definition main.c:213
size_t text_vbo_size
Definition main.c:377
ShaderProgram POINT_LIGHT_SHADOW_SHADER
render/point_light_shadows.vert render/point_light_shadows.geom render/point_light_shadows....
Definition main.c:130
GLuint SSAO_NOISE_TEXTURE
Definition main.c:176
GLuint INDIRECT_ELEMENTS_COMMAND_BUFFER_OUT
Definition main.c:170
GLuint UI_INSTANCE_VBO
Definition main.c:256
HRTFThreadData HRTF_TD
Definition main.c:74
GLuint LIGHTMAP_ID_TEXTURE
Definition main.c:221
Framebuffer TAA_FBO[2]
Definition main.c:99
int SSR_HEIGHT
Definition main.c:86
uint8_t * instance_vbo_ptr
Definition main.c:354
int DOF_WIDTH
Definition main.c:83
GLuint BRDF_FBO
Definition main.c:185
GLuint TEXT_EBO
Definition main.c:252
Framebuffer COC_FBO
Definition main.c:100
AssetInstance ASSET_INSTANCES[MAX_ASSET_INSTANCES]
Definition main.c:313
ShaderProgram WIREFRAME_SHADER
render/wireframe.vert render/wireframe.frag
Definition main.c:118
TriData TRI_DATA
Definition main.c:188
Framebuffer SSAO_BLUR_FBO
Definition main.c:98
GLuint LIGHTMAP_POS_TEXTURE
Definition main.c:219
uint8_t * light_cam_ubo_ptr
Definition main.c:336
GLuint SCENE_BVH_SSBO
Scene BVH Nodes (TLAS): Same structure as above, but pointing to instances instead of triangles.
Definition main.c:158
GLuint CAM_UBO
Definition main.c:135
Framebuffer DEPTH_DOWNSAMPLE_FBO
Definition main.c:275
GLuint RPROBE_BACK_TEXTURE
Definition main.c:144
ShaderProgram SSAO_SHADER
render/ssao.frag
Definition main.c:105
size_t cam_ubo_padded
Definition main.c:332
Manifold MANIFOLDS[MAX_MANIFOLDS]
Definition main.c:270
Framebuffer SHADOW_MAP_FBO
Definition main.c:92
Framebuffer TEXT_FBO
Definition main.c:103
GLuint TEXT_VBO
Definition main.c:251
GLuint UI_VAO
Definition main.c:254
GLuint SKYBOX
Definition main.c:302
GLuint RENDER_UBO
Definition main.c:136
ShaderProgram BRDF_SHADER
utilities/brdf.frag
Definition main.c:129
ShaderProgram SSR_SHADER
Definition main.c:107
GLuint LIGHTMAP_FBO
Definition main.c:216
uint8_t * instance_ssbo_ptr
Definition main.c:375
Framebuffer MAIN_RESOLVE_FBO
Definition main.c:91
ShaderProgram DYNAMIC_SHADER[32]
render/dynamic.vert render/dynamic.frag
Definition main.c:114
Framebuffer BOKEH_FBO
Definition main.c:101
size_t light_cam_ubo_padded
Definition main.c:335
int SCREEN_HEIGHT
Definition main.c:78
int DOF_HEIGHT
Definition main.c:84
uint8_t * mesh_node_ssbo_ptr
Definition main.c:359
Player player
The Player data.
Definition main.c:287
GLuint VERTEX_SSBO
Vertices: vec4 (x, y, z, padding) or just float arrays.
Definition main.c:154
GLuint LIGHT_SSBO
Definition main.c:150
Framebuffer POINT_LIGHT_SHADOWS_FBO
Definition main.c:307
GLuint PROBE_BAKE_SHADER
light/probe_bake.comp
Definition main.c:228
vec3 vec3_normalize(vec3 v)
Definition mathf.h:46
vec3 vec3_scale(vec3 a, float b)
Definition mathf.h:248
vec3 vec3_mul(vec3 a, vec3 b)
Definition mathf.h:298
vec3 vec3_add(vec3 a, vec3 b)
Definition mathf.h:286
float lerp(float a, float b, float t)
Definition mathf.h:372
bool m4_inverse(float out[16], const float m[16])
Definition mathf.h:164
void incremental_hull(ConvexHull *hull)
Deduplicates vertices.
Definition mesh.c:782
void build_adjacency_map(Mesh *mesh)
Builds a map of TriangleAdjacency over adjacent MeshFaces on the Mesh.
Definition mesh.c:96
void compute_mesh_hulls(ConvexHull *hull)
Combines contiguous triangles with same normals into one face.
Definition mesh.c:962
Mesh functions that modify and initialize Mesh data.
const int samples
int next
The pointer to the next node (either right child or parent's right child) for recursion-less traversa...
int index
The index of the node inside of the array.
int right
Right child.
int parent
Parent.
AABB aabb
The bounding box of all of this node's children.
int left
Left child.
Nodes for the acceleration structures of BVHs.
void bind_render_ssbos()
Binds SSBOs required for rendering.
Definition render.c:56
The main render pass, data GPU uploads, and SSBO management.
void SHADERS_INIT()
Deletes and then recompiles and links all engine shaders.
Definition shaders.c:1131
Initialization for ShaderPrograms.
float c2
Definition types.h:20
float a0
Definition types.h:20
vec3 normal
Definition types.h:284
float z
Definition types.h:13
float a1
Definition types.h:20
int material_id
Definition types.h:285
float x
Definition types.h:13
float c0
Definition types.h:20
float x
Definition types.h:12
float b0
Definition types.h:20
uint32_t x
Definition types.h:18
float max_z
Definition types.h:39
float b1
Definition types.h:20
float min_x
Definition types.h:37
float max_y
Definition types.h:38
float y
Definition types.h:13
size_t indices[3]
Definition types.h:283
float y
Definition types.h:12
uint32_t z
Definition types.h:18
uint32_t y
Definition types.h:18
vec3 pos
Definition types.h:220
float z
Definition types.h:12
size_t count
Definition types.h:209
float min_z
Definition types.h:39
float min_y
Definition types.h:38
@ X
Definition types.h:31
float b2
Definition types.h:20
size_t capacity
Definition types.h:209
void * buffer
Definition types.h:210
float a2
Definition types.h:20
vec2 uv
Definition types.h:224
vec3 normal
Definition types.h:221
float c1
Definition types.h:20
float max_x
Definition types.h:37
Definition types.h:218
Definition types.h:15
Definition types.h:16
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13
The standalone UI library that controls text rendering, ui rendering, and events.
void init_action_callbacks()
void init_hover_callbacks()
void init_vector(Vector *v, size_t i, size_t type_size)
Definition utilities.c:74
void calculate_aabb_bounds(AABB *aabb)
Definition utilities.c:305
void * vector_expand(Vector *v, size_t i)
Definition utilities.c:49
float fbm(vec3 p)
Definition utilities.c:222
vec4 vec3_to_vec4(vec3 v)
Definition utilities.h:166
#define CAST_VEC(vec, type)
Definition utilities.h:134
mat4 compose_trs_from_vectors(vec3 pos, vec4 rot, vec3 scale)
Definition utilities.h:481