From d6a7bfdeca1b1aa4bc3b283cea17bf7af74db3ad Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sun, 3 Jul 2011 20:08:17 -0500 Subject: tidy up the code --- objloader.c | 73 ++++++++++++++++++------------------------------------------- 1 file changed, 21 insertions(+), 52 deletions(-) (limited to 'objloader.c') diff --git a/objloader.c b/objloader.c index 63cf273..0e6aba9 100644 --- a/objloader.c +++ b/objloader.c @@ -15,6 +15,8 @@ #include #include +#include +#include #include "math3d.h" #include "obj.h" @@ -27,6 +29,7 @@ static void render(ObjModel *); /* global */ int program_running = 1; +char obj_last_fname[101]; static void resize(int w, int h) { @@ -45,7 +48,7 @@ static void resize(int w, int h) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - + /* this needs to be ran again, glut does it for you I suppose */ SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_RESIZABLE); } @@ -73,13 +76,13 @@ static void setup_opengl(ObjModel *model) /* enable color tracking */ glEnable(GL_COLOR_MATERIAL); - + /* set Material properties to follow glColor values */ glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); /* gray background */ glClearColor(0.5f, 0.5f, 0.5f, 1.0f); - + /* example of loading a targa file, note: RLE compression is not supported */ GLbyte *pBytes; GLint iWidth, iHeight, iComponents; @@ -90,7 +93,7 @@ static void setup_opengl(ObjModel *model) 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); @@ -117,7 +120,7 @@ static void process_events(void) { SDL_Event event; SDLKey sym; - + while (SDL_PollEvent(&event)) { sym = event.key.keysym.sym; @@ -158,53 +161,19 @@ static void render(ObjModel *model) /* clear the window with current clearing color */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_ground(); - + glPushMatrix(); glScalef(0.1f, 0.1f, 0.1f); glTranslatef(5.0f, 0.0f, -150.0f); + glBegin(GL_QUADS); - int i; - for (i = 0; i < model->nFace; i++) - { - glNormal3f(model->NormalArray[model->FaceArray[i].Normal[0] - 1].x, - model->NormalArray[model->FaceArray[i].Normal[0] - 1].y, - model->NormalArray[model->FaceArray[i].Normal[0] - 1].z ); - glTexCoord2f(model->TexCoordArray[model->FaceArray[i].TexCoord[0] - 1].u, - model->TexCoordArray[model->FaceArray[i].TexCoord[0] - 1].v); - glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[0] - 1].x, - model->VertexArray[model->FaceArray[i].Vertex[0] - 1].y, - model->VertexArray[model->FaceArray[i].Vertex[0] - 1].z ); - - glNormal3f(model->NormalArray[model->FaceArray[i].Normal[1] - 1].x, - model->NormalArray[model->FaceArray[i].Normal[1] - 1].y, - model->NormalArray[model->FaceArray[i].Normal[1] - 1].z ); - glTexCoord2f(model->TexCoordArray[model->FaceArray[i].TexCoord[1] - 1].u, - model->TexCoordArray[model->FaceArray[i].TexCoord[1] - 1].v); - glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[1] - 1].x, - model->VertexArray[model->FaceArray[i].Vertex[1] - 1].y, - model->VertexArray[model->FaceArray[i].Vertex[1] - 1].z ); - - glNormal3f(model->NormalArray[model->FaceArray[i].Normal[2] - 1].x, - model->NormalArray[model->FaceArray[i].Normal[2] - 1].y, - model->NormalArray[model->FaceArray[i].Normal[2] - 1].z ); - glTexCoord2f(model->TexCoordArray[model->FaceArray[i].TexCoord[2] - 1].u, - model->TexCoordArray[model->FaceArray[i].TexCoord[2] - 1].v); - glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[2] - 1].x, - model->VertexArray[model->FaceArray[i].Vertex[2] - 1].y, - model->VertexArray[model->FaceArray[i].Vertex[2] - 1].z ); - - glNormal3f(model->NormalArray[model->FaceArray[i].Normal[3] - 1].x, - model->NormalArray[model->FaceArray[i].Normal[3] - 1].y, - model->NormalArray[model->FaceArray[i].Normal[3] - 1].z ); - glTexCoord2f(model->TexCoordArray[model->FaceArray[i].TexCoord[3] - 1].u, - model->TexCoordArray[model->FaceArray[i].TexCoord[3] - 1].v); - glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[3] - 1].x, - model->VertexArray[model->FaceArray[i].Vertex[3] - 1].y, - model->VertexArray[model->FaceArray[i].Vertex[3] - 1].z ); - } + int i; + for (i = 0; i < model->nFace; i++) + ObjPutFaceGLCmd(model, i); glEnd(); + glPopMatrix(); - + /* buffer swap */ SDL_GL_SwapBuffers(); } @@ -227,13 +196,13 @@ int main(int argc, char **argv) SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); - + if ((screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_RESIZABLE)) == NULL) { fprintf(stderr, "unable to set video mode: %s\n", SDL_GetError()); exit(-1); } - + SDL_EnableUNICODE(1); /* SDL doesn't trigger off a ResizeEvent at startup, but as we need this * for OpenGL, we do this ourselves */ @@ -242,7 +211,7 @@ int main(int argc, char **argv) resizeEvent.resize.w = 640; resizeEvent.resize.h = 480; SDL_PushEvent(&resizeEvent); - + /* initalize glew */ GLenum glewerr = glewInit(); if (GLEW_OK != glewerr) @@ -263,7 +232,7 @@ int main(int argc, char **argv) exit(-1); } - /* setup few stuff before rendring */ + /* setup few stuff before rendering */ setup_opengl(model); /* main loop */ @@ -272,11 +241,11 @@ int main(int argc, char **argv) process_events(); render(model); } - + /* free up the resources */ ObjFree(model); puts("bye!"); - + return 0; } -- cgit v1.2.3