Support mtl loading by tinyobj_parse_obj() #1134
I don't like this solution but well... let's use it for the moment...
This commit is contained in:
parent
5fe0db347c
commit
d657537821
1 changed files with 19 additions and 21 deletions
40
src/models.c
40
src/models.c
|
@ -50,6 +50,14 @@
|
||||||
#include <string.h> // Required for: strncmp() [Used in LoadModelAnimations()], strlen() [Used in LoadTextureFromCgltfImage()]
|
#include <string.h> // Required for: strncmp() [Used in LoadModelAnimations()], strlen() [Used in LoadTextureFromCgltfImage()]
|
||||||
#include <math.h> // Required for: sinf(), cosf(), sqrtf(), fabsf()
|
#include <math.h> // Required for: sinf(), cosf(), sqrtf(), fabsf()
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
#include <direct.h> // Required for: _chdir() [Used in LoadOBJ()]
|
||||||
|
#define CHDIR _chdir
|
||||||
|
#else
|
||||||
|
#include <unistd.h> // Required for: chdir() (POSIX) [Used in LoadOBJ()]
|
||||||
|
#define CHDIR chdir
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2
|
#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2
|
||||||
|
|
||||||
#if defined(SUPPORT_FILEFORMAT_OBJ) || defined(SUPPORT_FILEFORMAT_MTL)
|
#if defined(SUPPORT_FILEFORMAT_OBJ) || defined(SUPPORT_FILEFORMAT_MTL)
|
||||||
|
@ -2832,29 +2840,17 @@ static Model LoadOBJ(const char *fileName)
|
||||||
tinyobj_material_t *materials = NULL;
|
tinyobj_material_t *materials = NULL;
|
||||||
unsigned int materialCount = 0;
|
unsigned int materialCount = 0;
|
||||||
|
|
||||||
int dataLength = 0;
|
char *fileData = LoadFileText(fileName);
|
||||||
char *data = NULL;
|
int dataSize = strlen(fileData);
|
||||||
|
|
||||||
// Load model data
|
if (fileData != NULL)
|
||||||
FILE *objFile = fopen(fileName, "rb");
|
|
||||||
|
|
||||||
if (objFile != NULL)
|
|
||||||
{
|
{
|
||||||
fseek(objFile, 0, SEEK_END);
|
char currentDir[1024] = { 0 };
|
||||||
long length = ftell(objFile); // Get file size
|
strcpy(currentDir, GetWorkingDirectory());
|
||||||
fseek(objFile, 0, SEEK_SET); // Reset file pointer
|
chdir(GetDirectoryPath(fileName));
|
||||||
|
|
||||||
data = (char *)RL_MALLOC(length);
|
|
||||||
|
|
||||||
fread(data, length, 1, objFile);
|
|
||||||
dataLength = length;
|
|
||||||
fclose(objFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data != NULL)
|
|
||||||
{
|
|
||||||
unsigned int flags = TINYOBJ_FLAG_TRIANGULATE;
|
unsigned int flags = TINYOBJ_FLAG_TRIANGULATE;
|
||||||
int ret = tinyobj_parse_obj(&attrib, &meshes, &meshCount, &materials, &materialCount, data, dataLength, flags);
|
int ret = tinyobj_parse_obj(&attrib, &meshes, &meshCount, &materials, &materialCount, fileData, dataSize, flags);
|
||||||
|
|
||||||
if (ret != TINYOBJ_SUCCESS) TRACELOG(LOG_WARNING, "[%s] Model data could not be loaded", fileName);
|
if (ret != TINYOBJ_SUCCESS) TRACELOG(LOG_WARNING, "[%s] Model data could not be loaded", fileName);
|
||||||
else TRACELOG(LOG_INFO, "[%s] Model data loaded successfully: %i meshes / %i materials", fileName, meshCount, materialCount);
|
else TRACELOG(LOG_INFO, "[%s] Model data loaded successfully: %i meshes / %i materials", fileName, meshCount, materialCount);
|
||||||
|
@ -2995,7 +2991,9 @@ static Model LoadOBJ(const char *fileName)
|
||||||
tinyobj_shapes_free(meshes, meshCount);
|
tinyobj_shapes_free(meshes, meshCount);
|
||||||
tinyobj_materials_free(materials, materialCount);
|
tinyobj_materials_free(materials, materialCount);
|
||||||
|
|
||||||
RL_FREE(data);
|
RL_FREE(fileData);
|
||||||
|
|
||||||
|
chdir(currentDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: At this point we have all model data loaded
|
// NOTE: At this point we have all model data loaded
|
||||||
|
@ -3122,7 +3120,7 @@ static Model LoadIQM(const char *fileName)
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
fread(&iqm,sizeof(IQMHeader), 1, iqmFile); // Read IQM header
|
fread(&iqm, sizeof(IQMHeader), 1, iqmFile); // Read IQM header
|
||||||
|
|
||||||
if (strncmp(iqm.magic, IQM_MAGIC, sizeof(IQM_MAGIC)))
|
if (strncmp(iqm.magic, IQM_MAGIC, sizeof(IQM_MAGIC)))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue