REVIEWED: Vox loading, mostly formating

This commit is contained in:
raysan5 2021-09-05 20:39:34 +02:00
parent dfc465ca6d
commit 4120f12375
2 changed files with 31 additions and 40 deletions

View file

@ -352,8 +352,6 @@ void Vox_AllocArray(VoxArray3D* voxarray, int _sx, int _sy, int _sz)
voxarray->m_arrayChunks[i].m_array = 0; voxarray->m_arrayChunks[i].m_array = 0;
voxarray->m_arrayChunks[i].arraySize = 0; voxarray->m_arrayChunks[i].arraySize = 0;
} }
return voxarray;
} }
// Set voxel ID from its position into VoxArray3D // Set voxel ID from its position into VoxArray3D
@ -578,7 +576,7 @@ int Vox_LoadFileName(const char* pszfileName, VoxArray3D* voxarray)
unsigned long signature; unsigned long signature;
unsigned long readed = 0; unsigned int readed = 0;
unsigned char* fileData; unsigned char* fileData;
fileData = LoadFileData(pszfileName, &readed); fileData = LoadFileData(pszfileName, &readed);
if (fileData == 0) if (fileData == 0)
@ -589,7 +587,7 @@ int Vox_LoadFileName(const char* pszfileName, VoxArray3D* voxarray)
unsigned char* fileDataPtr = fileData; unsigned char* fileDataPtr = fileData;
unsigned char* endfileDataPtr = fileData + readed; unsigned char* endfileDataPtr = fileData + readed;
signature = *((unsigned long*)fileDataPtr); signature = *((unsigned long *)fileDataPtr);
fileDataPtr += sizeof(unsigned long); fileDataPtr += sizeof(unsigned long);
if (signature != 0x20584F56) //56 4F 58 20 if (signature != 0x20584F56) //56 4F 58 20
@ -640,7 +638,6 @@ int Vox_LoadFileName(const char* pszfileName, VoxArray3D* voxarray)
unsigned long chunkTotalChildSize = *((unsigned long*)fileDataPtr); unsigned long chunkTotalChildSize = *((unsigned long*)fileDataPtr);
fileDataPtr += sizeof(unsigned long); fileDataPtr += sizeof(unsigned long);
if (strcmp(szChunkName, "SIZE") == 0) if (strcmp(szChunkName, "SIZE") == 0)
{ {
//(4 bytes x 3 : x, y, z ) //(4 bytes x 3 : x, y, z )

View file

@ -74,8 +74,10 @@
#endif #endif
#if defined(SUPPORT_FILEFORMAT_VOX) #if defined(SUPPORT_FILEFORMAT_VOX)
// TODO: Support custom memory allocators
#define VOX_LOADER_IMPLEMENTATION #define VOX_LOADER_IMPLEMENTATION
#include "external/vox_loader.h" // vox file format loading #include "external/vox_loader.h" // vox file format loading (MagikaVoxel)
#endif #endif
#if defined(SUPPORT_MESH_GENERATION) #if defined(SUPPORT_MESH_GENERATION)
@ -139,7 +141,7 @@ static bool ReadGLTFValue(cgltf_accessor *acc, unsigned int index, void *variabl
static void *ReadGLTFValuesAs(cgltf_accessor *acc, cgltf_component_type type, bool adjustOnDownCasting); static void *ReadGLTFValuesAs(cgltf_accessor *acc, cgltf_component_type type, bool adjustOnDownCasting);
#endif #endif
#if defined(SUPPORT_FILEFORMAT_VOX) #if defined(SUPPORT_FILEFORMAT_VOX)
static Model LoadVOX(const char* filename); //Load VOX mesh data static Model LoadVOX(const char *filename); // Load VOX mesh data
#endif #endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -3562,7 +3564,7 @@ static Model LoadOBJ(const char *fileName)
for (int fi = 0; fi< attrib.num_faces; fi++) for (int fi = 0; fi< attrib.num_faces; fi++)
{ {
tinyobj_vertex_index_t face = attrib.faces[fi]; //tinyobj_vertex_index_t face = attrib.faces[fi];
int idx = attrib.material_ids[fi]; int idx = attrib.material_ids[fi];
matFaces[idx]++; matFaces[idx]++;
} }
@ -5484,17 +5486,14 @@ static void GetGLTFPrimitiveCount(cgltf_node *node, int *outCount)
#endif #endif
#if defined(SUPPORT_FILEFORMAT_VOX) #if defined(SUPPORT_FILEFORMAT_VOX)
// Load OBJ mesh data // Load VOX (MagikaVoxel) mesh data
static Model LoadVOX(const char* fileName) static Model LoadVOX(const char *fileName)
{ {
Model model = { 0 }; Model model = { 0 };
int nbvertices = 0; int nbvertices = 0;
int meshescount = 0; int meshescount = 0;
////////////////////////////////// VoxArray3D voxarray = { 0 };
// Load MagicaVoxel fileformat
VoxArray3D voxarray;
int ret = Vox_LoadFileName(fileName, &voxarray); int ret = Vox_LoadFileName(fileName, &voxarray);
if (ret != VOX_SUCCESS) if (ret != VOX_SUCCESS)
@ -5506,74 +5505,69 @@ static Model LoadVOX(const char* fileName)
{ {
// Compute meshes count // Compute meshes count
nbvertices = voxarray.vertices.used; nbvertices = voxarray.vertices.used;
meshescount = 1 + (nbvertices / 65536); meshescount = 1 + (nbvertices/65536);
TRACELOG(LOG_INFO, "MODEL: [%s] VOX data loaded successfully : %i vertices/%i meshes", fileName, nbvertices, meshescount); TRACELOG(LOG_INFO, "MODEL: [%s] VOX data loaded successfully : %i vertices/%i meshes", fileName, nbvertices, meshescount);
} }
////////////////////////////////// // Build models from meshes
// Build model
// Build Models from meshes
model.transform = MatrixIdentity(); model.transform = MatrixIdentity();
model.meshCount = meshescount; model.meshCount = meshescount;
model.meshes = (Mesh*)MemAlloc(model.meshCount * sizeof(Mesh)); model.meshes = (Mesh *)MemAlloc(model.meshCount*sizeof(Mesh));
model.meshMaterial = (int*)MemAlloc(model.meshCount * sizeof(int)); model.meshMaterial = (int *)MemAlloc(model.meshCount*sizeof(int));
model.materialCount = 1; model.materialCount = 1;
model.materials = (Material*)MemAlloc(model.materialCount * sizeof(Material)); model.materials = (Material *)MemAlloc(model.materialCount*sizeof(Material));
model.materials[0] = LoadMaterialDefault(); model.materials[0] = LoadMaterialDefault();
// Init model meshes
// Init model's meshes
int verticesRemain = voxarray.vertices.used; int verticesRemain = voxarray.vertices.used;
int verticesMax = 65532; //5461 voxels x 12 vertices per voxel -> 65532 (must be inf 65536) int verticesMax = 65532; // 5461 voxels x 12 vertices per voxel -> 65532 (must be inf 65536)
Vector3* pvertices = voxarray.vertices.array; //6*4=12 vertices per voxel Vector3 *pvertices = voxarray.vertices.array; // 6*4 = 12 vertices per voxel
Color* pcolors = voxarray.colors.array; Color *pcolors = voxarray.colors.array;
unsigned short* pindices = voxarray.indices.array; //5461 * 6 * 6 -> 196596 indices max per mesh unsigned short *pindices = voxarray.indices.array; // 5461*6*6 = 196596 indices max per mesh
int size; int size = 0;
for (int idxMesh = 0; idxMesh < meshescount; idxMesh++) for (int idxMesh = 0; idxMesh < meshescount; idxMesh++)
{ {
Mesh* pmesh = &model.meshes[idxMesh]; Mesh *pmesh = &model.meshes[idxMesh];
memset(pmesh, 0, sizeof(Mesh)); memset(pmesh, 0, sizeof(Mesh));
// Copy Vertices // Copy vertices
pmesh->vertexCount = (int)fmin(verticesMax, verticesRemain); pmesh->vertexCount = (int)fmin(verticesMax, verticesRemain);
size = pmesh->vertexCount * sizeof(float) * 3; size = pmesh->vertexCount*sizeof(float)*3;
pmesh->vertices = MemAlloc(size); pmesh->vertices = MemAlloc(size);
memcpy(pmesh->vertices, pvertices, size); memcpy(pmesh->vertices, pvertices, size);
//Copy Indices TODO compute globals indices array // Copy indices
// TODO: compute globals indices array
size = voxarray.indices.used * sizeof(unsigned short); size = voxarray.indices.used * sizeof(unsigned short);
pmesh->indices = MemAlloc(size); pmesh->indices = MemAlloc(size);
memcpy(pmesh->indices, pindices, size); memcpy(pmesh->indices, pindices, size);
pmesh->triangleCount = (pmesh->vertexCount / 4) * 2; pmesh->triangleCount = (pmesh->vertexCount/4)*2;
// Copy Colors // Copy colors
size = pmesh->vertexCount * sizeof(Color); size = pmesh->vertexCount*sizeof(Color);
pmesh->colors = MemAlloc(size); pmesh->colors = MemAlloc(size);
memcpy(pmesh->colors, pcolors, size); memcpy(pmesh->colors, pcolors, size);
// First material index // First material index
model.meshMaterial[idxMesh] = 0; model.meshMaterial[idxMesh] = 0;
// Build GPU mesh // Upload mesh data to GPU
UploadMesh(pmesh, false); UploadMesh(pmesh, false);
//Next
verticesRemain -= verticesMax; verticesRemain -= verticesMax;
pvertices += verticesMax; pvertices += verticesMax;
pcolors += verticesMax; pcolors += verticesMax;
} }
//Free arrays
Vox_FreeArrays(&voxarray); Vox_FreeArrays(&voxarray);
return model; return model;