From 663a667df86e36acaade5070305692f3c9199879 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sat, 16 Oct 2010 15:17:08 -0500 Subject: Add more error checking --- obj.c | 10 ++++++++++ objloader.c | 43 +++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/obj.c b/obj.c index 3b834e5..20f66ca 100644 --- a/obj.c +++ b/obj.c @@ -1,4 +1,7 @@ /* obj.c + * + * Kamil Kaminski + * kamilkss@gmail.com * * OBJ Loader * initial code: http://www.gamedev.net/community/forums/topic.asp?topic_id=312335 @@ -21,6 +24,10 @@ ObjModel* ObjLoadModel(char *memory, size_t size) /* initialize to zero */ memset(ret, 0, sizeof(ObjModel)); + /* if size is 0, meaning file was not loaded correctly, return null */ + if (!size) + return ret; + p = memory; e = memory + size; @@ -228,6 +235,9 @@ size_t ObjLoadFile(char *szFileName, char **memory) fclose(file); } + else + perror("fopen"); + return bytes; } diff --git a/objloader.c b/objloader.c index 1f5eddd..1b2ba67 100644 --- a/objloader.c +++ b/objloader.c @@ -81,26 +81,28 @@ static void setup_opengl(ObjModel *model) /* get the texture filename from obj */ char *fname = ObjGetPath(model->mtl->map_Ka); /* ambient texture map */ if (model->mtl != NULL) + { printf("ambient texture map: %s\n", model->mtl->map_Ka); - /* load texture */ - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - pBytes = gltLoadTGA(fname, &iWidth, &iHeight, &iComponents, &eFormat); - if (!pBytes) - fprintf(stderr, "gltLoadTGA: failed to load texture!\n"); - - /* load texture image */ - glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, - GL_UNSIGNED_BYTE, pBytes); - free(pBytes); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glEnable(GL_TEXTURE_2D); + /* load texture */ + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + pBytes = gltLoadTGA(fname, &iWidth, &iHeight, &iComponents, &eFormat); + if (!pBytes) + fprintf(stderr, "gltLoadTGA: failed to load texture!\n"); + + /* load texture image */ + glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, + GL_UNSIGNED_BYTE, pBytes); + free(pBytes); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + glEnable(GL_TEXTURE_2D); + } } /* process SDL events */ @@ -223,6 +225,11 @@ int main(void) char *memory = NULL; size_t bytes = ObjLoadFile("./cube/cube.obj", &memory); ObjModel *model = ObjLoadModel(memory, bytes); + if (!bytes || model == NULL) + { + fprintf(stderr, "obj file could not be loaded!\n"); + exit(-1); + } /* setup few stuff before rendring */ setup_opengl(model); -- cgit v1.2.3