diff options
-rw-r--r-- | obj.c | 58 | ||||
-rw-r--r-- | obj.h | 29 | ||||
-rw-r--r-- | objloader.c | 10 |
3 files changed, 67 insertions, 30 deletions
@@ -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; } @@ -3,15 +3,20 @@ extern char obj_last_fname[101]; +#pragma pack(1) typedef struct { - float x, y, z; + float x, y, z, w; } ObjVertex; -typedef ObjVertex ObjNormal; typedef struct { - float u, v; + float x, y, z; +} ObjNormal; + +typedef struct +{ + float u, v, w; } ObjTexCoord; typedef struct @@ -23,23 +28,22 @@ typedef struct typedef struct { - float Ns; - float Ni; - float d; - float Tr; - int illum; + char *map_Ka; + char *map_Kd; ObjVertex Tf; ObjVertex Ka; ObjVertex Kd; ObjVertex Ks; ObjVertex Ke; - char *map_Ka; - char *map_Kd; + float Ns; + float Ni; + float d; + float Tr; + int illum; } ObjMtl; typedef struct { - char *mtllib; char *objectName; char *groupName; char *usemtl; @@ -50,6 +54,7 @@ typedef struct ObjTexCoord *TexCoordArray; ObjFace *FaceArray; unsigned nVertex, nNormal, nTexCoord, nFace; + char *mtllib; } ObjModel; /* function prototypes */ @@ -61,7 +66,7 @@ void ObjList(ObjModel *); /* once we load an .obj file, we can extract path up to last '/' to load a * mtl or texture that resides in the same folder */ -char *ObjGetPath(char *); +char *ObjGetPath(const char *); void ObjPutFaceGLCmd(const ObjModel *, const unsigned); void ObjFree(ObjModel *); diff --git a/objloader.c b/objloader.c index 0e6aba9..699efdd 100644 --- a/objloader.c +++ b/objloader.c @@ -29,7 +29,6 @@ static void render(ObjModel *); /* global */ int program_running = 1; -char obj_last_fname[101]; static void resize(int w, int h) { @@ -180,6 +179,12 @@ static void render(ObjModel *model) int main(int argc, char **argv) { + if (argc != 2) + { + fprintf(stderr, "da fuck think you're doing?\n"); + exit(-1); + } + SDL_Surface *screen; if (SDL_Init(SDL_INIT_VIDEO) < 0 ) @@ -224,7 +229,8 @@ int main(int argc, char **argv) /* OBJ Loading */ char *memory = NULL; - size_t bytes = ObjLoadFile("./cube/cube.obj", &memory); + /* size_t bytes = ObjLoadFile("./cube/cube.obj", &memory); */ + size_t bytes = ObjLoadFile(argv[1], &memory); ObjModel *model = ObjLoadModel(memory, bytes); if (!bytes || model == NULL) { |