diff --git a/src/Makefile b/src/Makefile index 3a90dd92a..a56ab1d38 100644 --- a/src/Makefile +++ b/src/Makefile @@ -186,16 +186,16 @@ ifeq ($(PLATFORM),PLATFORM_ANDROID) ANDROID_SYSROOT ?= $(ANDROID_TOOLCHAIN)/sysroot ifeq ($(ANDROID_ARCH),arm) - ANDROID_ARCH_NAME = armeabi-v7a + ANDROID_COMPILER_ARCH = armv7a endif ifeq ($(ANDROID_ARCH),arm64) - ANDROID_ARCH_NAME = arm64-v8a + ANDROID_COMPILER_ARCH = aarch64 endif ifeq ($(ANDROID_ARCH),x86) - ANDROID_ARCH_NAME = i686 + ANDROID_COMPILER_ARCH = i686 endif ifeq ($(ANDROID_ARCH),x86_64) - ANDROID_ARCH_NAME = x86_64 + ANDROID_COMPILER_ARCH = x86_64 endif endif @@ -254,22 +254,9 @@ ifeq ($(PLATFORM),PLATFORM_WEB) endif ifeq ($(PLATFORM),PLATFORM_ANDROID) # Android toolchain (must be provided for desired architecture and compiler) - ifeq ($(ANDROID_ARCH),arm) - CC = $(ANDROID_TOOLCHAIN)/bin/armv7a-linux-androideabi$(ANDROID_API_VERSION)-clang - AR = $(ANDROID_TOOLCHAIN)/bin/arm-linux-androideabi-ar - endif - ifeq ($(ANDROID_ARCH),arm64) - CC = $(ANDROID_TOOLCHAIN)/bin/aarch64-linux-android$(ANDROID_API_VERSION)-clang - AR = $(ANDROID_TOOLCHAIN)/bin/aarch64-linux-android-ar - endif - ifeq ($(ANDROID_ARCH),x86) - CC = $(ANDROID_TOOLCHAIN)/bin/i686-linux-android$(ANDROID_API_VERSION)-clang - AR = $(ANDROID_TOOLCHAIN)/bin/i686-linux-android-ar - endif - ifeq ($(ANDROID_ARCH),x86_64) - CC = $(ANDROID_TOOLCHAIN)/bin/x86_64-linux-android$(ANDROID_API_VERSION)-clang - AR = $(ANDROID_TOOLCHAIN)/bin/x86_64-linux-android-ar - endif + CC = $(ANDROID_TOOLCHAIN)/bin/$(ANDROID_COMPILER_ARCH)-linux-androideabi$(ANDROID_API_VERSION)-clang + # It seems from Android NDK r22 onwards we need to use llvm-ar + AR = $(ANDROID_TOOLCHAIN)/bin/llvm-ar endif # Define compiler flags: @@ -344,10 +331,10 @@ ifeq ($(PLATFORM),PLATFORM_ANDROID) ifeq ($(ANDROID_ARCH),arm64) CFLAGS += -target aarch64 -mfix-cortex-a53-835769 endif - ifeq ($(ANDROID_ARCH), x86) + ifeq ($(ANDROID_ARCH),x86) CFLAGS += -march=i686 endif - ifeq ($(ANDROID_ARCH), x86_64) + ifeq ($(ANDROID_ARCH),x86_64) CFLAGS += -march=x86-64 endif # Compilation functions attributes options @@ -509,7 +496,7 @@ else endif ifeq ($(PLATFORM_OS),OSX) $(CC) -dynamiclib -o $(RAYLIB_RELEASE_PATH)/lib$(RAYLIB_LIB_NAME).$(RAYLIB_VERSION).dylib $(OBJS) $(LDFLAGS) -compatibility_version $(RAYLIB_API_VERSION) -current_version $(RAYLIB_VERSION) -framework OpenGL -framework Cocoa -framework IOKit -framework CoreAudio -framework CoreVideo - install_name_tool -id "lib$(RAYLIB_LIB_NAME).$(VERSION).dylib" $(RAYLIB_RELEASE_PATH)/lib$(RAYLIB_LIB_NAME).$(RAYLIB_VERSION).dylib + install_name_tool -id "@rpath/lib$(RAYLIB_LIB_NAME).$(RAYLIB_API_VERSION).dylib" $(RAYLIB_RELEASE_PATH)/lib$(RAYLIB_LIB_NAME).$(RAYLIB_VERSION).dylib @echo "raylib shared library generated (lib$(RAYLIB_LIB_NAME).$(RAYLIB_VERSION).dylib)!" cd $(RAYLIB_RELEASE_PATH) && ln -fs lib$(RAYLIB_LIB_NAME).$(RAYLIB_VERSION).dylib lib$(RAYLIB_LIB_NAME).$(RAYLIB_API_VERSION).dylib cd $(RAYLIB_RELEASE_PATH) && ln -fs lib$(RAYLIB_LIB_NAME).$(RAYLIB_VERSION).dylib lib$(RAYLIB_LIB_NAME).dylib diff --git a/src/external/vox_loader.h b/src/external/vox_loader.h index 63f2719f5..2650b7129 100644 --- a/src/external/vox_loader.h +++ b/src/external/vox_loader.h @@ -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].arraySize = 0; } - - return voxarray; } // 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 readed = 0; + unsigned int readed = 0; unsigned char* fileData; fileData = LoadFileData(pszfileName, &readed); if (fileData == 0) @@ -589,7 +587,7 @@ int Vox_LoadFileName(const char* pszfileName, VoxArray3D* voxarray) unsigned char* fileDataPtr = fileData; unsigned char* endfileDataPtr = fileData + readed; - signature = *((unsigned long*)fileDataPtr); + signature = *((unsigned long *)fileDataPtr); fileDataPtr += sizeof(unsigned long); 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); fileDataPtr += sizeof(unsigned long); - if (strcmp(szChunkName, "SIZE") == 0) { //(4 bytes x 3 : x, y, z ) diff --git a/src/models.c b/src/models.c index 7a455be03..f0b31347e 100644 --- a/src/models.c +++ b/src/models.c @@ -74,8 +74,10 @@ #endif #if defined(SUPPORT_FILEFORMAT_VOX) + // TODO: Support custom memory allocators + #define VOX_LOADER_IMPLEMENTATION - #include "external/vox_loader.h" // vox file format loading + #include "external/vox_loader.h" // vox file format loading (MagikaVoxel) #endif #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); #endif #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 #if defined(SUPPORT_FILEFORMAT_VOX) static Model LoadVOX(const char* filename); //Load VOX mesh data @@ -2422,7 +2424,7 @@ Mesh GenMeshHeightmap(Image heightmap, Vector3 size) int vCounter = 0; // Used to count vertices float by float int tcCounter = 0; // Used to count texcoords float by float int nCounter = 0; // Used to count normals float by float - + int trisCounter = 0; Vector3 scaleFactor = { size.x/mapX, size.y/255.0f, size.z/mapZ }; @@ -3565,7 +3567,7 @@ static Model LoadOBJ(const char *fileName) 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]; matFaces[idx]++; } @@ -5462,18 +5464,52 @@ 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) +static Matrix GetNodeTransformationMatrix(cgltf_node *node, Matrix current) { - Matrix nodeTransform = { + if (node->has_matrix) + { + 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[2], node->matrix[6], node->matrix[10], node->matrix[14], node->matrix[3], node->matrix[7], node->matrix[11], node->matrix[15] }; + current= MatrixMultiply(nodeTransform, current); + } + if (node->has_translation) + { + Matrix tl = MatrixTranslate(node->translation[0],node->translation[1],node->translation[2]); + current = MatrixMultiply(tl, current); + } + if (node->has_rotation) + { + Matrix rot = QuaternionToMatrix((Quaternion){node->rotation[0],node->rotation[1],node->rotation[2],node->rotation[3]}); + current = MatrixMultiply(rot, current); + } + if (node->has_scale) + { + Matrix scale = MatrixScale(node->scale[0],node->scale[1],node->scale[2]); + current = MatrixMultiply(scale, current); + } + return current; +} - currentTransform = MatrixMultiply(nodeTransform, currentTransform); - - if (node->mesh != NULL) LoadGLTFMesh(data, node->mesh, outModel, currentTransform, primitiveIndex, fileName); - +void LoadGLTFNode(cgltf_data *data, cgltf_node *node, Model *outModel, Matrix currentTransform, int *primitiveIndex, const char *fileName) +{ + // Apply the transforms if they exist (Will still be applied even if no mesh is present to support emptys and bone structures) + Matrix localTransform = GetNodeTransformationMatrix(node, MatrixIdentity()); + currentTransform = MatrixMultiply(localTransform, currentTransform); + // Load mesh if it exists + if (node->mesh != NULL) + { + // Check if skinning is enabled and load Mesh accordingly + Matrix vertexTransform = currentTransform; + if((node->skin != NULL) && (node->parent != NULL)) + { + vertexTransform = localTransform; + TRACELOG(LOG_WARNING,"MODEL: GLTF Node %s is skinned but not root node! Parent transformations will be ignored (NODE_SKINNED_MESH_NON_ROOT)",node->name); + } + LoadGLTFMesh(data, node->mesh, outModel, vertexTransform, primitiveIndex, fileName); + } for (unsigned int i = 0; i < node->children_count; i++) LoadGLTFNode(data, node->children[i], outModel, currentTransform, primitiveIndex, fileName); } @@ -5487,17 +5523,14 @@ static void GetGLTFPrimitiveCount(cgltf_node *node, int *outCount) #endif #if defined(SUPPORT_FILEFORMAT_VOX) -// Load OBJ mesh data -static Model LoadVOX(const char* fileName) +// Load VOX (MagikaVoxel) mesh data +static Model LoadVOX(const char *fileName) { Model model = { 0 }; int nbvertices = 0; int meshescount = 0; - - ////////////////////////////////// - // Load MagicaVoxel fileformat - VoxArray3D voxarray; + VoxArray3D voxarray = { 0 }; int ret = Vox_LoadFileName(fileName, &voxarray); if (ret != VOX_SUCCESS) @@ -5509,74 +5542,69 @@ static Model LoadVOX(const char* fileName) { // Compute meshes count 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); } - ////////////////////////////////// - // Build model - - // Build Models from meshes + // Build models from meshes model.transform = MatrixIdentity(); 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.materials = (Material*)MemAlloc(model.materialCount * sizeof(Material)); + model.materials = (Material *)MemAlloc(model.materialCount*sizeof(Material)); model.materials[0] = LoadMaterialDefault(); - - // Init model's meshes + // Init model meshes 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 - Color* pcolors = voxarray.colors.array; - unsigned short* pindices = voxarray.indices.array; //5461 * 6 * 6 -> 196596 indices max per mesh + Vector3 *pvertices = voxarray.vertices.array; // 6*4 = 12 vertices per voxel + Color *pcolors = voxarray.colors.array; + 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++) { - Mesh* pmesh = &model.meshes[idxMesh]; + Mesh *pmesh = &model.meshes[idxMesh]; memset(pmesh, 0, sizeof(Mesh)); - // Copy Vertices + // Copy vertices pmesh->vertexCount = (int)fmin(verticesMax, verticesRemain); - size = pmesh->vertexCount * sizeof(float) * 3; + size = pmesh->vertexCount*sizeof(float)*3; pmesh->vertices = MemAlloc(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); pmesh->indices = MemAlloc(size); memcpy(pmesh->indices, pindices, size); - pmesh->triangleCount = (pmesh->vertexCount / 4) * 2; + pmesh->triangleCount = (pmesh->vertexCount/4)*2; - // Copy Colors - size = pmesh->vertexCount * sizeof(Color); + // Copy colors + size = pmesh->vertexCount*sizeof(Color); pmesh->colors = MemAlloc(size); memcpy(pmesh->colors, pcolors, size); // First material index model.meshMaterial[idxMesh] = 0; - // Build GPU mesh + // Upload mesh data to GPU UploadMesh(pmesh, false); - //Next verticesRemain -= verticesMax; pvertices += verticesMax; pcolors += verticesMax; } - //Free arrays Vox_FreeArrays(&voxarray); return model;