REVIEWED: Latest PR to follow raylib code conventions

This commit is contained in:
Ray 2021-06-26 13:45:15 +02:00
parent 71700254b4
commit 89708edf7f

View file

@ -50,12 +50,6 @@
#include <string.h> // Required for: memcmp(), strlen() #include <string.h> // Required for: memcmp(), strlen()
#include <math.h> // Required for: sinf(), cosf(), sqrtf(), fabsf() #include <math.h> // Required for: sinf(), cosf(), sqrtf(), fabsf()
#if defined(_MSC_VER)
#include <climits> // Required for numerical limit constants like CHAR_MAX
#else
#include <limits.h> // Required for numerical limit constants like CHAR_MAX
#endif
#if defined(_WIN32) #if defined(_WIN32)
#include <direct.h> // Required for: _chdir() [Used in LoadOBJ()] #include <direct.h> // Required for: _chdir() [Used in LoadOBJ()]
#define CHDIR _chdir #define CHDIR _chdir
@ -129,8 +123,8 @@ static void LoadGLTFNode(cgltf_data *data, cgltf_node* node, Model* outModel, Ma
static void InitGLTFBones(Model *model, const cgltf_data *data); static void InitGLTFBones(Model *model, const cgltf_data *data);
static void BindGLTFPrimitiveToBones(Model *model, const cgltf_data *data, int primitiveIndex); static void BindGLTFPrimitiveToBones(Model *model, const cgltf_data *data, int primitiveIndex);
static void GetGLTFPrimitiveCount(cgltf_node* node, int* outCount); static void GetGLTFPrimitiveCount(cgltf_node* node, int* outCount);
static bool GLTFReadValue(cgltf_accessor* acc, unsigned int index, void *variable); static bool ReadGLTFValue(cgltf_accessor* acc, unsigned int index, void *variable);
static void* GLTFReadValuesAs(cgltf_accessor* acc, cgltf_component_type type, bool adjustOnDownCasting); static void *ReadGLTFValuesAs(cgltf_accessor* acc, cgltf_component_type type, bool adjustOnDownCasting);
#endif #endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -4142,11 +4136,13 @@ static Image LoadImageFromCgltfImage(cgltf_image *image, const char *texPath, Co
return rimage; return rimage;
} }
//
static bool GLTFReadValue(cgltf_accessor* acc, unsigned int index, void *variable) static bool ReadGLTFValue(cgltf_accessor *acc, unsigned int index, void *variable)
{ {
unsigned int typeElements = 0; unsigned int typeElements = 0;
switch(acc->type) {
switch (acc->type)
{
case cgltf_type_scalar: typeElements = 1; break; case cgltf_type_scalar: typeElements = 1; break;
case cgltf_type_vec2: typeElements = 2; break; case cgltf_type_vec2: typeElements = 2; break;
case cgltf_type_vec3: typeElements = 3; break; case cgltf_type_vec3: typeElements = 3; break;
@ -4155,10 +4151,13 @@ static bool GLTFReadValue(cgltf_accessor* acc, unsigned int index, void *variabl
case cgltf_type_mat3: typeElements = 9; break; case cgltf_type_mat3: typeElements = 9; break;
case cgltf_type_mat4: typeElements = 16; break; case cgltf_type_mat4: typeElements = 16; break;
case cgltf_type_invalid: typeElements = 0; break; case cgltf_type_invalid: typeElements = 0; break;
default: break;
} }
unsigned int typeSize = 0; unsigned int typeSize = 0;
switch(acc->component_type) {
switch (acc->component_type)
{
case cgltf_component_type_r_8u: case cgltf_component_type_r_8u:
case cgltf_component_type_r_8: typeSize = 1; break; case cgltf_component_type_r_8: typeSize = 1; break;
case cgltf_component_type_r_16u: case cgltf_component_type_r_16u:
@ -4166,6 +4165,7 @@ static bool GLTFReadValue(cgltf_accessor* acc, unsigned int index, void *variabl
case cgltf_component_type_r_32f: case cgltf_component_type_r_32f:
case cgltf_component_type_r_32u: typeSize = 4; break; case cgltf_component_type_r_32u: typeSize = 4; break;
case cgltf_component_type_invalid: typeSize = 0; break; case cgltf_component_type_invalid: typeSize = 0; break;
default: break;
} }
unsigned int singleElementSize = typeSize*typeElements; unsigned int singleElementSize = typeSize*typeElements;
@ -4196,11 +4196,12 @@ static bool GLTFReadValue(cgltf_accessor* acc, unsigned int index, void *variabl
return true; return true;
} }
static void* GLTFReadValuesAs(cgltf_accessor* acc, cgltf_component_type type, bool adjustOnDownCasting) static void *ReadGLTFValuesAs(cgltf_accessor* acc, cgltf_component_type type, bool adjustOnDownCasting)
{ {
unsigned int count = acc->count; unsigned int count = acc->count;
unsigned int typeSize = 0; unsigned int typeSize = 0;
switch(type) { switch (type)
{
case cgltf_component_type_r_8u: case cgltf_component_type_r_8u:
case cgltf_component_type_r_8: typeSize = 1; break; case cgltf_component_type_r_8: typeSize = 1; break;
case cgltf_component_type_r_16u: case cgltf_component_type_r_16u:
@ -4208,10 +4209,12 @@ static void* GLTFReadValuesAs(cgltf_accessor* acc, cgltf_component_type type, bo
case cgltf_component_type_r_32f: case cgltf_component_type_r_32f:
case cgltf_component_type_r_32u: typeSize = 4; break; case cgltf_component_type_r_32u: typeSize = 4; break;
case cgltf_component_type_invalid: typeSize = 0; break; case cgltf_component_type_invalid: typeSize = 0; break;
default: break;
} }
unsigned int typeElements = 0; unsigned int typeElements = 0;
switch(acc->type) { switch (acc->type)
{
case cgltf_type_scalar: typeElements = 1; break; case cgltf_type_scalar: typeElements = 1; break;
case cgltf_type_vec2: typeElements = 2; break; case cgltf_type_vec2: typeElements = 2; break;
case cgltf_type_vec3: typeElements = 3; break; case cgltf_type_vec3: typeElements = 3; break;
@ -4220,23 +4223,23 @@ static void* GLTFReadValuesAs(cgltf_accessor* acc, cgltf_component_type type, bo
case cgltf_type_mat3: typeElements = 9; break; case cgltf_type_mat3: typeElements = 9; break;
case cgltf_type_mat4: typeElements = 16; break; case cgltf_type_mat4: typeElements = 16; break;
case cgltf_type_invalid: typeElements = 0; break; case cgltf_type_invalid: typeElements = 0; break;
default: break;
} }
if (acc->component_type == type) if (acc->component_type == type)
{ {
void *array = RL_MALLOC(count*typeElements*typeSize); void *array = RL_MALLOC(count*typeElements*typeSize);
for(unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++) ReadGLTFValue(acc, i, (char *)array + i*typeElements*typeSize);
{
GLTFReadValue(acc, i, (char*)array + i * typeElements * typeSize);
}
return array; return array;
} else { }
else
{
unsigned int accTypeSize = 0; unsigned int accTypeSize = 0;
switch(acc->component_type) { switch (acc->component_type)
{
case cgltf_component_type_r_8u: case cgltf_component_type_r_8u:
case cgltf_component_type_r_8: accTypeSize = 1; break; case cgltf_component_type_r_8: accTypeSize = 1; break;
case cgltf_component_type_r_16u: case cgltf_component_type_r_16u:
@ -4244,6 +4247,7 @@ static void* GLTFReadValuesAs(cgltf_accessor* acc, cgltf_component_type type, bo
case cgltf_component_type_r_32f: case cgltf_component_type_r_32f:
case cgltf_component_type_r_32u: accTypeSize = 4; break; case cgltf_component_type_r_32u: accTypeSize = 4; break;
case cgltf_component_type_invalid: accTypeSize = 0; break; case cgltf_component_type_invalid: accTypeSize = 0; break;
default: break;
} }
void *array = RL_MALLOC(count*typeElements*typeSize); void *array = RL_MALLOC(count*typeElements*typeSize);
@ -4251,427 +4255,286 @@ static void* GLTFReadValuesAs(cgltf_accessor* acc, cgltf_component_type type, bo
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)
{ {
GLTFReadValue(acc, i, (char*)additionalArray + i * typeElements * accTypeSize); ReadGLTFValue(acc, i, (char *)additionalArray + i*typeElements*accTypeSize);
} }
switch(acc->component_type) { switch (acc->component_type)
{
case cgltf_component_type_r_8u: case cgltf_component_type_r_8u:
{ {
unsigned char *typedAdditionalArray = (unsigned char *)additionalArray; unsigned char *typedAdditionalArray = (unsigned char *)additionalArray;
switch(type) { switch (type)
{
case cgltf_component_type_r_8: case cgltf_component_type_r_8:
{ {
char *typedArray = (char *)array; char *typedArray = (char *)array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (char)(typedAdditionalArray[i]/(UCHAR_MAX/CHAR_MAX));
{ else typedArray[i] = (char)typedAdditionalArray[i];
typedArray[i] = (char)(typedAdditionalArray[i] / (UCHAR_MAX / CHAR_MAX));
}
else
{
typedArray[i] = (char)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_16u: case cgltf_component_type_r_16u:
{ {
unsigned short *typedArray = (unsigned short *)array; unsigned short *typedArray = (unsigned short *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned short)typedAdditionalArray[i];
{ } break;
typedArray[i] = (unsigned short)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_16: case cgltf_component_type_r_16:
{ {
short *typedArray = (short *)array; short *typedArray = (short *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (short)typedAdditionalArray[i];
{ } break;
typedArray[i] = (short)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_32f: case cgltf_component_type_r_32f:
{ {
float *typedArray = (float *)array; float *typedArray = (float *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (float)typedAdditionalArray[i];
{ } break;
typedArray[i] = (float)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_32u: case cgltf_component_type_r_32u:
{ {
unsigned int *typedArray = (unsigned int *)array; unsigned int *typedArray = (unsigned int *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned int)typedAdditionalArray[i];
} break;
default:
{ {
typedArray[i] = (unsigned int)typedAdditionalArray[i];
}
break;
}
default: {
RL_FREE(array); RL_FREE(array);
RL_FREE(additionalArray); RL_FREE(additionalArray);
return NULL; return NULL;
} break;
} }
} } break;
break;
}
case cgltf_component_type_r_8: case cgltf_component_type_r_8:
{ {
char *typedAdditionalArray = (char *)additionalArray; char *typedAdditionalArray = (char *)additionalArray;
switch(type) { switch (type)
{
case cgltf_component_type_r_8u: case cgltf_component_type_r_8u:
{ {
unsigned char *typedArray = (unsigned char *)array; unsigned char *typedArray = (unsigned char *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned char)typedAdditionalArray[i];
{ } break;
typedArray[i] = (unsigned char)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_16u: case cgltf_component_type_r_16u:
{ {
unsigned short *typedArray = (unsigned short *)array; unsigned short *typedArray = (unsigned short *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned short)typedAdditionalArray[i];
{ } break;
typedArray[i] = (unsigned short)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_16: case cgltf_component_type_r_16:
{ {
short *typedArray = (short *)array; short *typedArray = (short *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (short)typedAdditionalArray[i];
{ } break;
typedArray[i] = (short)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_32f: case cgltf_component_type_r_32f:
{ {
float *typedArray = (float *)array; float *typedArray = (float *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (float)typedAdditionalArray[i];
{ } break;
typedArray[i] = (float)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_32u: case cgltf_component_type_r_32u:
{ {
unsigned int *typedArray = (unsigned int *)array; unsigned int *typedArray = (unsigned int *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned int)typedAdditionalArray[i];
} break;
default:
{ {
typedArray[i] = (unsigned int)typedAdditionalArray[i];
}
break;
}
default: {
RL_FREE(array); RL_FREE(array);
RL_FREE(additionalArray); RL_FREE(additionalArray);
return NULL; return NULL;
} break;
} }
} } break;
break;
}
case cgltf_component_type_r_16u: case cgltf_component_type_r_16u:
{ {
unsigned short *typedAdditionalArray = (unsigned short *)additionalArray; unsigned short *typedAdditionalArray = (unsigned short *)additionalArray;
switch(type) { switch (type)
{
case cgltf_component_type_r_8u: case cgltf_component_type_r_8u:
{ {
unsigned char *typedArray = (unsigned char *)array; unsigned char *typedArray = (unsigned char *)array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (unsigned char)(typedAdditionalArray[i]/(USHRT_MAX/UCHAR_MAX));
{ else typedArray[i] = (unsigned char)typedAdditionalArray[i];
typedArray[i] = (unsigned char)(typedAdditionalArray[i] / (USHRT_MAX / UCHAR_MAX));
}
else
{
typedArray[i] = (unsigned char)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_8: case cgltf_component_type_r_8:
{ {
char *typedArray = (char *) array; char *typedArray = (char *) array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (char)(typedAdditionalArray[i]/(USHRT_MAX/CHAR_MAX));
{ else typedArray[i] = (char)typedAdditionalArray[i];
typedArray[i] = (char)(typedAdditionalArray[i] / (USHRT_MAX / CHAR_MAX));
}
else
{
typedArray[i] = (char)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_16: case cgltf_component_type_r_16:
{ {
short *typedArray = (short *)array; short *typedArray = (short *)array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (short)(typedAdditionalArray[i]/(USHRT_MAX/SHRT_MAX));
{ else typedArray[i] = (short)typedAdditionalArray[i];
typedArray[i] = (short)(typedAdditionalArray[i] / (USHRT_MAX / SHRT_MAX));
}
else
{
typedArray[i] = (short)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_32f: case cgltf_component_type_r_32f:
{ {
float* typedArray = (float*) array; float* typedArray = (float*) array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (float)typedAdditionalArray[i];
{ } break;
typedArray[i] = (float)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_32u: case cgltf_component_type_r_32u:
{ {
unsigned int* typedArray = (unsigned int*) array; unsigned int* typedArray = (unsigned int*) array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned int)typedAdditionalArray[i];
} break;
default:
{ {
typedArray[i] = (unsigned int)typedAdditionalArray[i];
}
break;
}
default: {
RL_FREE(array); RL_FREE(array);
RL_FREE(additionalArray); RL_FREE(additionalArray);
return NULL; return NULL;
} break;
} }
} } break;
break;
}
case cgltf_component_type_r_16: case cgltf_component_type_r_16:
{ {
short *typedAdditionalArray = (short *)additionalArray; short *typedAdditionalArray = (short *)additionalArray;
switch(type) { switch (type)
{
case cgltf_component_type_r_8u: case cgltf_component_type_r_8u:
{ {
unsigned char *typedArray = (unsigned char *)array; unsigned char *typedArray = (unsigned char *)array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (unsigned char)(typedAdditionalArray[i]/(SHRT_MAX/UCHAR_MAX));
{ else typedArray[i] = (unsigned char)typedAdditionalArray[i];
typedArray[i] = (unsigned char)(typedAdditionalArray[i] / (SHRT_MAX / UCHAR_MAX));
}
else
{
typedArray[i] = (unsigned char)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_8: case cgltf_component_type_r_8:
{ {
char *typedArray = (char *)array; char *typedArray = (char *)array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (char)(typedAdditionalArray[i]/(SHRT_MAX/CHAR_MAX));
{ else typedArray[i] = (char)typedAdditionalArray[i];
typedArray[i] = (char)(typedAdditionalArray[i] / (SHRT_MAX / CHAR_MAX));
}
else
{
typedArray[i] = (char)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_16u: case cgltf_component_type_r_16u:
{ {
unsigned short *typedArray = (unsigned short *)array; unsigned short *typedArray = (unsigned short *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned short)typedAdditionalArray[i];
{ } break;
typedArray[i] = (unsigned short)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_32f: case cgltf_component_type_r_32f:
{ {
float *typedArray = (float *)array; float *typedArray = (float *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (float)typedAdditionalArray[i];
{ } break;
typedArray[i] = (float)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_32u: case cgltf_component_type_r_32u:
{ {
unsigned int *typedArray = (unsigned int *)array; unsigned int *typedArray = (unsigned int *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned int)typedAdditionalArray[i];
} break;
default:
{ {
typedArray[i] = (unsigned int)typedAdditionalArray[i];
}
break;
}
default: {
RL_FREE(array); RL_FREE(array);
RL_FREE(additionalArray); RL_FREE(additionalArray);
return NULL; return NULL;
} break;
} }
} } break;
break;
}
case cgltf_component_type_r_32f: case cgltf_component_type_r_32f:
{ {
float *typedAdditionalArray = (float *)additionalArray; float *typedAdditionalArray = (float *)additionalArray;
switch(type) { switch (type)
{
case cgltf_component_type_r_8u: case cgltf_component_type_r_8u:
{ {
unsigned char *typedArray = (unsigned char *)array; unsigned char *typedArray = (unsigned char *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned char)typedAdditionalArray[i];
{ } break;
typedArray[i] = (unsigned char)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_8: case cgltf_component_type_r_8:
{ {
char *typedArray = (char *)array; char *typedArray = (char *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (char)typedAdditionalArray[i];
{ } break;
typedArray[i] = (char)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_16u: case cgltf_component_type_r_16u:
{ {
unsigned short *typedArray = (unsigned short *)array; unsigned short *typedArray = (unsigned short *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned short)typedAdditionalArray[i];
{ } break;
typedArray[i] = (unsigned short)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_16: case cgltf_component_type_r_16:
{ {
short *typedArray = (short *)array; short *typedArray = (short *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (short)typedAdditionalArray[i];
{ } break;
typedArray[i] = (short)typedAdditionalArray[i];
}
break;
}
case cgltf_component_type_r_32u: case cgltf_component_type_r_32u:
{ {
unsigned int *typedArray = (unsigned int *)array; unsigned int *typedArray = (unsigned int *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (unsigned int)typedAdditionalArray[i];
} break;
default:
{ {
typedArray[i] = (unsigned int)typedAdditionalArray[i];
}
break;
}
default: {
RL_FREE(array); RL_FREE(array);
RL_FREE(additionalArray); RL_FREE(additionalArray);
return NULL; return NULL;
} break;
} }
} } break;
break;
}
case cgltf_component_type_r_32u: case cgltf_component_type_r_32u:
{ {
unsigned int *typedAdditionalArray = (unsigned int *)additionalArray; unsigned int *typedAdditionalArray = (unsigned int *)additionalArray;
switch(type) { switch (type)
{
case cgltf_component_type_r_8u: case cgltf_component_type_r_8u:
{ {
unsigned char *typedArray = (unsigned char *)array; unsigned char *typedArray = (unsigned char *)array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (unsigned char)(typedAdditionalArray[i]/(UINT_MAX/UCHAR_MAX));
{ else typedArray[i] = (unsigned char)typedAdditionalArray[i];
typedArray[i] = (unsigned char)(typedAdditionalArray[i] / (UINT_MAX / UCHAR_MAX));
}
else
{
typedArray[i] = (unsigned char)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_8: case cgltf_component_type_r_8:
{ {
char *typedArray = (char *)array; char *typedArray = (char *)array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (char)(typedAdditionalArray[i]/(UINT_MAX/CHAR_MAX));
{ else typedArray[i] = (char)typedAdditionalArray[i];
typedArray[i] = (char)(typedAdditionalArray[i] / (UINT_MAX / CHAR_MAX));
}
else
{
typedArray[i] = (char)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_16u: case cgltf_component_type_r_16u:
{ {
unsigned short *typedArray = (unsigned short *)array; unsigned short *typedArray = (unsigned short *)array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (unsigned short)(typedAdditionalArray[i]/(UINT_MAX/USHRT_MAX));
{ else typedArray[i] = (unsigned short)typedAdditionalArray[i];
typedArray[i] = (unsigned short)(typedAdditionalArray[i] / (UINT_MAX / USHRT_MAX));
}
else
{
typedArray[i] = (unsigned short)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_16: case cgltf_component_type_r_16:
{ {
short *typedArray = (short *)array; short *typedArray = (short *)array;
for (unsigned int i = 0; i < count*typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++)
{ {
if(adjustOnDownCasting) if (adjustOnDownCasting) typedArray[i] = (short)(typedAdditionalArray[i]/(UINT_MAX/SHRT_MAX));
{ else typedArray[i] = (short)typedAdditionalArray[i];
typedArray[i] = (short)(typedAdditionalArray[i] / (UINT_MAX / SHRT_MAX));
}
else
{
typedArray[i] = (short)typedAdditionalArray[i];
}
}
break;
} }
} break;
case cgltf_component_type_r_32f: case cgltf_component_type_r_32f:
{ {
float *typedArray = (float *)array; float *typedArray = (float *)array;
for (unsigned int i = 0; i < count * typeElements; i++) for (unsigned int i = 0; i < count*typeElements; i++) typedArray[i] = (float)typedAdditionalArray[i];
} break;
default:
{ {
typedArray[i] = (float)typedAdditionalArray[i];
}
break;
}
default: {
RL_FREE(array); RL_FREE(array);
RL_FREE(additionalArray); RL_FREE(additionalArray);
return NULL; return NULL;
} break;
} }
} } break;
break; default:
} {
default: {
RL_FREE(array); RL_FREE(array);
RL_FREE(additionalArray); RL_FREE(additionalArray);
return NULL; return NULL;
} } break;
} }
RL_FREE(additionalArray); RL_FREE(additionalArray);
@ -4984,7 +4847,7 @@ static ModelAnimation *LoadGLTFModelAnimations(const char *fileName, int *animCo
int frameCounts = (int)channel->sampler->input->count; int frameCounts = (int)channel->sampler->input->count;
float lastFrameTime = 0.0f; float lastFrameTime = 0.0f;
if (GLTFReadValue(channel->sampler->input, frameCounts - 1, &lastFrameTime)) if (ReadGLTFValue(channel->sampler->input, frameCounts - 1, &lastFrameTime))
{ {
animationDuration = fmaxf(lastFrameTime, animationDuration); animationDuration = fmaxf(lastFrameTime, animationDuration);
} }
@ -5046,7 +4909,7 @@ static ModelAnimation *LoadGLTFModelAnimations(const char *fileName, int *animCo
for (unsigned int j = 0; j < sampler->input->count; j++) for (unsigned int j = 0; j < sampler->input->count; j++)
{ {
float inputFrameTime; float inputFrameTime;
if (GLTFReadValue(sampler->input, j, &inputFrameTime)) if (ReadGLTFValue(sampler->input, j, &inputFrameTime))
{ {
if (frameTime < inputFrameTime) if (frameTime < inputFrameTime)
{ {
@ -5055,7 +4918,7 @@ static ModelAnimation *LoadGLTFModelAnimations(const char *fileName, int *animCo
outputMax = j; outputMax = j;
float previousInputTime = 0.0f; float previousInputTime = 0.0f;
if (GLTFReadValue(sampler->input, outputMin, &previousInputTime)) if (ReadGLTFValue(sampler->input, outputMin, &previousInputTime))
{ {
if ((inputFrameTime - previousInputTime) != 0) if ((inputFrameTime - previousInputTime) != 0)
{ {
@ -5079,13 +4942,13 @@ static ModelAnimation *LoadGLTFModelAnimations(const char *fileName, int *animCo
float values[3]; float values[3];
bool success = GLTFReadValue(sampler->output, outputMin, values); bool success = ReadGLTFValue(sampler->output, outputMin, values);
translationStart.x = values[0]; translationStart.x = values[0];
translationStart.y = values[1]; translationStart.y = values[1];
translationStart.z = values[2]; translationStart.z = values[2];
success = GLTFReadValue(sampler->output, outputMax, values) || success; success = ReadGLTFValue(sampler->output, outputMax, values) || success;
translationEnd.x = values[0]; translationEnd.x = values[0];
translationEnd.y = values[1]; translationEnd.y = values[1];
@ -5100,14 +4963,14 @@ static ModelAnimation *LoadGLTFModelAnimations(const char *fileName, int *animCo
float values[4]; float values[4];
bool success = GLTFReadValue(sampler->output, outputMin, &values); bool success = ReadGLTFValue(sampler->output, outputMin, &values);
rotationStart.x = values[0]; rotationStart.x = values[0];
rotationStart.y = values[1]; rotationStart.y = values[1];
rotationStart.z = values[2]; rotationStart.z = values[2];
rotationStart.w = values[3]; rotationStart.w = values[3];
success = GLTFReadValue(sampler->output, outputMax, &values) || success; success = ReadGLTFValue(sampler->output, outputMax, &values) || success;
rotationEnd.x = values[0]; rotationEnd.x = values[0];
rotationEnd.y = values[1]; rotationEnd.y = values[1];
@ -5126,13 +4989,13 @@ static ModelAnimation *LoadGLTFModelAnimations(const char *fileName, int *animCo
float values[3]; float values[3];
bool success = GLTFReadValue(sampler->output, outputMin, &values); bool success = ReadGLTFValue(sampler->output, outputMin, &values);
scaleStart.x = values[0]; scaleStart.x = values[0];
scaleStart.y = values[1]; scaleStart.y = values[1];
scaleStart.z = values[2]; scaleStart.z = values[2];
success = GLTFReadValue(sampler->output, outputMax, &values) || success; success = ReadGLTFValue(sampler->output, outputMax, &values) || success;
scaleEnd.x = values[0]; scaleEnd.x = values[0];
scaleEnd.y = values[1]; scaleEnd.y = values[1];
@ -5175,7 +5038,6 @@ static ModelAnimation *LoadGLTFModelAnimations(const char *fileName, int *animCo
RL_FREE(completedBones); RL_FREE(completedBones);
} }
} }
cgltf_free(data); cgltf_free(data);
@ -5200,7 +5062,7 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
int bufferSize = outModel->meshes[(*primitiveIndex)].vertexCount*3*sizeof(float); int bufferSize = outModel->meshes[(*primitiveIndex)].vertexCount*3*sizeof(float);
outModel->meshes[(*primitiveIndex)].animVertices = RL_MALLOC(bufferSize); outModel->meshes[(*primitiveIndex)].animVertices = RL_MALLOC(bufferSize);
outModel->meshes[(*primitiveIndex)].vertices = GLTFReadValuesAs(acc, cgltf_component_type_r_32f, false); outModel->meshes[(*primitiveIndex)].vertices = ReadGLTFValuesAs(acc, cgltf_component_type_r_32f, false);
// Transform using the nodes matrix attributes // Transform using the nodes matrix attributes
for (unsigned int v = 0; v < outModel->meshes[(*primitiveIndex)].vertexCount; v++) for (unsigned int v = 0; v < outModel->meshes[(*primitiveIndex)].vertexCount; v++)
@ -5208,8 +5070,7 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
Vector3 vertex = { Vector3 vertex = {
outModel->meshes[(*primitiveIndex)].vertices[(v*3 + 0)], outModel->meshes[(*primitiveIndex)].vertices[(v*3 + 0)],
outModel->meshes[(*primitiveIndex)].vertices[(v*3 + 1)], outModel->meshes[(*primitiveIndex)].vertices[(v*3 + 1)],
outModel->meshes[(*primitiveIndex)].vertices[(v * 3 + 2)], outModel->meshes[(*primitiveIndex)].vertices[(v*3 + 2)] };
};
vertex = Vector3Transform(vertex, currentTransform); vertex = Vector3Transform(vertex, currentTransform);
@ -5227,7 +5088,7 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
int bufferSize = (int)(acc->count*3*sizeof(float)); int bufferSize = (int)(acc->count*3*sizeof(float));
outModel->meshes[(*primitiveIndex)].animNormals = RL_MALLOC(bufferSize); outModel->meshes[(*primitiveIndex)].animNormals = RL_MALLOC(bufferSize);
outModel->meshes[(*primitiveIndex)].normals = GLTFReadValuesAs(acc, cgltf_component_type_r_32f, false); outModel->meshes[(*primitiveIndex)].normals = ReadGLTFValuesAs(acc, cgltf_component_type_r_32f, false);
// Transform using the nodes matrix attributes // Transform using the nodes matrix attributes
for (unsigned int v = 0; v < outModel->meshes[(*primitiveIndex)].vertexCount; v++) for (unsigned int v = 0; v < outModel->meshes[(*primitiveIndex)].vertexCount; v++)
@ -5235,8 +5096,7 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
Vector3 normal = { Vector3 normal = {
outModel->meshes[(*primitiveIndex)].normals[(v*3 + 0)], outModel->meshes[(*primitiveIndex)].normals[(v*3 + 0)],
outModel->meshes[(*primitiveIndex)].normals[(v*3 + 1)], outModel->meshes[(*primitiveIndex)].normals[(v*3 + 1)],
outModel->meshes[(*primitiveIndex)].normals[(v * 3 + 2)], outModel->meshes[(*primitiveIndex)].normals[(v*3 + 2)] };
};
normal = Vector3Transform(normal, currentTransform); normal = Vector3Transform(normal, currentTransform);
@ -5250,7 +5110,7 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
else if (mesh->primitives[p].attributes[j].type == cgltf_attribute_type_texcoord) else if (mesh->primitives[p].attributes[j].type == cgltf_attribute_type_texcoord)
{ {
cgltf_accessor *acc = mesh->primitives[p].attributes[j].data; cgltf_accessor *acc = mesh->primitives[p].attributes[j].data;
outModel->meshes[(*primitiveIndex)].texcoords = GLTFReadValuesAs(acc, cgltf_component_type_r_32f, false); outModel->meshes[(*primitiveIndex)].texcoords = ReadGLTFValuesAs(acc, cgltf_component_type_r_32f, false);
} }
else if (mesh->primitives[p].attributes[j].type == cgltf_attribute_type_joints) else if (mesh->primitives[p].attributes[j].type == cgltf_attribute_type_joints)
{ {
@ -5259,7 +5119,7 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
unsigned int totalBoneWeights = boneCount*4; unsigned int totalBoneWeights = boneCount*4;
outModel->meshes[(*primitiveIndex)].boneIds = RL_MALLOC(totalBoneWeights*sizeof(int)); outModel->meshes[(*primitiveIndex)].boneIds = RL_MALLOC(totalBoneWeights*sizeof(int));
short* bones = GLTFReadValuesAs(acc, cgltf_component_type_r_16, false); short *bones = ReadGLTFValuesAs(acc, cgltf_component_type_r_16, false);
for (unsigned int a = 0; a < totalBoneWeights; a++) for (unsigned int a = 0; a < totalBoneWeights; a++)
{ {
outModel->meshes[(*primitiveIndex)].boneIds[a] = 0; outModel->meshes[(*primitiveIndex)].boneIds[a] = 0;
@ -5280,12 +5140,12 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
else if (mesh->primitives[p].attributes[j].type == cgltf_attribute_type_weights) else if (mesh->primitives[p].attributes[j].type == cgltf_attribute_type_weights)
{ {
cgltf_accessor *acc = mesh->primitives[p].attributes[j].data; cgltf_accessor *acc = mesh->primitives[p].attributes[j].data;
outModel->meshes[(*primitiveIndex)].boneWeights = GLTFReadValuesAs(acc, cgltf_component_type_r_32f, false); outModel->meshes[(*primitiveIndex)].boneWeights = ReadGLTFValuesAs(acc, cgltf_component_type_r_32f, false);
} }
else if (mesh->primitives[p].attributes[j].type == cgltf_attribute_type_color) else if (mesh->primitives[p].attributes[j].type == cgltf_attribute_type_color)
{ {
cgltf_accessor *acc = mesh->primitives[p].attributes[j].data; cgltf_accessor *acc = mesh->primitives[p].attributes[j].data;
outModel->meshes[(*primitiveIndex)].colors = GLTFReadValuesAs(acc, cgltf_component_type_r_8u, true); outModel->meshes[(*primitiveIndex)].colors = ReadGLTFValuesAs(acc, cgltf_component_type_r_8u, true);
} }
} }
@ -5293,7 +5153,7 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
if (acc) if (acc)
{ {
outModel->meshes[(*primitiveIndex)].triangleCount = acc->count/3; outModel->meshes[(*primitiveIndex)].triangleCount = acc->count/3;
outModel->meshes[(*primitiveIndex)].indices = GLTFReadValuesAs(acc, cgltf_component_type_r_16u, false); outModel->meshes[(*primitiveIndex)].indices = ReadGLTFValuesAs(acc, cgltf_component_type_r_16u, false);
} }
else else
{ {
@ -5306,10 +5166,7 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
// Compute the offset // Compute the offset
outModel->meshMaterial[(*primitiveIndex)] = (int)(mesh->primitives[p].material - data->materials); outModel->meshMaterial[(*primitiveIndex)] = (int)(mesh->primitives[p].material - data->materials);
} }
else else outModel->meshMaterial[(*primitiveIndex)] = outModel->materialCount - 1;
{
outModel->meshMaterial[(*primitiveIndex)] = outModel->materialCount - 1;
}
BindGLTFPrimitiveToBones(outModel, data, *primitiveIndex); BindGLTFPrimitiveToBones(outModel, data, *primitiveIndex);
@ -5319,35 +5176,24 @@ void LoadGLTFMesh(cgltf_data* data, cgltf_mesh* mesh, Model* outModel, Matrix cu
void LoadGLTFNode(cgltf_data* data, cgltf_node* node, Model* outModel, Matrix currentTransform, int* primitiveIndex, const char *fileName) void LoadGLTFNode(cgltf_data* data, cgltf_node* node, Model* outModel, Matrix currentTransform, int* primitiveIndex, const char *fileName)
{ {
Matrix nodeTransform = { node->matrix[0], node->matrix[4], node->matrix[8], node->matrix[12], Matrix nodeTransform = {
node->matrix[0], node->matrix[4], node->matrix[8], node->matrix[12],
node->matrix[1], node->matrix[5], node->matrix[9], node->matrix[13], node->matrix[1], node->matrix[5], node->matrix[9], node->matrix[13],
node->matrix[2], node->matrix[6], node->matrix[10], node->matrix[14], node->matrix[2], node->matrix[6], node->matrix[10], node->matrix[14],
node->matrix[3], node->matrix[7], node->matrix[11], node->matrix[15] }; node->matrix[3], node->matrix[7], node->matrix[11], node->matrix[15] };
currentTransform = MatrixMultiply(nodeTransform, currentTransform); currentTransform = MatrixMultiply(nodeTransform, currentTransform);
if(node->mesh != NULL) if (node->mesh != NULL) LoadGLTFMesh(data, node->mesh, outModel, currentTransform, primitiveIndex, fileName);
{
LoadGLTFMesh(data, node->mesh, outModel, currentTransform, primitiveIndex, fileName);
}
for(unsigned int i = 0; i < node->children_count; i++) for (unsigned int i = 0; i < node->children_count; i++) LoadGLTFNode(data, node->children[i], outModel, currentTransform, primitiveIndex, fileName);
{
LoadGLTFNode(data, node->children[i], outModel, currentTransform, primitiveIndex, fileName);
}
} }
static void GetGLTFPrimitiveCount(cgltf_node* node, int* outCount) static void GetGLTFPrimitiveCount(cgltf_node* node, int* outCount)
{ {
if(node->mesh != NULL) if (node->mesh != NULL) *outCount += node->mesh->primitives_count;
{
*outCount += node->mesh->primitives_count;
}
for(unsigned int i = 0; i < node->children_count; i++) for (unsigned int i = 0; i < node->children_count; i++) GetGLTFPrimitiveCount(node->children[i], outCount);
{
GetGLTFPrimitiveCount(node->children[i], outCount);
}
} }
#endif #endif