#include "object.h" int verbose = 1; Object::Object () { initialize(); importFile ("reaphigh.obj"); finalize(); } Object::Object(char* fileName) { initialize(); importFile (fileName); finalize(); } bool Object::importFile (char* fileName) { if (verbose >=3) printf("preparing to read in file: %s\n", fileName); objFileName = fileName; this->readFromObjFile (objFileName); return true; } bool Object::initialize (void) { if (verbose >=3) printf("new 3D-Object is being created\n"); faceMode = -1; if ( (listNumber = glGenLists(1)) == 0 ) { printf ("list could not be created for this Object\n"); return false; } glNewList (listNumber, GL_COMPILE); glEnableClientState (GL_VERTEX_ARRAY); // glEnableClientState (GL_NORMAL_ARRAY); // glEnableClientState (GL_TEXTURE_COORD_ARRAY); return true; } bool Object::finalize(void) { if (verbose >=3) printf("finalizing the 3D-Object\n"); glEndList(); return true; } void Object::draw (void) { if (verbose >=3) printf("drawing the 3D-Object\n"); glCallList (listNumber); } bool Object::readFromObjFile (char* fileName) { OBJ_FILE = new ifstream (fileName); if (!OBJ_FILE->is_open()) { if (verbose >=1) printf ("unable to open .OBJ file: %s\n", fileName); return false; } objFileName = fileName; char Buffer[500]; vertices = new Array(); normals = new Array(); while(!OBJ_FILE->eof()) { OBJ_FILE->getline(Buffer, 500); if (verbose >=4) printf ("Read input line: %s\n",Buffer); // case vertice if (!strncmp(Buffer, "v ", 2)) { readVertex(Buffer+2); } // case face else if (!strncmp(Buffer, "f ", 2)) { readFace (Buffer+2); } else if (!strncmp(Buffer, "mtllib", 6)) { readMtlLib (Buffer+7); } else if (!strncmp(Buffer, "usemtl", 6)) { readUseMtl (Buffer+7); } // case VertexNormal else if (!strncmp(Buffer, "vn ", 2)) { readVertexNormal(Buffer+3); } // case vt else if (!strncmp(Buffer, "vt ", 2)) { // printf ("verticeTangent found"); } } OBJ_FILE->close(); glEnd(); } bool Object::readVertex (char* vertexString) { readVertices = true; char subbuffer1[20]; char subbuffer2[20]; char subbuffer3[20]; sscanf (vertexString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); if (verbose >= 3) printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3); vertices->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3)); return true; } bool Object::readFace (char* faceString) { if (readVertices == true) { vertices->finalizeArray(); glVertexPointer(3, GL_FLOAT, 0, vertices->getArray()); normals->finalizeArray(); glNormalPointer(GL_FLOAT, 0, normals->getArray()); } readVertices = false; char subbuffer1[20]; char subbuffer2[20]; char subbuffer3[20]; char subbuffer4[20] =""; sscanf (faceString, "%s %s %s %s", subbuffer1, subbuffer2, subbuffer3, subbuffer4); if (!strcmp(subbuffer4, "")) { if (faceMode != 3) { if (faceMode != -1) glEnd(); glBegin(GL_TRIANGLES); } faceMode = 3; if (verbose >=3) printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3); addGLElement(subbuffer1); addGLElement(subbuffer2); addGLElement(subbuffer3); return true; } else { if (faceMode != 4) { if (faceMode != -1) glEnd(); glBegin(GL_QUADS); } faceMode = 4; if (verbose >=3 ) printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4); addGLElement(subbuffer1); addGLElement(subbuffer2); addGLElement(subbuffer3); addGLElement(subbuffer4); return true; } } bool Object::addGLElement (char* elementString) { char* vertex = elementString; char* texture; char* normal; texture = strstr (vertex, "/"); texture[0] = '\0'; texture ++; normal = strstr (texture, "/"); normal[0] = '\0'; normal ++; if (verbose >= 4) printf ("importing grafical Element.... including to openGL\n"); glNormal3fv(normals->getArray() +(atoi(normal)-1)*3); glArrayElement(atoi(vertex)-1); // glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3); } bool Object::readVertexNormal (char* normalString) { readVertices = true; char subbuffer1[20]; char subbuffer2[20]; char subbuffer3[20]; sscanf (normalString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); if (verbose >=3 ) printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3); normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3)); return true; } bool Object::readMtlLib (char* mtlFile) { MTL_FILE = new ifstream (mtlFile); if (!MTL_FILE->is_open()) { if (verbose >= 1) printf ("unable to open file: %s\n", mtlFile); return false; } mtlFileName = mtlFile; if (verbose >=2) printf ("Opening mtlFile: %s\n", mtlFileName); char Buffer[500]; vertices = new Array(); material = new Material(); Material* tmpMat = material; while(!MTL_FILE->eof()) { MTL_FILE->getline(Buffer, 500); if (verbose >= 4) printf("found line in mtlFile: %s\n", Buffer); // create new Material if (!strncmp(Buffer, "newmtl ", 2)) { tmpMat = tmpMat->addMaterial(Buffer+7); // printf ("%s, %p\n", tmpMat->getName(), tmpMat); } // setting a illumMode else if (!strncmp(Buffer, "illum", 5)) { tmpMat->setIllum(Buffer+6); } // setting Diffuse Color else if (!strncmp(Buffer, "Kd", 2)) { tmpMat->setDiffuse(Buffer+3); } // setting Ambient Color else if (!strncmp(Buffer, "Ka", 2)) { tmpMat->setAmbient(Buffer+3); } // setting Specular Color else if (!strncmp(Buffer, "Ks", 2)) { tmpMat->setSpecular(Buffer+3); } } return true; } bool Object::readUseMtl (char* matString) { if (faceMode != -1) glEnd(); faceMode = 0; if (verbose >= 2) printf ("using material %s for coming Faces.\n", matString); material->search(matString)->select(); }