Untitled
Loading...
Searching...
No Matches
lightmap.c
Go to the documentation of this file.
1
2#include "core/camera.h"
3#include "core/engine_types.h"
4#include "core/mesh.h"
5#include "render/render.h"
6#include "stb/stb_image.h"
7#include "stb/stb_image_write.h"
8#include "load/load.h"
9#include "utilities/mathf.h"
11#include "utilities/types.h"
12#include "core/acceleration.h"
13#include "load/init.h"
14#include "render/render.h"
15#include "audio/audio.h"
16#include "utilities/utilities.h"
17
18#include <limits.h>
19#include <math.h>
20#include <stdlib.h>
21#include <omp.h>
22#include <assert.h>
23#include <winscard.h>
24
25
26
27
28
29#ifdef _WIN32
30 #include <windows.h>
31 #define SLEEP(x) Sleep(x)
32#else
33 #include <unistd.h>
34 #define SLEEP(x) sleep(x / 1000)
35#endif
36
37void walk_coplanar_adjacency(Mesh * mesh, UVIsland * island, UVMapping * uv_map, int island_idx, int current_face_index, bool * tri_list, TriangleAdjacency * tri_adj, vec3 normal, vec3 ref_point, float eps)
38{
39
40
41 MeshFace * fs = CAST_VEC(mesh->tris, MeshFace);
42 Point * pts = CAST_VEC(mesh->vertices, Point);
43 MeshFace * current_face = &fs[current_face_index];
44 if (tri_list[current_face_index]) return;
45
46 float angle_threshold = 0.7f;
47 vec3 A = pts[fs->indices[0]].pos;
48 vec3 B = pts[fs->indices[1]].pos;
49 vec3 C = pts[fs->indices[2]].pos;
50
51 float area = triangle_area(A, B, C);
52
53
54 if (vec3_dot(current_face->normal, normal) < angle_threshold /* && area >= 0.005f */)
55 {
56 return;
57 }
58 uv_map[current_face_index].uv_island = island_idx;
59 size_t * idx = vector_expand(&island->tris, 1);
60 *idx = current_face_index;
61 tri_list[current_face_index] = true;
62
63 size_t * adj = CAST_VEC(tri_adj[current_face_index].adjacent_indices, size_t);
64
65 for (int i = 0; i < tri_adj[current_face_index].adjacent_indices.count; i++)
66 {
67
68 walk_coplanar_adjacency(mesh, island, uv_map, island_idx, adj[i], tri_list, tri_adj, normal, ref_point, eps);
69
70 }
71
72
73}
74
75
76
77LightmapAtlas init_lightmap_atlas(int width, int height, int block_size) {
78 LightmapAtlas atlas = {0};
79 atlas.width = width;
80 atlas.height = height;
81 atlas.block_size = block_size;
82 int blocks_x = width / block_size;
83 int blocks_y = height / block_size;
84 atlas.occupied = calloc(blocks_x * blocks_y, sizeof(unsigned char));
85 return atlas;
86}
88 if (atlas->occupied != NULL) free(atlas->occupied);
89 atlas->width = 0;
90 atlas->height = 0;
91 atlas->block_size = 0;
92}
94{
95
98
99 if (ld->islands.buffer)
100 {
101
102 UVIsland * uvi = CAST_VEC(ld->islands, UVIsland);
103 for (int i = 0; i < ld->islands.count; i++)
104 {
105 if (uvi[i].uvs) free(uvi[i].uvs);
106 if (uvi[i].tris.buffer) free(uvi[i].tris.buffer);
107 }
108
109 free(ld->islands.buffer);
110
111 }
112
113 init_vector(&ld->islands, 0, sizeof(UVIsland));
114
115
116 ld->sample_cubemap = true;
117
118}
119
120
121
122
123
124void find_least_parallel_axis(vec3 normal, vec3 * u_dir, vec3 * v_dir)
125{
126 vec3 n = vec3_normalize(normal);
127
128 float dx = fabsf(vec3_dot(n, (vec3){1,0,0}));
129 float dy = fabsf(vec3_dot(n, (vec3){0,1,0}));
130 float dz = fabsf(vec3_dot(n, (vec3){0,0,1}));
131
132 vec3 helper;
133
134 if(dx < dy && dx < dz)
135 helper = (vec3){1,0,0};
136 else if(dy < dz)
137 helper = (vec3){0,1,0};
138 else
139 helper = (vec3){0,0,1};
140 vec3 up = (vec3){0,0,1};
141 if (fabsf(n.z) >= 0.999f) up = (vec3){1,0,0};
142
143 *u_dir = vec3_normalize(vec3_cross(up, n));
144 *v_dir = vec3_cross(n, *u_dir);
145}
146
148
149 int padding = 2;
150
151
152 int total_w = w_px + 2 * padding;
153 int total_h = h_px + 2 * padding;
154 int blocks_w = (total_w + atlas->block_size + 1) / atlas->block_size;
155 int blocks_h = (total_h + atlas->block_size + 1) / atlas->block_size;
156 int blocks_x = atlas->width / atlas->block_size;
157 int blocks_y = atlas->height / atlas->block_size;
158
159 for (int y = 0; y <= blocks_y - blocks_h; y++) {
160 for (int x = 0; x <= blocks_x - blocks_w; x++) {
161 bool fits = true;
162 for (int dy = 0; dy < blocks_h && fits; dy++) {
163 for (int dx = 0; dx < blocks_w; dx++) {
164 int idx = (y + dy) * blocks_x + (x + dx);
165 if (atlas->occupied[idx]) {
166 fits = false;
167 break;
168 }
169 }
170 }
171
172 if (fits) {
173 for (int dy = 0; dy < blocks_h; dy++) {
174 for (int dx = 0; dx < blocks_w; dx++) {
175 int idx = (y + dy) * blocks_x + (x + dx);
176 atlas->occupied[idx] = 1;
177 }
178 }
179
180 return (LightmapRect){
181 .u = x * atlas->block_size + padding,
182 .v = y * atlas->block_size + padding,
183 .w = w_px + 2 * padding,
184 .h = h_px + 2 * padding,
185 .success = 1,
186 .padding = padding
187 };
188 }
189 }
190 }
191
192 return (LightmapRect){ .success = 0 };
193}
194
196{
197
198 int x = (rect->u - rect->padding) / ld->atlas.block_size;
199 int y = (rect->v - rect->padding) / ld->atlas.block_size;
200
201 int blocks_w = (rect->w + ld->atlas.block_size + 1) / ld->atlas.block_size;
202 int blocks_h = (rect->h + ld->atlas.block_size + 1) / ld->atlas.block_size;
203
204 int blocks_x = ld->atlas.width / ld->atlas.block_size;
205 int blocks_y = ld->atlas.height / ld->atlas.block_size;
206
207 for (int dy = 0; dy < blocks_h; dy++) {
208 for (int dx = 0; dx < blocks_w; dx++) {
209 int idx = (y + dy) * blocks_x + (x + dx);
210 ld->atlas.occupied[idx] = 0;
211 }
212 }
213
214 memset(rect, 0, sizeof(LightmapRect));
215
216}
217
218
219typedef struct UVAssignmentPair
220{
221
222 float pu, pv;
223 size_t idx;
224 vec3 p; // manually check for equivalency so that we avoid bad unwrapping on very close duplicate points
225
226
228
229
230void assign_uvs(UVIsland * uvi, LightmapAtlas *atlas) {
231
232
233 const float eps = 0.001f;
234
235 float min_u = INFINITY, max_u = -INFINITY;
236 float min_v = INFINITY, max_v = -INFINITY;
237 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
239 MeshNode * mn = &mns[uvi->mesh_node];
240 if (mn->mesh == -1) return;
241 Mesh * m = &meshes[mn->mesh];
242 Point * pts = CAST_VEC(m->vertices, Point);
243 MeshFace * mfs = CAST_VEC(m->tris, MeshFace);
244 size_t * tris = CAST_VEC(uvi->tris, size_t);
245
246 vec3 u_dir;
247 vec3 v_dir;
248 find_least_parallel_axis(uvi->normal, &u_dir, &v_dir);
249
250
251 UVAssignmentPair * uv_pairs = malloc(sizeof(UVAssignmentPair) * uvi->tris.count * 3);
252 int uv_pair_count = 0;
253
254 size_t * uv_pair_indices = malloc(sizeof(size_t) * uvi->tris.count * 3);
255 int uv_pair_indices_count = 0;
256
257 vec3 pA = uvi->centroid;
258
259 float tri_area = 0;
260
261 bool one_valid = false;
262
263 for (int i = 0; i < uvi->tris.count; i++)
264 {
265
266 MeshFace * mf = &mfs[tris[i]];
267 float area = triangle_area(pts[mf->indices[0]].pos,pts[mf->indices[1]].pos,pts[mf->indices[2]].pos);
268 tri_area += area;
269 }
270
271 for (int i = 0; i < uvi->tris.count; i++)
272 {
273
274 MeshFace * mf = &mfs[tris[i]];
275
276 for (int j = 0; j < 3; j++) {
277
278 vec3 p = vec3_sub(pts[mf->indices[j]].pos, pA);
279 float pu = vec3_dot(p, u_dir);
280 float pv = vec3_dot(p, v_dir);
281 if (pu < min_u) min_u = pu;
282 if (pu > max_u) max_u = pu;
283 if (pv < min_v) min_v = pv;
284 if (pv > max_v) max_v = pv;
285 bool match_found = false;
286 if (!match_found)
287 {
288 uv_pair_indices[uv_pair_indices_count++] = uv_pair_count;
289 uv_pairs[uv_pair_count++] = (UVAssignmentPair)
290 {
291 .pu = pu,
292 .pv = pv,
293 .idx = mf->indices[j],
294 .p = pts[mf->indices[j]].pos
295 };
296 }
297
298 }
299 }
300
301 float range_u = max_u - min_u;
302 float range_v = max_v - min_v;
303
304 float mfactor = sqrtf(tri_area) * EDITOR_SETTINGS.mfactor;
305
306 int texel_width = MAX(ceilf(range_u * EDITOR_SETTINGS.texels_per_uv_unit * (EDITOR_SETTINGS.mfactor_enabled ? mfactor : 1)), 6);
307 int texel_height = MAX(ceilf(range_v * EDITOR_SETTINGS.texels_per_uv_unit * (EDITOR_SETTINGS.mfactor_enabled ? mfactor : 1)), 6);
308 LightmapRect rect = allocate_lightmap_rect(atlas, texel_width, texel_height);
309 if (rect.success == 0) printf("OUT OF SPACE\n");
310
311 uvi->rect = rect;
312 int pad = rect.padding;
313 float inner_w = rect.w - 2 * pad;
314 float inner_h = rect.h - 2 * pad;
315 float inner_u = rect.u;
316 float inner_v = rect.v;
317
318 int index = 0;
319
320 for (int i = 0; i < uvi->tris.count; i++)
321 {
322
323 MeshFace * mf = &mfs[tris[i]];
324 mn->uv_map[tris[i]].uv_idx = i;
325
326
327
328 for (int j = 0; j < 3; j++) {
329
330 float pu = uv_pairs[uv_pair_indices[index]].pu;
331 float pv = uv_pairs[uv_pair_indices[index]].pv;
332
333 float norm_u = (pu - min_u) / (range_u > 1e-6f ? range_u : 1.0f);
334 float norm_v = (pv - min_v) / (range_v > 1e-6f ? range_v : 1.0f);
335 float pixel_u = inner_u + norm_u * (inner_w > 0 ? (inner_w - 1) : 0.0f);
336 float pixel_v = inner_v + norm_v * (inner_h > 0 ? (inner_h - 1) : 0.0f);
337
338 uvi->uvs[i][j].x = (pixel_u + 0.5f) / (float)atlas->width;
339 uvi->uvs[i][j].y = (pixel_v + 0.5f) / (float)atlas->height;
340 index++;
341 }
342 }
343
344
345 free(uv_pair_indices);
346 free(uv_pairs);
347
348}
349
350
351
352
354{
355
356 float area = 0.0f;
357 size_t * tris = CAST_VEC(uvi->tris, size_t);
358 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
360 MeshNode * mn = &mns[uvi->mesh_node];
361 if (mn->mesh == -1) return;
362 Mesh * m = &meshes[mn->mesh];
363 Point * pts = CAST_VEC(m->vertices, Point);
364 MeshFace * mfs = CAST_VEC(m->tris, MeshFace);
365
366 if (uvi->tris.count > 0)
367 {
368
369 uvi->uvs = malloc(uvi->tris.count * sizeof(LightmapUV));
370 }
371 else
372 {
373 uvi->uvs = NULL;
374 }
375 uvi->currently_rasterizing = false;
376
377 assign_uvs(uvi, &ld->atlas);
378
379
380}
381
382
383
385{
386
387 UVIsland * islands = CAST_VEC(ld->islands, UVIsland);
388
389 for (int i = 0; i < ld->islands.count; i++)
390 {
391
392 UVIsland * island = &islands[i];
393 add_uv_island_to_lightmap(ld, island);
394
395 }
396
397}
398
399
401{
402
403 const float eps = 1.0f;
404 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
405 if (mn->mesh == -1) return;
406 Mesh * mesh = &meshes[mn->mesh];
407 MeshFace * fs = CAST_VEC(mesh->tris, MeshFace);
408 Point * pts = CAST_VEC(mesh->vertices, Point);
409 TriangleAdjacency * tri_adj = mesh->tri_adjacency;
410
412 bool * tri_list = calloc(mesh->tris.count, sizeof(bool));
413
414 if (mn->uv_map) free(mn->uv_map);
415 mn->uv_map = calloc(mesh->tris.count, sizeof(UVMapping));
416
417
418 Vector invalid_islands;
419 init_vector(&invalid_islands, 0, sizeof(size_t));
420 size_t starting_island = ld->islands.count;
421
422 for (int i = 0; i < mesh->tris.count; i++)
423 {
424 MeshFace * f = &fs[i];
425 if (tri_list[i]) continue;
426
427 UVIsland * island = vector_expand(&ld->islands, 1);
428 init_vector(&island->tris, 0, sizeof(size_t));
429 island->mesh_node = mn->id;
430 int island_idx = ld->islands.count-1;
431 mn->uv_map[i].uv_island = island_idx;
432 island->uvs = NULL;
433 size_t * tris = vector_expand(&island->tris, 1);
434 *tris = i;
435 tri_list[i] = true;
436
437 vec3 normal = f->normal;
438 island->normal = normal;
439
440 size_t * adj = CAST_VEC(tri_adj[i].adjacent_indices, size_t);
441
442
443 for (int j = 0; j < tri_adj[i].adjacent_indices.count; j++)
444 {
445
446 walk_coplanar_adjacency(mesh, island, mn->uv_map, island_idx, adj[j], tri_list, tri_adj, normal, pts[f->indices[0]].pos, eps);
447
448 }
449 tris = CAST_VEC(island->tris, size_t);
450 vec3 new_normal = {0,0,0};
451 bool no_valid = true;
452 vec3 centroids = (vec3){0,0,0};
453 for (int j = 0; j < island->tris.count; j++)
454 {
455 new_normal = vec3_add(new_normal, fs[tris[j]].normal);
456 vec3 A = pts[fs[tris[j]].indices[0]].pos;
457 vec3 B = pts[fs[tris[j]].indices[1]].pos;
458 vec3 C = pts[fs[tris[j]].indices[2]].pos;
459
460 float area = triangle_area(A, B, C);
461
462 centroids = vec3_add(centroids, triangle_centroid(A, B, C));
463
464 }
465
466 centroids = vec3_scale(centroids, 1.0f / island->tris.count);
467
468 island->centroid = centroids;
469
470 new_normal = vec3_normalize(new_normal);
471 island->normal = new_normal;
472
473 }
474 UVIsland * islands = CAST_VEC(ld->islands, UVIsland);
475 for (int i = starting_island; i < ld->islands.count; i++)
476 {
477 add_uv_island_to_lightmap(ld, &islands[i]);
478 }
479
480
481 free(tri_list);
482
483}
484
485
487{
489 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
491 for (int i = 0; i < GLOBALS.mesh_nodes.count; i++)
492 {
493 if (mns[i].mesh == -1 || mns[i].free) continue;
494 if (!meshes[mns[i].mesh].static_mesh || !mns[i].static_mesh || mns[i].physics_mode) continue;
495
496 build_uv_islands(&mns[i]);
497 }
500}
501
503{
504
506 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
507
508 for (int i = 0; i < GLOBALS.mesh_nodes.count; i++)
509 {
510
511 if (mns[i].mesh == -1 || mns[i].free) continue;
512 if (!meshes[mns[i].mesh].static_mesh || !mns[i].static_mesh || mns[i].physics_mode) continue;
513 if (mns[i].uv_map) continue;
514
515 build_uv_islands(&mns[i]);
516
517 }
518
521
522}
523
524
525void save_lightmap_to_binary(LightmapData * ld, const char * name)
526{
527
528 FILE * f = fopen(name, "wb");
529
530 if (!f) return;
531
532 UVIsland * islands = CAST_VEC(ld->islands, UVIsland);
533 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
535 Asset * assets = CAST_VEC(GLOBALS.assets, Asset);
536
537
538
539 fwrite(&EDITOR_SETTINGS.texels_per_uv_unit, sizeof(float), 1, f);
540
541
542 fwrite(&ld->atlas.width, sizeof(int), 1, f);
543 fwrite(&ld->atlas.height, sizeof(int), 1, f);
544 fwrite(&ld->atlas.block_size, sizeof(int), 1, f);
545 if (ld->atlas.occupied)
546 {
547 bool t = true;
548 fwrite(&t, sizeof(bool), 1, f);
549 fwrite(ld->atlas.occupied, sizeof(unsigned char), (ld->atlas.width / ld->atlas.block_size) * (ld->atlas.height / ld->atlas.block_size), f);
550 }
551 else
552 {
553 bool fl = false;
554
555 fwrite(&fl, sizeof(bool), 1, f);
556 }
557 fwrite(&ld->sample_cubemap, sizeof(bool), 1, f);
558
559
560 fwrite(&ld->islands.count, sizeof(size_t), 1, f);
564 for (int i = 0; i < ld->islands.count; i++)
565 {
566
567 UVIsland * uvi = &islands[i];
568
569 MeshNode * mn = &mns[uvi->mesh_node];
570 fwrite(&mn->name, sizeof(char) * 256, 1, f);
571 fwrite(&uvi->tris.count, sizeof(size_t), 1, f);
572 if (uvi->tris.buffer) fwrite(uvi->tris.buffer, sizeof(size_t), uvi->tris.count, f);
573 if (uvi->uvs) fwrite(uvi->uvs, sizeof(LightmapUV), uvi->tris.count, f);
574 fwrite(&uvi->normal, sizeof(vec3), 1, f);
575 fwrite(&uvi->rect, sizeof(LightmapRect), 1, f);
576
577 Mesh * m = &meshes[mn->mesh];
578
579 fwrite(&m->tris.count, sizeof(size_t), 1, f);
580 fwrite(mn->uv_map, sizeof(UVMapping), m->tris.count, f);
581
582
583
584 }
585
586 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
587 vec4 * pixels = malloc(sizeof(vec4) * ld->atlas.height * ld->atlas.width);
588 glBindTexture(GL_TEXTURE_2D, ld->texture_id);
589 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
590
591 fwrite(pixels, sizeof(vec4), ld->atlas.width * ld->atlas.height, f);
592
593
594 free(pixels);
595
596 vec4 * dir_pixels = malloc(sizeof(vec4) * ld->atlas.width * ld->atlas.height);
597
598 glBindTexture(GL_TEXTURE_2D, DIRECTIONAL_MAP_TEXTURE);
599 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, dir_pixels);
600
601 fwrite(dir_pixels, sizeof(vec4), ld->atlas.width * ld->atlas.height, f);
602
603 free(dir_pixels);
604
605 fclose(f);
606
607}
608
609
610void load_lightmap_from_binary(LightmapData * ld, const char * name)
611{
612
613 FILE * f = fopen(name, "rb");
614
615 if (!f) return;
616
617 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
619
620 if (ld->texture_id != -1)
621 {
622 glDeleteTextures(1, &ld->texture_id);
623 }
624
625 float texels_in = 1.5f;
626 fread(&texels_in, sizeof(float), 1, f);
627 if (texels_in != EDITOR_SETTINGS.texels_per_uv_unit)
628 {
630 }
631
632
633 for (int i = 0; i < GLOBALS.mesh_nodes.count; i++)
634 {
635 MeshNode * mn = &mns[i];
636
637 if (mn->uv_map)
638 {
639 free(mn->uv_map);
640 }
641 mn->uv_map = NULL;
642
643 }
644
645
646 fread(&ld->atlas.width, sizeof(int), 1, f);
647 fread(&ld->atlas.height, sizeof(int), 1, f);
648 fread(&ld->atlas.block_size, sizeof(int), 1, f);
649 bool t = false;
650 fread(&t, sizeof(bool), 1, f);
651 if(ld->atlas.occupied) free(ld->atlas.occupied);
652 ld->atlas.occupied = NULL;
653 if (t)
654 {
655 ld->atlas.occupied = calloc((ld->atlas.width / ld->atlas.block_size) * (ld->atlas.height / ld->atlas.block_size), sizeof(unsigned char));
656 fread(ld->atlas.occupied, sizeof(unsigned char), (ld->atlas.width / ld->atlas.block_size) * (ld->atlas.height / ld->atlas.block_size), f);
657 }
658 else
659 {
660 ld->atlas.occupied = NULL;
661 }
662 fread(&ld->sample_cubemap, sizeof(bool), 1, f);
663
664
668 fread(&ld->islands.count, sizeof(size_t), 1, f);
669 if (ld->islands.count> 0)
670 {
671
672
673 ld->islands.buffer = malloc(sizeof(UVIsland) * ld->islands.count);
674 ld->islands.capacity = ld->islands.count;
675
676 }
677 else
678 {
679
680 init_vector(&ld->islands, 0, sizeof(UVIsland));
681 }
682 UVIsland * islands = CAST_VEC(ld->islands, UVIsland);
683 for (int i = 0; i < ld->islands.count; i++)
684 {
685
686 UVIsland * uvi = &islands[i];
687 char name[256];
688 fread(name, sizeof(char) * 256, 1, f);
689
690 int mn_idx = find_mn_by_name(name);
691 uvi->mesh_node = mn_idx;
692
693 fread(&uvi->tris.count, sizeof(size_t), 1, f);
694 if (uvi->tris.count > 0)
695 {
696 uvi->tris.buffer = malloc(sizeof(size_t) * uvi->tris.count);
697 uvi->tris.capacity = uvi->tris.count;
698 fread(uvi->tris.buffer, sizeof(size_t), uvi->tris.count, f);
699 uvi->uvs = malloc(sizeof(LightmapUV) * uvi->tris.count);
700 fread(uvi->uvs, sizeof(LightmapUV), uvi->tris.count, f);
701
702 }
703 else
704 {
705 init_vector(&uvi->tris, 0, sizeof(size_t));
706 uvi->uvs = NULL;
707 }
708 fread(&uvi->normal, sizeof(vec3), 1, f);
709 fread(&uvi->rect, sizeof(LightmapRect), 1, f);
710 size_t tri_count = 0;
711 fread(&tri_count, sizeof(size_t), 1, f);
712
713 if (mn_idx == -1)
714 {
715
716 fseek(f, sizeof(UVMapping) * tri_count, SEEK_CUR);
717 if (uvi->tris.buffer) free(uvi->tris.buffer);
718 init_vector(&uvi->tris, 0, sizeof(size_t));
720
721 }
722 else
723 {
724 MeshNode * mn = &mns[mn_idx];
725 Mesh * m = &meshes[mn->mesh];
726
727 mn->uv_map = malloc(sizeof(UVMapping) * m->tris.count);
728 fread(mn->uv_map, sizeof(UVMapping), m->tris.count, f);
729
730 }
731
732
733 }
734
735
736 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
737 vec4 * pixels = malloc(sizeof(vec4) * ld->atlas.height * ld->atlas.width);
738 glGenTextures(1, &ld->texture_id);
739 glBindTexture(GL_TEXTURE_2D, ld->texture_id);
740
741 fread(pixels, sizeof(vec4), ld->atlas.width * ld->atlas.height, f);
742
743
744 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, ld->atlas.width, ld->atlas.height, 0, GL_RGBA, GL_FLOAT, pixels);
745
746 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
747 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
748 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
749 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
750
751
752 free(pixels);
753
754
755
756 vec4 * dir_pixels = malloc(sizeof(vec4) * ld->atlas.width * ld->atlas.height);
757
758
759 if (DIRECTIONAL_MAP_TEXTURE != -1)
760 {
761 glDeleteTextures(1, &DIRECTIONAL_MAP_TEXTURE);
762 }
763 glGenTextures(1, &DIRECTIONAL_MAP_TEXTURE);
764
765 glBindTexture(GL_TEXTURE_2D, DIRECTIONAL_MAP_TEXTURE);
766
767 fread(dir_pixels, sizeof(vec4), ld->atlas.width * ld->atlas.height, f);
768
769 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, ld->atlas.width, ld->atlas.height, 0, GL_RGBA, GL_FLOAT, dir_pixels);
770
771 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
772 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
773 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
774 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
775
776 free(dir_pixels);
777
778 fclose(f);
781
782}
783
784
785
787{
788
789
790 glBindFramebuffer(GL_FRAMEBUFFER, RPROBE_FBO);
791
792 float yaws[6] = {0, M_PI, M_PI, M_PI, M_PI / 2.0f + M_PI, M_PI / 2.0f};
793 float pitches[6] = {0, 0, M_PI / 2.0f, -M_PI / 2.0f, 0, 0};
794 vec3 flips[6];
795 flips[0] = (vec3){0, 0, -1};
796 flips[1] = (vec3){0, 0, -1};
797 flips[2] = (vec3){0, -1, 0};
798 flips[3] = (vec3){0, 1, 0};
799 flips[4] = (vec3){0, 0, -1};
800 flips[5] = (vec3){0, 0, -1};
801
802
803 glEnable(GL_DEPTH_TEST);
804 glDepthMask(GL_TRUE);
805 glDepthFunc(GL_LEQUAL);
806
808 bool has_aa = EDITOR_SETTINGS.MSAA;
809 EDITOR_SETTINGS.MSAA = false;
810 for (int i = 0; i < GLOBALS.scene_data.reflection_probes.count; i++)
811 {
812
813 ReflectionProbe * probe = &rprobes[i];
814
815
816 mat4 proj = make_projection(90, (float)RPROBE_WIDTH / RPROBE_HEIGHT, 0.01, 100.0f);
817 for (int j = 0; j < 6; j++)
818 {
819
820
821 glBindFramebuffer(GL_FRAMEBUFFER, RPROBE_FBO);
822 glFramebufferTextureLayer(
823 GL_FRAMEBUFFER,
824 GL_COLOR_ATTACHMENT0,
826 0,
827 6 * i + j
828 );
829 glViewport(0, 0, RPROBE_WIDTH, RPROBE_HEIGHT);
830 glClearColor(0, 0, 0, 1.0f);
831 glClearDepth(1.0);
832 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
833
834 vec3 pos = probe->capture_pos;
835
836 Camera sky_cam;
837 sky_cam.cam_data.cam_pos = vec3_to_vec4(pos);
838
839 sky_cam.yaw = yaws[j];
840 sky_cam.pitch = pitches[j];
841
842 compute_camera_basis(&sky_cam, sky_cam.yaw, sky_cam.pitch, 0, flips[j]);
843
844 vec3 target = vec3_add(
845 pos,
846 sky_cam.forward
847 );
848
850 &sky_cam,
851 pos
852 );
853 sky_cam.cam_data.view = view;
854 sky_cam.cam_data.proj = proj;
855
856 m4_inverse((float*)&sky_cam.cam_data.inv_proj, (float*)&sky_cam.cam_data.proj);
857 m4_inverse((float*)&sky_cam.cam_data.inv_view, (float*)&sky_cam.cam_data.view);
858 sky_cam.cam_data.unjittered_proj = proj;
860
862
863
864 glFinish();
866
867 }
868
869
870 }
871
872
873 glFinish();
874 EDITOR_SETTINGS.MSAA = has_aa;
875 // glBindVertexArray(CONVOLVE_TRIANGLE_VAO);
876 // glBindBuffer(GL_ARRAY_BUFFER, CONVOLVE_TRIANGLE_VBO);
877
878
879
880 // glUseProgram(GGX_RPROBE_SHADER.program);
881 // glUniform1f(glGetUniformLocation(GGX_RPROBE_SHADER.program, "uResolution"), RPROBE_WIDTH);
882
883
884 // glBindFramebuffer(GL_FRAMEBUFFER, 0);
885
886 RProbe * probes = calloc(GLOBALS.scene_data.reflection_probes.count, sizeof(RProbe));
887
888 for (int i = 0; i < GLOBALS.scene_data.reflection_probes.count; i++)
889 {
890
891 ReflectionProbe * probe = &rprobes[i];
892
893
894 probes[i].center = probe->aabb.center;
895 probes[i].min = (vec3){probe->aabb.min_x, probe->aabb.min_y, probe->aabb.min_z};
896 probes[i].max = (vec3){probe->aabb.max_x, probe->aabb.max_y, probe->aabb.max_z};
897
898 }
899 glBindBuffer(GL_SHADER_STORAGE_BUFFER, RPROBE_SSBO);
900 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(RProbe) * GLOBALS.scene_data.reflection_probes.count, probes, GL_STATIC_DRAW);
901
902 free(probes);
903
904
905}
906
907
908
909
910
911#define MAX_DIRECTIONAL_LIGHTS_LM 4
912#define MAX_POINT_LIGHTS_LM 2048
913#define MAX_EMISSIVE_TRIS_LM 100000
914
915typedef struct LMDebug
916{
918 uint32_t pad0;
920 uint32_t pad1;
922 uint32_t pad2;
923} LMDebug;
924#define LMDEBUG_SIZE 4096
941
942
943void upload_bake_lights_gpu(GLuint * lights_ssbo)
944{
945
947 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
948
949 float dir_power = 0.0f;
950 float point_power = 0.0f;
951
952 if (GLOBALS.scene_data.light_bvh.lights.count <= 0) return;
953 LightGPU * lgpu = malloc(sizeof(LightGPU) * GLOBALS.scene_data.light_bvh.lights.count);
956 BVHNodeGPU * snodes = calloc(GLOBALS.scene_data.light_bvh.nodes.count, sizeof(BVHNodeGPU));
957 for (int i = 0; i < GLOBALS.scene_data.light_bvh.indices_count; i++)
958 {
959
961
962 copy_light_to_gpu_light(&lgpu[i], l);
964
965 }
966
967
968 size_t light_offset = current_frame * light_ssbo_size;
969
970 uint32_t * light_data = (uint32_t*)(light_ssbo_ptr + light_offset);
971 memcpy(light_data, lgpu, sizeof(LightGPU) * GLOBALS.scene_data.light_bvh.lights.count);
972 glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 0, LIGHT_SSBO, light_offset, light_ssbo_size);
973
974
975 glFinish();
976
977
978 for (int i = 0; i < GLOBALS.scene_data.light_bvh.nodes.count; i++)
979 {
980
981 BVHNode * node = &light_nodes[i];
982 BVHNodeGPU * n = &snodes[i];
983 n->parent = node->parent;
984 n->left = node->left;
985 n->right = node->right;
986 n->next = node->next;
987
988 n->first_index = node->first_index;
989 n->count = node->count;
990 n->center = node->aabb.center;
991 n->extents = node->aabb.extents;
992 n->min = (vec3){node->aabb.min_x, node->aabb.min_y, node->aabb.min_z};
993 n->max = (vec3){node->aabb.max_x, node->aabb.max_y, node->aabb.max_z};
994 n->flux = node->flux;
995 n->emissive_flux = node->emissive_flux;
996
997 }
998
999 glGenBuffers(1, lights_ssbo);
1000 glBindBuffer(GL_SHADER_STORAGE_BUFFER, *lights_ssbo);
1001 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(BVHNodeGPU) * GLOBALS.scene_data.light_bvh.nodes.count, snodes, 0);
1002
1003
1004
1005 free(lgpu);
1006 free(snodes);
1007}
1008
1010{
1011
1012 if (GLOBALS.scene_data.light_bvh.nodes.buffer == NULL) return;
1013
1014
1016 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
1017 UVIsland * uvis = CAST_VEC(ld->islands, UVIsland);
1018
1019 GLuint lights_ssbo;
1020 upload_bake_lights_gpu(&lights_ssbo);
1021
1023 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 10, lights_ssbo);
1024
1025 glBindFramebuffer(GL_FRAMEBUFFER, LIGHTMAP_FBO);
1026 glViewport(0, 0, LIGHTMAP_WIDTH, LIGHTMAP_HEIGHT);
1027 glClearColor(0, 0, 0, 1.0f);
1028 glClearDepth(1.0);
1029 glDisable(GL_DEPTH_TEST);
1030 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1031 glUseProgram(LIGHTMAP_SHADER.program);
1032 glBindVertexArray(LIGHTMAP_VAO);
1033 glDisable(GL_CULL_FACE);
1034
1035
1036 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_ID_TEXTURE);
1037 int * buf = malloc(LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT * sizeof(int) * 3);
1038 for (int i = 0; i < LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT * 3; i++)
1039 {
1040 buf[i] = -1;
1041 }
1042
1043 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32I, LIGHTMAP_WIDTH, LIGHTMAP_HEIGHT, 0, GL_RGB_INTEGER, GL_INT, buf);
1044
1045 free(buf);
1046 for (int i = 0; i < GLOBALS.scene_data.bvh_indices_count; i++)
1047 {
1048 size_t mn_idx = GLOBALS.scene_data.bvh_indices[i];
1049 if (!meshes[mns[mn_idx].mesh].static_mesh) continue;
1050 MeshNode * mn = &mns[mn_idx];
1051 if (mn->mesh == -1 || mn->free || !mn->uv_map) continue;
1052 Mesh * m = &meshes[mn->mesh];
1053
1054 glUniform1i(glGetUniformLocation(LIGHTMAP_SHADER.program, "uMeshNode"), i);
1055 LightmapVertex * lmv = NULL;
1056 lmv = calloc(m->tris.count * 3, sizeof(LightmapVertex));
1057 if (!lmv) continue;
1058
1059 Point * pts = CAST_VEC(m->vertices, Point);
1060 MeshFace * mfs = CAST_VEC(m->tris, MeshFace);
1061
1062 // NOTE: we can use submeshes and iterate them to pass material as a uniform rather than per vertex. not worth the complexity right now due to their indexing
1063 int count = 0;
1064 for (int j = 0; j < m->tris.count; j++)
1065 {
1066
1067 uint32_t idx = m->bvh_indices[j];
1068 UVIsland * uvi = &uvis[mn->uv_map[idx].uv_island];
1069 if (!uvi->currently_rasterizing) continue;
1070 MeshFace * mf = &mfs[idx];
1071
1072 for (int p = 0; p < 3; p++)
1073 {
1074
1075 lmv[count].pos = pts[mf->indices[p]].pos;
1076 lmv[count].normal = pts[mf->indices[p]].normal;
1077 lmv[count].uv = pts[mf->indices[p]].uv;
1078 lmv[count].lm_uv = uvi->uvs[mn->uv_map[idx].uv_idx][p];
1079 lmv[count].material_id = mf->material_id;
1080 lmv[count].tri_id = j;
1081 count++;
1082
1083 }
1084 }
1085
1086 glBindBuffer(GL_ARRAY_BUFFER, LIGHTMAP_VBO);
1087 glBufferData(GL_ARRAY_BUFFER, sizeof(LightmapVertex) * count, lmv, GL_DYNAMIC_DRAW);
1088 glDrawArrays(GL_TRIANGLES, 0, count);
1089
1090
1091 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1092 glDrawArrays(GL_TRIANGLES, 0, count);
1093
1094 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1095
1096 free(lmv);
1097 }
1098 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1099
1100
1101 GLuint ping;
1102 glGenTextures(1, &ping);
1103 glBindTexture(GL_TEXTURE_2D, ping);
1104 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, ld->atlas.width, ld->atlas.height, 0, GL_RGBA, GL_FLOAT, NULL);
1105
1106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1110
1111 GLuint pong;
1112 glGenTextures(1, &pong);
1113 glBindTexture(GL_TEXTURE_2D, pong);
1114 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, ld->atlas.width, ld->atlas.height, 0, GL_RGBA, GL_FLOAT, NULL);
1115
1116 glClearTexImage(GL_TEXTURE_2D, 0, GL_RGBA32F, GL_FLOAT, NULL);
1117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
1118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1121 glGenerateMipmap(GL_TEXTURE_2D);
1122
1123
1124 GLuint directional_out;
1125 glGenTextures(1, &directional_out);
1126 glBindTexture(GL_TEXTURE_2D, directional_out);
1127 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, ld->atlas.width, ld->atlas.height, 0, GL_RGBA, GL_FLOAT, NULL);
1128
1129 glClearTexImage(GL_TEXTURE_2D, 0, GL_RGBA32F, GL_FLOAT, NULL);
1130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1134
1135 GLuint directional_ping;
1136 glGenTextures(1, &directional_ping);
1137 glBindTexture(GL_TEXTURE_2D, directional_ping);
1138 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, ld->atlas.width, ld->atlas.height, 0, GL_RGBA, GL_FLOAT, NULL);
1139
1140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1142 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1144
1145 GLuint directional_pong;
1146 glGenTextures(1, &directional_pong);
1147 glBindTexture(GL_TEXTURE_2D, directional_pong);
1148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, ld->atlas.width, ld->atlas.height, 0, GL_RGBA, GL_FLOAT, NULL);
1149
1150 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
1151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1152 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1153 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1154 glGenerateMipmap(GL_TEXTURE_2D);
1155
1156 glUseProgram(LIGHTMAP_BOUNCE_SHADER);
1157 int ITERATIONS = EDITOR_SETTINGS.lm_iterations;
1158 const int TILE_SIZE = 2048;
1159 // const int TILE_SIZE = 512;
1160 Timer timer;
1161 glActiveTexture(GL_TEXTURE1);
1162 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_POS_TEXTURE);
1163 glUniform1i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uLightmapPosTex"), 1);
1164 glActiveTexture(GL_TEXTURE2);
1165 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_NORM_TEXTURE);
1166 glUniform1i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uLightmapNormalTex"), 2);
1167 glActiveTexture(GL_TEXTURE3);
1168 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_ID_TEXTURE);
1169 glUniform1i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uLightmapIDTex"), 3);
1170 glActiveTexture(GL_TEXTURE4);
1171 glBindTexture(GL_TEXTURE_CUBE_MAP, SKYBOX);
1172 glUniform1i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uSkybox"), 4);
1173 glUniform1i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uLightmapWidth"), ld->atlas.width);
1174 glUniform1i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uLightmapHeight"), ld->atlas.height);
1175 glUniform1i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uLightCount"), GLOBALS.lights.count);
1176 glUniform1i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uAOIterations"), EDITOR_SETTINGS.ao_iterations);
1177 glUniform1f(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uAOThreshold"), EDITOR_SETTINGS.ao_threshold);
1178
1179
1180
1181 // this is done in chunks to avoid tdr crashes on big scenes
1182 for (int i = 0; i < ITERATIONS; i++)
1183 {
1184
1185 if (i % 10 == 0)
1186 {
1187 start_timer(&timer);
1188 }
1189
1190 glBindImageTexture(0, pong, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
1191 glBindImageTexture(1, directional_out, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
1192
1193
1194
1195 for (int y = 0; y < ld->atlas.height; y += TILE_SIZE) {
1196 for (int x = 0; x < ld->atlas.width; x += TILE_SIZE) {
1197
1198
1199 glUniform2i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uTileOffset"), x, y);
1200 glUniform1i(glGetUniformLocation(LIGHTMAP_BOUNCE_SHADER, "uIteration"), i);
1201
1202 int dispatchX = MIN(TILE_SIZE, ld->atlas.width - x);
1203 int dispatchY = MIN(TILE_SIZE, ld->atlas.height - y);
1204
1205 glDispatchCompute((dispatchX + 15) / 16, (dispatchY + 15) / 16, 1);
1206 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
1207
1208 }
1209 }
1210
1211 glFinish();
1212 double iter_time = end_timer(&timer);
1213 }
1214
1215
1217 glBindImageTexture(0, pong, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
1218 glBindImageTexture(1, ping, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
1219 glBindImageTexture(2, directional_out, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
1220 glBindImageTexture(3, directional_ping, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA16F);
1221
1222 glUniform1i(glGetUniformLocation(DIRECTIONAL_MAP_NORMALIZE_SHADER, "uLightmapWidth"), ld->atlas.width);
1223 glUniform1i(glGetUniformLocation(DIRECTIONAL_MAP_NORMALIZE_SHADER, "uLightmapHeight"), ld->atlas.height);
1224 glUniform1i(glGetUniformLocation(DIRECTIONAL_MAP_NORMALIZE_SHADER, "uSampleCount"), ITERATIONS);
1225 glDispatchCompute((ld->atlas.width+15) / 16, (ld->atlas.height+15) / 16, 1);
1226 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
1227
1228 glUseProgram(LIGHTMAP_DENOISE_SHADER);
1229
1230 glActiveTexture(GL_TEXTURE0);
1231 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_POS_TEXTURE);
1232 glUniform1i(glGetUniformLocation(LIGHTMAP_DENOISE_SHADER, "uLightmapPosTex"), 0);
1233 glActiveTexture(GL_TEXTURE1);
1234 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_NORM_TEXTURE);
1235 glUniform1i(glGetUniformLocation(LIGHTMAP_DENOISE_SHADER, "uLightmapNormalTex"), 1);
1236 glActiveTexture(GL_TEXTURE2);
1237 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_ID_TEXTURE);
1238 glUniform1i(glGetUniformLocation(LIGHTMAP_DENOISE_SHADER, "uLightmapIDTex"), 2);
1239 glUniform1i(glGetUniformLocation(LIGHTMAP_DENOISE_SHADER, "uLightmapWidth"), ld->atlas.width);
1240 glUniform1i(glGetUniformLocation(LIGHTMAP_DENOISE_SHADER, "uLightmapHeight"), ld->atlas.height);
1241
1242 glBindImageTexture(0, ping, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
1243 glBindImageTexture(1, pong, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
1244 glBindImageTexture(2, directional_ping, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA16F);
1245 glBindImageTexture(3, directional_pong, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA16F);
1246
1247
1248 glDispatchCompute((ld->atlas.width+15) / 16, (ld->atlas.height+15) / 16, 1);
1249 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
1250
1251
1252
1253 glUseProgram(LIGHTMAP_DILATE_SHADER);
1254
1255 glActiveTexture(GL_TEXTURE2);
1256 glBindTexture(GL_TEXTURE_2D, LIGHTMAP_ID_TEXTURE);
1257 glUniform1i(glGetUniformLocation(LIGHTMAP_DILATE_SHADER, "uLightmapIDTex"), 2);
1258 glUniform1i(glGetUniformLocation(LIGHTMAP_DILATE_SHADER, "uLightmapWidth"), ld->atlas.width);
1259 glUniform1i(glGetUniformLocation(LIGHTMAP_DILATE_SHADER, "uLightmapHeight"), ld->atlas.height);
1260
1261
1262 for (int i = 0; i < 8; i++)
1263 {
1264 glBindImageTexture(0, pong, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
1265 glBindImageTexture(1, ping, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
1266 glBindImageTexture(2, directional_pong, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA16F);
1267 glBindImageTexture(3, directional_ping, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA16F);
1268
1269
1270 glDispatchCompute((ld->atlas.width+15) / 16, (ld->atlas.height+15) / 16, 1);
1271 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
1272
1273 GLuint temp;
1274 temp = pong;
1275 pong = ping;
1276 ping = temp;
1277
1278 GLuint directional_temp;
1279 directional_temp = directional_pong;
1280 directional_pong = directional_ping;
1281 directional_ping = directional_temp;
1282 }
1283
1284
1285 glBindTexture(GL_TEXTURE_2D, directional_pong);
1286 glGenerateMipmap(GL_TEXTURE_2D);
1287 glBindTexture(GL_TEXTURE_2D, pong);
1288 glGenerateMipmap(GL_TEXTURE_2D);
1289 if (ld->texture_id != 0) {
1290 glDeleteTextures(1, &ld->texture_id);
1291 ld->texture_id = 0;
1292 }
1293 if (DIRECTIONAL_MAP_TEXTURE != 0) {
1294 glDeleteTextures(1, &DIRECTIONAL_MAP_TEXTURE);
1296 }
1297 ld->texture_id = pong;
1298 DIRECTIONAL_MAP_TEXTURE = directional_pong;
1299 glDeleteTextures(1, &ping);
1300 glDeleteTextures(1, &directional_ping);
1301 glDeleteTextures(1, &directional_out);
1302
1303
1304 for (int i = 0; i < ld->islands.count; i++)
1305 {
1306 UVIsland * uvi = &uvis[i];
1307 uvi->currently_rasterizing = false;
1308
1309 }
1310
1311 glDeleteBuffers(1, &lights_ssbo);
1312
1314
1315
1316}
1317
1318
1319void remove_lighting_pv(LPV * lpv, bool * lpb)
1320{
1321 if (lpv->lprobes)
1322 {
1323 free(lpv->lprobes);
1324 lpv->lprobes = NULL;
1325 }
1326 lpv->pv_id = 0;
1327 *lpb = false;
1328}
1329
1331{
1332 for (int i = 0; i < LPV_SLOTS; i++)
1333 {
1335 {
1337 }
1340 }
1341}
1342
1343
1344void load_lighting_pv(LPV * lpv, ProbeVolume * pv, int replacing_id)
1345{
1346
1347 fseek(GLOBALS.scene_data.LPV_FILE, pv->byte_offset, SEEK_SET);
1348
1349 lpv->lprobes = malloc(sizeof(float) * LPV_PADDED_COUNT * 3 * SH_MAX);
1350
1351 fread(lpv->lprobes, sizeof(float), LPV_PADDED_COUNT * 3 * SH_MAX, GLOBALS.scene_data.LPV_FILE);
1352
1353 glBindTexture(GL_TEXTURE_3D, LPV_TEXTURE);
1354 glTexSubImage3D(
1355 GL_TEXTURE_3D,
1356 0,
1357 0, 0, replacing_id * SH_MAX * PROBE_VOLUME_DEPTH_PADDED,
1359 GL_RGB, GL_FLOAT, lpv->lprobes
1360 );
1361
1362}
1363
1364typedef struct DistancePair
1365{
1366 float d;
1367 int id;
1368} DistancePair;
1369
1370int compare_distances(const void * a, const void * b)
1371{
1372 DistancePair * da = (DistancePair *)a;
1373 DistancePair * db = (DistancePair *)b;
1374 if (da->d < db->d) return 1;
1375 if (da->d > db->d) return -1;
1376 return 0;
1377
1378}
1379int compare_current_distances(const void * a, const void * b)
1380{
1381 DistancePair * da = (DistancePair *)a;
1382 DistancePair * db = (DistancePair *)b;
1383 if (da->d > db->d) return 1;
1384 if (da->d < db->d) return -1;
1385 return 0;
1386
1387}
1388
1389
1390
1392{
1393
1394 if (GLOBALS.scene_data.LPV_FILE == NULL) return;
1395
1396
1398 vec3 current_pos = player.skeleton.neck.pos;
1399
1400 DistancePair distances[LPV_SLOTS];
1401 DistancePair current_distances[LPV_SLOTS];
1402 for (int i = 0; i < LPV_SLOTS; i++)
1403 {
1404 distances[i].d = INFINITY;
1405 distances[i].id = -1;
1406 current_distances[i].d = INFINITY;
1407 current_distances[i].id = i;
1408 }
1409
1410 for (int i = 0; i < GLOBALS.scene_data.lighting_probe_volumes.count; i++)
1411 {
1412
1413 bool already_loaded = false;
1414 for (int j = 0; j < LPV_SLOTS; j++)
1415 {
1417 {
1418
1419 already_loaded = true;
1420 break;
1421 }
1422 }
1423
1424 if (already_loaded) continue;
1425 ProbeVolume * pv = &pvs[i];
1426 vec3 diff = vec3_sub(pv->obb.center, current_pos);
1427 vec3 dir = vec3_normalize(diff);
1428 vec3 intersection_point = current_pos;
1429 bool hit = ray_intersects_obb_with_point(pv->obb, current_pos, dir, &intersection_point);
1430 float dist = vec3_dist(current_pos, intersection_point);
1431 if (!hit)
1432 {
1433 printf("how the hell did the lighting pv ray not hit it is uhm like directed at the volume dude?\n");
1434 continue;
1435 }
1436
1437 for (int j = 0; j < LPV_SLOTS; j++)
1438 {
1439 if (distances[j].d > dist)
1440 {
1441 distances[j].d = dist;
1442 distances[j].id = i;
1443 break;
1444 }
1445 }
1446 }
1447 int count = 0;
1448 for (int i = 0; i < LPV_SLOTS; i++)
1449 {
1450 if (distances[i].id != -1)
1451 {
1452 count++;
1453 }
1454 }
1455
1456 qsort(distances, count, sizeof(DistancePair), compare_distances);
1457
1458 int current_count = 0;
1459 for (int i = 0; i < LPV_SLOTS; i++)
1460 {
1461
1462 if (!GLOBALS.scene_data.LOADED_LIGHTING_PV_FLAG[i]) continue;
1463
1465 vec3 diff = vec3_sub(pv->obb.center, current_pos);
1466 vec3 dir = vec3_normalize(diff);
1467 vec3 intersection_point = current_pos;
1468 bool hit = ray_intersects_obb_with_point(pv->obb, current_pos, dir, &intersection_point);
1469 float dist = vec3_dist(current_pos, intersection_point);
1470 if (!hit)
1471 {
1472 printf("how the hell did the lighting pv ray not hit it is uhm like directed at the volume dude?\n");
1473 continue;
1474 }
1475
1476 current_distances[current_count++].d = dist;
1477 current_distances[current_count++].id = i;
1478 }
1479
1480 qsort(current_distances, LPV_SLOTS, sizeof(DistancePair), compare_current_distances);
1481
1482
1483 int replaced_offset = 0;
1484 for (int i = 0; i < count; i++)
1485 {
1486 for (int j = replaced_offset; j < LPV_SLOTS; j++)
1487 {
1488 if (distances[i].d < current_distances[j].d)
1489 {
1490
1491 int replacing_id = current_distances[j].id;
1493
1494 load_lighting_pv(&GLOBALS.scene_data.LOADED_LIGHTING_PV[replacing_id], &pvs[distances[i].id], replacing_id);
1495 GLOBALS.scene_data.LOADED_LIGHTING_PV[replacing_id].pv_id = distances[i].id;
1496 GLOBALS.scene_data.LOADED_LIGHTING_PV_FLAG[replacing_id] = true;
1497
1498 replaced_offset = j + 1;
1499 break;
1500
1501 }
1502 }
1503 }
1504
1505}
1506
1507
1508
1510{
1511
1512 if (GLOBALS.scene_data.light_bvh.nodes.buffer == NULL) return;
1513 if (GLOBALS.scene_data.LPV_FILE == NULL) return;
1514 if (EDITOR_SETTINGS.probe_iterations == 0) return;
1516 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
1517
1518 GLuint lights_ssbo;
1519 GLuint probe_out_ssbo;
1520 upload_bake_lights_gpu(&lights_ssbo);
1521
1523 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 10, lights_ssbo);
1524
1525 glDeleteBuffers(1, &PROBE_SSBO);
1526 glGenBuffers(1, &PROBE_SSBO);
1527 glBindBuffer(GL_SHADER_STORAGE_BUFFER, PROBE_SSBO);
1528
1530 OBB obb = pv->obb;
1531
1532 vec3 step_x, step_y, step_z;
1533 PV_step_size(pv, &step_x, &step_y, &step_z);
1534
1535 vec3 pos = OBB_min_corner(&obb);
1536
1537 vec3 current_pos = pos;
1538 for (int z = 0; z < PROBE_VOLUME_DEPTH; z++)
1539 {
1540 for (int y = 0; y < PROBE_VOLUME_HEIGHT; y++)
1541 {
1542 for (int x = 0; x < PROBE_VOLUME_WIDTH; x++)
1543 {
1544
1545 current_pos.x = pos.x + x * step_x.x + y * step_y.x + z * step_z.x;
1546 current_pos.y = pos.y + x * step_x.y + y * step_y.y + z * step_z.y;
1547 current_pos.z = pos.z + x * step_x.z + y * step_y.z + z * step_z.z;
1548 sh_data[z * PROBE_VOLUME_HEIGHT * PROBE_VOLUME_WIDTH + y * PROBE_VOLUME_WIDTH + x].pos = current_pos;
1549 }
1550 }
1551 }
1552
1553 glBufferStorage(GL_SHADER_STORAGE_BUFFER, PROBE_VOLUME_WIDTH * PROBE_VOLUME_HEIGHT * PROBE_VOLUME_DEPTH * sizeof(SHDataGPU), sh_data, GL_MAP_WRITE_BIT | GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
1554 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 9, PROBE_SSBO);
1555 glGenBuffers(1, &probe_out_ssbo);
1556 glBindBuffer(GL_SHADER_STORAGE_BUFFER, probe_out_ssbo);
1557 glBufferStorage(GL_SHADER_STORAGE_BUFFER, LPV_PADDED_COUNT * sizeof(float) * 3 * SH_MAX, NULL, GL_MAP_READ_BIT);
1558 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 11, probe_out_ssbo);
1559
1560
1561 int PROBE_ITERATIONS = EDITOR_SETTINGS.probe_iterations;
1562 glUseProgram(PROBE_BAKE_SHADER);
1563 glActiveTexture(GL_TEXTURE4);
1564 glBindTexture(GL_TEXTURE_CUBE_MAP, SKYBOX);
1565 glUniform1i(glGetUniformLocation(PROBE_BAKE_SHADER, "uSkybox"), 4);
1566 glUniform1i(glGetUniformLocation(PROBE_BAKE_SHADER, "uLightCount"), GLOBALS.lights.count);
1567 glUniform3i(glGetUniformLocation(PROBE_BAKE_SHADER, "uProbeSize"), PROBE_VOLUME_WIDTH, PROBE_VOLUME_HEIGHT, PROBE_VOLUME_DEPTH);
1568 glUniform1f(glGetUniformLocation(PROBE_BAKE_SHADER, "uSpacing"), GLOBALS.probe_spacing);
1569 glUniform1i(glGetUniformLocation(PROBE_BAKE_SHADER, "uIterations"), PROBE_ITERATIONS);
1570
1571 for (int i = 0; i < PROBE_ITERATIONS; i++)
1572 {
1573
1574 glUniform1i(glGetUniformLocation(PROBE_BAKE_SHADER, "uCurrentIteration"), i);
1575 glDispatchCompute((PROBE_VOLUME_WIDTH + 7) / 8, (PROBE_VOLUME_HEIGHT+ 7) / 8, (PROBE_VOLUME_DEPTH + 7) / 8);
1576 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
1577 glFinish();
1578
1579 }
1580
1581 glFinish();
1582
1583 glUseProgram(PROBE_UNPACK_SHADER);
1584 glUniform3i(glGetUniformLocation(PROBE_UNPACK_SHADER, "uProbeSize"), PROBE_VOLUME_WIDTH, PROBE_VOLUME_HEIGHT, PROBE_VOLUME_DEPTH);
1585 glUniform1i(glGetUniformLocation(PROBE_UNPACK_SHADER, "uIterations"), PROBE_ITERATIONS);
1586 glUniform1ui(glGetUniformLocation(PROBE_UNPACK_SHADER, "uPaddedDepth"), PROBE_VOLUME_DEPTH_PADDED);
1587
1588 glDispatchCompute((PROBE_VOLUME_WIDTH + 7) / 8, (PROBE_VOLUME_HEIGHT+ 7) / 8, (PROBE_VOLUME_DEPTH + 7) / 8);
1589 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
1590
1591 glFinish();
1592
1593 glBindBuffer(GL_SHADER_STORAGE_BUFFER, probe_out_ssbo);
1594 float * out = glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
1595 if (!out)
1596 {
1597 printf("CANNOT READ OUT\n");
1598 }
1599 else
1600 {
1601
1602 size_t byte_offset = 0;
1603 if (pv->size > 0)
1604 {
1605 byte_offset = pv->byte_offset;
1606 fseek(GLOBALS.scene_data.LPV_FILE, byte_offset, SEEK_SET);
1607 }
1608 else
1609 {
1610 fseek(GLOBALS.scene_data.LPV_FILE, 0, SEEK_END);
1611 byte_offset = ftell(GLOBALS.scene_data.LPV_FILE);
1612 }
1613
1614 fwrite(out, sizeof(float), LPV_PADDED_COUNT * 3 * SH_MAX, GLOBALS.scene_data.LPV_FILE);
1615
1616
1617 pv->size = ftell(GLOBALS.scene_data.LPV_FILE) - byte_offset;
1618 pv->byte_offset = byte_offset;
1619
1620 }
1621
1622 glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
1623
1624
1625 glDeleteBuffers(1, &lights_ssbo);
1626 glDeleteBuffers(1, &probe_out_ssbo);
1627
1628 free(sh_data);
1629
1631
1632
1633
1634}
1635
bool ray_intersects_obb_with_point(OBB obb, vec3 origin, vec3 dir, vec3 *intersection_point)
Checks if a ray intersects an obb and outputs an intersection point.
Acceleration structure generation and how we traverse them.
Audio probe baking, portal creation, probe volumes, and emitter functions.
RayHit hit
void compute_camera_basis(Camera *cam, float yaw, float pitch, float roll, vec3 world_up)
Computes camera vectors based on yaw, pitch, and roll.
Definition camera.c:136
mat4 make_look_at(Camera *cam, vec3 eye)
Makes a 4x4 view matrix using camera vectors.
Definition camera.c:258
mat4 make_projection(float fov_deg, float aspect, float near, float far)
Makes an 4x4 projection matrix.
Definition camera.c:221
Functions for camera.
uniform uint n
shared uint temp[1024]
Engine specific types and defines.
#define LIGHTMAP_WIDTH
LightBVH light_bvh
The LightBVH, required for baking lighting.
Spring neck
Neck Spring (target based on pelvis).
int block_size
How much to allocate in one rect.
#define PROBE_VOLUME_DEPTH
AABB aabb
Bounding box of area.
int bvh_indices_count
float * lprobes
The raw SH data.
FILE * LPV_FILE
FILE pointer to the opened binary file.
int v
The height of the rect in UV space.
vec2 LightmapUV[3]
The lightmap UVs stored on UVIslands per MeshFace.
int material_id
The Material this belongs to.
Vector mesh_nodes
MeshNodes.
#define PROBE_VOLUME_HEIGHT
GLuint texture_id
The texture ID for the current lightmap.
unsigned char * occupied
The map of what is currently occupied by LightmapRects.
Vector lights
The Lights that were created (emissive triangles become AREA_LIGHTS).
LightmapUV * uvs
The uvs for each MeshFace on this islands.
SceneData scene_data
SceneData that is more-so scene specific.
int tri_id
The ID of the MeshFace.
vec3 pos
The position.
int height
Height of the atlas.
#define PROBE_VOLUME_WIDTH
Skeleton skeleton
Player Skeleton.
vec3 centroid
The centroid of the MeshFaces on this island.
vec3 capture_pos
The position to capture the cubemap from.
Vector vertex_layouts
VertexLayouts.
int width
Width of the atlas.
#define LPV_SLOTS
Vector meshes
Meshes.
GLuint program
Program handle.
mat4 inv_proj
The inverse projection matrix.
#define RPROBE_WIDTH
int mesh_node
The MeshNode this island belongs to.
vec3 normal
The normal of the island.
int w
The width of the rect in pixels.
Vector tris
size_t indices of the MeshFaces in this island.
Globals GLOBALS
Engine Globals.
Definition main.c:310
LPV LOADED_LIGHTING_PV[6]
Load slots for lighting ProbeVolumes.
Vector reflection_probes
ReflectionProbes in the scene.
Vector lights
Lights.
mat4 inv_view
The inverse view matrix.
int h
The height of the rect in piexles.
int u
The width of the rect in UV space.
Vector adjacent_indices
size_t indices.
float probe_spacing
The spacing between lighting probes.
vec3 forward
mat4 unjittered_proj
The projection matrix without TAA jittering.
int success
If the rect was allocated.
Vector lighting_probe_volumes
All of the lighting probe volumes, regardless of if they have been loaded yet.
vec2 uv
The texture coordinates for the actual face's GLTexture.
#define LIGHTMAP_HEIGHT
size_t uv_island
What island inside of LightmapData.islands that this MeshFace refers to.
#define RPROBE_HEIGHT
mat4 view
The view matrix.
float pitch
mat4 unjittered_inv_proj
The inverse projection matrix without TAA jittering.
Vector assets
Assets.
bool sample_cubemap
Unused - was used to toggle skybox sampling.
size_t * indices
The indices sorted when initialized.
#define BUFFER_FRAMES
Triple buffer count.
int pv_id
The ProbeVolume in SceneData.lighting_probe_volume.
Vector nodes
Each node of the BVH.
bool LOADED_LIGHTING_PV_FLAG[6]
Flags that indicate if the slot is loaded.
vec3 pos
Position of the spring.
float yaw
vec3 normal
The normal.
size_t uv_idx
The UV slot on the UVIsland.
mat4 proj
The projection matrix.
#define PROBE_VOLUME_DEPTH_PADDED
Padding to avoid leaking on edges in Z.
#define LPV_PADDED_COUNT
CamData cam_data
int padding
How much padding to avoid light leaking.
vec2 lm_uv
The texture coordinates for the LightmapAtlas.
LightmapData lightmap_data
The loaded lightmap and lightmap configuration for the scene.
size_t * bvh_indices
size_t indices_count
The amount of indices on init (some lights are omitted).
Vector islands
The UVIslands on the current lightmap.
vec4 cam_pos
The position of the Camera (padded).
LightmapRect rect
The rect that this island allocates.
uint32_t bvh_index
bool currently_rasterizing
If we are going to rasterize this island on the next lightmap bake.
LightmapAtlas atlas
The atlas settings for the lightmap.
An Asset after it is loaded in from file into Globals.assets.
The GPU side structure of a BVHNode, used for path traced baking.
The camera structure for players and entities.
A ProbeVolume for indirect lighting probes.
The GPU side structure for Lights padded for GPU.
The atlas that UVIslands are allocated on in LightmapRects during UV lightmapping.
The metadata for the lightmap settings.
The rects that are slotted into thet LightmapAtlas during UV generation for UV lightmaps.
The structure used when rasterizing lightmap data to a texture before baking.
ReflectionProbes used for parallax corrected cubemaps.
Used to store SH coefficients when baking LPVs into ProbeVolumes.
Adjacent triangles for each MeshFace on a Mesh.
The UVIslands that are generated from contiguous MeshFaces with similar planes for UV lightmapping.
This bridges from MeshNode's MeshFaces to their respective UVIslands.
An unused abstraction for VertexLayouts.
double end_timer(Timer *t)
void start_timer(Timer *t)
void PV_step_size(ProbeVolume *pv, vec3 *step_x, vec3 *step_y, vec3 *step_z)
size_t size
The byte size of this volume.
size_t byte_offset
The byte offset into the binary file storing this volume.
OBB obb
The OBB that defines the size.
#define SH_MAX
The ProbeVolumes for APVs and LPVs, controlled by OBBs.
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.
int lm_iterations
The amount of iterations to bake a lightmap.
float texels_per_uv_unit
Controls the texel density of the uv lightmapper.
bool mfactor_enabled
Controls how many iterations per lightmap iteration perform the AO check.
float mfactor
Controls how many iterations per lightmap iteration perform the AO check.
size_t id
The ID inside of Globals.mesh_nodes.
vec3 center
Definition dynamic.frag:15
char name[256]
The name generated on loading from file.
vec3 center
bool free
If the MeshNode can be replaced.
int mesh
The Mesh it references.
UVMapping * uv_map
The mapping between MeshNodes and UVIslands for uv lightmapping.
The CPU side structure for Lights.
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
Oriented Bounding Boxes.
Definition dynamic.frag:14
The structure for ReflectionProbes padded for the GPU.
Important globals for the entire engine and initialization functions that are mainly called on engine...
int current_frame
Definition render.c:25
void free_lightmap_atlas(LightmapAtlas *atlas)
Definition lightmap.c:87
LightmapAtlas init_lightmap_atlas(int width, int height, int block_size)
Definition lightmap.c:77
void LM_allocate_empty_mns()
Allocates texels for MeshNodes that don't have UVIslands generated yet.
Definition lightmap.c:502
vec3 pos
Definition lightmap.c:917
vec3 origin
Definition lightmap.c:921
void build_uv_islands(MeshNode *mn)
Builds UVIslands over a MeshNode by stepping through TriangleAdjacency and stopping when normals beco...
Definition lightmap.c:400
void deallocate_lightmap_rect(LightmapData *ld, LightmapRect *rect)
Definition lightmap.c:195
#define MAX_POINT_LIGHTS_LM
Definition lightmap.c:912
#define LMDEBUG_SIZE
Definition lightmap.c:924
uint32_t pad1
Definition lightmap.c:920
void remove_lighting_pv(LPV *lpv, bool *lpb)
Definition lightmap.c:1319
int compare_current_distances(const void *a, const void *b)
Definition lightmap.c:1379
void bake_reflection_probes()
Bakes ReflectionProbes for the entire scene onto cubemaps on RPROBE_CUBE_MAP_ARRAY.
Definition lightmap.c:786
LightGPU point_lights[2048]
Definition lightmap.c:937
int point_count
Definition lightmap.c:929
LMDebug LMDEBUG[4096]
Definition lightmap.c:938
void walk_coplanar_adjacency(Mesh *mesh, UVIsland *island, UVMapping *uv_map, int island_idx, int current_face_index, bool *tri_list, TriangleAdjacency *tri_adj, vec3 normal, vec3 ref_point, float eps)
Definition lightmap.c:37
void upload_bake_lights_gpu(GLuint *lights_ssbo)
Definition lightmap.c:943
void add_uv_island_to_lightmap(LightmapData *ld, UVIsland *uvi)
Definition lightmap.c:353
uint32_t pad2
Definition lightmap.c:922
void bake_lightmap(LightmapData *ld)
Bakes a UV Lightmap based on which UVIslands have UVIsland.currently_rasterizing set.
Definition lightmap.c:1009
void find_least_parallel_axis(vec3 normal, vec3 *u_dir, vec3 *v_dir)
Definition lightmap.c:124
float point_w
Definition lightmap.c:933
uint32_t pad0
Definition lightmap.c:918
float tri_w
Definition lightmap.c:934
vec3 dir
Definition lightmap.c:919
float total_power
Definition lightmap.c:935
void LM_regenerate_texels()
Regenerates lightmap texels: re-inits LightmapData, then builds UVIslands, then calls add_all_meshes_...
Definition lightmap.c:486
LightmapRect allocate_lightmap_rect(LightmapAtlas *atlas, int w_px, int h_px)
Definition lightmap.c:147
void load_lightmap_from_binary(LightmapData *ld, const char *name)
Loads a UV lightmap and relevant UVIsland and MeshNode data fro, binary.
Definition lightmap.c:610
uint32_t sample_count
Definition lightmap.c:931
void check_lighting_pv()
Determines which LPVs we may need to replace due to distance to new ones.
Definition lightmap.c:1391
float dir_w
Definition lightmap.c:932
void init_uv_islands(LightmapData *ld)
Adds all current UVIslands to the lightmap by allocating LightmapRects.
Definition lightmap.c:384
int compare_distances(const void *a, const void *b)
Definition lightmap.c:1370
void init_lightmap_data(LightmapData *ld)
Frees current LightmapAtlas and UVIsland data and re-initializes the LightmapAtlas array and initiali...
Definition lightmap.c:93
void bake_lighting_probe_volume(ProbeVolume *pv)
Loads a UV lightmap and relevant UVIsland and MeshNode data fro, binary.
Definition lightmap.c:1509
void load_lighting_pv(LPV *lpv, ProbeVolume *pv, int replacing_id)
Definition lightmap.c:1344
LightGPU directional_lights[4]
Definition lightmap.c:936
void save_lightmap_to_binary(LightmapData *ld, const char *name)
Saves a UV lightmap and relevant UVIsland and MeshNode data to binary.
Definition lightmap.c:525
#define MAX_DIRECTIONAL_LIGHTS_LM
Definition lightmap.c:911
void reset_lighting_pvs()
Resets current LPVs from their slots in SceneData.LOADED_LIGHTING_PV.
Definition lightmap.c:1330
void assign_uvs(UVIsland *uvi, LightmapAtlas *atlas)
Definition lightmap.c:230
TriangleAdjacency * tri_adjacency
Adjacent triangles allocated per MeshFace.
size_t * bvh_indices
Indices for the BVH, sorted as the BVH is generated.
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.
Saves and loads various files that store engine data.
GLuint PROBE_SSBO
Definition main.c:159
uint8_t * light_ssbo_ptr
Definition main.c:345
GLuint LPV_TEXTURE
Definition main.c:268
GLuint DIRECTIONAL_MAP_TEXTURE
Definition main.c:222
GLuint RPROBE_FBO
Definition main.c:306
GLuint RPROBE_CUBE_MAP_ARRAY
Definition main.c:305
GLuint LIGHTMAP_BOUNCE_SHADER
light/lightmap_bounce.comp
Definition main.c:225
GLuint RPROBE_SSBO
Definition main.c:160
GLuint LIGHTMAP_VAO
Definition main.c:217
EditorSettings EDITOR_SETTINGS
Definition main.c:330
GLuint LIGHTMAP_VBO
Definition main.c:218
ShaderProgram LIGHTMAP_SHADER
light/lightmap.vert light/lightmap.frag
Definition main.c:128
size_t light_ssbo_size
Definition main.c:344
GLuint LIGHTMAP_DILATE_SHADER
light/lightmap_dilate.comp
Definition main.c:227
GLuint PROBE_UNPACK_SHADER
light/probe_unpack.comp
Definition main.c:230
GLuint LIGHTMAP_DENOISE_SHADER
light/lightmap_denoise.comp
Definition main.c:226
GLuint DIRECTIONAL_MAP_NORMALIZE_SHADER
light/directional_map_normalize.comp
Definition main.c:231
GLuint LIGHTMAP_NORM_TEXTURE
Definition main.c:220
GLuint LIGHTMAP_ID_TEXTURE
Definition main.c:221
GLuint LIGHTMAP_POS_TEXTURE
Definition main.c:219
GLuint SKYBOX
Definition main.c:302
GLuint LIGHTMAP_FBO
Definition main.c:216
Player player
The Player data.
Definition main.c:287
GLuint LIGHT_SSBO
Definition main.c:150
GLuint PROBE_BAKE_SHADER
light/probe_bake.comp
Definition main.c:228
vec3 vec3_cross(vec3 a, vec3 b)
Definition mathf.h:80
float vec3_dist(vec3 a, vec3 b)
Definition mathf.h:318
float vec3_dot(vec3 a, vec3 b)
Definition mathf.h:322
#define MIN(a, b)
Definition mathf.h:435
vec3 vec3_normalize(vec3 v)
Definition mathf.h:46
vec3 vec3_scale(vec3 a, float b)
Definition mathf.h:248
vec3 vec3_sub(vec3 a, vec3 b)
Definition mathf.h:266
vec3 vec3_add(vec3 a, vec3 b)
Definition mathf.h:286
bool m4_inverse(float out[16], const float m[16])
Definition mathf.h:164
#define MAX(a, b)
Definition mathf.h:433
int find_mn_by_name(char name[256])
Finds a Mesh inside of Globals.meshes via string.
Definition mesh.c:229
Mesh functions that modify and initialize Mesh data.
float flux
The total light flux of this node.
int next
The pointer to the next node (either right child or parent's right child) for recursion-less traversa...
int right
Right child.
int parent
Parent.
AABB aabb
The bounding box of all of this node's children.
int left
Left child.
float emissive_flux
The only-emissive flux.
Nodes for the acceleration structures of BVHs.
void bind_baking_ssbos()
Binds SSBOs required for baking.
Definition render.c:31
void unbind_baking_ssbos()
Unbinds SSBOs required for baking.
Definition render.c:42
void copy_light_to_gpu_light(LightGPU *lgpu, Light *l)
Transfers a Light to a LightGPU.
Definition render.c:602
void render_to_cubemap_face(Camera *rprobe_cam, GLuint render_fbo, int render_width, int render_height, bool probe_bake)
Uploads per-frame data to GPU and renders main render pass to a target FBO (omits Text,...
Definition render.c:2278
void add_all_meshes_to_buffer(VertexLayout *layout)
Adds all Meshes that have valid MeshNodes.
Definition render.c:441
The main render pass, data GPU uploads, and SSBO management.
uniform mat4 view
Definition shadow.vert:16
vec3 normal
Definition types.h:284
int material_id
Definition types.h:285
@ B
Definition types.h:26
@ A
Definition types.h:26
@ C
Definition types.h:26
float x
Definition types.h:12
float max_z
Definition types.h:39
float min_x
Definition types.h:37
float max_y
Definition types.h:38
size_t indices[3]
Definition types.h:283
float y
Definition types.h:12
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
size_t capacity
Definition types.h:209
void * buffer
Definition types.h:210
vec2 uv
Definition types.h:224
vec3 normal
Definition types.h:221
float max_x
Definition types.h:37
Definition types.h:218
Definition types.h:333
Definition types.h:21
Definition types.h:12
Definition types.h:13
void init_vector(Vector *v, size_t i, size_t type_size)
Definition utilities.c:74
float triangle_area(vec3 a, vec3 b, vec3 c)
Definition utilities.c:318
void * vector_expand(Vector *v, size_t i)
Definition utilities.c:49
vec4 vec3_to_vec4(vec3 v)
Definition utilities.h:166
#define CAST_VEC(vec, type)
Definition utilities.h:134
vec3 OBB_min_corner(OBB *obb)
Definition utilities.h:607
#define M_PI
Definition utilities.h:52
vec3 triangle_centroid(vec3 A, vec3 B, vec3 C)
Definition utilities.h:640