/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #include "object.h" /** \brief Creates a 3D-Object, but does not load any 3D-models pretty useless */ Object::Object () { initialize(); BoxObject(); importToGL (); cleanup(); } /** \brief Crates a 3D-Object and loads in a File \param fileName file to parse and load (must be a .obj file) */ Object::Object(char* fileName) { initialize(); importFile (fileName); importToGL (); cleanup(); } /** \brief Crates a 3D-Object, loads in a File and scales it. \param fileName file to parse and load (must be a .obj file) \param scaling The factor that the object will be scaled with. */ Object::Object(char* fileName, float scaling) { initialize(); scaleFactor = scaling; importFile (fileName); importToGL (); cleanup(); } /** \brief deletes an Object */ Object::~Object() { if (verbose >= 2) printf ("Deleting display Lists.\n"); Group* walker = firstGroup; while (walker != NULL) { glDeleteLists (walker->listNumber, 1); Group* delWalker = walker; walker = walker->next; delete delWalker; } } /** \brief Draws the Objects of all Groups. It does this by just calling the Lists that must have been created earlier. */ void Object::draw (void) const { if (verbose >=2) printf("drawing the 3D-Objects\n"); Group* walker = firstGroup; while (walker != NULL) { if (verbose >= 3) printf ("Drawing object %s\n", walker->name); glCallList (walker->listNumber); walker = walker->next; } } /** \brief Draws the Object number groupNumber It does this by just calling the List that must have been created earlier. \param groupNumber The number of the group that will be displayed. */ void Object::draw (int groupNumber) const { if (groupNumber >= groupCount) { if (verbose>=1) printf ("You requested object number %i, but this File only contains of %i Objects.\n", groupNumber-1, groupCount); return; } if (verbose >=2) printf("drawing the requested 3D-Objects if found.\n"); Group* walker = firstGroup; int counter = 0; while (walker != NULL) { if (counter == groupNumber) { if (verbose >= 2) printf ("Drawing object number %i named %s\n", counter, walker->name); glCallList (walker->listNumber); return; } ++counter; walker = walker->next; } if (verbose >= 1) printf("Object number %i in %s not Found.\n", groupNumber, objFileName); return; } /** \brief Draws the Object with a specific groupName It does this by just calling the List that must have been created earlier. \param groupName The name of the group that will be displayed. */ void Object::draw (char* groupName) const { if (verbose >=2) printf("drawing the requested 3D-Objects if found.\n"); Group* walker = firstGroup; while (walker != NULL) { if (!strcmp(walker->name, groupName)) { if (verbose >= 2) printf ("Drawing object %s\n", walker->name); glCallList (walker->listNumber); return; } walker = walker->next; } if (verbose >= 2) printf("Object Named %s in %s not Found.\n", groupName, objFileName); return; } /** \returns Count of the Objects in this File */ int Object::getGroupCount (void) const { return groupCount; } /** \brief initializes the Object This Function initializes all the needed arrays, Lists and clientStates */ bool Object::initialize (void) { if (verbose >=3) printf("new 3D-Object is being created\n"); // setting the start group; firstGroup = new Group; currentGroup = firstGroup; groupCount = 0; initGroup (firstGroup); mtlFileName = ""; scaleFactor = 1; material = new Material(); vertices = new Array(); vTexture = new Array(); normals = new Array(); return true; } /** \brief initializes a new Group object */ bool Object::initGroup(Group* group) { if (verbose >= 2) printf("Adding new Group\n"); group->name = ""; group->faceMode = -1; group->faceCount = 0; group->next = NULL; group->firstFace = new Face; initFace (group->firstFace); group->currentFace = group->firstFace; } /** \brief initializes a new Face. (sets default Values) */ bool Object::initFace (Face* face) { face->vertexCount = 0; face->firstElem = NULL; face->materialString = NULL; face->next = NULL; return true; } /** \brief finalizes an Object. This funcion is needed, to delete all the Lists, and arrays that are no more needed because they are already imported into openGL. This will be applied at the end of the importing Process. */ bool Object::cleanup(void) { if (verbose >=2) printf("cleaning up the 3D-Object to save Memory.\n"); if (vertices != NULL) delete vertices; if (vTexture != NULL) delete vTexture; if (normals != NULL) delete normals; if (material != NULL) delete material; cleanupGroup(firstGroup); return true; } /** \brief Cleans up all groups starting from group. \param group the first Group to clean */ bool Object::cleanupGroup (Group* group) { if (verbose>=4) printf ("Cleaning up group\n"); if (group->firstFace != NULL) { cleanupFace (group->firstFace); delete group->firstFace; } if (group->next !=NULL) cleanupGroup (group->next); return true; } /** \brief Cleans up all Faces starting from face. \param face the first face to clean */ bool Object::cleanupFace (Face* face) { if (verbose>=4) printf ("Cleaning up Face\n"); if (face->materialString != NULL) delete []face->materialString; if (face->firstElem != NULL) { cleanupFaceElement(face->firstElem); delete face->firstElem; } if (face->next != NULL) { cleanupFace (face->next); delete face->next; } } /** \brief Cleans up all FaceElements starting from faceElem. \param faceElem the first FaceElement to clean. */ bool Object::cleanupFaceElement(FaceElement* faceElem) { if (faceElem->next != NULL) { cleanupFaceElement (faceElem->next); delete faceElem->next; } } /** \brief Imports a obj file and handles the the relative location \param fileName The file to import */ bool Object::importFile (char* fileName) { if (verbose >=3) printf("preparing to read in file: %s\n", fileName); objFileName = fileName; this->readFromObjFile (fileName); return true; } /** \brief Reads in the .obj File and sets all the Values. This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks \param fileName the File that will be parsed (.obj-file) */ 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 Loading Box Object instead.\n", fileName); BoxObject(); return false; } objFileName = fileName; char Buffer[10000]; while(!OBJ_FILE->eof()) { OBJ_FILE->getline(Buffer, 10000); 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 ", 7)) { readMtlLib (Buffer+7); } else if (!strncmp(Buffer, "usemtl ", 7)) { readUseMtl (Buffer+7); } // case VertexNormal else if (!strncmp(Buffer, "vn ", 3)) { readVertexNormal(Buffer+3); } // case VertexTextureCoordinate else if (!strncmp(Buffer, "vt ", 3)) { readVertexTexture(Buffer+3); } // case group else if (!strncmp(Buffer, "g ", 2)) { readGroup (Buffer+2); } else if (!strncmp(Buffer, "s ", 2)) { if (verbose >= 2) printf("smoothing groups not supportet yet. line: %s\n", Buffer); } } OBJ_FILE->close(); return true; } /** \brief parses a group String This function initializes a new Group. With it you should be able to import .obj-files with more than one Objects inside. \param groupString the new Group to create */ bool Object::readGroup (char* groupString) { // setting the group name if not default. if (strcmp(currentGroup->name, "default")) { currentGroup->name = new char [strlen(groupString)]; strcpy(currentGroup->name, groupString); } if (groupCount != 0 && currentGroup->faceCount>0) { // finalizeGroup(currentGroup); currentGroup = currentGroup->next = new Group; initGroup(currentGroup); } ++groupCount; } /** \brief parses a vertex-String If a vertex line is found this function will inject it into the vertex-Array \param vertexString The String that will be parsed. */ bool Object::readVertex (char* vertexString) { float subbuffer1; float subbuffer2; float subbuffer3; sscanf (vertexString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3); if (verbose >= 3) printf ("reading in a vertex: %f %f %f\n", &subbuffer1, &subbuffer2, &subbuffer3); vertices->addEntry(subbuffer1*scaleFactor, subbuffer2*scaleFactor, subbuffer3*scaleFactor); return true; } /** \brief parses a face-string If a face line is found this function will add it to the glList. The function makes a difference between QUADS and TRIANGLES, and will if changed re-open, set and re-close the gl-processe. \param faceString The String that will be parsed. */ bool Object::readFace (char* faceString) { if (currentGroup->faceCount >0) currentGroup->currentFace = currentGroup->currentFace->next = new Face; initFace (currentGroup->currentFace); FaceElement* tmpElem = currentGroup->currentFace->firstElem = new FaceElement; tmpElem->next = NULL; while(strcmp (faceString, "\0")) { if (currentGroup->currentFace->vertexCount>0) tmpElem = tmpElem->next = new FaceElement; tmpElem->next = NULL; char tmpValue [50]; int tmpLen; char* vertex = NULL; char* texture = NULL; char* normal = NULL; sscanf (faceString, "%s", tmpValue); tmpLen = strlen(tmpValue); vertex = tmpValue; if ((texture = strstr (vertex, "/")) != NULL) { texture[0] = '\0'; texture ++; if ((normal = strstr (texture, "/")) !=NULL) { normal[0] = '\0'; normal ++; } } if (vertex) tmpElem->vertexNumber = atoi(vertex)-1; else tmpElem->vertexNumber = -1; if (texture) tmpElem->texCoordNumber = atoi(texture)-1; else tmpElem->texCoordNumber = -1; if (normal) tmpElem->normalNumber = atoi(normal)-1; else tmpElem->normalNumber = -1; faceString += tmpLen; if (strcmp (faceString, "\0")) faceString++; currentGroup->currentFace->vertexCount++; } currentGroup->faceCount += currentGroup->currentFace->vertexCount -2; } /** \brief parses a vertexNormal-String If a vertexNormal line is found this function will inject it into the vertexNormal-Array \param normalString The String that will be parsed. */ bool Object::readVertexNormal (char* normalString) { float subbuffer1; float subbuffer2; float subbuffer3; sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3); if (verbose >=3 ) printf("found vertex-Normal %f, %f, %f\n", &subbuffer1,&subbuffer2,&subbuffer3); normals->addEntry(subbuffer1, subbuffer2, subbuffer3); return true; } /** \brief parses a vertexTextureCoordinate-String If a vertexTextureCoordinate line is found this function will inject it into the vertexTexture-Array \param vTextureString The String that will be parsed. */ bool Object::readVertexTexture (char* vTextureString) { float subbuffer1; float subbuffer2; sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2); if (verbose >=3 ) printf("found vertex-Texture %f, %f\n", &subbuffer1, &subbuffer2); vTexture->addEntry(subbuffer1); vTexture->addEntry(subbuffer2); return true; } /** \brief Function to read in a mtl File. this Function parses all Lines of an mtl File \param mtlFile The .mtl file to read */ 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]; 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 ", 7)) { tmpMat = tmpMat->addMaterial(Buffer+7); // printf ("%s, %p\n", tmpMat->getName(), tmpMat); } // setting a illumMode else if (!strncmp(Buffer, "illum ", 6)) { tmpMat->setIllum(Buffer+6); } // setting Diffuse Color else if (!strncmp(Buffer, "Kd ", 3)) { tmpMat->setDiffuse(Buffer+3); } // setting Ambient Color else if (!strncmp(Buffer, "Ka ", 3)) { tmpMat->setAmbient(Buffer+3); } // setting Specular Color else if (!strncmp(Buffer, "Ks ", 3)) { tmpMat->setSpecular(Buffer+3); } // setting The Specular Shininess else if (!strncmp(Buffer, "Ns ", 3)) { tmpMat->setShininess(Buffer+3); } // setting up transparency else if (!strncmp(Buffer, "d ", 2)) { tmpMat->setTransparency(Buffer+2); } else if (!strncmp(Buffer, "Tf ", 3)) { tmpMat->setTransparency(Buffer+3); } else if (!strncmp(Buffer, "map_Kd ", 7)) { tmpMat->setDiffuseMap(Buffer+7); } else if (!strncmp(Buffer, "map_Ka ", 7)) { tmpMat->setAmbientMap(Buffer+7); } else if (!strncmp(Buffer, "map_Ks ", 7)) { tmpMat->setSpecularMap(Buffer+7); } else if (!strncmp(Buffer, "bump ", 5)) { tmpMat->setBump(Buffer+7); } } return true; } /** \brief Function that selects a material, if changed in the obj file. \param matString the Material that will be set. */ bool Object::readUseMtl (char* matString) { if (!strcmp (mtlFileName, "")) { if (verbose >= 1) printf ("Not using new defined material, because no mtlFile found yet\n"); return false; } if (currentGroup->faceCount >0) currentGroup->currentFace = currentGroup->currentFace->next = new Face; initFace (currentGroup->currentFace); currentGroup->currentFace->materialString = new char[strlen(matString)]; strcpy (currentGroup->currentFace->materialString, matString); if (currentGroup->faceCount == 0) currentGroup->faceCount ++; } /** \brief reads and includes the Faces/Materials into the openGL state Machine */ bool Object::importToGL (void) { // finalize the Arrays vertices->finalizeArray(); vTexture->finalizeArray(); if (normals->getCount() == 0) // vertices-Array must be uilt for this buildVertexNormals(); normals->finalizeArray(); currentGroup = firstGroup; while (currentGroup != NULL) { // creating a glList for the Group if ((currentGroup->listNumber = glGenLists(1)) == 0) { printf ("list could not be created for this Object\n"); return false; } glNewList (currentGroup->listNumber, GL_COMPILE); // Putting Faces to GL Face* tmpFace = currentGroup->firstFace; while (tmpFace != NULL) { if (tmpFace->vertexCount == 0 && tmpFace->materialString != NULL) { if (currentGroup->faceMode != -1) glEnd(); currentGroup->faceMode = 0; if (verbose >= 2) printf ("using material %s for coming Faces.\n", tmpFace->materialString); Material* tmpMat; if ((tmpMat = material->search(tmpFace->materialString)) != NULL) tmpMat->select(); } else if (tmpFace->vertexCount == 3) { if (currentGroup->faceMode != 3) { if (currentGroup->faceMode != -1) glEnd(); glBegin(GL_TRIANGLES); } currentGroup->faceMode = 3; if (verbose >=3) printf ("found triag.\n"); } else if (tmpFace->vertexCount == 4) { if (currentGroup->faceMode != 4) { if (currentGroup->faceMode != -1) glEnd(); glBegin(GL_QUADS); } currentGroup->faceMode = 4; if (verbose >=3 ) printf ("found quad.\n"); } else if (tmpFace->vertexCount > 4) { if (currentGroup->faceMode != -1) glEnd(); glBegin(GL_POLYGON); if (verbose >=3) printf ("Polygon with %i faces found.", tmpFace->vertexCount); currentGroup->faceMode = tmpFace->vertexCount; } FaceElement* tmpElem = tmpFace->firstElem; while (tmpElem != NULL) { // printf ("%s\n", tmpElem->value); addGLElement(tmpElem); tmpElem = tmpElem->next; } tmpFace = tmpFace->next; } glEnd(); glEndList(); currentGroup = currentGroup->next; } } /** \brief Adds a Face-element (one vertex of a face) with all its information. It does this by searching: 1. The Vertex itself 2. The VertexNormale 3. The VertexTextureCoordinate merging this information, the face will be drawn. */ bool Object::addGLElement (FaceElement* elem) { if (verbose >=3) printf ("importing grafical Element to openGL.\n"); if (elem->texCoordNumber != -1) glTexCoord2fv(vTexture->getArray() + elem->texCoordNumber * 2); if (elem->normalNumber != -1) glNormal3fv(normals->getArray() + elem->normalNumber * 3); if (elem->vertexNumber != -1) glVertex3fv(vertices->getArray() + elem->vertexNumber * 3); } /** \brief A routine that is able to create normals. The algorithm does the following: 1. It calculates creates Vectors for each normale, and sets them to zero. 2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements 3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal. 4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array. */ bool Object::buildVertexNormals () { if (verbose >=2) printf("Normals are being calculated.\n"); Vector* normArray = new Vector [vertices->getCount()/3]; for (int i=0; igetCount()/3;i++) normArray[i] = Vector(.0,.0,.0); int firstTouch; int secondTouch; Vector prevV; Vector nextV; Vector curV; Group* tmpGroup = firstGroup; while (tmpGroup) { Face* tmpFace = tmpGroup->firstFace; while (tmpFace) { if (tmpFace->firstElem) { FaceElement* firstElem = tmpFace->firstElem; FaceElement* prevElem; FaceElement* curElem = firstElem; FaceElement* nextElem; FaceElement* lastElem; // find last Element of the Chain. !! IMPORTANT:the last Element of the Chain must point to NULL, or it will resolv into an infinity-loop. while (curElem) { prevElem = curElem; curElem = curElem->next; } lastElem = prevElem; curElem = firstElem; for (int j=0; jvertexCount; j++) { if (!(nextElem = curElem->next)) nextElem = firstElem; curElem->normalNumber = curElem->vertexNumber; curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]); prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV; nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV; normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV); prevElem = curElem; curElem = curElem->next; } } tmpFace = tmpFace->next; } tmpGroup = tmpGroup->next; } for (int i=0; igetCount()/3;i++) { normArray[i].normalize(); if (verbose >=3) printf ("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z); normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z); } delete []normArray; } /** \brief Includes a default object This will inject a Cube, because this is the most basic object. */ void Object::BoxObject(void) { readVertex ("-0.500000 -0.500000 0.500000"); readVertex ("0.500000 -0.500000 0.500000"); readVertex ("-0.500000 0.500000 0.500000"); readVertex ("0.500000 0.500000 0.500000"); readVertex ("-0.500000 0.500000 -0.500000"); readVertex ("0.500000 0.500000 -0.500000"); readVertex ("-0.500000 -0.500000 -0.500000"); readVertex ("0.500000 -0.500000 -0.500000"); readVertexTexture ("0.000000 0.000000"); readVertexTexture ("1.000000 0.000000"); readVertexTexture ("0.000000 1.000000"); readVertexTexture ("1.000000 1.000000"); readVertexTexture ("0.000000 2.000000"); readVertexTexture ("1.000000 2.000000"); readVertexTexture ("0.000000 3.000000"); readVertexTexture ("1.000000 3.000000"); readVertexTexture ("0.000000 4.000000"); readVertexTexture ("1.000000 4.000000"); readVertexTexture ("2.000000 0.000000"); readVertexTexture ("2.000000 1.000000"); readVertexTexture ("-1.000000 0.000000"); readVertexTexture ("-1.000000 1.000000"); readVertexNormal ("0.000000 0.000000 1.000000"); readVertexNormal ("0.000000 0.000000 1.000000"); readVertexNormal ("0.000000 0.000000 1.000000"); readVertexNormal ("0.000000 0.000000 1.000000"); readVertexNormal ("0.000000 1.000000 0.000000"); readVertexNormal ("0.000000 1.000000 0.000000"); readVertexNormal ("0.000000 1.000000 0.000000"); readVertexNormal ("0.000000 1.000000 0.000000"); readVertexNormal ("0.000000 0.000000 -1.000000"); readVertexNormal ("0.000000 0.000000 -1.000000"); readVertexNormal ("0.000000 0.000000 -1.000000"); readVertexNormal ("0.000000 0.000000 -1.000000"); readVertexNormal ("0.000000 -1.000000 0.000000"); readVertexNormal ("0.000000 -1.000000 0.000000"); readVertexNormal ("0.000000 -1.000000 0.000000"); readVertexNormal ("0.000000 -1.000000 0.000000"); readVertexNormal ("1.000000 0.000000 0.000000"); readVertexNormal ("1.000000 0.000000 0.000000"); readVertexNormal ("1.000000 0.000000 0.000000"); readVertexNormal ("1.000000 0.000000 0.000000"); readVertexNormal ("-1.000000 0.000000 0.000000"); readVertexNormal ("-1.000000 0.000000 0.000000"); readVertexNormal ("-1.000000 0.000000 0.000000"); readVertexNormal ("-1.000000 0.000000 0.000000"); /* normaleLess-testingMode readFace ("1 2 4 3"); readFace ("3 4 6 5"); readFace ("5 6 8 7"); readFace ("7 8 2 1"); readFace ("2 8 6 4"); readFace ("7 1 3 5"); */ readFace ("1/1/1 2/2/2 4/4/3 3/3/4"); readFace ("3/3/5 4/4/6 6/6/7 5/5/8"); readFace ("5/5/9 6/6/10 8/8/11 7/7/12"); readFace ("7/7/13 8/8/14 2/10/15 1/9/16"); readFace ("2/2/17 8/11/18 6/12/19 4/4/20"); readFace ("7/13/21 1/1/22 3/3/23 5/14/24"); }