14#include "nuklear/nuklear.h"
15#include "nuklear/nuklear_sdl_gl3.h"
16#include "cJSON/cJSON.h"
21 if (strcmp(type,
"SCALAR") == 0)
return 1;
22 if (strcmp(type,
"VEC2") == 0)
return 2;
23 if (strcmp(type,
"VEC3") == 0)
return 3;
24 if (strcmp(type,
"VEC4") == 0)
return 4;
25 if (strcmp(type,
"MAT4") == 0)
return 16;
30 switch (componentType) {
45 FILE *f = fopen(path,
"rb");
47 fseek(f, 0, SEEK_END);
48 long len = ftell(f); rewind(f);
49 char *buf = (
char*)malloc(len+1);
57 FILE *f = fopen(path,
"rb");
60 long len = ftell(f); rewind(f);
61 if (expectedLen > 0 && len < expectedLen) {
62 fclose(f);
return NULL;
64 unsigned char *data = (
unsigned char*)malloc(len);
70static int str_eq(
const char* a,
const char* b){
return a && b && strcmp(a,b)==0; }
73 cJSON* it = cJSON_GetObjectItemCaseSensitive(obj, key);
74 return cJSON_IsNumber(it) ? it->valueint : defv;
78 cJSON* it = cJSON_GetObjectItemCaseSensitive(obj, key);
79 return cJSON_IsString(it) ? it->valuestring : NULL;
82 cJSON *arr = cJSON_GetObjectItemCaseSensitive(obj, key);
83 if (cJSON_IsArray(arr) && cJSON_GetArraySize(arr) == 3) {
84 cJSON *val = cJSON_GetArrayItem(arr,0);
85 out->
x = cJSON_IsNumber(val) ? (float)val->valuedouble : def[0];
86 val = cJSON_GetArrayItem(arr,1);
87 out->
y = cJSON_IsNumber(val) ? (float)val->valuedouble : def[1];
88 val = cJSON_GetArrayItem(arr,2);
89 out->
z = cJSON_IsNumber(val) ? (float)val->valuedouble : def[2];
91 memcpy(out, def,
sizeof(
float)*3);
95 cJSON *arr = cJSON_GetObjectItemCaseSensitive(obj, key);
96 if (cJSON_IsArray(arr) && cJSON_GetArraySize(arr) == 4) {
97 cJSON *val = cJSON_GetArrayItem(arr,0);
98 out->
x = cJSON_IsNumber(val) ? (float)val->valuedouble : def[0];
99 val = cJSON_GetArrayItem(arr,1);
100 out->
y = cJSON_IsNumber(val) ? (float)val->valuedouble : def[1];
101 val = cJSON_GetArrayItem(arr,2);
102 out->
z = cJSON_IsNumber(val) ? (float)val->valuedouble : def[2];
103 val = cJSON_GetArrayItem(arr,3);
104 out->
w = cJSON_IsNumber(val) ? (float)val->valuedouble : def[3];
106 memcpy(out, def,
sizeof(
float)*4);
111static void json_get_float_array(cJSON *parent,
const char *name,
float *out,
int count,
const float *defaults) {
112 cJSON *arr = cJSON_GetObjectItemCaseSensitive(parent, name);
113 if (cJSON_IsArray(arr)) {
114 for (
int i = 0; i < count; i++) {
115 cJSON *item = cJSON_GetArrayItem(arr, i);
116 out[i] = cJSON_IsNumber(item) ? (float)item->valuedouble : defaults[i];
119 for (
int i = 0; i < count; i++) out[i] = defaults[i];
124 cJSON *obj = cJSON_GetObjectItemCaseSensitive(parent, name);
126 cJSON *idx = cJSON_GetObjectItemCaseSensitive(obj,
"index");
127 if (cJSON_IsNumber(idx))
return idx->valueint;
133 cJSON *materials = cJSON_GetObjectItemCaseSensitive(root,
"materials");
134 if (!materials)
return;
141 cJSON *mat = cJSON_GetArrayItem(materials, i);
162 cJSON *name = cJSON_GetObjectItemCaseSensitive(mat,
"name");
163 if (cJSON_IsString(name)) strncpy(dst->
name, name->valuestring,
sizeof(dst->
name) - 1);
165 cJSON *pbr = cJSON_GetObjectItemCaseSensitive(mat,
"pbrMetallicRoughness");
167 float defColor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
170 cJSON *mf = cJSON_GetObjectItemCaseSensitive(pbr,
"metallicFactor");
173 cJSON *rf = cJSON_GetObjectItemCaseSensitive(pbr,
"roughnessFactor");
184 cJSON *normObj = cJSON_GetObjectItemCaseSensitive(mat,
"normalTexture");
186 cJSON *scale = cJSON_GetObjectItemCaseSensitive(normObj,
"scale");
187 if (cJSON_IsNumber(scale)) dst->
normal_scale = (float)scale->valuedouble;
190 cJSON *occObj = cJSON_GetObjectItemCaseSensitive(mat,
"occlusionTexture");
192 cJSON *strength = cJSON_GetObjectItemCaseSensitive(occObj,
"strength");
196 float defEmissive[3] = {0.0f, 0.0f, 0.0f};
199 cJSON *alphaMode = cJSON_GetObjectItemCaseSensitive(mat,
"alphaMode");
200 if (cJSON_IsString(alphaMode)) {
205 cJSON *cutoff = cJSON_GetObjectItemCaseSensitive(mat,
"alphaCutoff");
206 if (cJSON_IsNumber(cutoff)) dst->
alpha_cutoff = (float)cutoff->valuedouble;
208 cJSON *doubleSided = cJSON_GetObjectItemCaseSensitive(mat,
"doubleSided");
209 if (cJSON_IsBool(doubleSided)) dst->
double_sided = cJSON_IsTrue(doubleSided);
211 cJSON *extensions= cJSON_GetObjectItemCaseSensitive(mat,
"extensions");
217 cJSON * emissive_strength_ext = cJSON_GetObjectItemCaseSensitive(extensions,
"KHR_materials_emissive_strength");
218 if (emissive_strength_ext)
221 cJSON * emissive_strength = cJSON_GetObjectItemCaseSensitive(emissive_strength_ext,
"emissiveStrength");
222 if (cJSON_IsNumber(emissive_strength))
229 cJSON *extras= cJSON_GetObjectItemCaseSensitive(mat,
"extras");
244 cJSON * fwd_shader = cJSON_GetObjectItemCaseSensitive(extras,
"fwd_shader");
245 if (cJSON_IsString(fwd_shader))
258 cJSON * gbuffer_shader = cJSON_GetObjectItemCaseSensitive(extras,
"gbuffer_shader");
259 if (cJSON_IsString(gbuffer_shader))
272 cJSON * absorption_coefficients = cJSON_GetObjectItemCaseSensitive(extras,
"absorption_coefficients");
273 if (cJSON_IsArray(absorption_coefficients))
278 cJSON * c0 = cJSON_GetArrayItem(absorption_coefficients, j);
280 if (cJSON_IsNumber(c0))
288 cJSON * scattering_coefficients = cJSON_GetObjectItemCaseSensitive(extras,
"scattering_coefficients");
289 if (cJSON_IsArray(scattering_coefficients))
293 cJSON * c0 = cJSON_GetArrayItem(scattering_coefficients, j);
295 if (cJSON_IsNumber(c0))
303 cJSON *
override = cJSON_GetObjectItemCaseSensitive(extras,
"override_transparent");
304 if (cJSON_IsBool(
override))
314 memset(doc,0,
sizeof(*doc));
318 printf(
"EARLY RETURN LOAD\n");
322 cJSON *root = cJSON_Parse(json);
323 if (!root) { free(json); printf(
"NO ROOT\n");
return 0; }
325 cJSON *buffers = cJSON_GetObjectItemCaseSensitive(root,
"buffers");
329 cJSON *b = cJSON_GetArrayItem(buffers,i);
341 cJSON *views = cJSON_GetObjectItemCaseSensitive(root,
"bufferViews");
342 doc->
viewCount = cJSON_GetArraySize(views);
345 cJSON *v = cJSON_GetArrayItem(views,i);
352 cJSON *accs = cJSON_GetObjectItemCaseSensitive(root,
"accessors");
356 cJSON *a = cJSON_GetArrayItem(accs,i);
367 cJSON *meshes = cJSON_GetObjectItemCaseSensitive(root,
"meshes");
368 doc->
meshCount = cJSON_GetArraySize(meshes);
371 cJSON *m = cJSON_GetArrayItem(meshes, mi);
372 cJSON *prims = cJSON_GetObjectItemCaseSensitive(m,
"primitives");
373 int pc = cJSON_GetArraySize(prims);
377 for (
int pi=0; pi<pc; pi++){
378 cJSON *p = cJSON_GetArrayItem(prims, pi);
384 cJSON *attrs = cJSON_GetObjectItemCaseSensitive(p,
"attributes");
386 cJSON *POS = cJSON_GetObjectItemCaseSensitive(attrs,
"POSITION");
387 cJSON *NOR = cJSON_GetObjectItemCaseSensitive(attrs,
"NORMAL");
388 cJSON *TAN = cJSON_GetObjectItemCaseSensitive(attrs,
"TANGENT");
389 cJSON *UV0 = cJSON_GetObjectItemCaseSensitive(attrs,
"TEXCOORD_0");
390 cJSON *J0 = cJSON_GetObjectItemCaseSensitive(attrs,
"JOINTS_0");
391 cJSON *W0 = cJSON_GetObjectItemCaseSensitive(attrs,
"WEIGHTS_0");
392 if (cJSON_IsNumber(POS)) dst->
position = POS->valueint;
393 if (cJSON_IsNumber(NOR)) dst->
normal = NOR->valueint;
394 if (cJSON_IsNumber(TAN)) dst->
tangent = TAN->valueint;
395 if (cJSON_IsNumber(UV0)) dst->
texcoord0= UV0->valueint;
396 if (cJSON_IsNumber(J0)) dst->
joints0 = J0->valueint;
397 if (cJSON_IsNumber(W0)) dst->
weights0 = W0->valueint;
399 cJSON *IND = cJSON_GetObjectItemCaseSensitive(p,
"indices");
400 if (cJSON_IsNumber(IND)) dst->
indices = IND->valueint;
406 cJSON *textures = cJSON_GetObjectItemCaseSensitive(root,
"textures");
411 cJSON *
n = cJSON_GetArrayItem(textures, i);
415 cJSON *sampler = cJSON_GetObjectItemCaseSensitive(
n,
"sampler");
416 if (cJSON_IsNumber(sampler)) {
417 tex->
sampler = sampler->valueint;
420 cJSON *src = cJSON_GetObjectItemCaseSensitive(
n,
"source");
421 if (cJSON_IsNumber(src)) {
422 tex->
source = src->valueint;
427 cJSON *images = cJSON_GetObjectItemCaseSensitive(root,
"images");
433 cJSON *
n = cJSON_GetArrayItem(images, i);
436 cJSON *mime_type = cJSON_GetObjectItemCaseSensitive(
n,
"mimeType");
437 if (cJSON_IsString(mime_type))
439 strncpy(img->
mime_type, mime_type->valuestring, 254);
442 cJSON *name = cJSON_GetObjectItemCaseSensitive(
n,
"name");
443 if (cJSON_IsString(name))
445 strncpy(img->
name, name->valuestring, 254);
446 img->
name[255] =
'\0';
448 cJSON *uri = cJSON_GetObjectItemCaseSensitive(
n,
"uri");
449 if (cJSON_IsString(uri))
451 strncpy(img->
uri, prefix, 254);
452 strncat(img->
uri, uri->valuestring, 254);
456 cJSON *buffer_view= cJSON_GetObjectItemCaseSensitive(
n,
"bufferView");
457 if (cJSON_IsNumber(buffer_view)) {
463 cJSON *samplers = cJSON_GetObjectItemCaseSensitive(root,
"samplers");
468 cJSON *
n = cJSON_GetArrayItem(samplers, i);
473 cJSON *magFilter = cJSON_GetObjectItemCaseSensitive(
n,
"magFilter");
474 if (cJSON_IsNumber(magFilter)) {
477 cJSON *minFilter = cJSON_GetObjectItemCaseSensitive(
n,
"minFilter");
478 if (cJSON_IsNumber(minFilter)) {
481 cJSON *wrapS = cJSON_GetObjectItemCaseSensitive(
n,
"wrapS");
482 if (cJSON_IsNumber(wrapS)) {
483 sampler->
wrap_s = wrapS->valueint;
486 cJSON *wrapT = cJSON_GetObjectItemCaseSensitive(
n,
"wrapT");
487 if (cJSON_IsNumber(wrapT)) {
488 sampler->
wrap_t = wrapT->valueint;
490 cJSON *name = cJSON_GetObjectItemCaseSensitive(
n,
"name");
491 if (cJSON_IsString(name))
493 strncpy(sampler->
name, name->valuestring, 254);
494 sampler->
name[255] =
'\0';
500 cJSON *nodes = cJSON_GetObjectItemCaseSensitive(root,
"nodes");
506 cJSON *
n = cJSON_GetArrayItem(nodes,i);
513 cJSON *name = cJSON_GetObjectItemCaseSensitive(
n,
"name");
514 if (cJSON_IsString(name)) strncpy(dst->
name, name->valuestring,
sizeof(dst->
name)-1);
516 cJSON *mesh = cJSON_GetObjectItemCaseSensitive(
n,
"mesh");
517 if (cJSON_IsNumber(mesh)) dst->
mesh = mesh->valueint;
519 cJSON *skin = cJSON_GetObjectItemCaseSensitive(
n,
"skin");
520 if (cJSON_IsNumber(skin)) dst->
skin = skin->valueint;
526 cJSON *children = cJSON_GetObjectItemCaseSensitive(
n,
"children");
528 int cc = cJSON_GetArraySize(children);
530 for (
int ci=0; ci<cc; ci++) {
531 dst->
children[ci] = cJSON_GetArrayItem(children,ci)->valueint;
536 cJSON *extras = cJSON_GetObjectItemCaseSensitive(
n,
"extras");
540 cJSON * mesh_mode = cJSON_GetObjectItemCaseSensitive(extras,
"mesh_mode");
543 if (strcmp(mesh_mode->valuestring,
"dynamic") == 0)
559 cJSON * physics_mode= cJSON_GetObjectItemCaseSensitive(extras,
"physics_type");
563 if (physics_mode->valueint >= 1)
578 cJSON * collision_type = cJSON_GetObjectItemCaseSensitive(extras,
"collision_type");
596 cJSON *skins = cJSON_GetObjectItemCaseSensitive(root,
"skins");
601 cJSON *s = cJSON_GetArrayItem(skins,i);
604 cJSON *joints = cJSON_GetObjectItemCaseSensitive(s,
"joints");
605 int jc = cJSON_GetArraySize(joints);
607 dst->
joints = calloc(jc,
sizeof(
int));
608 for (
int ji=0; ji<jc; ji++)
609 dst->
joints[ji] = cJSON_GetArrayItem(joints,ji)->valueint;
611 cJSON *ibm = cJSON_GetObjectItemCaseSensitive(s,
"inverseBindMatrices");
612 if (cJSON_IsNumber(ibm)) {
615 cJSON * name = cJSON_GetObjectItemCaseSensitive(s,
"name");
616 if (cJSON_IsString(name)){
617 strncpy(dst->
name, name->valuestring, 62);
618 dst->
name[63] =
'\0';
620 sprintf(dst->
name,
"default");
625 cJSON *animations = cJSON_GetObjectItemCaseSensitive(root,
"animations");
630 cJSON *a = cJSON_GetArrayItem(animations, ai);
633 cJSON *name = cJSON_GetObjectItemCaseSensitive(a,
"name");
634 if (cJSON_IsString(name)) strncpy(dst->
name, name->valuestring,
sizeof(dst->
name)-1);
636 cJSON *samplers = cJSON_GetObjectItemCaseSensitive(a,
"samplers");
640 cJSON *s = cJSON_GetArrayItem(samplers, si);
650 cJSON *channels = cJSON_GetObjectItemCaseSensitive(a,
"channels");
654 cJSON *c = cJSON_GetArrayItem(channels, ci);
657 cJSON *target = cJSON_GetObjectItemCaseSensitive(c,
"target");
677 char dir[512]; strcpy(dir, gltfPath);
678 for (
int i=(
int)strlen(dir)-1; i>=0; --i){
if (dir[i]==
'/'||dir[i]==
'\\'){ dir[i+1]=
'\0';
break; } }
682 char full[1024]; snprintf(full,
sizeof(full),
"%s%s", dir, doc->
buffers[bi].
uri);
686 if (!doc->
buffers[bi].
data) { printf(
"NO DATA AT %s\n", full); cJSON_Delete(root); free(json);
return 0; }
698 return b->
data + offset;
705 size_t base_off = (size_t)(
view->byteOffset) + (size_t)(acc->
byteOffset);
708 int tight = comps * csize;
709 int stride =
view->byteStride ?
view->byteStride : tight;
710 return buf->
data + base_off + (size_t)index * stride;
715 if (!p) { out[0]=out[1]=out[2]=0.0f;
return; }
720 if (comp == 5126 && comps >= 3) {
721 const float *f = (
const float*)p;
728 if (comp == 5123 && comps >= 3) {
729 const unsigned short *s = (
const unsigned short*)p;
730 bool normalized =
false;
732 out[0] = s[0] / 65535.0f;
733 out[1] = s[1] / 65535.0f;
734 out[2] = s[2] / 65535.0f;
736 out[0] = (float)s[0];
737 out[1] = (float)s[1];
738 out[2] = (float)s[2];
743 if (comp == 5121 && comps >= 3) {
744 const unsigned char *c = (
const unsigned char*)p;
745 bool normalized =
false;
747 out[0] = c[0] / 255.0f;
748 out[1] = c[1] / 255.0f;
749 out[2] = c[2] / 255.0f;
751 out[0] = (float)c[0];
752 out[1] = (float)c[1];
753 out[2] = (float)c[2];
758 if (comp == 5122 && comps >= 3) {
759 const short *s = (
const short*)p;
760 out[0] = (float)s[0];
761 out[1] = (float)s[1];
762 out[2] = (float)s[2];
765 if (comp == 5125 && comps >= 3) {
766 const unsigned int *u = (
const unsigned int*)p;
767 out[0] = (float)u[0];
768 out[1] = (float)u[1];
769 out[2] = (float)u[2];
773 out[0]=out[1]=out[2]=0.0f;
777 if (!p) { out->
x=out->
y=out->
z=out->
w=0.0f;
return; }
782 if (comp == 5126 && comps >= 3) {
783 const float *f = (
const float*)p;
791 if (comp == 5123 && comps >= 3) {
792 const unsigned short *s = (
const unsigned short*)p;
793 bool normalized =
false;
795 out->
x = s[0] / 65535.0f;
796 out->
y = s[1] / 65535.0f;
797 out->
z = s[2] / 65535.0f;
798 out->
w = s[3] / 65535.0f;
800 out->
x = (float)s[0];
801 out->
y = (float)s[1];
802 out->
z = (float)s[2];
803 out->
w = (float)s[3];
808 if (comp == 5121 && comps >= 3) {
809 const unsigned char *c = (
const unsigned char*)p;
810 bool normalized =
false;
812 out->
x = c[0] / 255.0f;
813 out->
y = c[1] / 255.0f;
814 out->
z = c[2] / 255.0f;
815 out->
w = c[3] / 255.0f;
817 out->
x = (float)c[0];
818 out->
y = (float)c[1];
819 out->
z = (float)c[2];
820 out->
w = (float)c[3];
825 if (comp == 5122 && comps >= 3) {
826 const short *s = (
const short*)p;
827 out->
x = (float)s[0];
828 out->
y = (float)s[1];
829 out->
z = (float)s[2];
830 out->
w = (float)s[3];
833 if (comp == 5125 && comps >= 3) {
834 const unsigned int *u = (
const unsigned int*)p;
835 out->
x = (float)u[0];
836 out->
y = (float)u[1];
837 out->
z = (float)u[2];
838 out->
w = (float)u[3];
842 out->
x=out->
y=out->
z=out->
w=0.0f;
847 const float *f = (
const float*)p;
848 out[0]=f[0]; out[1]=f[1];
854 out_joints[0]=out_joints[1]=out_joints[2]=out_joints[3] = 0;
858 if (elems < 1)
return;
864 size_t elem_sz = comp_sz * elems;
865 size_t stride = (
view->byteStride &&
view->byteStride > 0) ?
view->byteStride : elem_sz;
867 uint8_t *src = base + (size_t)vertexIndex * stride;
868 for (
int k = 0; k < elems && k < 4; ++k) {
870 out_joints[k] = *(uint8_t *)(src + k * comp_sz);
872 out_joints[k] = *(uint16_t *)(src + k * comp_sz);
874 out_joints[k] = *(uint32_t *)(src + k * comp_sz);
880 out_weights[0]=out_weights[1]=out_weights[2]=out_weights[3]=0.0f;
883 if (elems < 1)
return;
889 size_t elem_sz = comp_sz * elems;
890 size_t stride = (
view->byteStride &&
view->byteStride > 0) ?
view->byteStride : elem_sz;
892 uint8_t *src = base + (size_t)vertexIndex * stride;
894 for (
int k = 0; k < elems && k < 4; ++k) {
897 memcpy(&v, src + k*4, 4);
901 out_weights[k] = (float)v / 255.0f;
903 uint16_t v; memcpy(&v, src + k*2, 2);
904 out_weights[k] = (float)v / 65535.0f;
906 out_weights[k] = 0.0f;
909 float s = out_weights[0] + out_weights[1] + out_weights[2] + out_weights[3];
910 if (s > 0.0f && fabsf(s - 1.0f) > 1e-6f) {
911 out_weights[0] /= s; out_weights[1] /= s; out_weights[2] /= s; out_weights[3] /= s;
918 case 5125:
return *(
const unsigned int*)p;
919 case 5123:
return *(
const unsigned short*)p;
920 case 5121:
return *(
const unsigned char*)p;
938 if (parent_global == NULL) {
960 vec3 mn = { INFINITY, INFINITY, INFINITY}, mx = { -INFINITY, -INFINITY, -INFINITY};
961 for (
int i=0;i<
n;i++){
962 mn.
x = fminf(mn.
x, pts[i].
x); mx.x = fmaxf(mx.x, pts[i].
x);
963 mn.
y = fminf(mn.
y, pts[i].
y); mx.y = fmaxf(mx.y, pts[i].
y);
964 mn.
z = fminf(mn.
z, pts[i].
z); mx.z = fmaxf(mx.z, pts[i].
z);
966 *minp = mn; *maxp = mx;
971 float src[3] = { p.
x, p.
y, p.
z };
973 out.
x = sign[0] * src[
perm[0]];
974 out.
y = sign[1] * src[
perm[1]];
975 out.
z = sign[2] * src[
perm[2]];
980 {0,1,2}, {0,2,1}, {1,0,2}, {1,2,0}, {2,0,1}, {2,1,0}
984 int count = posAcc->
count;
985 int maxSample = 1500;
986 int step = 1 + (count / maxSample);
988 vec3 *pts = malloc(
sizeof(
vec3) * ((count+step-1)/step));
989 for (
int i=0; i<count; i+=step) {
995 float bestScore = 1e30f;
996 int best_p = 0, best_s = 0;
997 for (
int pi=0; pi<6; ++pi) {
998 for (
int s=0; s<8; ++s) {
999 int sign[3] = { (s&1)?-1:1, (s&2)?-1:1, (s&4)?-1:1 };
1001 vec3 mn = { INFINITY, INFINITY, INFINITY}, mx = { -INFINITY, -INFINITY, -INFINITY};
1002 for (
int k=0; k<
n; ++k) {
1004 if (q.
x < mn.
x) mn.
x = q.
x;
if (q.
x > mx.x) mx.x = q.
x;
1005 if (q.
y < mn.
y) mn.
y = q.
y;
if (q.
y > mx.y) mx.y = q.
y;
1006 if (q.
z < mn.
z) mn.
z = q.
z;
if (q.
z > mx.z) mx.z = q.
z;
1008 float extX = mx.
x - mn.
x;
1010 float extY = mx.y - mn.
y;
1011 float extZ = mx.z - mn.
z;
1012 if (extX < 0.001f || extY < 0.001f)
continue;
1013 float ratio = fabsf(extX - extY) / fmaxf(extX, extY);
1014 float zpen = extZ / fmaxf(1.0f, fmaxf(extX, extY));
1015 float score = ratio + 0.4f * zpen;
1016 if (score < bestScore) { bestScore = score; best_p = pi; best_s = s; }
1021 out_perm[0] =
perms[best_p][0]; out_perm[1] =
perms[best_p][1]; out_perm[2] =
perms[best_p][2];
1022 out_sign[0] = (best_s & 1) ? -1 : 1;
1023 out_sign[1] = (best_s & 2) ? -1 : 1;
1024 out_sign[2] = (best_s & 4) ? -1 : 1;
1031 if (strcmp(acc->
type,
"SCALAR") == 0) {
1033 }
else if (strcmp(acc->
type,
"VEC2") == 0){
1036 }
else if (strcmp(acc->
type,
"VEC3") == 0){
1039 }
else if (strcmp(acc->
type,
"VEC4") == 0){
1042 }
else if (strcmp(acc->
type,
"MAT3") == 0){
1045 }
else if (strcmp(acc->
type,
"MAT4") == 0){
1049 if (out_elem_count) *out_elem_count = elem_count;
1056 float *out = malloc(
sizeof(
float) * acc->
count * elem_count);
1058 for (
int i = 0; i < acc->
count; i++) {
1059 memcpy(&out[i * elem_count],
1060 src + i * (
view->byteStride ?
view->byteStride : elem_count *
sizeof(
float)),
1061 elem_count *
sizeof(
float));
1078 char * dot = strchr(name,
'.');
1082 strncpy(anim.
name, name, 63);
1083 anim.
name[63] =
'\0';
1098 for (
int k = 0; k < 4; ++k) {
1099 int nodeIdx = raw[k];
1102 if (skin->
joints[j] == nodeIdx) { found = j;
break; }
1104 out[k] = (found >= 0) ? found : 0;
1118 if (texture->
source != -1)
1132 wrap_s = dsampler->
wrap_s;
1133 wrap_t = dsampler->
wrap_t;
1138 min_filter = GL_LINEAR_MIPMAP_LINEAR;
1139 mag_filter = GL_LINEAR;
1146 if (idx == -1) printf(
"AAAA\n");
1148 Image * img = &images[idx];
1149 strncpy(img->
name, dimage->
name, 254);
1150 printf(
"LOADING: %s\n", img->
name);
1151 img->
name[255] =
'\0';
1154 strncpy(img->
path, dimage->
uri, 254);
1155 img->
path[255] =
'\0';
1190 aabb.
min_x = INFINITY;
1191 aabb.
min_y = INFINITY;
1192 aabb.
min_z = INFINITY;
1193 aabb.
max_x = -INFINITY;
1194 aabb.
max_y = -INFINITY;
1195 aabb.
max_z = -INFINITY;
1231 if (strcmp(mn->
name,
"Root") == 0)
1254 strcpy(prefix, gltf_path);
1255 char * last = strrchr(prefix,
'/');
1265 int * mesh_indices = NULL;
size_t mesh_indices_count = 0;
size_t mesh_indices_capacity = 0;
1266 int * node_indices = NULL;
size_t node_indices_count = 0;
size_t node_indices_capacity = 0;
1298 if (
material->base_color_tex != -1)
1334 if (
material->metallic_roughness_tex != -1)
1352 if (
material->metallic_roughness_tex != -1)
1387 mat->
ka = (
vec4){0,0,0,0};
1388 mat->
ks = (
vec4){0,0,0,0};
1392 mat->
name[63] =
'\0';
1410 for (
int ni = 0; ni < doc.
node_count; ++ni) {
1412 if (node->
parent == -1) {
1422 for (
int i = 0; i < mesh_indices_count; i++) {
1423 if (node->
mesh == mesh_indices[i]) valid =
false;
1425 if (!valid)
continue;
1429 if (mesh_indices_count >= mesh_indices_capacity){
1430 mesh_indices_capacity = (mesh_indices_capacity + 2) * 2;
1431 mesh_indices = realloc(mesh_indices,
sizeof(
int) * mesh_indices_capacity);
1433 mesh_indices[mesh_indices_count++] = node->
mesh;
1454 node->
mesh = current_mesh;
1459 for (
int p = 0; p < mesh->
primCount; ++p) {
1485 int vertex_count = aPos->
count;
1487 for (
int i = 0; i < vertex_count; i++)
1492 vec3 V0 = (
vec3){p0[0],p0[1],p0[2]};
1495 vec4 T0 = {1,0,0,1};
1497 float uv0f[2]={0,0};
1503 vec2 UV = {uv0f[0],uv0f[1]};
1504 int rawJ0[4] = {0}, rawJ1[4] = {0}, rawJ2[4] = {0};
1505 float W0[4] = {1}, W1[4] ={1}, W2[4] = {1};
1506 int J0[4] = {0}, J1[4] = {0}, J2[4] = {0};
1511 V0 = (
vec3){p0[0],p0[1],p0[2]};
1523 N0 = (
vec3){nn0[0], nn0[1], nn0[2]};
1553 vec3 n0 = {nn0[0], nn0[1], nn0[2]};
1583 pt->
joints = (
ivec4){rawJ0[0], rawJ0[1], rawJ0[2], rawJ0[3]};
1597 int indexCount = aIdx->
count;
1598 for (
int i = 0; i < indexCount; i += 3) {
1620 strcpy(asset->
path, gltf_path);
1626 size_t * root_nodes =
CAST_VEC(roots,
size_t);
1627 for (
int i = 0; i < roots.
count; i++)
1630 if (root->
mesh != -1)
1646 Mesh * m = &meshes[i];
1689 size_t stride =
view->byteStride ?
view->byteStride :
sizeof(float) * 16;
1717 printf(
"SKIN: %s\n", skin->
name);
1725 bool match_found =
false;
1735 printf(
"MAX ANIMATIONS REACHED\n");
1740 if (match_found)
break;
1742 if (match_found)
break;
1745 char idle_anim_name[128];
1746 sprintf(idle_anim_name,
"%s_Idle", asset->
skins[i].
name);
1754 asset->
skins = NULL;
1789 asset->
nodes = NULL;
1824 if (mesh_indices) free(mesh_indices);
1825 if (node_indices) free(node_indices);
Engine specific types and defines.
size_t first_vertex
The first Point in Mesh.vertices that this SubMesh refers to.
int joint_count
The amount of joints.
#define GBUFFER_VERSIONS
Maxmimum gbuffer shader branches.
bool override_transparent
Vector mesh_nodes
MeshNodes.
size_t lm_offset
The starting index in LIGHTMAP_DATA that this SubMesh points to.
AnimationInterpolation interpolation
GLint min_filter
Refer to OpenGL texture loading.
float scattering_coefficients[6]
int asset_id
The ID into Globals.assets.
float base_color_factor[4]
size_t index_count
The amount of indices that this SubMesh refers to: Mesh.tris -> MeshFace.indices.
int animation_indices_count
The amount of Animation indices.
int animation_indices[256]
Animation indices into Asset.
AnimationSampler * samplers
GLint wrap_t
Refer to OpenGL texture loading.
GLTFAnimation * animations
mat4 * inverse_bind
The inverse_bind that is computed on load_gltf().
AABB aabb
AABB over each MeshNode for this Asset.
Globals GLOBALS
Engine Globals.
size_t first_index
The first index that this SubMesh refers to (Mesh.tris count * 3).
int * joints
The joints for skeletal animating.
int metallic_roughness_tex
GLint wrap_s
Refer to OpenGL texture loading.
char name[256]
The name of the skin from load.
#define UBERSHADER_VERSIONS
Maxmimum main shader brances.
Vector mesh_indices
int - Indices into Globals.meshes which indicate what Meshes are within this Asset.
GLTFAnimationSampler * samplers
GLint mag_filter
Refer to OpenGL texture loading.
size_t material
The Material.
unsigned char * buf
If available, the raw bytes of the Image.
char name[256]
The name of the Image file.
Vector materials
Materials.
#define MAX_ANIMATION_INDICES
The maximum amount of animation indices per GLTFSkin.
mat4 * final_mat
The transformation after update_skin_matrices().
float absorption_coefficients[6]
GLTFAnimationChannel * channels
char name[256]
Name of the shader.
int ibm_accessor
Which GLTFAccessor this refers to.
char path[256]
The path to the Asset.
size_t vertex_count
The amount of Points that this SubMesh refers to.
GLTFAnimationChannel * channels
char path[256]
The path to the Image.
size_t draw_offset
Currently the same index as SubMesh.lm_offset.
char mime_type[256]
Unused, refer to GLTF spec.
The structure of an Skeletatal Animation loaded in from GLTFAnimation.
Engine-side sampler version of GLTFAnimationSampler with the field allocated.
An Asset after it is loaded in from file into Globals.assets.
The accessors of a GLTF file.
The GLTF structure of an Animation.
The channels of an Animation.
The individual interpolation of an Animation.
The raw bytes of a GLTF file.
The views of a GLTF file.
The entire GLTF after it is parsed via load_gltf().
The Image before it is processed.
The structure for Materials when loaded in from file.
The Meshes of a GLTF file.
The primitives of a GLTF file.
The sampler for the Image before it is processed.
The structure of an Skeletatal Animation loaded in from GLTFAnimation.
The GLTFTexture before it is processed.
Where Material textures are saved upon loading from file.
Sections of Meshes that are divided based on Material for batching and DrawCommand ordering purposes.
mat4 compose_trs_from_mesh_node(MeshNode *mn)
char * load_text_file(const char *path)
vec3 apply_mapping_to_point(const vec3 p, const int perm[3], const int sign[3])
unsigned char * accessor_base_ptr(GLTFDoc *doc, const GLTFAccessor *acc)
int str_eq(const char *a, const char *b)
int comp_count(const char *type)
void fetch_vec3f(const GLTFDoc *doc, const GLTFAccessor *acc, int idx, float out[3])
int parse_gltf_json(const char *gltfPath, const char *prefix, GLTFDoc *doc)
AnimationSampler expand_sampler(const GLTFAccessor *input_acc, const GLTFAccessor *output_acc, int interpolation, GLTFDoc *doc)
int detect_best_axis_mapping(GLTFDoc *doc, GLTFAccessor *posAcc, MeshNode *node, int out_perm[3], int out_sign[3])
vec3 map_x_z_neg_y(vec3 p)
void parse_gltf_materials(cJSON *root, GLTFDoc *doc)
void fetch_vec2f(GLTFDoc *doc, const GLTFAccessor *acc, int idx, float out[2])
void bbox_of_points(vec3 *pts, int n, vec3 *minp, vec3 *maxp)
void compute_all_node_globals(GLTFDoc *doc)
int comp_size(int componentType)
void apply_static_mesh_to_children(MeshNode *mns, Mesh *meshes, MeshNode *mn, bool static_mesh)
const unsigned char * accessor_elem_ptr(const GLTFDoc *doc, const GLTFAccessor *acc, int index)
int json_get_tex_index(cJSON *parent, const char *name)
MeshNode * find_root_node_armature(MeshNode *mns, MeshNode *mn)
void remap_joints_to_skin(const GLTFSkin *skin, const int raw[4], int out[4])
void fetch_weights4_from_accessor(const GLTFDoc *doc, const GLTFAccessor *acc, int vertexIndex, float out_weights[4])
int load_gltf_texture(GLTFTexture *texture, GLTFSampler *samplers, GLTFImage *gltf_images, bool normal)
void fetch_joints4_from_accessor(const GLTFDoc *doc, const GLTFAccessor *acc, int vertexIndex, int out_joints[4])
void fetch_vec4(const GLTFDoc *doc, const GLTFAccessor *acc, int idx, vec4 *out)
void load_gltf(const char *gltf_path)
Loads a GLTF.
float * expand_accessor(const GLTFAccessor *acc, int *out_elem_count, GLTFDoc *doc)
Animation build_animation(const char *name, GLTFAnimation *animation, GLTFDoc *doc)
vec3 map_x_negz_y(vec3 p)
float extent(vec3 a, vec3 b)
void json_get_float_array(cJSON *parent, const char *name, float *out, int count, const float *defaults)
unsigned int fetch_index(GLTFDoc *doc, const GLTFAccessor *acc, int idx)
int json_get_int(cJSON *obj, const char *key, int defv)
mat4 gltf_to_engine_basis()
const char * json_get_str(cJSON *obj, const char *key)
void json_get_vec4(cJSON *obj, const char *key, vec4 *out, const float def[3])
void compute_node_globals_recursive(GLTFDoc *doc, int idx, const mat4 *parent_global)
AABB determine_aabb_of_children(MeshNode *mns, Mesh *meshes, MeshNode *mn)
void json_get_vec3(cJSON *obj, const char *key, vec3 *out, const float def[3])
unsigned char * load_bin_file(const char *path, int expectedLen)
#define AUDIO_NUM_BANDS
The amount of bands to use for audio probe baking.
@ COL_HULL
Incremental hull generates a ConvexHull which is used for collision tests.
@ COL_TRIANGLE
The triangle BVH is generated for this mesh and triangle tests are performed on it.
int fwd_shader
The corresponding forward rendering shader branch in DYNAMIC_SHADER.
size_t id
The ID inside of Globals.mesh_nodes.
int skin
The GLTFSkin it controls.
float normal_scale
Unused - Used to determine how much of the texture normal to use.
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 override_transparent
If we are overriding transparency of this Material.
CollisionType collision_type
The type of collision to apply for the PhysicsComponent that may not have been created yet.
char name[256]
The name generated on loading from file.
int children[128]
The children to this node, a global index into Globals.mesh_nodes.
int mesh
The Mesh it references.
int parent
The parent to this node, a global index into Globals.mesh_nodes.
PhysicsType physics_mode
The type of physics to apply for the PhysicsComponent that may not have been created yet.
bool override_arm
If we override the ARM texture and bind a dummy instead.
int child_count
The amount of children.
AABB aabb
The AABB of the MeshNode and its children.
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...
float alpha_cutoff
Used to discard transparent Materials.
float absorption_coefficients[6]
How much audio is absorbed when hitting this Material.
float scattering_coefficients[6]
How much audio specularly reflects when hitting this Material.
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 ...
void init_mesh(Mesh *mesh)
Initializes a static or dynamic Mesh.
Important globals for the entire engine and initialization functions that are mainly called on engine...
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.
AABB aabb
The AABB of the mesh.
Vector submeshes
The SubMeshes split via Materials.
char name[256]
The Mesh name assigned on load.
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.
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.
Saves and loads various files that store engine data.
ShaderProgram DEPTH_PRE_PASS_SHADER[1]
render/depth_pre_pass.vert render/depth_pre_pass.frag
ShaderProgram DYNAMIC_SHADER[32]
render/dynamic.vert render/dynamic.frag
vec3 vec3_normalize(vec3 v)
mat4 m4_mul(mat4 a, mat4 b)
bool m4_inverse(float out[16], const float m[16])
void init_vector(Vector *v, size_t i, size_t type_size)
void calculate_aabb_extents(AABB *aabb)
void * vector_expand(Vector *v, size_t i)
vec4 vec3_to_vec4(vec3 v)
#define CAST_VEC(vec, type)
vec3 mat4_mul_point(mat4 m, vec3 v)
vec3 compute_tri_normal(vec3 a, vec3 b, vec3 c)
vec3 vec4_to_vec3(vec4 v)