aboutsummaryrefslogtreecommitdiff
path: root/src/obj.c
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2022-10-31 10:28:50 -0500
committerjvech <jmvalenciae@unal.edu.co>2022-10-31 10:28:50 -0500
commit1915909047d2a00c898106e4173c76b171368f38 (patch)
tree79d89454070a923162c667e8c4ef509993e25acd /src/obj.c
parent947ecc07b307b649f3380898923a21a303aa0b6a (diff)
fix: blank lines segmentation fault bug fixed
Diffstat (limited to 'src/obj.c')
-rw-r--r--src/obj.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/obj.c b/src/obj.c
index 200fe5c..7d65722 100644
--- a/src/obj.c
+++ b/src/obj.c
@@ -95,7 +95,7 @@ objCreate(const char *filename)
char key[500];
int vIndex, vtIndex, vnIndex;
int mtlSize, mtlIndex, meshIndex;
- vIndex = vtIndex = vnIndex = meshIndex = 0;
+ vIndex = vtIndex = vnIndex = meshIndex = mtlSize = 0;
while (fgets(lineBuffer, OBJ_LINE_MAX_SIZE, fi)) {
@@ -107,13 +107,14 @@ objCreate(const char *filename)
else if (!strcmp("v" , key)) readV3(lineBuffer + n, &v, vIndex++);
else if (!strcmp("mtllib", key)) mtl = readMtl(lineBuffer + n, filename, &mtlSize);
- else if (!strcmp("usemtl", key)) {
+ else if (!strcmp("usemtl", key) && mtlSize > 0) {
mtlIndex = useMtl (lineBuffer + n, mtl, mtlSize);
mesh = (Mesh *)realloc(mesh, (++meshIndex + 1) * sizeof(Mesh));
memset(mesh + meshIndex, 0, sizeof(Mesh));
mesh[meshIndex].material = mtl[mtlIndex];
mesh[meshIndex].indexSize = 0;
}
+ key[0] = '\0';
}
o.mesh = mesh;
@@ -350,8 +351,8 @@ Material *
readMtl(char *line, const char *objFile, int *size)
{
Material *out;
- char buffer[OBJ_LINE_MAX_SIZE], mtlFilename[OBJ_LINE_MAX_SIZE], key[500];
- char path[512], fileName[512];
+ char buffer[OBJ_LINE_MAX_SIZE], mtlFilename[OBJ_LINE_MAX_SIZE], key[OBJ_MAX_WORD];
+ char path[OBJ_MAX_WORD], fileName[OBJ_MAX_WORD];
FILE *fin;
int i, n;
@@ -372,6 +373,7 @@ readMtl(char *line, const char *objFile, int *size)
else if (!strcmp(key, "illum")) sscanf(buffer + n, "%u", &out[i - 1].illum);
else if (!strcmp(key, "Ns")) sscanf(buffer + n, "%f", &out[i - 1].ns);
}
+ key[0] = '\0';
}
if (size) *size = i;
Feel free to download, copy and edit any repo