diff options
-rw-r--r-- | obj.c | 32 | ||||
-rw-r--r-- | obj.h | 15 | ||||
-rw-r--r-- | objloader.c | 53 |
3 files changed, 65 insertions, 35 deletions
@@ -55,17 +55,22 @@ ObjModel* ObjLoadModel(char *memory, size_t size) &ret->VertexArray[nV].z); nV++; } + /* quad */ else if (memcmp(p, "f", 1) == 0) /* or *p == 'f' */ { - sscanf(p, "f %d/%d/%d %d/%d/%d %d/%d/%d", &ret->FaceArray[nF].Vertex[0], - &ret->FaceArray[nF].TexCoord[0], - &ret->FaceArray[nF].Normal[0], - &ret->FaceArray[nF].Vertex[1], - &ret->FaceArray[nF].TexCoord[1], - &ret->FaceArray[nF].Normal[1], - &ret->FaceArray[nF].Vertex[2], - &ret->FaceArray[nF].TexCoord[2], - &ret->FaceArray[nF].Normal[2]); + sscanf(p, "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", + &ret->FaceArray[nF].Vertex[0], + &ret->FaceArray[nF].TexCoord[0], + &ret->FaceArray[nF].Normal[0], + &ret->FaceArray[nF].Vertex[1], + &ret->FaceArray[nF].TexCoord[1], + &ret->FaceArray[nF].Normal[1], + &ret->FaceArray[nF].Vertex[2], + &ret->FaceArray[nF].TexCoord[2], + &ret->FaceArray[nF].Normal[2], + &ret->FaceArray[nF].Vertex[3], + &ret->FaceArray[nF].TexCoord[3], + &ret->FaceArray[nF].Normal[3]); nF++; } /* seek to a newline */ @@ -109,3 +114,12 @@ size_t ObjLoadFile(char *szFileName, char **memory) return bytes; } +void ObjFree(ObjModel *model) +{ + free(model->NormalArray); + free(model->TexCoordArray); + free(model->FaceArray); + free(model->VertexArray); + free(model); +} + @@ -14,15 +14,18 @@ typedef struct typedef struct { - int Vertex[3]; - int Normal[3]; - int TexCoord[3]; + int Vertex[4]; + int Normal[4]; + int TexCoord[4]; } ObjFace; typedef struct { + char *mtllib; + char *o_name; + char *g_name; int nVertex, nNormal, nTexCoord, nFace; - + ObjVertex *VertexArray; ObjNormal *NormalArray; ObjTexCoord *TexCoordArray; @@ -30,12 +33,10 @@ typedef struct } ObjModel; /* function prototypes */ - - ObjModel *ObjLoadModel(char *, size_t); - /* read a file into argument 2, and return amount of bytes read */ size_t ObjLoadFile(char *, char **); +void ObjFree(ObjModel *); #endif diff --git a/objloader.c b/objloader.c index 4c3548b..688c643 100644 --- a/objloader.c +++ b/objloader.c @@ -3,7 +3,6 @@ * kamilkss@gmail.com * * OBJ Loader - * Look at line 194 * * Code: * http://www.gamedev.net/community/forums/topic.asp?topic_id=312335 @@ -13,7 +12,6 @@ #include <SDL/SDL.h> #include <SDL/SDL_image.h> #include "math3d.h" - #include "obj.h" /* global */ @@ -32,7 +30,7 @@ static void resize(int w, int h) glLoadIdentity(); /* produce the perspective projection */ - gluPerspective(40.0f, fAspect, 1.0, 40.0); + gluPerspective(60.0f, fAspect, 1.0, 40.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -41,7 +39,7 @@ static void resize(int w, int h) SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_RESIZABLE); } -static void SetupRC() +static void setup_opengl() { /* light values and coordinates */ GLfloat whiteLight[] = { 0.05f, 0.05f, 0.05f, 1.0f }; @@ -99,7 +97,7 @@ static void SetupRC() } /* process SDL events */ -void processEvents() +void process_events() { SDL_Event event; SDLKey sym; @@ -139,13 +137,37 @@ static void draw_ground(void) glEnd(); } -void render() +void render(ObjModel *model) { /* clear the window with current clearing color */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - draw_ground(); + + int i; + + glPushMatrix(); + glTranslatef(0.0f, -2.0f, -2.0f); + glScalef(0.2f, 0.2f, 0.2f); + glBegin(GL_QUADS); + + for (i = 0; i < model->nFace; i++) + { + glNormal3f(model->NormalArray[model->FaceArray[i].Normal[0]].x, model->NormalArray[model->FaceArray[i].Normal[0]].y, model->NormalArray[model->FaceArray[i].Normal[0]].z ); + glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[0]].x, model->VertexArray[model->FaceArray[i].Vertex[0]].y, model->VertexArray[model->FaceArray[i].Vertex[0]].z ); + + glNormal3f(model->NormalArray[model->FaceArray[i].Normal[1]].x, model->NormalArray[model->FaceArray[i].Normal[1]].y, model->NormalArray[model->FaceArray[i].Normal[1]].z ); + glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[1]].x, model->VertexArray[model->FaceArray[i].Vertex[1]].y, model->VertexArray[model->FaceArray[i].Vertex[1]].z ); + + glNormal3f(model->NormalArray[model->FaceArray[i].Normal[2]].x, model->NormalArray[model->FaceArray[i].Normal[2]].y, model->NormalArray[model->FaceArray[i].Normal[2]].z ); + glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[2]].x, model->VertexArray[model->FaceArray[i].Vertex[2]].y, model->VertexArray[model->FaceArray[i].Vertex[2]].z ); + glNormal3f(model->NormalArray[model->FaceArray[i].Normal[3]].x, model->NormalArray[model->FaceArray[i].Normal[3]].y, model->NormalArray[model->FaceArray[i].Normal[3]].z ); + glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[3]].x, model->VertexArray[model->FaceArray[i].Vertex[3]].y, model->VertexArray[model->FaceArray[i].Vertex[3]].z ); + + } + glEnd(); + glPopMatrix(); + /* buffer swap */ SDL_GL_SwapBuffers(); } @@ -189,7 +211,7 @@ int main(void) fprintf(stdout, "status: using GLEW %s\n", glewGetString(GLEW_VERSION)); /* setup few stuff before rendring */ - SetupRC(); + setup_opengl(); /* OBJ Loading */ char *memory = NULL; @@ -198,22 +220,15 @@ int main(void) ObjModel *model = ObjLoadModel(memory, bytes); printf("Object Model has: %d faces!\n", model->nFace); - /* XXX: Insert Code Here */ - - /* main loop */ while (program_running) { - processEvents(); - render(); + process_events(); + render(model); } - /* might never be reached, meh */ - free(model->NormalArray); - free(model->TexCoordArray); - free(model->FaceArray); - free(model->VertexArray); - free(model); + /* free up the resources */ + ObjFree(model); puts("bye!"); return 0; |