From 1915909047d2a00c898106e4173c76b171368f38 Mon Sep 17 00:00:00 2001 From: jvech Date: Mon, 31 Oct 2022 10:28:50 -0500 Subject: fix: blank lines segmentation fault bug fixed --- src/main.c | 10 +++++++++- src/obj.c | 10 ++++++---- src/obj.h | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index e6d8584..636172e 100644 --- a/src/main.c +++ b/src/main.c @@ -306,14 +306,22 @@ int main(int argc, char *argv[]) Mat4 T, S, R; float t, t0, dt; int width, height; + char title[1024]; t0 = 0; glEnable(GL_DEPTH_TEST); while (!glfwWindowShouldClose(window)) { processInput(window); - glClearColor(0.5f, 0.5f, 0.5f, 1.0f); + glClearColor(0.7f, 0.7f, 0.7f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwGetWindowSize(window, &width, &height); + sprintf(title, "mverse: x: %f y: %f z: %f", + mainCamera.front.vector[0] + mainCamera.position.vector[0], + mainCamera.front.vector[1] + mainCamera.position.vector[1], + mainCamera.front.vector[2] + mainCamera.position.vector[2]); + glfwSetWindowTitle(window, title); + + t = (float)glfwGetTime(); dt = t - t0; 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; diff --git a/src/obj.h b/src/obj.h index f15f0c8..44790a2 100644 --- a/src/obj.h +++ b/src/obj.h @@ -20,7 +20,7 @@ #define __OBJ__ #define OBJ_LINE_MAX_SIZE 1024 -#define OBJ_MAX_NAME 512 +#define OBJ_MAX_WORD 512 typedef struct { float position[3]; -- cgit v1.2.3-70-g09d2