Untitled
Loading...
Searching...
No Matches
load.c
Go to the documentation of this file.
1
2#include "load.h"
3#include <stdio.h>
4#include <string.h>
5#include "load/init.h"
6#include "light/lightmap.h"
7#include "utilities/types.h"
8#include "stb/stb_image.h"
9#include "render/render.h"
10#include "audio/audio.h"
11#include "ui/fonts.h"
12#include "editor/editor.h"
13
14
15
16
17
18GLuint
20 (
21 const char * path,
22 GLint min_filter,
23 GLint mag_filter,
24 GLint wrap_s,
25 GLint wrap_t,
26 bool sRGB
27 )
28
29{
30
31 int w, h, channels;
32
33 unsigned char *data = stbi_load(path, &w, &h, &channels, 0);
34
35 if (!data) {
36 printf("Failed to load image: %s\n", path);
37 return 0;
38 }
39
40 GLuint textureID;
41 glGenTextures(1, &textureID);
42
43
44
45
46 glBindTexture(GL_TEXTURE_2D, textureID);
47 if (sRGB)
48 {
49
50 if (channels == 3)
51 {
52
53 printf("LOADING: %s %d\n", path, min_filter == GL_LINEAR_MIPMAP_LINEAR);
54 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
55
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
58 float max_aniso = 0.0f;
59 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_aniso);
60 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MIN(8.0f, max_aniso));
61 glGenerateMipmap(GL_TEXTURE_2D);
62
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t);
65 }
66 else
67 {
68 printf("LOADING: %s %d\n", path, min_filter == GL_LINEAR_MIPMAP_LINEAR);
69 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
70
71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
72 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
73 float max_aniso = 0.0f;
74 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_aniso);
75 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MIN(8.0f, max_aniso));
76 glGenerateMipmap(GL_TEXTURE_2D);
77
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t);
80
81 }
82 }
83 else
84 {
85 if (channels == 3)
86 {
87
88 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
89
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
92 float max_aniso = 0.0f;
93 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_aniso);
94 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MIN(8.0f, max_aniso));
95 glGenerateMipmap(GL_TEXTURE_2D);
96
97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s);
98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t);
99 }
100 else if (channels == 4)
101 {
102 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
103
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
105 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
106
107 float max_aniso = 0.0f;
108 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_aniso);
109 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MIN(8.0f, max_aniso));
110
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t);
113
114 glGenerateMipmap(GL_TEXTURE_2D);
115
116
117
118 }
119 else if (channels == 2)
120 {
121 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, w, h, 0, GL_RG, GL_UNSIGNED_BYTE, data);
122
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
125 float max_aniso = 0.0f;
126 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_aniso);
127 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MIN(8.0f, max_aniso));
128 glGenerateMipmap(GL_TEXTURE_2D);
129
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s);
131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t);
132
133 }
134 else if (channels == 1)
135 {
136 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, data);
137
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
140 float max_aniso = 0.0f;
141 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_aniso);
142 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MIN(8.0f, max_aniso));
143 glGenerateMipmap(GL_TEXTURE_2D);
144
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t);
147
148 }
149
150 }
151
152
153 stbi_image_free(data);
154 return textureID;
155
156}
157
158
159GLuint
161 ( const char * path )
162
163{
164 const char a[6][12] =
165 {
166 "/pos_x.hdr", "/neg_x.hdr", "/pos_y.hdr", "/neg_y.hdr", "/pos_z.hdr", "/neg_z.hdr"
167 };
168
169 GLuint cubemap = 0;
170 glGenTextures(1, &cubemap);
171 glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
172 for (int i = 0; i < 6; i++)
173 {
174
175 stbi_set_flip_vertically_on_load(0);
176 char c[428];
177 strcpy(c, path);
178 strcat(c, a[i]);
179 printf("%s\n", c);
180
181 int w = 0, h = 0, channels = 0;
182
183 float * data = stbi_loadf(c, &w, &h, &channels, 0);
184 printf("W %d H %d C %d\n", w, h, channels);
185
186 if (!data) {
187
188 printf("Failed to load image: %s\n", c);
189 return 0;
190 }
191
192 if (channels == 3)
193 {
194
195 glPixelStorei(GL_UNPACK_ALIGNMENT, 3);
196 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, w, h, 0, GL_RGB, GL_FLOAT, NULL);
197 glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, w, h, GL_RGB, GL_FLOAT, data);
198 }
199 else
200 {
201 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
202 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA16F, w, h, 0, GL_RGBA, GL_FLOAT, NULL);
203 glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, w, h, GL_RGBA, GL_FLOAT, data);
204
205 }
206
207 stbi_image_free(data);
208
209 }
210
211 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
212 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
213 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
214 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
215 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
216
217 return cubemap;
218
219}
220
221GLuint load_3d_lut(const char* filepath) {
222 FILE* file = fopen(filepath, "r");
223 if (!file) {
224 printf("Failed to open LUT file: %s\n", filepath);
225 return 0;
226 }
227
228 int lut_size = 0;
229 char line[256];
230
231 while (fgets(line, sizeof(line), file)) {
232 if (strncmp(line, "LUT_3D_SIZE", 11) == 0) {
233 sscanf(line, "LUT_3D_SIZE %d", &lut_size);
234 break;
235 }
236 }
237
238 if (lut_size != 33) {
239 printf("Warning: Expected LUT size 33, got %d\n", lut_size);
240 }
241
242 int total_elements = lut_size * lut_size * lut_size * 3;
243 float* lut_data = (float*)malloc(total_elements * sizeof(float));
244
245 int index = 0;
246 while (fgets(line, sizeof(line), file)) {
247 if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') continue;
248
249 float r, g, b;
250 if (sscanf(line, "%f %f %f", &r, &g, &b) == 3) {
251 lut_data[index++] = r;
252 lut_data[index++] = g;
253 lut_data[index++] = b;
254 }
255 }
256 fclose(file);
257
258 GLuint lut_texture;
259 glGenTextures(1, &lut_texture);
260 glBindTexture(GL_TEXTURE_3D, lut_texture);
261
262 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
263 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
264 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
265
266 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
267 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
268
269 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F, lut_size, lut_size, lut_size, 0, GL_RGB, GL_FLOAT, lut_data);
270
271 free(lut_data);
272 return lut_texture;
273}
274
275unsigned char * open_image(Image * image)
276{
277
278 int w, h, channels;
279
280 unsigned char *data = stbi_load(image->path, &w, &h, &channels, 0);
281 image->width = w;
282 image->height = h;
283 image->channels = channels;
284 if (!data) {
285 printf("Failed to load image: %s\\n", image->path);
286 return NULL;
287 }
288 return data;
289}
290
291
292int add_image_to_registry(char * filename, GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t, bool sRGB)
293{
294
295 GLuint i = load_texture(filename, min_filter, mag_filter, wrap_s, wrap_t, sRGB);
296 if (i == 0) return -1;
297
298 GLuint64 handle = glGetTextureHandleARB(i);
299
300 glMakeTextureHandleResidentARB(handle);
301
302 Image * image = vector_expand(&GLOBALS.images, 1);
303 image->buf = NULL;
304 image->sRGB = sRGB;
305 strncpy(image->path, filename, 255);
306 image->path[255] = '\0';
307 image->channels = 4;
308 image->i = i;
309 image->handle = handle;
310 return (GLOBALS.images.count - 1);
311
312}
313
314
315void save_ui(const char * name)
316{
317
318 char ui_name[256];
319 strcpy(ui_name, ui_registry.directory);
320 strcat(ui_name, name);
321 strcat(ui_name, ".bin");
322
323 FILE * f = fopen(ui_name, "wb");
324 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
325 fwrite(&MENUS.count, sizeof(size_t), 1, f);
326
327 for (int i = 0; i < MENUS.count; i++)
328 {
329 UIMenu * m = &ms[i];
330 fwrite(&m->uID, sizeof(uint32_t), 1, f);
331 fwrite(&m->id, sizeof(char), 256, f);
332 fwrite(m->elements, sizeof(UIElement), MAX_UI_ELEMENTS, f);
333 fwrite(m->containers, sizeof(UIContainer), MAX_UI_CONTAINERS, f);
334
335 for (int j = 0; j < MAX_TEXT_BOXES; j++)
336 {
337
338 TextBox * tb = &m->text_boxes[j];
339 fwrite(tb, sizeof(TextBox), 1, f);
340 fwrite(tb->text, sizeof(char), tb->text_cap, f);
341
342 }
343
344 }
345}
346
347
348void load_ui(const char * name)
349{
350
351 char ui_name[256];
352 strcpy(ui_name, ui_registry.directory);
353 strcat(ui_name, name);
354 strcat(ui_name, ".bin");
355
356
357 FILE * f = fopen(ui_name, "rb");
358 if (MENUS.buffer) free(MENUS.buffer);
359 init_vector(&MENUS, 0, sizeof(UIMenu));
360 fread(&MENUS.count, sizeof(size_t), 1, f);
361 MENUS.buffer = malloc(sizeof(UIMenu) * MENUS.count);
363 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
364
365
366 for (int i = 0; i < MENUS.count; i++)
367 {
368
369 UIMenu * m = &ms[i];
370 fread(&m->uID, sizeof(uint32_t), 1, f);
371 fread(&m->id, sizeof(char), 256, f);
372 fread(m->elements, sizeof(UIElement), MAX_UI_ELEMENTS, f);
373 fread(m->containers, sizeof(UIContainer), MAX_UI_CONTAINERS, f);
374
375 for (int j = 0; j < MAX_TEXT_BOXES; j++)
376 {
377
378 TextBox * tb = &m->text_boxes[j];
379 fread(tb, sizeof(TextBox), 1, f);
380 tb->text = malloc(sizeof(char) * tb->text_cap);
381 fread(tb->text, sizeof(char), tb->text_cap, f);
382
383 }
384
385 }
386
387
388}
389
390
391
393{
394
395 glDeleteTextures(1, &RPROBE_CUBE_MAP_ARRAY);
396 glGenTextures(1, &RPROBE_CUBE_MAP_ARRAY);
397 glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, RPROBE_CUBE_MAP_ARRAY);
398 glTexStorage3D(GL_TEXTURE_CUBE_MAP_ARRAY, MAX_RPROBE_MIPS, GL_RGBA16F, RPROBE_WIDTH, RPROBE_HEIGHT, GLOBALS.scene_data.reflection_probes.count * 6);
399 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
400 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
401 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
402 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
403 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
404
405}
406
407void write_mesh_node(MeshNode * mn, FILE * f)
408{
409
410 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
411 // fwrite(mn->name, sizeof(char), 256, f);
412 fwrite(&mn->active, sizeof(bool), 1, f);
413 fwrite(&mn->id, sizeof(int), 1, f);
414 fwrite(&mn->parent, sizeof(int), 1, f);
415 fwrite(&mn->translation, sizeof(vec3), 1, f);
416 fwrite(&mn->rotation, sizeof(vec4), 1, f);
417 fwrite(&mn->scale, sizeof(vec3), 1, f);
418 fwrite(&mn->local_matrix, sizeof(mat4), 1, f);
419 fwrite(&mn->global_matrix, sizeof(mat4), 1, f);
420 fwrite(&mn->mesh, sizeof(int), 1, f);
421 fwrite(&mn->asset, sizeof(int), 1, f);
422 fwrite(&mn->skin, sizeof(int), 1, f);
423 fwrite(&mn->child_count, sizeof(int), 1, f);
424 fwrite(mn->children, sizeof(int), mn->child_count, f);
425
426 fwrite(&mn->static_mesh, sizeof(bool), 1, f);
427 fwrite(&mn->physics_mode, sizeof(bool), 1, f);
428
429 fwrite(&mn->skip_directional_uv_lighting, sizeof(bool), 1, f);
430
431 if (mn->mesh != -1)
432 {
433
434 Mesh * m = &meshes[mn->mesh];
435 if (m->static_mesh)
436 {
437
438 if (mn->uv_map)
439 {
440
441 bool uv_map = true;
442 fwrite(&uv_map, sizeof(bool), 1, f);
443 fwrite(mn->uv_map, sizeof(UVMapping), m->tris.count, f);
444 }
445 else
446 {
447 bool uv_map = false;
448 fwrite(&uv_map, sizeof(bool), 1, f);
449 }
450
451 }
452
453 }
454
455 if (mn->pc)
456 {
457
458 int id = mn->pc->id;
459 fwrite(&id, sizeof(int), 1, f);
460 }
461 else
462 {
463 int zero = -1;
464 fwrite(&zero, sizeof(int), 1, f);
465 }
466
467 if (mn->ent)
468 {
469
470 int id = mn->ent->id;
471 fwrite(&id, sizeof(int), 1, f);
472 }
473 else
474 {
475 int zero = -1;
476 fwrite(&zero, sizeof(int), 1, f);
477 }
478}
479
480void write_skin(GLTFSkin * skin, FILE * f)
481{
482
483 fwrite(&skin->joint_count, sizeof(int), 1, f);
484 if (skin->joints) fwrite(skin->joints, sizeof(int), skin->joint_count, f);
485
486 if (skin->final_mat) fwrite(skin->final_mat, sizeof(mat4), skin->joint_count, f);
487 if (skin->inverse_bind) fwrite(skin->inverse_bind, sizeof(mat4), skin->joint_count, f);
488
489 fwrite(&skin->ibm_accessor, sizeof(int), 1, f);
490
491 // animation buffer not here
492 fwrite(&skin->animation_indices_count, sizeof(int), 1, f);
493 fwrite(skin->animation_indices, sizeof(int), skin->animation_indices_count, f);
494
495
496}
497
498
499
500
501
502void scene_save(const char * name)
503{
504
505 char scene_name[256];
506 strcpy(scene_name, scene_registry.directory);
507 strcat(scene_name, name);
508 strcat(scene_name, ".bin");
509
510 char lm_name[256];
511 strcpy(lm_name, lightmap_registry.directory);
512 strcat(lm_name, name);
513 strcat(lm_name, "_lm.bin");
514
515 char light_name[256];
516 strcpy(light_name, light_registry.directory);
517 strcat(light_name, name);
518 strcat(light_name, "_lights.bin");
519
520 char aportal_name[256];
521 strcpy(aportal_name, aportal_registry.directory);
522 strcat(aportal_name, name);
523 strcat(aportal_name, "_aportals.bin");
524
525 char rprobe_name[256];
526 strcpy(rprobe_name, rprobe_registry.directory);
527 strcat(rprobe_name, name);
528 strcat(rprobe_name, "_rprobes.bin");
529
530 char lprobe_name[256];
531 strcpy(lprobe_name, lprobe_registry.directory);
532 strcat(lprobe_name, name);
533 strcat(lprobe_name, "_lprobes.bin");
534
535 char aprobe_name[256];
536 strcpy(aprobe_name, aprobe_registry.directory);
537 strcat(aprobe_name, name);
538 strcat(aprobe_name, "_aprobes.bin");
539
540 char scene_apv_name[256];
541 strcpy(scene_apv_name, scene_apv_registry.directory);
542 strcat(scene_apv_name, name);
543 strcat(scene_apv_name, "_scene_apvs.bin");
544
545
546 FILE * f = fopen(scene_name, "wb");
547 printf("%s\n", scene_name);
548 if (!f) return;
549
551
552 save_aportals(aportal_name, &GLOBALS.audio_portals);
553
554 save_aprobes(aprobe_name);
555
557
558 save_vector(light_name, &GLOBALS.lights);
559
560 printf("GLOBALS\n");
561 fwrite(GLOBALS.scene_data.name, sizeof(char), 256, f);
562
563 fwrite(&GLOBALS.scene_data.aabb, sizeof(AABB), 1, f);
564
565 fwrite(&GLOBALS.scene_data.bvh_nodes.count, sizeof(size_t), 1, f);
567
568 fwrite(&GLOBALS.scene_data.light_bvh.nodes.count, sizeof(size_t), 1, f);
570 fwrite(&GLOBALS.scene_data.light_bvh.lights.count, sizeof(size_t), 1, f);
573
574 fwrite(&GLOBALS.scene_data.bvh_indices_count, sizeof(int), 1, f);
576
577 // fwrite(&GLOBALS.scene_data.reflection_probes.count, sizeof(size_t), 1, f);
578 // if (GLOBALS.scene_data.reflection_probes.buffer) fwrite(GLOBALS.scene_data.reflection_probes.buffer, sizeof(ReflectionProbe), GLOBALS.scene_data.reflection_probes.count, f);
579
580 // fwrite(&GLOBALS.scene_data.audio_probe_volumes.count, sizeof(size_t), 1, f);
581 // if (GLOBALS.scene_data.audio_probe_volumes.buffer) fwrite(GLOBALS.scene_data.audio_probe_volumes.buffer, sizeof(ProbeVolume), GLOBALS.scene_data.audio_probe_volumes.count, f);
582
583 // fwrite(&GLOBALS.scene_data.lighting_probe_volumes.count, sizeof(size_t), 1, f);
584 // if (GLOBALS.scene_data.lighting_probe_volumes.buffer) fwrite(GLOBALS.scene_data.lighting_probe_volumes.buffer, sizeof(ProbeVolume), GLOBALS.scene_data.lighting_probe_volumes.count, f);
585
586
587 // fwrite(GLOBALS.scene_data.APV_PATH, sizeof(char), 256, f);
588
589
590 printf("LD\n");
591
593 UVIsland * islands = CAST_VEC(ld->islands, UVIsland);
594 fwrite(&ld->atlas.width, sizeof(int), 1, f);
595 fwrite(&ld->atlas.height, sizeof(int), 1, f);
596 fwrite(&ld->atlas.block_size, sizeof(int), 1, f);
597 if (ld->atlas.occupied)
598 {
599 bool t = true;
600 fwrite(&t, sizeof(bool), 1, f);
601 fwrite(ld->atlas.occupied, sizeof(unsigned char), (ld->atlas.width / ld->atlas.block_size) * (ld->atlas.height / ld->atlas.block_size), f);
602 }
603 else
604 {
605 bool fl = false;
606
607 fwrite(&fl, sizeof(bool), 1, f);
608 }
609 fwrite(&ld->sample_cubemap, sizeof(bool), 1, f);
610
611
612 fwrite(&ld->islands.count, sizeof(size_t), 1, f);
613 for (int i = 0; i < ld->islands.count; i++)
614 {
615
616 UVIsland * uvi = &islands[i];
617 fwrite(&uvi->mesh_node, sizeof(int), 1, f);
618 fwrite(&uvi->tris.count, sizeof(size_t), 1, f);
619 if (uvi->tris.buffer) fwrite(uvi->tris.buffer, sizeof(size_t), uvi->tris.count, f);
620 if (uvi->uvs) fwrite(uvi->uvs, sizeof(LightmapUV), uvi->tris.count, f);
621 fwrite(&uvi->normal, sizeof(vec3), 1, f);
622 fwrite(&uvi->rect, sizeof(LightmapRect), 1, f);
623
624 }
625
626
627 fwrite(&GLOBALS.world_min, sizeof(vec3), 1, f);
628 fwrite(&GLOBALS.world_size, sizeof(vec3), 1, f);
629 fwrite(&GLOBALS.probe_tex_size, sizeof(ivec3), 1, f);
630 fwrite(&GLOBALS.probe_spacing, sizeof(float), 1, f);
631 fwrite(&GLOBALS.noclip, sizeof(bool), 1, f);
632 // fwrite(&GLOBALS.keyboard_locked, sizeof(bool), 1, f);
633
634
635
636
637
638 printf("IMAGES\n");
639
640
641
642 // Images
643 Image * images = CAST_VEC(GLOBALS.images, Image);
644 fwrite(&GLOBALS.images.count, sizeof(size_t), 1, f);
645 for (int i = DEFAULT_TEXTURES; i < GLOBALS.images.count; i++)
646 {
647
648 Image * image = &images[i];
649 fwrite(image->path, sizeof(char), 256, f);
650 fwrite(image->mime_type, sizeof(char), 256, f);
651 fwrite(image->name, sizeof(char), 256, f);
652 fwrite(&image->min_filter, sizeof(GLint), 1, f);
653 fwrite(&image->mag_filter, sizeof(GLint), 1, f);
654 fwrite(&image->wrap_s, sizeof(GLint), 1, f);
655 fwrite(&image->wrap_t, sizeof(GLint), 1, f);
656 fwrite(&image->sRGB, sizeof(bool), 1, f);
657
658 }
659 printf("MATERIALS\n");
660
661 // Materials
663 fwrite(&GLOBALS.materials.count, sizeof(size_t), 1, f);
664 for (int i = 0; i < GLOBALS.materials.count; i++)
665 {
666
667 Material * mat = &mats[i];
668 fwrite(mat->name, sizeof(char), 256, f);
669 fwrite(&mat->fwd_shader, sizeof(int), 1, f);
670 fwrite(&mat->gbuffer_shader, sizeof(int), 1, f);
671
672 fwrite(&mat->ka, sizeof(vec4), 1, f);
673 fwrite(&mat->kd, sizeof(vec4), 1, f);
674 fwrite(&mat->ks, sizeof(vec4), 1, f);
675 fwrite(&mat->ke, sizeof(vec4), 1, f);
676
677 fwrite(mat->absorption_coefficients, sizeof(float), AUDIO_NUM_BANDS, f);
678 fwrite(mat->scattering_coefficients, sizeof(float), AUDIO_NUM_BANDS, f);
679
680 fwrite(&mat->diffuse_tex, sizeof(int), 1, f);
681 fwrite(&mat->normal_tex, sizeof(int), 1, f);
682 fwrite(&mat->emissive_tex, sizeof(int), 1, f);
683 fwrite(&mat->arm_tex, sizeof(int), 1, f);
684 fwrite(&mat->ao_tex, sizeof(int), 1, f);
685 fwrite(&mat->mr_tex, sizeof(int), 1, f);
686
687 fwrite(&mat->normal_scale, sizeof(float), 1, f);
688 fwrite(&mat->ao_strength, sizeof(float), 1, f);
689 fwrite(&mat->metallic, sizeof(float), 1, f);
690 fwrite(&mat->roughness, sizeof(float), 1, f);
691 fwrite(&mat->alpha_mode, sizeof(int), 1, f);
692 fwrite(&mat->alpha_cutoff, sizeof(float), 1, f);
693 fwrite(&mat->emissive_strength, sizeof(float), 1, f);
694 fwrite(&mat->double_sided, sizeof(bool), 1, f);
695
696 }
697
698
699 printf("MESHES\n");
700 // Meshes
701 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
702 fwrite(&GLOBALS.meshes.count, sizeof(size_t), 1, f);
703 for (int i = 0; i < GLOBALS.meshes.count; i++)
704 {
705
706 Mesh * m = &meshes[i];
707 fwrite(m->name, sizeof(char), 256, f);
708 fwrite(&m->bvh_prim, sizeof(bool), 1, f);
709 fwrite(&m->asset_id, sizeof(size_t), 1, f);
710 fwrite(&m->id, sizeof(size_t), 1, f);
711 fwrite(&m->static_mesh, sizeof(bool), 1, f);
712 fwrite(&m->aabb, sizeof(AABB), 1, f);
713
714 fwrite(&m->submeshes.count, sizeof(size_t), 1, f);
715 if (m->submeshes.buffer) fwrite(m->submeshes.buffer, sizeof(SubMesh), m->submeshes.count, f);
716
717 fwrite(&m->hulls.count, sizeof(size_t), 1, f);
718 ConvexHull * hulls = CAST_VEC(m->hulls, ConvexHull);
719 for (int j = 0; j < m->hulls.count; j++)
720 {
721
722 ConvexHull * h = &hulls[j];
723 fwrite(&h->vertices.count, sizeof(size_t), 1, f);
724 if (h->vertices.buffer) fwrite(h->vertices.buffer, sizeof(vec3), h->vertices.count, f);
725 fwrite(&h->faces.count, sizeof(size_t), 1, f);
726 if (h->faces.buffer) fwrite(h->faces.buffer, sizeof(MeshFace), h->faces.count, f);
727 fwrite(&h->hull_faces.count, sizeof(size_t), 1, f);
728 if (h->hull_faces.buffer) fwrite(h->hull_faces.buffer, sizeof(HullFace), h->hull_faces.count, f);
729 fwrite(&h->centroid, sizeof(vec3), 1, f);
730 fwrite(&h->aabb, sizeof(AABB), 1, f);
731
732 }
733
734
735 fwrite(&m->vertices.count, sizeof(size_t), 1, f);
736 if (m->vertices.buffer) fwrite(m->vertices.buffer, sizeof(Point), m->vertices.count, f);
737
738 fwrite(&m->tris.count, sizeof(size_t), 1, f);
739 if (m->tris.buffer) fwrite(m->tris.buffer, sizeof(MeshFace), m->tris.count, f);
740
741
742 TriangleAdjacency * tri_adj = m->tri_adjacency;
743 for (int j = 0; j < m->tris.count; j++)
744 {
745
746 TriangleAdjacency * ta = &tri_adj[j];
747 fwrite(&ta->adjacent_indices.count, sizeof(size_t), 1, f);
748 if (ta->adjacent_indices.buffer) fwrite(ta->adjacent_indices.buffer, sizeof(size_t), ta->adjacent_indices.count, f);
749
750 }
751
752 if (m->static_mesh)
753 {
754
755 fwrite(&m->nodes.count, sizeof(size_t), 1, f);
756 if (m->nodes.buffer) fwrite(m->nodes.buffer, sizeof(BVHNode), m->nodes.count, f);
757
758 fwrite(&m->tri_aabbs.count, sizeof(size_t), 1, f);
759 if (m->tri_aabbs.buffer) fwrite(m->tri_aabbs.buffer, sizeof(AABB), m->tri_aabbs.count, f);
760
761 if (m->bvh_indices) fwrite(m->bvh_indices, sizeof(size_t), m->tris.count, f);
762
763 }
764
765 }
766 printf("MESH NODES\n");
767
768 // Mesh Nodes
770 fwrite(&GLOBALS.mesh_nodes.count, sizeof(size_t), 1, f);
771 for (int i = 0; i < GLOBALS.mesh_nodes.count; i++)
772 {
773 write_mesh_node(&mns[i], f);
774 }
775
776 printf("SKINS\n");
777 // Skins
778
780 fwrite(&GLOBALS.skins.count, sizeof(size_t), 1, f);
781 for (int i = 0; i < GLOBALS.skins.count; i++)
782 {
783
784 GLTFSkin * skin = &skins[i];
785 write_skin(skin, f);
786
787 }
788
789
790
791 printf("ASSETS\n");
792 // Assets
793 Asset * assets = CAST_VEC(GLOBALS.assets, Asset);
794 fwrite(&GLOBALS.assets.count, sizeof(size_t), 1, f);
795 for (int i = 0; i < GLOBALS.assets.count; i++)
796 {
797
798 Asset * asset = &assets[i];
799
800 fwrite(asset->path, sizeof(char), 256, f);
801
802 fwrite(&asset->mesh_indices.count, sizeof(size_t), 1, f);
803 if (asset->mesh_indices.buffer) fwrite(asset->mesh_indices.buffer, sizeof(int), asset->mesh_indices.count, f);
804
805
806 fwrite(&asset->node_count, sizeof(size_t), 1, f);
807 printf("NODE COUNT: %zu\n", asset->node_count);
808 for (int j = 0; j < asset->node_count; j++)
809 {
810 write_mesh_node(&asset->nodes[j], f);
811 }
812
813 fwrite(&asset->skin_count, sizeof(size_t), 1, f);
814 for (int j = 0; j < asset->skin_count; j++)
815 {
816 write_skin(&asset->skins[j], f);
817 }
818
819 fwrite(&asset->animation_count, sizeof(size_t), 1, f);
820 for (int j = 0; j < asset->animation_count; j++)
821 {
822
823 Animation * anim = &asset->animations[j];
824 fwrite(anim->name, sizeof(char), 256, f);
825 fwrite(&anim->asset_id, sizeof(int), 1, f);
826 fwrite(&anim->sampler_count, sizeof(int), 1, f);
827 for (int k = 0; k < anim->sampler_count; k++)
828 {
829 AnimationSampler * sampler = &anim->samplers[k];
830 fwrite(&sampler->count, sizeof(int), 1, f);
831 if (sampler->times) fwrite(sampler->times, sizeof(float), sampler->count, f);
832 fwrite(&sampler->elem_count, sizeof(int), 1, f);
833 if (sampler->values) fwrite(sampler->values, sizeof(float), sampler->elem_count, f);
834 fwrite(&sampler->interpolation, sizeof(int), 1, f);
835
836 }
837
838 fwrite(&anim->channel_count, sizeof(int), 1, f);
839 if (anim->channels) fwrite(anim->channels, sizeof(GLTFAnimationChannel), anim->channel_count, f);
840 // fwrite(&anim->event_count, sizeof(int), 1, f);
841 // fwrite(anim->events, sizeof(AnimationEvent), anim->event_count, f);
842
843
844 }
845
846 fwrite(&asset->asset_id, sizeof(int), 1, f);
847 fwrite(&asset->aabb, sizeof(AABB), 1, f);
848
849 }
850
851
852
853 printf("ASSET INSTANCES\n");
854 // Asset Instances
855 for (int i = 0; i < MAX_ASSET_INSTANCES; i++)
856 {
857
859 fwrite(&ai->free, sizeof(bool), 1, f);
860
861 if (ai->free) continue;
862
863 fwrite(&ai->first_skin, sizeof(size_t), 1, f);
864 fwrite(&ai->skin_count, sizeof(size_t), 1, f);
865
866 fwrite(&ai->asset_id, sizeof(int), 1, f);
867
868 fwrite(&ai->node_count, sizeof(size_t), 1, f);
869
870 fwrite(&ai->aabb, sizeof(AABB), 1, f);
871
872 }
873
874 printf("ENTITIES\n");
875 // Enitites
876 for (int i = 0; i < MAX_ENTITIES; i++)
877 {
878
879 Entity * ent = &ENTITIES[i];
880 fwrite(&ent->free, sizeof(bool), 1, f);
881
882 if (ent->free) continue;
883
884 fwrite(&ent->id, sizeof(size_t), 1, f);
885 fwrite(&ent->mesh_node, sizeof(int), 1, f);
886 fwrite(&ent->skin, sizeof(int), 1, f);
887 fwrite(&ent->aabb, sizeof(AABB), 1, f);
888
889
890 if (ent->instance)
891 {
892 int id = ent->instance->id;
893 fwrite(&id, sizeof(int), 1, f);
894 }
895 else
896 {
897 int n1 = -1;
898 fwrite(&n1, sizeof(int), 1, f);
899 }
900
901 if (ent->pc)
902 {
903
904 int id = ent->pc->id;
905 fwrite(&id, sizeof(int), 1, f);
906
907 }
908 else
909 {
910 int n1 = -1;
911 fwrite(&n1, sizeof(int), 1, f);
912
913 }
914
915 // fwrite(&ent->audio_emitter_count, sizeof(int), 1, f);
916
917
918 }
919
920 printf("PCS\n");
921 // Physics Components
922
923 for (int i = 0; i < MAX_PHYSICS_COMPONENTS; i++)
924 {
925
927 fwrite(&pc->free, sizeof(bool), 1, f);
928 if (pc->free) continue;
929
930
931 fwrite(&pc->id, sizeof(size_t), 1, f);
932 fwrite(&pc->at_rest, sizeof(bool), 1, f);
933 fwrite(&pc->player_pc, sizeof(bool), 1, f);
934 fwrite(&pc->mass, sizeof(float), 1, f);
935 fwrite(&pc->restitution, sizeof(float), 1, f);
936 fwrite(&pc->mu, sizeof(float), 1, f);
937
938 fwrite(&pc->vel, sizeof(vec3), 1, f);
939 fwrite(&pc->angular_velocity, sizeof(vec3), 1, f);
940 fwrite(&pc->center_of_mass, sizeof(vec3), 1, f);
941 fwrite(&pc->inverse_inertia, sizeof(mat3), 1, f);
942 fwrite(&pc->inverse_world, sizeof(mat3), 1, f);
943
944
945 fwrite(&pc->aabb, sizeof(AABB), 1, f);
946
947 fwrite(&pc->cd, sizeof(CollisionData), 1, f);
948
949 if (pc->ent)
950 {
951
952 int id = pc->ent->id;
953 fwrite(&id, sizeof(int), 1, f);
954 }
955 else
956 {
957
958 int n1 = -1;
959 fwrite(&n1, sizeof(int), 1, f);
960 }
961
962
963 }
964
965
966 for (int i = 0; i < MAX_ANIMATION_COMPONENTS; i++)
967 {
968
970 fwrite(&ac->free, sizeof(bool), 1, f);
971 if (ac->free) continue;
972 fwrite(&ac->track_count, sizeof(int), 1, f);
973
974 for (int j = 0; j < ac->track_count; j++)
975 {
976
977 AnimationTrack * t = &ac->tracks[j];
978 fwrite(&t->type, sizeof(AnimationTargetType), 1, f);
979 fwrite(&t->loop, sizeof(bool), 1, f);
980 fwrite(&t->active, sizeof(bool), 1, f);
981 fwrite(&t->current_time, sizeof(float), 1, f);
982 fwrite(&t->duration, sizeof(float), 1, f);
983 fwrite(&t->kf_count, sizeof(int), 1, f);
984
985 for (int k = 0; k < t->kf_count; k++)
986 {
987
988 AnimationKeyframe * kf = &t->kf[k];
989 fwrite(&kf->time, sizeof(float), 1, f);
990 switch(t->type)
991 {
993 fwrite(&kf->val.v, sizeof(float), 1, f);
994 break;
995 case ANIM_TARGET_VEC3:
996 fwrite(&kf->val.v3, sizeof(vec3), 1, f);
997 break;
998 case ANIM_TARGET_INT:
999 fwrite(&kf->val.i, sizeof(int), 1, f);
1000 break;
1001 case ANIM_TARGET_BOOL:
1002 break;
1003 default:
1004 break;
1005 }
1006
1007 }
1008
1009 fwrite(&t->data_type, sizeof(AnimationDataType), 1, f);
1010 fwrite(&t->data_index_offset, sizeof(size_t), 1, f);
1011 fwrite(&t->data_byte_offset, sizeof(size_t), 1, f);
1012
1013 }
1014
1015 if (ac->ent)
1016 {
1017 int id = ac->ent->id;
1018 fwrite(&id, sizeof(int), 1, f);
1019 }
1020 else
1021 {
1022 int n1 = -1;
1023 fwrite(&n1, sizeof(int), 1, f);
1024 }
1025
1026
1027 }
1028
1029 printf("LIGHTS\n");
1030 // Lights
1031 // Light * lights = CAST_VEC(GLOBALS.lights, Light);
1032 // fwrite(&GLOBALS.lights.count, sizeof(size_t), 1, f);
1033 // for (int i = 0; i < GLOBALS.lights.count; i++)
1034 // {
1035 // Light * l = &lights[i];
1036 // fwrite(l, sizeof(Light), 1, f);
1037 // }
1038
1039
1040 Font * fonts = CAST_VEC(FONTS, Font);
1041 fwrite(&FONTS.count, sizeof(size_t), 1, f);
1042 for (int i = 0; i < FONTS.count; i++)
1043 {
1044
1045 Font * font = &fonts[i];
1046 fwrite(font, sizeof(Font), 1, f);
1047 fwrite(&font->glyph_count, sizeof(size_t), 1, f);
1048 if (font->glyphs) fwrite(font->glyphs, sizeof(Glyph), font->glyph_count, f);
1049
1050
1051 }
1052
1053
1054 // TextBox * tbs = CAST_VEC(GLOBALS.text_boxes, TextBox);
1055 // fwrite(&GLOBALS.text_boxes.count, sizeof(size_t), 1, f);
1056 // for (int i = PERFORMANCE_OVERLAY_TEXT_BOX_COUNT; i < GLOBALS.text_boxes.count; i++)
1057 // {
1058 // TextBox * tb = &tbs[i];
1059 // fwrite(tb, sizeof(TextBox), 1, f);
1060 // if (tb->text)
1061 // {
1062
1063 // uint32_t len = strlen(tb->text);
1064 // fwrite(&len, sizeof(uint32_t), 1, f);
1065 // fwrite(tb->text, sizeof(char), len, f);
1066 // }
1067 // else
1068 // {
1069 // uint32_t len = 0;
1070 // fwrite(&len, sizeof(uint32_t), 1, f);
1071 // }
1072 // }
1073
1074 // AudioPortal * portals = CAST_VEC(GLOBALS.audio_portals, AudioPortal);
1075 // fwrite(&GLOBALS.audio_portals.count, sizeof(size_t), 1, f);
1076 // for (int i = 0; i < GLOBALS.audio_portals.count; i++)
1077 // {
1078
1079 // AudioPortal * p = &portals[i];
1080 // fwrite(&p->pos, sizeof(vec3), 1, f);
1081 // fwrite(&p->adjacent_portals.count, sizeof(size_t), 1, f);
1082 // if (p->adjacent_portals.buffer) fwrite(p->adjacent_portals.buffer, sizeof(int), p->adjacent_portals.count, f);
1083
1084 // }
1085
1086
1087
1088
1089
1090 fclose(f);
1091
1092
1093}
1094
1095
1096void read_mesh_node(MeshNode * mn, FILE * f)
1097{
1098
1099 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
1100 // fread(mn->name, sizeof(char), 256, f);
1101 fread(&mn->active, sizeof(bool), 1, f);
1102 fread(&mn->id, sizeof(int), 1, f);
1103 fread(&mn->parent, sizeof(int), 1, f);
1104 fread(&mn->translation, sizeof(vec3), 1, f);
1105 fread(&mn->rotation, sizeof(vec4), 1, f);
1106 fread(&mn->scale, sizeof(vec3), 1, f);
1107 fread(&mn->local_matrix, sizeof(mat4), 1, f);
1108 fread(&mn->global_matrix, sizeof(mat4), 1, f);
1109 fread(&mn->mesh, sizeof(int), 1, f);
1110 fread(&mn->asset, sizeof(int), 1, f);
1111 fread(&mn->skin, sizeof(int), 1, f);
1112 fread(&mn->child_count, sizeof(int), 1, f);
1113 fread(mn->children, sizeof(int), mn->child_count, f);
1114
1115 fread(&mn->static_mesh, sizeof(bool), 1, f);
1116 fread(&mn->physics_mode, sizeof(bool), 1, f);
1117
1118 fread(&mn->skip_directional_uv_lighting, sizeof(bool), 1, f);
1119
1120 if (mn->mesh != -1)
1121 {
1122
1123 Mesh * m = &meshes[mn->mesh];
1124 if (m->static_mesh)
1125 {
1126
1127 bool uv_map = false;
1128 fread(&uv_map, sizeof(bool), 1, f);
1129 if (uv_map)
1130 {
1131 mn->uv_map = malloc(sizeof(UVMapping) * m->tris.count);
1132 fread(mn->uv_map, sizeof(UVMapping), m->tris.count, f);
1133 }
1134 else
1135 {
1136 mn->uv_map = NULL;
1137 }
1138 }
1139 else
1140 {
1141 mn->uv_map = NULL;
1142 }
1143
1144 }
1145
1146 int pc_id = -1;
1147 fread(&pc_id, sizeof(int), 1, f);
1148 if (pc_id != -1)
1149 {
1150
1151 mn->pc = &PHYSICS_COMPONENTS[pc_id];
1152 }
1153 else
1154 {
1155 mn->pc = NULL;
1156 }
1157
1158 int ent_id = -1;
1159 fread(&ent_id, sizeof(int), 1, f);
1160
1161 if (ent_id != -1)
1162 {
1163 mn->ent = &ENTITIES[ent_id];
1164 }
1165 else
1166 {
1167 mn->ent = NULL;
1168 }
1169
1170
1171}
1172
1173void read_skin(GLTFSkin * skin, FILE * f)
1174{
1175
1176 fread(&skin->joint_count, sizeof(int), 1, f);
1177
1178 if (skin->joint_count > 0)
1179 {
1180 skin->joints = malloc(sizeof(int) * skin->joint_count);
1181 skin->final_mat = malloc(sizeof(mat4) * skin->joint_count);
1182 skin->inverse_bind = malloc(sizeof(mat4) * skin->joint_count);
1183
1184 fread(skin->joints, sizeof(int), skin->joint_count, f);
1185 fread(skin->final_mat, sizeof(mat4), skin->joint_count, f);
1186 fread(skin->inverse_bind, sizeof(mat4), skin->joint_count, f);
1187
1188 }
1189
1190 fread(&skin->ibm_accessor, sizeof(int), 1, f);
1191
1192 // animation buffer not here
1193 fread(&skin->animation_indices_count, sizeof(int), 1, f);
1194 fread(skin->animation_indices, sizeof(int), skin->animation_indices_count, f);
1195
1196
1197}
1198
1199
1200void read_vector(Vector * v, size_t byte_size, FILE * f)
1201{
1202 if (v->buffer)
1203 {
1204 free(v->buffer);
1205 v->buffer = NULL;
1206 init_vector(v, 0, byte_size);
1207 }
1208
1209 fread(&v->count, sizeof(size_t), 1, f);
1210
1211 if (v->count > 0)
1212 {
1213 v->buffer = malloc(byte_size * v->count);
1214 v->capacity = v->count;
1215 fread(v->buffer, byte_size, v->count, f);
1216 }
1217 else
1218 {
1219 init_vector(v, 0, byte_size);
1220 }
1221
1222}
1223
1224void read_alloc_vector(Vector * v, size_t byte_size, FILE * f)
1225{
1226
1227 if (v->buffer)
1228 {
1229 free(v->buffer);
1230 v->buffer = NULL;
1231 init_vector(v, 0, byte_size);
1232 }
1233
1234 fread(&v->count, sizeof(size_t), 1, f);
1235
1236 if (v->count > 0)
1237 {
1238 v->buffer = malloc(byte_size * v->count);
1239 v->capacity = v->count;
1240 }
1241 else
1242 {
1243 init_vector(v, 0, byte_size);
1244 }
1245}
1246
1247
1249{
1250
1251 if (mn->uv_map) free(mn->uv_map);
1252}
1253
1255{
1256 if (skin->joints) free(skin->joints);
1257 if (skin->final_mat) free(skin->final_mat);
1258 if (skin->inverse_bind) free(skin->inverse_bind);
1259
1260}
1261
1262void scene_load(const char * name)
1263{
1264
1265 char scene_name[256];
1266 strcpy(scene_name, scene_registry.directory);
1267 strcat(scene_name, name);
1268 strcat(scene_name, ".bin");
1269
1270 char lm_name[256];
1271 strcpy(lm_name, lightmap_registry.directory);
1272 strcat(lm_name, name);
1273 strcat(lm_name, "_lm.bin");
1274
1275 char light_name[256];
1276 strcpy(light_name, light_registry.directory);
1277 strcat(light_name, name);
1278 strcat(light_name, "_lights.bin");
1279
1280 char aportal_name[256];
1281 strcpy(aportal_name, aportal_registry.directory);
1282 strcat(aportal_name, name);
1283 strcat(aportal_name, "_aportals.bin");
1284
1285 char rprobe_name[256];
1286 strcpy(rprobe_name, rprobe_registry.directory);
1287 strcat(rprobe_name, name);
1288 strcat(rprobe_name, "_rprobes.bin");
1289
1290 char lprobe_name[256];
1291 strcpy(lprobe_name, lprobe_registry.directory);
1292 strcat(lprobe_name, name);
1293 strcat(lprobe_name, "_lprobes.bin");
1294
1295 char aprobe_name[256];
1296 strcpy(aprobe_name, aprobe_registry.directory);
1297 strcat(aprobe_name, name);
1298 strcat(aprobe_name, "_aprobes.bin");
1299
1300 char scene_apv_name[256];
1301 strcpy(scene_apv_name, scene_apv_registry.directory);
1302 strcat(scene_apv_name, name);
1303 strcat(scene_apv_name, "_scene_apvs.bin");
1304
1305
1306 FILE * f = fopen(scene_name, "rb");
1307
1308 printf("%s\n", scene_name);
1309 if (!f) return;
1310
1312
1313 load_aportals(aportal_name, &GLOBALS.audio_portals);
1314
1315 load_aprobes(aprobe_name);
1316
1318
1319 load_vector(light_name, &GLOBALS.lights);
1320
1321 printf("GLOBALS\n");
1322 fread(GLOBALS.scene_data.name, sizeof(char), 256, f);
1323
1324 fread(&GLOBALS.scene_data.aabb, sizeof(AABB), 1, f);
1325
1327
1328 // fread(&GLOBALS.scene_data.bvh_nodes.count, sizeof(size_t), 1, f);
1329 // if (GLOBALS.scene_data.bvh_nodes.count > 0)
1330 // {
1331 // GLOBALS.scene_data.bvh_nodes.buffer = malloc(sizeof(BVHNode) * GLOBALS.scene_data.bvh_nodes.count);
1332 // GLOBALS.scene_data.bvh_nodes.capacity = GLOBALS.scene_data.bvh_nodes.count;
1333 // fread(GLOBALS.scene_data.bvh_nodes.buffer, sizeof(BVHNode), GLOBALS.scene_data.bvh_nodes.count, f);
1334 // }
1335 // else
1336 // {
1337 // init_vector(&GLOBALS.scene_data.bvh_nodes, 0, sizeof(BVHNode));
1338 // }
1339
1340 // read_vector(&GLOBALS.scene_data.bvh_nodes, sizeof(BVHNode), f);
1344 {
1347 }
1348 // fread(&GLOBALS.scene_data.leaf_indices.count, sizeof(size_t), 1, f);
1349 // if (GLOBALS.scene_data.leaf_indices.count > 0)
1350 // {
1351
1352 // GLOBALS.scene_data.leaf_indices.buffer = malloc(sizeof(size_t) * GLOBALS.scene_data.leaf_indices.count);
1353 // GLOBALS.scene_data.leaf_indices.capacity = GLOBALS.scene_data.leaf_indices.count;
1354 // fread(GLOBALS.scene_data.leaf_indices.buffer, sizeof(size_t), GLOBALS.scene_data.leaf_indices.count, f);
1355 // }
1356 // else
1357 // {
1358
1359 // init_vector(&GLOBALS.scene_data.leaf_indices, 0, sizeof(size_t));
1360 // }
1362 fread(&GLOBALS.scene_data.bvh_indices_count, sizeof(int), 1, f);
1364 {
1365
1367
1369 }
1370 else
1371 {
1372
1374 }
1375
1376 // read_vector(&GLOBALS.scene_data.reflection_probes, sizeof(ReflectionProbe), f);
1377 // read_vector(&GLOBALS.scene_data.audio_probe_volumes, sizeof(ProbeVolume), f);
1378 // read_vector(&GLOBALS.scene_data.lighting_probe_volumes, sizeof(ProbeVolume), f);
1379
1380 // reset_audio_pvs();
1381 // fread(GLOBALS.scene_data.APV_PATH, sizeof(char), 256, f);
1382 // if (GLOBALS.scene_data.APV_FILE)
1383 // {
1384 // fclose(GLOBALS.scene_data.APV_FILE);
1385 // GLOBALS.scene_data.APV_FILE = NULL;
1386 // }
1387 // GLOBALS.scene_data.APV_FILE = fopen(GLOBALS.scene_data.APV_PATH, "rb+");
1388
1389 // fread(&GLOBALS.scene_data.reflection_probes.count, sizeof(size_t), 1, f);
1390 // if (GLOBALS.scene_data.reflection_probes.count > 0)
1391 // {
1392
1393 // GLOBALS.scene_data.reflection_probes.buffer = malloc(sizeof(ReflectionProbe) * GLOBALS.scene_data.reflection_probes.count);
1394 // GLOBALS.scene_data.reflection_probes.capacity = GLOBALS.scene_data.reflection_probes.count;
1395 // fread(GLOBALS.scene_data.reflection_probes.buffer, sizeof(ReflectionProbe), GLOBALS.scene_data.reflection_probes.count, f);
1396 // }
1397 // else
1398 // {
1399
1400 // init_vector(&GLOBALS.scene_data.reflection_probes, 0, sizeof(size_t));
1401 // }
1402
1403 printf("LD\n");
1404
1406 fread(&ld->atlas.width, sizeof(int), 1, f);
1407 fread(&ld->atlas.height, sizeof(int), 1, f);
1408 fread(&ld->atlas.block_size, sizeof(int), 1, f);
1409 bool t = false;
1410 fread(&t, sizeof(bool), 1, f);
1411 if(ld->atlas.occupied) free(ld->atlas.occupied);
1412 ld->atlas.occupied = NULL;
1413 if (t)
1414 {
1415 ld->atlas.occupied = calloc((ld->atlas.width / ld->atlas.block_size) * (ld->atlas.height / ld->atlas.block_size), sizeof(unsigned char));
1416 fread(ld->atlas.occupied, sizeof(unsigned char), (ld->atlas.width / ld->atlas.block_size) * (ld->atlas.height / ld->atlas.block_size), f);
1417 }
1418 else
1419 {
1420 ld->atlas.occupied = NULL;
1421 }
1422 fread(&ld->sample_cubemap, sizeof(bool), 1, f);
1423
1424
1425 fread(&ld->islands.count, sizeof(size_t), 1, f);
1426 if (ld->islands.count> 0)
1427 {
1428
1429
1430 ld->islands.buffer = malloc(sizeof(UVIsland) * ld->islands.count);
1431 ld->islands.capacity = ld->islands.count;
1432
1433 }
1434 else
1435 {
1436
1437 init_vector(&ld->islands, 0, sizeof(UVIsland));
1438 }
1439 UVIsland * islands = CAST_VEC(ld->islands, UVIsland);
1440 for (int i = 0; i < ld->islands.count; i++)
1441 {
1442
1443 UVIsland * uvi = &islands[i];
1444 fread(&uvi->mesh_node, sizeof(int), 1, f);
1445 fread(&uvi->tris.count, sizeof(size_t), 1, f);
1446 if (uvi->tris.count > 0)
1447 {
1448 uvi->tris.buffer = malloc(sizeof(size_t) * uvi->tris.count);
1449 uvi->tris.capacity = uvi->tris.count;
1450 fread(uvi->tris.buffer, sizeof(size_t), uvi->tris.count, f);
1451 uvi->uvs = malloc(sizeof(LightmapUV) * uvi->tris.count);
1452 fread(uvi->uvs, sizeof(LightmapUV), uvi->tris.count, f);
1453
1454 }
1455 else
1456 {
1457 init_vector(&uvi->tris, 0, sizeof(size_t));
1458 uvi->uvs = NULL;
1459 }
1460 fread(&uvi->normal, sizeof(vec3), 1, f);
1461 fread(&uvi->rect, sizeof(LightmapRect), 1, f);
1462
1463
1464 }
1465
1466
1467 fread(&GLOBALS.world_min, sizeof(vec3), 1, f);
1468 fread(&GLOBALS.world_size, sizeof(vec3), 1, f);
1469 fread(&GLOBALS.probe_tex_size, sizeof(ivec3), 1, f);
1470 fread(&GLOBALS.probe_spacing, sizeof(float), 1, f);
1471 fread(&GLOBALS.noclip, sizeof(bool), 1, f);
1472 // fread(&GLOBALS.keyboard_locked, sizeof(bool), 1, f);
1473
1474
1475
1476
1477
1478
1479 printf("IMAGES\n");
1480
1481
1482
1483 // Images
1484 size_t old_count = GLOBALS.images.count;
1485 Image * images = CAST_VEC(GLOBALS.images, Image);
1486 for (int i = DEFAULT_TEXTURES; i < GLOBALS.images.count; i++)
1487 {
1488
1489 Image * image = &images[i];
1490 if (image->buf)
1491 {
1492 free(image->buf);
1493 image->buf = NULL;
1494 }
1495 if (image->i)
1496 {
1497 glDeleteTextures(1, &image->i);
1498 }
1499 }
1500
1501 fread(&GLOBALS.images.count, sizeof(size_t), 1, f);
1502 if (GLOBALS.images.count > 0)
1503 {
1504
1505
1506 if (old_count < GLOBALS.images.count)
1507 {
1508
1511 }
1512
1513 }
1514 else
1515 {
1516
1517 init_vector(&GLOBALS.images, 0, sizeof(Image));
1518 }
1519 images = CAST_VEC(GLOBALS.images, Image);
1520 for (int i = DEFAULT_TEXTURES; i < GLOBALS.images.count; i++)
1521 {
1522
1523 Image * image = &images[i];
1524 fread(image->path, sizeof(char), 256, f);
1525 fread(image->mime_type, sizeof(char), 256, f);
1526 fread(image->name, sizeof(char), 256, f);
1527 fread(&image->min_filter, sizeof(GLint), 1, f);
1528 fread(&image->mag_filter, sizeof(GLint), 1, f);
1529 fread(&image->wrap_s, sizeof(GLint), 1, f);
1530 fread(&image->wrap_t, sizeof(GLint), 1, f);
1531 fread(&image->sRGB, sizeof(bool), 1, f);
1532 GLuint i = load_texture(image->path, image->min_filter, image->mag_filter, image->wrap_s, image->wrap_t, image->sRGB);
1533
1534 // GLuint64 handle = 0;
1535 GLuint64 handle = glGetTextureHandleARB(i);
1536
1537 glMakeTextureHandleResidentARB(handle);
1538 image->handle = handle;
1539 image->i = i;
1540 image->channels = 4;
1541
1542
1543
1544 }
1545 printf("MATERIALS\n");
1547 {
1548 free(GLOBALS.materials.buffer);
1549 init_vector(&GLOBALS.materials, 0, sizeof(Material));
1550
1551 }
1552
1553 // Materials
1554 fread(&GLOBALS.materials.count, sizeof(size_t), 1, f);
1555 if (GLOBALS.materials.count > 0)
1556 {
1557
1558
1561
1562 // fread(GLOBALS.materials.buffer, sizeof(Material), GLOBALS.materials.count, f);
1563 }
1564 else
1565 {
1566 init_vector(&GLOBALS.materials, 0, sizeof(Material));
1567 }
1569 for (int i = 0; i < GLOBALS.materials.count; i++)
1570 {
1571
1572 Material * mat = &mats[i];
1573 fread(mat->name, sizeof(char), 256, f);
1574 fread(&mat->fwd_shader, sizeof(int), 1, f);
1575 fread(&mat->gbuffer_shader, sizeof(int), 1, f);
1576
1577 fread(&mat->ka, sizeof(vec4), 1, f);
1578 fread(&mat->kd, sizeof(vec4), 1, f);
1579 fread(&mat->ks, sizeof(vec4), 1, f);
1580 fread(&mat->ke, sizeof(vec4), 1, f);
1581
1582
1583 fread(mat->absorption_coefficients, sizeof(float), AUDIO_NUM_BANDS, f);
1584 fread(mat->scattering_coefficients, sizeof(float), AUDIO_NUM_BANDS, f);
1585
1586 fread(&mat->diffuse_tex, sizeof(int), 1, f);
1587 fread(&mat->normal_tex, sizeof(int), 1, f);
1588 fread(&mat->emissive_tex, sizeof(int), 1, f);
1589 fread(&mat->arm_tex, sizeof(int), 1, f);
1590 fread(&mat->ao_tex, sizeof(int), 1, f);
1591 fread(&mat->mr_tex, sizeof(int), 1, f);
1592
1593 fread(&mat->normal_scale, sizeof(float), 1, f);
1594 fread(&mat->ao_strength, sizeof(float), 1, f);
1595 fread(&mat->metallic, sizeof(float), 1, f);
1596 fread(&mat->roughness, sizeof(float), 1, f);
1597 fread(&mat->alpha_mode, sizeof(int), 1, f);
1598 fread(&mat->alpha_cutoff, sizeof(float), 1, f);
1599 fread(&mat->emissive_strength, sizeof(float), 1, f);
1600 fread(&mat->double_sided, sizeof(bool), 1, f);
1601
1602 }
1603
1604
1605 printf("MESHES\n");
1606 // Meshes
1607 if (GLOBALS.meshes.buffer)
1608 {
1609 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
1610 for (int i = 0; i < GLOBALS.meshes.count; i++)
1611 {
1612
1613 Mesh * m = &meshes[i];
1614 if (m->submeshes.buffer) free(m->submeshes.buffer);
1615 if (m->hulls.buffer) free(m->hulls.buffer);
1616 if (m->nodes.buffer) free(m->nodes.buffer);
1617 if (m->vertices.buffer) free(m->vertices.buffer);
1618 if (m->tris.buffer) free(m->tris.buffer);
1619 if (m->tri_aabbs.buffer) free(m->tri_aabbs.buffer);
1620 if (m->tri_adjacency)
1621 {
1622 for (int j = 0; j < m->tris.count; j++)
1623 {
1625 {
1627 }
1628 }
1629 free(m->tri_adjacency);
1630
1631 }
1632 if (m->bvh_indices)
1633 {
1634 free(m->bvh_indices);
1635 }
1636 }
1637
1638 free(GLOBALS.meshes.buffer);
1639 init_vector(&GLOBALS.meshes, 0, sizeof(Mesh));
1640
1641 }
1642 fread(&GLOBALS.meshes.count, sizeof(size_t), 1, f);
1643 if (GLOBALS.meshes.count > 0)
1644 {
1645
1646
1647 GLOBALS.meshes.buffer = malloc(sizeof(Mesh) * GLOBALS.meshes.count);
1649
1650 // fread(GLOBALS.meshes.buffer, sizeof(Mesh), GLOBALS.meshes.count, f);
1651 }
1652 else
1653 {
1654
1655 init_vector(&GLOBALS.meshes, 0, sizeof(Mesh));
1656 }
1657 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
1658 for (int i = 0; i < GLOBALS.meshes.count; i++)
1659 {
1660
1661 Mesh * m = &meshes[i];
1662 fread(m->name, sizeof(char), 256, f);
1663 fread(&m->bvh_prim, sizeof(bool), 1, f);
1664 fread(&m->asset_id, sizeof(size_t), 1, f);
1665 fread(&m->id, sizeof(size_t), 1, f);
1666 fread(&m->static_mesh, sizeof(bool), 1, f);
1667 fread(&m->aabb, sizeof(AABB), 1, f);
1668
1669 // fread(&m->submeshes.count, sizeof(size_t), 1, f);
1670 m->submeshes.buffer = NULL;
1671 read_vector(&m->submeshes, sizeof(SubMesh), f);
1672 // if (m->submeshes.count > 0)
1673 // {
1674
1675 // m->submeshes.buffer = malloc(sizeof(SubMesh) * m->submeshes.count);
1676 // m->submeshes.capacity = m->submeshes.count;
1677
1678 // fread(m->submeshes.buffer, sizeof(SubMesh), m->submeshes.count, f);
1679 // }
1680 // else
1681 // {
1682
1683 // init_vector(&m->submeshes, 0, sizeof(SubMesh));
1684 // }
1685
1686
1687 fread(&m->hulls.count, sizeof(size_t), 1, f);
1688 if (m->hulls.count> 0)
1689 {
1690
1691
1692 m->hulls.buffer = malloc(sizeof(ConvexHull) * m->hulls.count);
1693 m->hulls.capacity = m->hulls.count;
1694
1695 // fread(m->hulls.buffer, sizeof(ConvexHull), m->hulls.count, f);
1696 }
1697 else
1698 {
1699
1700 init_vector(&m->hulls, 0, sizeof(ConvexHull));
1701 }
1702 ConvexHull * hulls = CAST_VEC(m->hulls, ConvexHull);
1703 for (int j = 0; j < m->hulls.count; j++)
1704 {
1705
1706 ConvexHull * h = &hulls[j];
1707 h->vertices.buffer = NULL;
1708 read_vector(&h->vertices, sizeof(vec3), f);
1709 // fread(&h->vertices.count, sizeof(size_t), 1, f);
1710 // if (h->vertices.count > 0)
1711 // {
1712
1713 // h->vertices.buffer = malloc(sizeof(vec3) * h->vertices.count);
1714 // h->vertices.capacity = h->vertices.count;
1715
1716 // fread(h->vertices.buffer, sizeof(vec3), h->vertices.count, f);
1717 // }
1718 // else
1719 // {
1720
1721 // init_vector(&h->vertices, 0, sizeof(vec3));
1722 // }
1723 h->faces.buffer = NULL;
1724 read_vector(&h->faces, sizeof(MeshFace), f);
1725 // fread(&h->faces.count, sizeof(size_t), 1, f);
1726 // if (h->faces.count > 0)
1727 // {
1728
1729 // h->faces.buffer = malloc(sizeof(MeshFace) * h->faces.count);
1730 // h->faces.capacity = h->faces.count;
1731
1732 // fread(h->faces.buffer, sizeof(MeshFace), h->faces.count, f);
1733 // }
1734 // else
1735 // {
1736
1737 // init_vector(&h->faces, 0, sizeof(MeshFace));
1738 // }
1739 h->hull_faces.buffer = NULL;
1740 read_vector(&h->hull_faces, sizeof(HullFace), f);
1741 // fread(&h->hull_faces.count, sizeof(size_t), 1, f);
1742 // if (h->hull_faces.count > 0)
1743 // {
1744
1745 // h->hull_faces.buffer = malloc(sizeof(HullFace) * h->hull_faces.count);
1746 // h->hull_faces.capacity = h->hull_faces.count;
1747
1748 // fread(h->hull_faces.buffer, sizeof(HullFace), h->hull_faces.count, f);
1749 // }
1750 // else
1751 // {
1752
1753 // init_vector(&h->hull_faces, 0, sizeof(HullFace));
1754 // }
1755 fread(&h->centroid, sizeof(vec3), 1, f);
1756 fread(&h->aabb, sizeof(AABB), 1, f);
1757
1758 }
1759
1760
1761 m->vertices.buffer = NULL;
1762 read_vector(&m->vertices, sizeof(Point), f);
1763 // fread(&m->vertices.count, sizeof(size_t), 1, f);
1764 // if (m->vertices.count > 0)
1765 // {
1766
1767 // m->vertices.buffer = malloc(sizeof(Point) * m->vertices.count);
1768 // m->vertices.capacity = m->vertices.count;
1769
1770 // fread(m->vertices.buffer, sizeof(Point), m->vertices.count, f);
1771 // }
1772 // else
1773 // {
1774
1775 // init_vector(&m->vertices, 0, sizeof(Point));
1776 // }
1777
1778 m->tris.buffer = NULL;
1779 read_vector(&m->tris, sizeof(MeshFace), f);
1780 // fread(&m->tris.count, sizeof(size_t), 1, f);
1781 // if (m->tris.count > 0)
1782 // {
1783
1784 // m->tris.buffer = malloc(sizeof(MeshFace) * m->tris.count);
1785 // m->tris.capacity = m->tris.count;
1786
1787 // fread(m->tris.buffer, sizeof(MeshFace), m->tris.count, f);
1788 // }
1789 // else
1790 // {
1791
1792 // init_vector(&m->tris, 0, sizeof(MeshFace));
1793 // }
1794
1795
1796 m->tri_adjacency = malloc(sizeof(TriangleAdjacency) * m->tris.count);
1797 TriangleAdjacency * tri_adj = m->tri_adjacency;
1798 for (int j = 0; j < m->tris.count; j++)
1799 {
1800
1801 TriangleAdjacency * ta = &tri_adj[j];
1802 ta->adjacent_indices.buffer = NULL;
1803 read_vector(&ta->adjacent_indices, sizeof(size_t), f);
1804 // fread(&ta->adjacent_indices.count, sizeof(size_t), 1, f);
1805 // if (ta->adjacent_indices.count > 0)
1806 // {
1807
1808 // ta->adjacent_indices.buffer = malloc(sizeof(size_t) * ta->adjacent_indices.count);
1809 // ta->adjacent_indices.capacity = ta->adjacent_indices.count;
1810
1811 // fread(ta->adjacent_indices.buffer, sizeof(size_t), ta->adjacent_indices.count, f);
1812 // }
1813 // else
1814 // {
1815
1816 // init_vector(&ta->adjacent_indices, 0, sizeof(size_t));
1817 // }
1818
1819 }
1820
1821 if (m->static_mesh)
1822 {
1823
1824 m->nodes.buffer = NULL;
1825 read_vector(&m->nodes, sizeof(BVHNode), f);
1826
1827 m->tri_aabbs.buffer = NULL;
1828 read_vector(&m->tri_aabbs, sizeof(AABB), f);
1829
1830 m->bvh_indices = malloc(sizeof(size_t) * m->tris.count);
1831
1832 fread(m->bvh_indices, sizeof(size_t), m->tris.count, f);
1833
1834 }
1835 else
1836 {
1837 init_vector(&m->nodes, 0, sizeof(BVHNode));
1838 init_vector(&m->tri_aabbs, 0, sizeof(AABB));
1839 m->bvh_indices = NULL;
1840 }
1841
1842 }
1843 printf("MESH NODES\n");
1844
1846 {
1847
1849 for (int i = 0; i < GLOBALS.mesh_nodes.count; i++)
1850 {
1851 MeshNode * mn = &mns[i];
1852 if (mn->uv_map) free(mn->uv_map);
1853 mn->uv_map = NULL;
1854 }
1855
1857 init_vector(&GLOBALS.mesh_nodes, 0, sizeof(MeshNode));
1858
1859 }
1860 // Mesh Nodes
1861 fread(&GLOBALS.mesh_nodes.count, sizeof(size_t), 1, f);
1862 if (GLOBALS.mesh_nodes.count> 0)
1863 {
1864
1865
1868
1869 }
1870 else
1871 {
1872
1873 init_vector(&GLOBALS.mesh_nodes, 0, sizeof(MeshNode));
1874 }
1876 for (int i = 0; i < GLOBALS.mesh_nodes.count; i++)
1877 {
1878 read_mesh_node(&mns[i], f);
1879 }
1880
1881 printf("SKINS\n");
1882 // Skins
1883
1884 if (GLOBALS.skins.buffer)
1885 {
1886
1888 for (int i = 0; i < GLOBALS.skins.count; i++)
1889 {
1890
1891 GLTFSkin * skin = &skins[i];
1892 if (skin->joints) free(skin->joints);
1893 if (skin->final_mat) free(skin->final_mat);
1894 if (skin->inverse_bind) free(skin->inverse_bind);
1895
1896 }
1897
1898
1899 free(GLOBALS.skins.buffer);
1900 init_vector(&GLOBALS.skins, 0, sizeof(GLTFSkin));
1901
1902 }
1903 fread(&GLOBALS.skins.count, sizeof(size_t), 1, f);
1904 if (GLOBALS.skins.count> 0)
1905 {
1906
1907
1908 GLOBALS.skins.buffer = malloc(sizeof(GLTFSkin) * GLOBALS.skins.count);
1910
1911 }
1912 else
1913 {
1914
1915 init_vector(&GLOBALS.skins, 0, sizeof(GLTFSkin));
1916 }
1918 for (int i = 0; i < GLOBALS.skins.count; i++)
1919 {
1920
1921 GLTFSkin * skin = &skins[i];
1922 read_skin(skin, f);
1923
1924 }
1925
1926
1927
1928 printf("ASSETS\n");
1929 // Assets
1930 if (GLOBALS.assets.buffer)
1931 {
1932
1933 Asset * assets = CAST_VEC(GLOBALS.assets, Asset);
1934 for (int i = 0; i < GLOBALS.assets.count; i++)
1935 {
1936 Asset * asset = &assets[i];
1937 for (int j = 0; j < asset->node_count; j++)
1938 {
1939 free_mesh_node(&asset->nodes[j]);
1940 }
1941 if (asset->nodes) free(asset->nodes);
1942 for (int j = 0; j < asset->skin_count; j++)
1943 {
1944 free_skin(&asset->skins[j]);
1945 }
1946 if (asset->skins) free(asset->skins);
1947
1948 for (int j = 0; j < asset->animation_count; j++)
1949 {
1950 Animation * anim = &asset->animations[j];
1951 for (int k = 0; k < anim->sampler_count; k++)
1952 {
1953 AnimationSampler * s = &anim->samplers[k];
1954 if (s->times) free(s->times);
1955 if (s->values) free(s->values);
1956
1957 }
1958 if (anim->samplers) free(anim->samplers);
1959
1960 if (anim->channels) free(anim->channels);
1961
1962 }
1963
1964 if (asset->animations) free(asset->animations);
1965 }
1966
1967 free(GLOBALS.assets.buffer);
1968 init_vector(&GLOBALS.assets, 0, sizeof(Asset));
1969
1970 }
1971 fread(&GLOBALS.assets.count, sizeof(size_t), 1, f);
1972 if (GLOBALS.assets.count> 0)
1973 {
1974
1975
1976 GLOBALS.assets.buffer = malloc(sizeof(Asset) * GLOBALS.assets.count);
1978
1979 // fread(GLOBALS.assets.buffer, sizeof(Asset), GLOBALS.assets.count, f);
1980 }
1981 else
1982 {
1983
1984 init_vector(&GLOBALS.assets, 0, sizeof(Asset));
1985 }
1986 Asset * assets = CAST_VEC(GLOBALS.assets, Asset);
1987 for (int i = 0; i < GLOBALS.assets.count; i++)
1988 {
1989
1990 Asset * asset = &assets[i];
1991
1992 fread(asset->path, sizeof(char), 256, f);
1993
1994 fread(&asset->mesh_indices.count, sizeof(size_t), 1, f);
1995 if (asset->mesh_indices.count > 0)
1996 {
1997
1998 asset->mesh_indices.buffer = malloc(sizeof(int) * asset->mesh_indices.count);
1999 asset->mesh_indices.capacity = asset->mesh_indices.count;
2000
2001 fread(asset->mesh_indices.buffer, sizeof(int), asset->mesh_indices.count, f);
2002 }
2003 else
2004 {
2005
2006 init_vector(&asset->mesh_indices, 0, sizeof(int));
2007 }
2008
2009
2010 fread(&asset->node_count, sizeof(size_t), 1, f);
2011 printf("NODE COUNT: %zu\n", asset->node_count);
2012 if (asset->node_count > 0)
2013 {
2014
2015 asset->nodes = malloc(sizeof(MeshNode) * asset->node_count);
2016 for (int j = 0; j < asset->node_count; j++)
2017 {
2018 read_mesh_node(&asset->nodes[j], f);
2019 }
2020 }
2021 else
2022 {
2023 asset->nodes = NULL;
2024 }
2025
2026 fread(&asset->skin_count, sizeof(size_t), 1, f);
2027 if (asset->skin_count > 0)
2028 {
2029
2030 asset->skins = malloc(sizeof(GLTFSkin) * asset->skin_count);
2031 for (int j = 0; j < asset->skin_count; j++)
2032 {
2033 read_skin(&asset->skins[j], f);
2034 }
2035 }
2036 else
2037 {
2038 asset->skins = NULL;
2039
2040 }
2041
2042 fread(&asset->animation_count, sizeof(size_t), 1, f);
2043 if (asset->animation_count > 0)
2044 {
2045 asset->animations = malloc(sizeof(Animation) * asset->animation_count);
2046 for (int j = 0; j < asset->animation_count; j++)
2047 {
2048
2049 Animation * anim = &asset->animations[j];
2050 fread(anim->name, sizeof(char), 256, f);
2051 fread(&anim->asset_id, sizeof(int), 1, f);
2052 fread(&anim->sampler_count, sizeof(int), 1, f);
2053 for (int k = 0; k < anim->sampler_count; k++)
2054 {
2055 AnimationSampler * sampler = &anim->samplers[k];
2056 fread(&sampler->count, sizeof(int), 1, f);
2057 if (sampler->count > 0)
2058 {
2059
2060 sampler->times = malloc(sizeof(float) * sampler->count);
2061
2062 fread(sampler->times, sizeof(float), sampler->count, f);
2063 }
2064 else
2065 {
2066 sampler->times = NULL;
2067 }
2068 fread(&sampler->elem_count, sizeof(int), 1, f);
2069 if (sampler->elem_count > 0)
2070 {
2071
2072 sampler->values = malloc(sizeof(float) * sampler->elem_count);
2073
2074 fread(sampler->values, sizeof(float), sampler->elem_count, f);
2075 }
2076 else
2077 {
2078 sampler->values = NULL;
2079 }
2080 fread(&sampler->interpolation, sizeof(int), 1, f);
2081
2082 }
2083
2084 fread(&anim->channel_count, sizeof(int), 1, f);
2085 if (anim->channel_count> 0)
2086 {
2087
2088 anim->channels= malloc(sizeof(GLTFAnimationChannel) * anim->channel_count);
2089
2090 fread(anim->channels, sizeof(GLTFAnimationChannel), anim->channel_count, f);
2091 }
2092 else
2093 {
2094 anim->channels= NULL;
2095 }
2096
2097 // fwrite(&anim->event_count, sizeof(int), 1, f);
2098 // fwrite(anim->events, sizeof(AnimationEvent), anim->event_count, f);
2099
2100
2101 }
2102
2103 }
2104 else
2105 {
2106 asset->animations = NULL;
2107 }
2108
2109 fread(&asset->asset_id, sizeof(int), 1, f);
2110 fread(&asset->aabb, sizeof(AABB), 1, f);
2111
2112 }
2113
2114
2115
2116 printf("ASSET INSTANCES\n");
2117 // Asset Instances
2118 for (int i = 0; i < MAX_ASSET_INSTANCES; i++)
2119 {
2120
2121 AssetInstance * ai = &ASSET_INSTANCES[i];
2122 fread(&ai->free, sizeof(bool), 1, f);
2123
2124 if (ai->free) continue;
2125
2126 fread(&ai->first_skin, sizeof(size_t), 1, f);
2127 fread(&ai->skin_count, sizeof(size_t), 1, f);
2128
2129 fread(&ai->asset_id, sizeof(int), 1, f);
2130
2131 fread(&ai->node_count, sizeof(size_t), 1, f);
2132
2133 fread(&ai->aabb, sizeof(AABB), 1, f);
2134
2135 }
2136
2137 printf("ENTITIES\n");
2138 // Enitites
2139 for (int i = 0; i < MAX_ENTITIES; i++)
2140 {
2141
2142 Entity * ent = &ENTITIES[i];
2143 fread(&ent->free, sizeof(bool), 1, f);
2144
2145 if (ent->free) continue;
2146
2147 fread(&ent->id, sizeof(size_t), 1, f);
2148 fread(&ent->mesh_node, sizeof(int), 1, f);
2149 fread(&ent->skin, sizeof(int), 1, f);
2150 fread(&ent->aabb, sizeof(AABB), 1, f);
2151
2152
2153 int instance_id = -1;
2154 fread(&instance_id, sizeof(int), 1, f);
2155 if (instance_id != -1)
2156 {
2157 ent->instance = &ASSET_INSTANCES[instance_id];
2158 }
2159 else
2160 {
2161 ent->instance = NULL;
2162 }
2163 int pc_id = -1;
2164 fread(&pc_id, sizeof(int), 1, f);
2165 if (pc_id != -1)
2166 {
2167 ent->pc = &PHYSICS_COMPONENTS[pc_id];
2168 }
2169 else
2170 {
2171 ent->pc = NULL;
2172 }
2173 }
2174
2175 printf("PCS\n");
2176 // Physics Components
2177
2178 for (int i = 0; i < MAX_PHYSICS_COMPONENTS; i++)
2179 {
2180
2182 fread(&pc->free, sizeof(bool), 1, f);
2183 if (pc->free) continue;
2184
2185
2186 fread(&pc->id, sizeof(size_t), 1, f);
2187 fread(&pc->at_rest, sizeof(bool), 1, f);
2188 fread(&pc->player_pc, sizeof(bool), 1, f);
2189 fread(&pc->mass, sizeof(float), 1, f);
2190 fread(&pc->restitution, sizeof(float), 1, f);
2191 fread(&pc->mu, sizeof(float), 1, f);
2192
2193 fread(&pc->vel, sizeof(vec3), 1, f);
2194 fread(&pc->angular_velocity, sizeof(vec3), 1, f);
2195 fread(&pc->center_of_mass, sizeof(vec3), 1, f);
2196 fread(&pc->inverse_inertia, sizeof(mat3), 1, f);
2197 fread(&pc->inverse_world, sizeof(mat3), 1, f);
2198
2199
2200 fread(&pc->aabb, sizeof(AABB), 1, f);
2201
2202 fread(&pc->cd, sizeof(CollisionData), 1, f);
2203
2204 int ent_id = -1;
2205 fread(&ent_id, sizeof(int), 1, f);
2206 if (ent_id != -1)
2207 {
2208 pc->ent = &ENTITIES[ent_id];
2209 }
2210 else
2211 {
2212 pc->ent = NULL;
2213 }
2214
2215
2216 }
2217
2218 for (int i = 0; i < MAX_ANIMATION_COMPONENTS; i++)
2219 {
2220
2222 fread(&ac->free, sizeof(bool), 1, f);
2223 if (ac->free) continue;
2224 fread(&ac->track_count, sizeof(int), 1, f);
2225
2226 for (int j = 0; j < ac->track_count; j++)
2227 {
2228
2229 AnimationTrack * t = &ac->tracks[j];
2230 fread(&t->type, sizeof(AnimationTargetType), 1, f);
2231 fread(&t->loop, sizeof(bool), 1, f);
2232 fread(&t->active, sizeof(bool), 1, f);
2233 fread(&t->current_time, sizeof(float), 1, f);
2234 fread(&t->duration, sizeof(float), 1, f);
2235 fread(&t->kf_count, sizeof(int), 1, f);
2236
2237 for (int k = 0; k < t->kf_count; k++)
2238 {
2239
2240 AnimationKeyframe * kf = &t->kf[k];
2241 fread(&kf->time, sizeof(float), 1, f);
2242 switch(t->type)
2243 {
2244 case ANIM_TARGET_FLOAT:
2245 fread(&kf->val.v, sizeof(float), 1, f);
2246 break;
2247 case ANIM_TARGET_VEC3:
2248 fread(&kf->val.v3, sizeof(vec3), 1, f);
2249 break;
2250 case ANIM_TARGET_INT:
2251 fread(&kf->val.i, sizeof(int), 1, f);
2252 break;
2253 case ANIM_TARGET_BOOL:
2254 break;
2255 default:
2256 break;
2257 }
2258
2259 }
2260
2261 fread(&t->data_type, sizeof(AnimationDataType), 1, f);
2262 fread(&t->data_index_offset, sizeof(size_t), 1, f);
2263 fread(&t->data_byte_offset, sizeof(size_t), 1, f);
2264 // NOTE leaving this at null, come back to it when necessary
2265 t->data = NULL;
2266 t->completion_data = NULL;
2267
2268 }
2269
2270 int ent_id = -1;
2271 fread(&ent_id, sizeof(int), 1, f);
2272 if (ent_id != -1)
2273 {
2274 ac->ent = &ENTITIES[ent_id];
2275 }
2276 else
2277 {
2278 ac->ent = NULL;
2279 }
2280
2281
2282 }
2283
2284 printf("LIGHTS\n");
2285 // Lights
2286
2287 // if (GLOBALS.lights.buffer)
2288 // {
2289 // free(GLOBALS.lights.buffer);
2290 // init_vector(&GLOBALS.lights, 0, sizeof(Asset));
2291
2292 // }
2293 // fread(&GLOBALS.lights.count, sizeof(size_t), 1, f);
2294 // if (GLOBALS.lights.count> 0)
2295 // {
2296
2297
2298 // GLOBALS.lights.buffer = malloc(sizeof(Light) * GLOBALS.lights.count);
2299 // GLOBALS.lights.capacity = GLOBALS.lights.count;
2300
2301 // // fread(GLOBALS.lights.buffer, sizeof(Light), GLOBALS.lights.count, f);
2302 // }
2303 // else
2304 // {
2305
2306 // init_vector(&GLOBALS.lights, 0, sizeof(Light));
2307 // }
2308 // Light * lights = CAST_VEC(GLOBALS.lights, Light);
2309 // for (int i = 0; i < GLOBALS.lights.count; i++)
2310 // {
2311 // Light * l = &lights[i];
2312 // fread(l, sizeof(Light), 1, f);
2313 // l->sm_dirty = true;
2314 // }
2315
2316 read_alloc_vector(&FONTS, sizeof(Font), f);
2317 Font * fonts = CAST_VEC(FONTS, Font);
2318 for (int i = 0; i < FONTS.count; i++)
2319 {
2320
2321 Font * font = &fonts[i];
2322 fread(font, sizeof(Font), 1, f);
2323 fread(&font->glyph_count, sizeof(size_t), 1, f);
2324 if (font->glyph_count > 0)
2325 {
2326 font->glyphs = malloc(sizeof(Glyph) * font->glyph_count);
2327 fread(font->glyphs, sizeof(Glyph), font->glyph_count, f);
2328 }
2329
2330
2331 }
2332
2333
2334
2335 // fread(&GLOBALS.text_boxes.count, sizeof(size_t), 1, f);
2336 // if (GLOBALS.text_boxes.count> 0)
2337 // {
2338
2339
2340 // GLOBALS.text_boxes.buffer = malloc(sizeof(TextBox) * GLOBALS.text_boxes.count);
2341 // GLOBALS.text_boxes.capacity = GLOBALS.text_boxes.count;
2342
2343 // }
2344 // else
2345 // {
2346
2347 // init_vector(&GLOBALS.text_boxes, 0, sizeof(TextBox));
2348 // }
2349 // TextBox * tbs = CAST_VEC(GLOBALS.text_boxes, TextBox);
2350 // for (int i = PERFORMANCE_OVERLAY_TEXT_BOX_COUNT; i < GLOBALS.text_boxes.count; i++)
2351 // {
2352 // TextBox * tb = &tbs[i];
2353 // fread(tb, sizeof(TextBox), 1, f);
2354 // uint32_t len = 0;
2355 // fread(&len, sizeof(uint32_t), 1, f);
2356 // if (len > 0)
2357 // {
2358 // tb->text = malloc(sizeof(char) * len);
2359 // fread(tb->text, sizeof(char), len, f);
2360
2361 // }
2362 // else
2363 // {
2364 // tb->text = NULL;
2365 // }
2366 // }
2367
2368
2369 // read_alloc_vector(&GLOBALS.audio_portals, sizeof(AudioPortal), f);
2370 // AudioPortal * portals = CAST_VEC(GLOBALS.audio_portals, AudioPortal);
2371 // for (int i = 0; i < GLOBALS.audio_portals.count; i++)
2372 // {
2373 // AudioPortal * p = &portals[i];
2374 // fread(&p->pos, sizeof(vec3), 1, f);
2375 // p->adjacent_portals.buffer = NULL;
2376 // p->adjacent_portals.count = 0;
2377 // read_vector(&p->adjacent_portals, sizeof(int), f);
2378 // }
2379
2380
2381
2382
2383 fclose(f);
2384
2387 upload_lights();
2390 printf("FINISHED\n");
2393
2395
2396 draw_dirty = true;
2397}
2398
2399void scene_save_volumes(const char * name)
2400{
2401
2402 char scene_name[256];
2403 strcpy(scene_name, scene_registry.directory);
2404 strcat(scene_name, name);
2405 strcat(scene_name, ".bin");
2406
2407 char lm_name[256];
2408 strcpy(lm_name, lightmap_registry.directory);
2409 strcat(lm_name, name);
2410 strcat(lm_name, "_lm.bin");
2411
2412 char light_name[256];
2413 strcpy(light_name, light_registry.directory);
2414 strcat(light_name, name);
2415 strcat(light_name, "_lights.bin");
2416
2417 char aportal_name[256];
2418 strcpy(aportal_name, aportal_registry.directory);
2419 strcat(aportal_name, name);
2420 strcat(aportal_name, "_aportals.bin");
2421
2422 char rprobe_name[256];
2423 strcpy(rprobe_name, rprobe_registry.directory);
2424 strcat(rprobe_name, name);
2425 strcat(rprobe_name, "_rprobes.bin");
2426
2427 char lprobe_name[256];
2428 strcpy(lprobe_name, lprobe_registry.directory);
2429 strcat(lprobe_name, name);
2430 strcat(lprobe_name, "_lprobes.bin");
2431
2432 char aprobe_name[256];
2433 strcpy(aprobe_name, aprobe_registry.directory);
2434 strcat(aprobe_name, name);
2435 strcat(aprobe_name, "_aprobes.bin");
2436
2437 char scene_apv_name[256];
2438 strcpy(scene_apv_name, scene_apv_registry.directory);
2439 strcat(scene_apv_name, name);
2440 strcat(scene_apv_name, "_scene_apvs.bin");
2441
2442 save_aportals(aportal_name, &GLOBALS.audio_portals);
2443
2444 save_aprobes(aprobe_name);
2445
2447
2448 save_vector(light_name, &GLOBALS.lights);
2449
2450}
2451
2452void scene_load_volumes(const char * name)
2453{
2454
2455 char scene_name[256];
2456 strcpy(scene_name, scene_registry.directory);
2457 strcat(scene_name, name);
2458 strcat(scene_name, ".bin");
2459
2460 char lm_name[256];
2461 strcpy(lm_name, lightmap_registry.directory);
2462 strcat(lm_name, name);
2463 strcat(lm_name, "_lm.bin");
2464
2465 char light_name[256];
2466 strcpy(light_name, light_registry.directory);
2467 strcat(light_name, name);
2468 strcat(light_name, "_lights.bin");
2469
2470 char aportal_name[256];
2471 strcpy(aportal_name, aportal_registry.directory);
2472 strcat(aportal_name, name);
2473 strcat(aportal_name, "_aportals.bin");
2474
2475 char rprobe_name[256];
2476 strcpy(rprobe_name, rprobe_registry.directory);
2477 strcat(rprobe_name, name);
2478 strcat(rprobe_name, "_rprobes.bin");
2479
2480 char lprobe_name[256];
2481 strcpy(lprobe_name, lprobe_registry.directory);
2482 strcat(lprobe_name, name);
2483 strcat(lprobe_name, "_lprobes.bin");
2484
2485 char aprobe_name[256];
2486 strcpy(aprobe_name, aprobe_registry.directory);
2487 strcat(aprobe_name, name);
2488 strcat(aprobe_name, "_aprobes.bin");
2489
2490 char scene_apv_name[256];
2491 strcpy(scene_apv_name, scene_apv_registry.directory);
2492 strcat(scene_apv_name, name);
2493 strcat(scene_apv_name, "_scene_apvs.bin");
2494
2495
2496 load_aportals(aportal_name, &GLOBALS.audio_portals);
2497
2498 load_aprobes(aprobe_name);
2499
2501
2502 load_vector(light_name, &GLOBALS.lights);
2503
2505 upload_lights();
2507
2509 for (int i = 0; i < GLOBALS.lights.count; i++)
2510 {
2511 Light * l = &ls[i];
2512 printf("%d", l->light_type);
2513 }
2514
2516}
2517void save_vector(const char * filename, Vector * vec)
2518{
2519 FILE * f = fopen(filename, "wb");
2520 if (!f) return;
2521
2522 fwrite(&vec->count, sizeof(size_t), 1, f);
2523 if (vec->buffer) fwrite(vec->buffer, vec->type_size, vec->count, f);
2524
2525
2526 if (f) fclose(f);
2527}
2528
2529void load_vector(const char * filename, Vector * vec)
2530{
2531 FILE * f = fopen(filename, "rb");
2532 if (!f) return;
2533
2534 read_vector(vec, vec->type_size, f);
2535
2536
2537 if (f) fclose(f);
2538
2539}
2540
2541
2542
2543void save_rprobes(const char * filename, Vector * vec)
2544{
2545 FILE * f = fopen(filename, "wb");
2546 if (!f) return;
2547
2548 fwrite(&vec->count, sizeof(size_t), 1, f);
2549 if (vec->buffer) fwrite(vec->buffer, vec->type_size, vec->count, f);
2550
2551
2552 if (f) fclose(f);
2553}
2554
2555void load_rprobes(const char * filename, Vector * vec)
2556{
2557 FILE * f = fopen(filename, "rb");
2558 if (!f) return;
2559
2560 read_vector(vec, vec->type_size, f);
2562
2563
2564 if (f) fclose(f);
2565
2566}
2567
2568void save_lprobes(const char * filename, Vector * vec)
2569{
2570
2571 FILE * f = fopen(filename, "wb");
2572 if (!f) return;
2573
2574 fwrite(GLOBALS.scene_data.LPV_PATH, sizeof(char), 256, f);
2575 fwrite(&vec->count, sizeof(size_t), 1, f);
2576 if (vec->buffer) fwrite(vec->buffer, vec->type_size, vec->count, f);
2577
2578 if (f) fclose(f);
2579
2580}
2581
2582void load_lprobes(const char * filename, Vector * vec)
2583{
2584
2585 FILE * f = fopen(filename, "rb");
2586 if (!f) return;
2587
2588 fread(GLOBALS.scene_data.LPV_PATH, sizeof(char), 256, f);
2589
2591 {
2592 fclose(GLOBALS.scene_data.LPV_FILE);
2594 }
2597 {
2599 }
2601 read_vector(vec, vec->type_size, f);
2602
2603 if (f) fclose(f);
2604
2605}
2606
2607void save_aportals(const char * filename, Vector * vec)
2608{
2609 FILE * f = fopen(filename, "wb");
2610 if (!f) return;
2611
2612 fwrite(&vec->count, sizeof(size_t), 1, f);
2613
2614 AudioPortal * portals = CAST_VEC(*vec, AudioPortal);
2615
2616 for (int i = 0; i < vec->count; i++)
2617 {
2618
2619 AudioPortal * p = &portals[i];
2620
2621 fwrite(&p->pos, sizeof(vec3), 1, f);
2622 fwrite(&p->adjacent_portals.count, sizeof(size_t), 1, f);
2623 if (p->adjacent_portals.buffer) fwrite(p->adjacent_portals.buffer, sizeof(int), p->adjacent_portals.count, f);
2624
2625 }
2626
2627
2628 // if (vec->buffer) fwrite(vec->buffer, vec->type_size, vec->count, f);
2629
2630
2631 if (f) fclose(f);
2632}
2633
2634void load_aportals(const char * filename, Vector * vec)
2635{
2636 FILE * f = fopen(filename, "rb");
2637 if (!f) return;
2638
2639 size_t count = 0;
2640 fread(&count, sizeof(size_t), 1, f);
2641 if (count > 0)
2642 {
2643 if (vec->buffer) free(vec->buffer);
2644 init_vector(vec, 0, vec->type_size);
2645 vec->buffer = calloc(count, sizeof(AudioPortal));
2646 vec->count = count;
2647 vec->capacity = 0;
2648
2649 AudioPortal * portals = CAST_VEC(*vec, AudioPortal);
2650 for (int i = 0; i < vec->count; i++)
2651 {
2652
2653 AudioPortal * p = &portals[i];
2654 fread(&p->pos, sizeof(vec3), 1, f);
2655 init_vector(&p->adjacent_portals, 0, sizeof(int));
2656 fread(&p->adjacent_portals.count, sizeof(size_t), 1, f);
2657 if (p->adjacent_portals.count > 0)
2658 {
2659 p->adjacent_portals.buffer = malloc(sizeof(int) * p->adjacent_portals.count);
2661 fread(p->adjacent_portals.buffer, sizeof(int), p->adjacent_portals.count, f);
2662 }
2663
2664 // init_vector(&p->adjacent_portals, 0, sizeof(int));
2665 // read_vector(&p->adjacent_portals, sizeof(int), f);
2666 // int * adj = CAST_VEC(p->adjacent_portals, int);
2667 // for (int j = 0; j < p->adjacent_portals.count; j++)
2668 // {
2669 // printf("ADJ: %d\n", adj[j]);
2670 // }
2671
2672 }
2673 }
2674
2675
2676 if (f) fclose(f);
2677}
2678
2679void save_aprobes(const char * filename)
2680{
2681
2682 FILE * f = fopen(filename, "wb");
2683
2684 fwrite(GLOBALS.scene_data.APV_PATH, sizeof(char), 256, f);
2685 fwrite(&GLOBALS.scene_data.audio_probe_volumes.count, sizeof(size_t), 1, f);
2687
2688
2689 // fwrite(&GLOBALS.aprobe_size, sizeof(ivec3), 1, f);
2690 // fwrite(&GLOBALS.aprobe_start, sizeof(vec3), 1, f);
2691 // fwrite(&GLOBALS.aprobe_spacing, sizeof(float), 1, f);
2692 // fwrite(GLOBALS.audio_probes, sizeof(AudioProbe), GLOBALS.aprobe_size.x * GLOBALS.aprobe_size.y * GLOBALS.aprobe_size.z, f);
2693
2694
2695 if (f) fclose(f);
2696
2697}
2698
2699void load_aprobes(const char * filename)
2700{
2701
2702 FILE * f = fopen(filename, "rb");
2703
2704 fread(GLOBALS.scene_data.APV_PATH, sizeof(char), 256, f);
2705
2707 {
2708 fclose(GLOBALS.scene_data.APV_FILE);
2710 }
2713 {
2715 }
2717
2719
2720 // ivec3 potential_size = {0};
2721 // fread(&potential_size, sizeof(ivec3), 1, f);
2722 // int aprobe_count = potential_size.x * potential_size.y * potential_size.z;
2723 // // int aprobe_count = GLOBALS.aprobe_size.x * GLOBALS.aprobe_size.y * GLOBALS.aprobe_size.z;
2724 // if (aprobe_count > 0)
2725 // {
2726 // GLOBALS.aprobe_size = potential_size;
2727 // fread(&GLOBALS.aprobe_start, sizeof(vec3), 1, f);
2728 // fread(&GLOBALS.aprobe_spacing, sizeof(float), 1, f);
2729
2730 // if (GLOBALS.audio_probes) free(GLOBALS.audio_probes);
2731 // GLOBALS.audio_probes = NULL;
2732
2733
2734 // GLOBALS.audio_probes = malloc(sizeof(AudioProbe) * aprobe_count);
2735 // fread(GLOBALS.audio_probes, sizeof(AudioProbe), aprobe_count, f);
2736 // }
2737
2738 if (f) fclose(f);
2739
2740}
2741
Audio probe baking, portal creation, probe volumes, and emitter functions.
The debug editor UI for the engine.
LightBVH light_bvh
The LightBVH, required for baking lighting.
int block_size
How much to allocate in one rect.
int joint_count
The amount of joints.
int bvh_indices_count
FILE * LPV_FILE
FILE pointer to the opened binary file.
vec2 LightmapUV[3]
The lightmap UVs stored on UVIslands per MeshFace.
bool noclip
If collision is enabled.
FILE * APV_FILE
FILE pointer to the opened binary file.
Vector mesh_nodes
MeshNodes.
bool free
Indicates an empty slot in ASSET_INSTANCES.
int skin
The GLTFSkin to be modified by this Entity (skeletal animations).
AnimationInterpolation interpolation
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.
AnimationKeyframe kf[64]
The AnimationKeyframes for this track.
GLint min_filter
Refer to OpenGL texture loading.
bool loop
If the animation loops.
size_t animation_count
Vector bvh_nodes
int height
Height of the atlas.
ivec3 probe_tex_size
The texture size for lighting probes.
char LPV_PATH[256]
Path to binary file with lighting ProbeVolumes.
size_t skin_count
The amount of GLTFSkins in this instance.
int asset_id
The ID into Globals.assets.
size_t skin_count
Vector vertex_layouts
VertexLayouts.
int mesh_node
The top-most MeshNode that belongs to this Entity.
int width
Width of the atlas.
#define DEFAULT_TEXTURES
How many dummy default textures are on init.
Vector audio_portals
AudioPortals.
Vector meshes
Meshes.
int animation_indices_count
The amount of Animation indices.
int animation_indices[256]
Animation indices into Asset.
AnimationSampler * samplers
bool sRGB
To load in this Image as sRGB.
AnimationTargetType
Animation targets based on type to cast.
@ ANIM_TARGET_BOOL
@ ANIM_TARGET_VEC3
@ ANIM_TARGET_INT
@ ANIM_TARGET_FLOAT
GLint wrap_t
Refer to OpenGL texture loading.
#define RPROBE_WIDTH
AnimationTrack tracks[64]
The AnimationTracks with individual values that occur simultaneously.
int mesh_node
The MeshNode this island belongs to.
int asset_id
The Asset ID in Globals.assets.
int kf_count
The amount of AnimationKeyframes.
vec3 normal
The normal of the island.
AABB aabb
The AABB over each MeshNode via Asset.
Vector tris
size_t indices of the MeshFaces in this island.
mat4 * inverse_bind
The inverse_bind that is computed on load_gltf().
int channels
The amount of channels for an Image (RGBA).
AnimationDataType
Indicates what type of structure the animation is targeting.
#define MAX_RPROBE_MIPS
The amount of mips to allocate for rprobes.
vec3 world_min
Unused but uploaded to main shader.
AABB aabb
AABB over each MeshNode for this Asset.
Globals GLOBALS
Engine Globals.
Definition main.c:310
bool free
If the component is empty.
int * joints
The joints for skeletal animating.
Vector reflection_probes
ReflectionProbes in the scene.
Vector lights
Lights.
GLint wrap_s
Refer to OpenGL texture loading.
Vector adjacent_indices
size_t indices.
float probe_spacing
The spacing between lighting probes.
float duration
The duration of all of the AnimationKeyframes.
void * data
Stored pointer to data to change during animation.
vec3 world_size
Unused but uploaded to main shader.
bool free
If the Entity is empty.
GLTFSkin * skins
struct AssetInstance * instance
The AssetInstance that spawned this Entity.
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.
GLint mag_filter
Refer to OpenGL texture loading.
size_t node_count
The amount of MeshNodes in this instance.
size_t data_byte_offset
The byte offset in the structure we are targeting.
Vector images
Images.
size_t data_index_offset
The index of the structure we are targeting.
size_t id
The ID in ASSET_INSTANCES.
MeshNode * nodes
#define RPROBE_HEIGHT
size_t first_skin
Refer to todo.
unsigned char * buf
If available, the raw bytes of the Image.
char name[256]
The name of the Image file.
char APV_PATH[256]
Path to binary file with Audio ProbeVolumes.
Entity * ent
The pointer to the Entity.
Vector materials
Materials.
mat4 * final_mat
The transformation after update_skin_matrices().
Vector assets
Assets.
GLuint64 handle
The handle for the bindless texture.
int track_count
The amount of AnimationTracks.
GLTFAnimationChannel * channels
AABB aabb
AABB over the entire scene.
bool sample_cubemap
Unused - was used to toggle skybox sampling.
GLuint i
The handle for the texture.
PhysicsComponent * pc
The PhysicsComponent inside of PHYSICS_COMPONENTS.
size_t node_count
Animation * animations
size_t * indices
The indices sorted when initialized.
size_t id
ID inside of ENTITIES.
AABB aabb
The bounding box over all MeshNodes for this Entity.
Vector nodes
Each node of the BVH.
char directory[256]
Path to directory for the file.
int ibm_accessor
Which GLTFAccessor this refers to.
Vector skins
GLTFSkins.
char path[256]
The path to the Asset.
AnimationDataType data_type
The type of data that is changed during animation.
union AnimationKeyframe::@243377164052067242366020157201307241363031166101 val
char name[256]
char path[256]
The path to the Image.
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
char name[256]
Modifiable name of the scene.
AnimationTargetType type
The type of the data stored on AnimationKeyframe.
bool active
If this animation is currently running.
char mime_type[256]
Unused, refer to GLTF spec.
Vector islands
The UVIslands on the current lightmap.
float current_time
How long the animation has been running.
LightmapRect rect
The rect that this island allocates.
LightmapAtlas atlas
The atlas settings for the lightmap.
The structure of an Skeletatal Animation loaded in from GLTFAnimation.
The animation component for interpolation between AnimationKeyframes.
Tagged union for the value stored in a keyframe.
Engine-side sampler version of GLTFAnimationSampler with the field allocated.
Tagged union for the value stored in a keyframe.
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 ...
Entity structure that holds components and references to the data it owns.
The channels of an Animation.
The structure of an Skeletatal Animation loaded in from GLTFAnimation.
Where Material textures are saved upon loading from file.
The metadata for the lightmap settings.
The rects that are slotted into thet LightmapAtlas during UV generation for UV lightmaps.
Sections of Meshes that are divided based on Material for batching and DrawCommand ordering purposes.
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.
Vector FONTS
All loaded Fonts.
Definition fonts.c:14
Functions that modify TextBoxes and load Fonts.
void reset_audio_pvs()
Reset audio pvs, frees each slot in the global audio pv slots.
Definition audio.c:964
#define AUDIO_NUM_BANDS
The amount of bands to use for audio probe baking.
The ProbeVolumes for APVs and LPVs, controlled by OBBs.
Vector adjacent_portals
int - Vector of AudioPortals that can be seen from this one.
vec3 pos
The position of the AudioPortal.
AudioPortals used for determining paths to transmit sound when emitter and listener are occluded.
FileDirectoryRegistry rprobe_registry
Definition editor.c:57
FileDirectoryRegistry ui_registry
Definition editor.c:62
FileDirectoryRegistry aportal_registry
Definition editor.c:56
FileDirectoryRegistry scene_apv_registry
Definition editor.c:60
FileDirectoryRegistry lightmap_registry
Definition editor.c:53
FileDirectoryRegistry lprobe_registry
Definition editor.c:58
FileDirectoryRegistry light_registry
Definition editor.c:54
void create_performance_overlay()
Allocates and initializes position of the TextBoxes for the performance overlay.
Definition editor.c:1674
FileDirectoryRegistry scene_registry
Definition editor.c:52
FileDirectoryRegistry aprobe_registry
Definition editor.c:59
Vector hull_faces
HullFaces that are generated after incremental_hull() that combine contiguous triangles into singular...
CollisionData cd
Tagged union for collision options.
Vector vertices
Points that initially copy the Mesh vertices but then are deduplicated and modified in incremental_hu...
mat3 inverse_world
PhysicsComponent.inverse_inertia transformed by rotation into world space for the current step.
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.
float mass
Mass of the component.
AABB aabb
Bounding box of the PC, determined based on Mesh or MeshNode.
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).
The tagged union for collision type, stored in PhysicsComponent.
Convex Hulls that are generated via incremental hull for non-static meshes.
Entity Component used for physics resolution.
int fwd_shader
The corresponding forward rendering shader branch in DYNAMIC_SHADER.
size_t id
The ID inside of Globals.mesh_nodes.
vec3 translation
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.
bool skip_directional_uv_lighting
Skips directional Lights and rays go through them.
vec4 ka
Ambient.
int children[128]
The children to this node, a global index into Globals.mesh_nodes.
mat4 local_matrix
float emissive_strength
vec4 ke
Emissive.
int mesh
The Mesh it references.
int parent
The parent to this node, a global index into Globals.mesh_nodes.
bool active
If the MeshNode can be effected.
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
sampler2D normal_tex
UVMapping * uv_map
The mapping between MeshNodes and UVIslands for uv lightmapping.
vec4 ks
Specular (unused).
bool static_mesh
If this mesh is static (DrawArrays) or dynamic (DrawElements), used to indicate Lightmap status.
int gbuffer_shader
The corresponding gbuffer rendering shader branch in DEPTH_PRE_PASS_SHADER or DEPTH_PRE_PASS_SHADER_M...
int asset
The Asset it comes from.
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.
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 ...
TextBox text_boxes[1024]
The TextBoxes.
Definition ui.h:567
UIElement elements[1024]
The UIElements.
Definition ui.h:566
Glyph * glyphs
Glyphs for each character.
Definition ui.h:78
int text_cap
The amount of characters allocated.
Definition ui.h:266
uint32_t uID
The index inside of MENUS.
Definition ui.h:564
UIContainer containers[1024]
The UIContainers.
Definition ui.h:568
char * text
The characters to render.
Definition ui.h:265
size_t glyph_count
The amount of Glyphs.
Definition ui.h:79
char id[256]
The unique name of this menu.
Definition ui.h:565
#define MAX_UI_ELEMENTS
Definition ui.h:33
#define MAX_TEXT_BOXES
Definition ui.h:37
#define MAX_UI_CONTAINERS
Definition ui.h:34
Vector MENUS
All UIMenus.
Definition ui.c:13
A Font and its Glyphs used for rendering TextBoxes.
Definition ui.h:74
A single Glyph in a Font.
Definition ui.h:52
A structure holding a Font ID and text to be rendered and transformed with its properties.
Definition text.vert:5
UIContainer that contains both TextBoxes and UIElements and can mask and scroll them.
Definition ui.h:225
UIElements that are contained within a UIMenu that may receive events, be contained within UIContaine...
Definition text.vert:31
The top-most UI structure that contains UIElements, TextBoxes, and UIContainers.
Definition ui.h:562
void GPU_upload_scene_data()
Uploads current Globals and SceneData structures to the GPU for baking SSBOs.
Definition init.c:661
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 MAX_PHYSICS_COMPONENTS
Definition init.h:445
#define MAX_ANIMATION_COMPONENTS
Definition init.h:451
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
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
void reset_lighting_pvs()
Resets current LPVs from their slots in SceneData.LOADED_LIGHTING_PV.
Definition lightmap.c:1330
UV Lightmap setup and baking, ReflectionProbe baking, and lighting ProbeVolume baking.
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.
size_t asset_id
The Asset ID into Globals.assets.
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 submeshes
The SubMeshes split via Materials.
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.
void read_skin(GLTFSkin *skin, FILE *f)
Definition load.c:1173
void free_mesh_node(MeshNode *mn)
Definition load.c:1248
void scene_save_volumes(const char *name)
Saves only AudioProbes, AudioPortals, and ReflectionProbes.
Definition load.c:2399
void save_ui(const char *name)
Definition load.c:315
void read_alloc_vector(Vector *v, size_t byte_size, FILE *f)
Definition load.c:1224
void save_vector(const char *filename, Vector *vec)
Definition load.c:2517
void load_ui(const char *name)
Definition load.c:348
void load_rprobes(const char *filename, Vector *vec)
Definition load.c:2555
void scene_save(const char *name)
Saves an entire scene into separate files.
Definition load.c:502
void write_skin(GLTFSkin *skin, FILE *f)
Definition load.c:480
void free_skin(GLTFSkin *skin)
Definition load.c:1254
void save_lprobes(const char *filename, Vector *vec)
Definition load.c:2568
unsigned char * open_image(Image *image)
Opens an image in bytes via stb.
Definition load.c:275
void scene_load_volumes(const char *name)
Loads only AudioProbes, AudioPortals, and ReflectionProbes.
Definition load.c:2452
GLuint load_cubemap(const char *path)
Loads a cubemap in OpenGL naming convention (must be HDR).
Definition load.c:161
void load_lprobes(const char *filename, Vector *vec)
Definition load.c:2582
void load_aportals(const char *filename, Vector *vec)
Definition load.c:2634
void load_aprobes(const char *filename)
Definition load.c:2699
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
void save_aportals(const char *filename, Vector *vec)
Definition load.c:2607
void load_vector(const char *filename, Vector *vec)
Definition load.c:2529
void write_mesh_node(MeshNode *mn, FILE *f)
Definition load.c:407
void save_aprobes(const char *filename)
Definition load.c:2679
void read_mesh_node(MeshNode *mn, FILE *f)
Definition load.c:1096
void scene_load(const char *name)
Loads an entire scene from separate files in FileDirectoryRegistry directories.
Definition load.c:1262
GLuint load_texture(const char *path, GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t, bool sRGB)
Loads a texture through OpenGL.
Definition load.c:20
void reallocate_rprobe_array()
Reallocates RPROBE_CUBE_MAP_ARRAY to new dimensions or when loading.
Definition load.c:392
void read_vector(Vector *v, size_t byte_size, FILE *f)
Definition load.c:1200
GLuint load_3d_lut(const char *filepath)
Loads a 3D LUT for postprocessing.
Definition load.c:221
void save_rprobes(const char *filename, Vector *vec)
Definition load.c:2543
Saves and loads various files that store engine data.
PhysicsComponent PHYSICS_COMPONENTS[MAX_PHYSICS_COMPONENTS]
Definition main.c:314
Entity ENTITIES[MAX_ENTITIES]
Definition main.c:312
GLuint RPROBE_CUBE_MAP_ARRAY
Definition main.c:305
AnimationComponent ANIMATION_COMPONENTS[MAX_ANIMATION_COMPONENTS]
Definition main.c:315
bool draw_dirty
If we need to reupload and resort transforms in create_and_sort_draw_commands() and upload_instance_d...
Definition main.c:388
AssetInstance ASSET_INSTANCES[MAX_ASSET_INSTANCES]
Definition main.c:313
#define MIN(a, b)
Definition mathf.h:435
Nodes for the acceleration structures of BVHs.
void upload_lights()
Uploads all current Lights inside of Globals.lights to the GPU in a triple buffered LIGHT_SSBO.
Definition render.c:634
void upload_material_data()
Uploads Material data to the GPU as MaterialGPU.
Definition render.c:854
void add_all_text_boxes_to_draw_buffer()
Adds all TextBoxes to TEXT_VBO and iterates through each character to push indices to TEXT_EBO.
Definition render.c:1220
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.
size_t type_size
Definition types.h:209
size_t count
Definition types.h:209
size_t capacity
Definition types.h:209
void * buffer
Definition types.h:210
Definition types.h:218
Definition types.h:15
Definition types.h:20
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
void * vector_expand(Vector *v, size_t i)
Definition utilities.c:49
#define CAST_VEC(vec, type)
Definition utilities.h:134