summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2010-10-16 15:17:08 -0500
committerKamil Kaminski <kamilkss@gmail.com>2010-10-16 15:17:08 -0500
commit663a667df86e36acaade5070305692f3c9199879 (patch)
treecab2230d0bf4b142969a409128e610163ca4b486
parent9e4401021950b72bbdf2e1db67ad393966bbd3df (diff)
downloadOBJLoader-663a667df86e36acaade5070305692f3c9199879.tar.gz
OBJLoader-663a667df86e36acaade5070305692f3c9199879.tar.bz2
OBJLoader-663a667df86e36acaade5070305692f3c9199879.zip
Add more error checking
-rw-r--r--obj.c10
-rw-r--r--objloader.c43
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,5 +1,8 @@
/* 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);