summaryrefslogtreecommitdiffstats
path: root/obj.c
diff options
context:
space:
mode:
Diffstat (limited to 'obj.c')
-rw-r--r--obj.c58
1 files changed, 42 insertions, 16 deletions
diff --git a/obj.c b/obj.c
index e6262d3..576f89f 100644
--- a/obj.c
+++ b/obj.c
@@ -18,6 +18,8 @@
#include <memory.h>
#include "obj.h"
+char obj_last_fname[101];
+
ObjModel* ObjLoadModel(char *memory, size_t size)
{
/* if size is 0, meaning file was not loaded correctly, return null */
@@ -56,9 +58,9 @@ ObjModel* ObjLoadModel(char *memory, size_t size)
ret->NormalArray = (ObjNormal *) malloc(sizeof(ObjNormal) * ret->nNormal);
ret->TexCoordArray = (ObjTexCoord *) malloc(sizeof(ObjTexCoord) * ret->nTexCoord);
ret->FaceArray = (ObjFace *) malloc(sizeof(ObjFace) * ret->nFace);
- ret->mtllib = (char *) malloc(sizeof(char) * 21);
- ret->objectName = (char *) malloc(sizeof(char) * 21);
- ret->usemtl = (char *) malloc(sizeof(char) * 21);
+ ret->mtllib = (char *) malloc(sizeof(char) * 31);
+ ret->objectName = (char *) malloc(sizeof(char) * 31);
+ ret->usemtl = (char *) malloc(sizeof(char) * 31);
*ret->mtllib = '\0';
*ret->objectName = '\0';
@@ -72,12 +74,6 @@ ObjModel* ObjLoadModel(char *memory, size_t size)
exit(-1);
}
-#if 0 /* already done by memset */
- memset(ret->mtllib, 0, 21);
- memset(ret->objectName, 0, 21);
- memset(ret->usemtl, 0, 21);
-#endif
-
p = memory;
unsigned int nV = 0, nN = 0, nT = 0, nF = 0;
@@ -87,7 +83,7 @@ ObjModel* ObjLoadModel(char *memory, size_t size)
{
/* check for mtl file */
if (memcmp(p, "mtllib", 6) == 0)
- sscanf(p, "mtllib %s", ret->mtllib);
+ sscanf(p, "mtllib %s", ret->mtllib); /* valgrind reports this as false positive */
/* check if mtl file will be used */
if (memcmp(p, "usemtl", 6) == 0)
@@ -115,6 +111,7 @@ ObjModel* ObjLoadModel(char *memory, size_t size)
if (nread != 2)
printf("vt: read only %d instead of 2\n", nread);
+ ret->TexCoordArray[nT].w = 0.0f;
nT++;
}
/* parse a vertex */
@@ -126,6 +123,7 @@ ObjModel* ObjLoadModel(char *memory, size_t size)
if (nread != 3)
printf("v: read only %d instead of 3\n", nread);
+ ret->VertexArray[nV].w = 0.0f;
nV++;
}
/* quad */
@@ -195,8 +193,8 @@ ObjModel* ObjLoadModel(char *memory, size_t size)
memset(mtl, 0, sizeof(ObjMtl));
/* allocate space for dynamic members */
- mtl->map_Ka = (char *) malloc(sizeof(char) * 21);
- mtl->map_Kd = (char *) malloc(sizeof(char) * 21);
+ mtl->map_Ka = (char *) malloc(sizeof(char) * 31);
+ mtl->map_Kd = (char *) malloc(sizeof(char) * 31);
if (!mtl->map_Ka || !mtl->map_Kd)
{
fprintf(stderr, "ObjLoadModel() failed, %s:%d\n", __FILE__, __LINE__);
@@ -258,6 +256,9 @@ ObjModel* ObjLoadModel(char *memory, size_t size)
}
/* set the mtl */
ret->mtl = mtl;
+
+ /* free the opened mtl file */
+ free(mtl_mem);
}
else
ret->mtl = NULL; /* should already be NULL but what the hell */
@@ -266,6 +267,9 @@ ObjModel* ObjLoadModel(char *memory, size_t size)
free(fname);
}
+ /* free the opened file */
+ free(memory);
+
return ret;
}
@@ -282,13 +286,21 @@ size_t ObjLoadFile(char *szFileName, char **memory)
fseek(file, 0, SEEK_SET);
*memory = (char *) malloc(sizeof(char) * end);
+ if (!memory)
+ {
+ fprintf(stderr, "ObjLoadFile() failed, %s:%d\n", __FILE__, __LINE__);
+ perror("malloc");
+ exit(-1);
+ }
+
bytes = fread(*memory, sizeof(char), end, file);
fclose(file);
}
else
{
- fprintf(stderr, "ObjLoadModel() failed, %s:%d\n", __FILE__, __LINE__);
+ fprintf(stderr, "ObjLoadModel(): failed to open \"%s\", %s:%d\n",
+ szFileName, __FILE__, __LINE__);
perror("fopen");
}
@@ -332,16 +344,30 @@ void ObjList(ObjModel *model)
}
}
-char *ObjGetPath(char *fname)
+char *ObjGetPath(const char *fname)
{
- if (obj_last_fname == NULL)
+ if (fname == NULL || obj_last_fname == NULL)
return NULL;
char *path = (char *) malloc(sizeof(char) * 101);
+ if (!path)
+ {
+ fprintf(stderr, "ObjGetPath() failed, %s:%d\n", __FILE__, __LINE__);
+ perror("malloc");
+ exit(-1);
+ }
+
char *delimeter = strrchr(obj_last_fname, '/');
unsigned int offset = delimeter - obj_last_fname;
+
+#if 0
+ fprintf(stdout, "ObjGetPath(): fname = \"%s\", delim = \"%s\", offset = %u"
+ ", obj_last_fname = \"%s\"\n", fname, delimeter, offset, obj_last_fname);
+#endif
+
strncpy(path, obj_last_fname, offset + 1);
- strcat(path, fname);
+ strcpy(path + offset + 1, fname);
+ /* strcat(path, fname); */
return path;
}