From 1a7e2a8e625d4ef2038b3ae162fa892ac6826f85 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Fri, 15 Oct 2010 17:30:54 -0500 Subject: Cleanups --- math3d.c | 394 ++---------------------------------------------------------- obj.c | 41 +++++-- obj.h | 11 +- objloader.c | 6 +- 4 files changed, 49 insertions(+), 403 deletions(-) diff --git a/math3d.c b/math3d.c index 8e3aa50..9a4be87 100644 --- a/math3d.c +++ b/math3d.c @@ -1,13 +1,3 @@ -/* revision 5 */ - -/* @2009 Kamil Kaminski - * - * this code is not yet endian aware - * the style of the syntax is original k&r except there's \n - * after the opening { and extra space after if statement, - * and for/while loops - */ - #include "math3d.h" void m3dFindNormal(M3DVector3f result, const M3DVector3f point1, @@ -101,375 +91,13 @@ void m3dRotationMatrix44(M3DMatrix44f m, float angle, float x, float y, float z) #undef M } -/* draw a torus (doughnut), using the current 1d texture for light shading */ -/* this funct accepts 4x4 trans matrix to be applied to the vertices */ -void gltDrawTorus(GLfloat majorRadius, GLfloat minorRadius, GLint numMajor, - GLint numMinor) -{ - M3DVector3f vNormal; - double majorStep = 2.0f * M3D_PI / numMajor; - double minorStep = 2.0f * M3D_PI / numMinor; - int i, j; - - for (i = 0; i < numMajor; ++i) - { - double a0 = i * majorStep; - double a1 = a0 + majorStep; - GLfloat x0 = (GLfloat) cos(a0); - GLfloat y0 = (GLfloat) sin(a0); - GLfloat x1 = (GLfloat) cos(a1); - GLfloat y1 = (GLfloat) sin(a1); - - glBegin(GL_TRIANGLE_STRIP); - for (j = 0; j <= numMinor; ++j) - { - double b = j * minorStep; - GLfloat c = (GLfloat) cos(b); - GLfloat r = minorRadius * c + majorRadius; - GLfloat z = minorRadius * (GLfloat) sin(b); - - glTexCoord2f((float) (i) / (float) (numMajor), (float) (j) \ - / (float) (numMinor)); - vNormal[0] = x0 * c; - vNormal[1] = y0 * c; - vNormal[2] = z / minorRadius; - m3dNormalizeVector(vNormal); - glNormal3fv(vNormal); - glVertex3f(x0 * r, y0 * r, z); - - glTexCoord2f((float) (i + 1) / (float) (numMajor), (float) (j) \ - / (float) (numMinor)); - vNormal[0] = x1 * c; - vNormal[1] = y1 * c; - vNormal[2] = z / minorRadius; - m3dNormalizeVector(vNormal); - glNormal3fv(vNormal); - glVertex3f(x1 * r, y1 * r, z); - } - glEnd(); - } -} - -/* this function just specifically draws the jet */ -/* FIXME needs to accepts parameters of location and lightning */ -void DrawJet(int nShadow) -{ - M3DVector3f vNormal; - - /* nose cone, set material color, note we only have to set to black - * for the shadow once - */ - if (nShadow == 0) - glColor3ub(128, 128, 128); - else - glColor3ub(0, 0, 0); - - /* nose cone, points straight down, set material color */ - /* follow few lines use manual approach */ - glBegin(GL_TRIANGLES); - glNormal3f(0.0f, -1.0f, 0.0f); - glNormal3f(0.0f, -1.0f, 0.0f); - glVertex3f(0.0f, 0.0f, 60.0f); - glVertex3f(-15.0f, 0.0f, 30.0f); - glVertex3f(15.0f, 0.0f, 30.0f); - - /* verticies for this panel */ - { - M3DVector3f vPoints[3] = { {15.0f, 0.0f, 30.0f} - , - {0.0f, 15.0f, 30.0f} - , - {0.0f, 0.0f, 60.0f} - }; - - /* calculate the normal for the plane */ - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {0.0f, 0.0f, 60.0f} - , - {0.0f, 15.0f, 30.0f} - , - {-15.0f, 0.0f, 30.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - /* body of the plane */ - { - M3DVector3f vPoints[3] = { {-15.0f, 0.0f, 30.0f} - , - {0.0f, 15.0f, 30.0f} - , - {0.0f, 0.0f, -56.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {0.0f, 0.0f, -56.0f} - , - {0.0f, 15.0f, 30.0f} - , - {15.0f, 0.0f, 30.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - glNormal3f(0.0f, -1.0f, 0.0f); - glVertex3f(15.0f, 0.0f, 30.0f); - glVertex3f(-15.0f, 0.0f, 30.0f); - glVertex3f(0.0f, 0.0f, -56.0f); - - /* left wing, large triangle for bottom of wing */ - { - M3DVector3f vPoints[3] = { {0.0f, 2.0f, 27.0f} - , - {-60.0f, 2.0f, -8.0f} - , - {60.0f, 2.0f, -8.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {60.0f, 2.0f, -8.0f} - , - {0.0f, 7.0f, -8.0f} - , - {0.0f, 2.0f, 27.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {60.0f, 2.0f, -8.0f} - , - {-60.0f, 2.0f, -8.0f} - , - {0.0f, 7.0f, -8.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {0.0f, 2.0f, 27.0f} - , - {0.0f, 7.0f, -8.0f} - , - {-60.0f, 2.0f, -8.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - /* tail section */ - /* bottom of back fin */ - glNormal3f(0.0f, -1.0f, 0.0f); - glVertex3f(-30.0f, -0.50f, -57.0f); - glVertex3f(30.0f, -0.50f, -57.0f); - glVertex3f(0.0f, -0.50f, -40.0f); - - { - M3DVector3f vPoints[3] = { {0.0f, -0.5f, -40.0f} - , - {30.0f, -0.5f, -57.0f} - , - {0.0f, 4.0f, -57.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {0.0f, 4.0f, -57.0f} - , - {-30.0f, -0.5f, -57.0f} - , - {0.0f, -0.5f, -40.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {30.0f, -0.5f, -57.0f} - , - {-30.0f, -0.5f, -57.0f} - , - {0.0f, 4.0f, -57.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {0.0f, 0.5f, -40.0f} - , - {3.0f, 0.5f, -57.0f} - , - {0.0f, 25.0f, -65.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {0.0f, 25.0f, -65.0f} - , - {-3.0f, 0.5f, -57.0f} - , - {0.0f, 0.5f, -40.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - { - M3DVector3f vPoints[3] = { {3.0f, 0.5f, -57.0f} - , - {-3.0f, 0.5f, -57.0f} - , - {0.0f, 25.0f, -65.0f} - }; - - m3dFindNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]); - glNormal3fv(vNormal); - glVertex3fv(vPoints[0]); - glVertex3fv(vPoints[1]); - glVertex3fv(vPoints[2]); - } - - glEnd(); -} - -void gltDrawUnitAxes(void) -{ - GLUquadricObj *pObj; /* temporary, used for quadrics */ - - /* measurements */ - float fAxisRadius = 0.025f; - float fAxisHeight = 1.0f; - float fArrowRadius = 0.06f; - float fArrowHeight = 0.1f; - - /* setup the quadric object */ - pObj = gluNewQuadric(); - gluQuadricDrawStyle(pObj, GLU_FILL); - gluQuadricNormals(pObj, GLU_SMOOTH); - gluQuadricOrientation(pObj, GLU_OUTSIDE); - gluQuadricTexture(pObj, GLU_FALSE); - - /* draw the blue z axis first with arrowed head */ - glColor3f(0.0f, 0.0f, 1.0f); - gluCylinder(pObj, fAxisRadius, fAxisRadius, fAxisHeight, 10, 1); - glPushMatrix(); - glTranslatef(0.0f, 0.0f, 1.0f); - gluCylinder(pObj, fArrowRadius, 0.0f, fArrowHeight, 10, 1); - glRotatef(180.0f, 1.0f, 0.0f, 0.0f); - gluDisk(pObj, fAxisRadius, fArrowRadius, 10, 1); - glPopMatrix(); - - /* draw the red x axis 2nd with arrowed head */ - glColor3f(1.0f, 0.0f, 0.0f); - glPushMatrix(); - glRotatef(90.0f, 0.0f, 1.0f, 0.0f); - gluCylinder(pObj, fAxisRadius, fAxisRadius, fAxisHeight, 10, 1); - glPushMatrix(); - glTranslatef(0.0f, 0.0f, 1.0f); - gluCylinder(pObj, fArrowRadius, 0.0f, fArrowHeight, 10, 1); - glRotatef(180.0f, 0.0f, 1.0f, 0.0f); - gluDisk(pObj, fAxisRadius, fArrowRadius, 10, 1); - glPopMatrix(); - glPopMatrix(); - - /* draw the green y axis 3rd with arrowed head */ - glColor3f(0.0f, 1.0f, 0.0f); - glPushMatrix(); - glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); - gluCylinder(pObj, fAxisRadius, fAxisRadius, fAxisHeight, 10, 1); - glPushMatrix(); - glTranslatef(0.0f, 0.0f, 1.0f); - gluCylinder(pObj, fArrowRadius, 0.0f, fArrowHeight, 10, 1); - glRotatef(180.0f, 1.0f, 0.0f, 0.0f); - gluDisk(pObj, fAxisRadius, fArrowRadius, 10, 1); - glPopMatrix(); - glPopMatrix(); - - /* white sphere at origin */ - glColor3f(1.0f, 1.0f, 1.0f); - gluSphere(pObj, 0.05f, 15, 15); - - /* delete the quadric */ - gluDeleteQuadric(pObj); -} - -#define A(row,col) a[(col<<2)+row] -#define B(row,col) b[(col<<2)+row] -#define P(row,col) product[(col<<2)+row] - void m3dMatrixMultiply44(M3DMatrix44f product, const M3DMatrix44f a, const M3DMatrix44f b) { + #define A(row,col) a[(col<<2)+row] + #define B(row,col) b[(col<<2)+row] + #define P(row,col) product[(col<<2)+row] + int i; for (i = 0; i < 4; i++) { @@ -479,18 +107,12 @@ void m3dMatrixMultiply44(M3DMatrix44f product, const M3DMatrix44f a, P(i, 2) = ai0 * B(0, 2) + ai1 * B(1, 2) + ai2 * B(2, 2) + ai3 * B(3, 2); P(i, 3) = ai0 * B(0, 3) + ai1 * B(1, 3) + ai2 * B(2, 3) + ai3 * B(3, 3); } + + #undef A + #undef B + #undef P } -#undef A -#undef B -#undef P - - -/*************************************************************/ -/* unrelated functions that do not have much to do with math */ -/* */ -/*************************************************************/ - GLint gltWriteTGA(const char *szFileName) { FILE *pFile; /* file pointer */ diff --git a/obj.c b/obj.c index e871fda..9ab4a6a 100644 --- a/obj.c +++ b/obj.c @@ -12,20 +12,22 @@ ObjModel* ObjLoadModel(char *memory, size_t size) p = memory; e = memory + size; + /* count the number of normals, texcoords, vertices, and faces, line by line */ while (p != e) { if (memcmp(p, "vn", 2) == 0) ret->nNormal++; else if (memcmp(p, "vt", 2) == 0) ret->nTexCoord++; else if (memcmp(p, "v", 1) == 0) ret->nVertex++; - else if (memcmp(p, "f", 1) == 0) ret->nTriangle++; + else if (memcmp(p, "f", 1) == 0) ret->nFace++; while (*p++ != (char) 0x0A); } + /* allocate memory for arrays */ ret->VertexArray = (ObjVertex *) malloc(sizeof(ObjVertex) * ret->nVertex); ret->NormalArray = (ObjNormal *) malloc(sizeof(ObjNormal) * ret->nNormal); ret->TexCoordArray = (ObjTexCoord *) malloc(sizeof(ObjTexCoord) * ret->nTexCoord); - ret->TriangleArray = (ObjTriangle *) malloc(sizeof(ObjTriangle) * ret->nTriangle); + ret->FaceArray = (ObjFace *) malloc(sizeof(ObjFace) * ret->nFace); p = memory; @@ -55,19 +57,36 @@ ObjModel* ObjLoadModel(char *memory, size_t size) } else if (memcmp(p, "f", 1) == 0) /* or *p == 'f' */ { - sscanf(p, "f %d/%d/%d %d/%d/%d %d/%d/%d", &ret->TriangleArray[nF].Vertex[0], - &ret->TriangleArray[nF].TexCoord[0], - &ret->TriangleArray[nF].Normal[0], - &ret->TriangleArray[nF].Vertex[1], - &ret->TriangleArray[nF].TexCoord[1], - &ret->TriangleArray[nF].Normal[1], - &ret->TriangleArray[nF].Vertex[2], - &ret->TriangleArray[nF].TexCoord[2], - &ret->TriangleArray[nF].Normal[2]); + 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]); nF++; } + /* seek to a newline */ while (*p++ != (char) 0x0A); } + + /* sanity check */ + if ((ret->nVertex != nV) || (ret->nNormal != nN) || (ret->nTexCoord != nT) + || (ret->nFace != nF)) + { + fprintf(stdout, "obj loader: warning, the number of scanned items does not equal to number of read\n"); + if (ret->nVertex != nV) + fprintf(stdout, "vertices: scanned %d, read %d\n", ret->nVertex, nV); + if (ret->nNormal != nN) + fprintf(stdout, "normals: scanned %d, read %d\n", ret->nNormal, nN); + if (ret->nTexCoord != nT) + fprintf(stdout, "texcoords: scanned %d, read %d\n", ret->nTexCoord, nT); + if (ret->nFace != nF) + fprintf(stdout, "faces: scanned %d, read %d\n", ret->nFace, nF); + } + return ret; } diff --git a/obj.h b/obj.h index 962d325..18b1e2b 100644 --- a/obj.h +++ b/obj.h @@ -17,19 +17,24 @@ typedef struct int Vertex[3]; int Normal[3]; int TexCoord[3]; -} ObjTriangle; +} ObjFace; typedef struct { - int nVertex, nNormal, nTexCoord, nTriangle; + int nVertex, nNormal, nTexCoord, nFace; ObjVertex *VertexArray; ObjNormal *NormalArray; ObjTexCoord *TexCoordArray; - ObjTriangle *TriangleArray; + ObjFace *FaceArray; } 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 **); #endif diff --git a/objloader.c b/objloader.c index 1842033..4c3548b 100644 --- a/objloader.c +++ b/objloader.c @@ -195,8 +195,8 @@ int main(void) char *memory = NULL; size_t bytes = ObjLoadFile("./cube/cube.obj", &memory); - ObjModel* model = ObjLoadModel(memory, bytes); - printf("Object Model has: %d faces!\n", model->nTriangle); + ObjModel *model = ObjLoadModel(memory, bytes); + printf("Object Model has: %d faces!\n", model->nFace); /* XXX: Insert Code Here */ @@ -211,7 +211,7 @@ int main(void) /* might never be reached, meh */ free(model->NormalArray); free(model->TexCoordArray); - free(model->TriangleArray); + free(model->FaceArray); free(model->VertexArray); free(model); puts("bye!"); -- cgit v1.2.3