Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ASCLoader.cpp

00001 /*************************************************************************** 00002 * This file is part of OpenCAL: Open Computer Animation Library * 00003 * I created OpenCAL as my master's thesis Computer Science (multimedia) * 00004 * at the tUL university in Diepenbeek, Belgium * 00005 * * 00006 * Copyright (C) 2003-2004 by Jeroen Dierckx * 00007 * jeroen.dierckx@student.luc.ac.be * 00008 * * 00009 ***************************************************************************/ 00010 00011 // Includes 00012 #include "ASCLoader.h" 00013 #include <OpenCAL/TriangleMesh.h> 00014 #include <OpenCAL/Vertex.h> 00015 #include <OpenCAL/Triangle.h> 00016 using namespace OpenCAL; 00017 00018 #include <OpenCAL/Vector3.h> 00019 using namespace Utils; 00020 00021 #include <fstream> 00022 using namespace std; 00023 00024 00025 /****************************** 00026 * Constructors and destructor * 00027 ******************************/ 00028 00029 ASCLoader::ASCLoader() 00030 { 00031 } 00032 00033 ASCLoader::~ASCLoader() 00034 { 00035 } 00036 00037 00038 /************************ 00039 * Get and set functions * 00040 ************************/ 00041 00042 00043 /****************** 00044 * Other functions * 00045 ******************/ 00046 00047 void ASCLoader::loadMesh(TriangleMesh *mesh, const string &filename) 00048 { 00049 const float scale = 1.0f; 00050 00051 cout << "Let's parse this ASC file (" << filename << ")" << endl; 00052 00053 // Open the file 00054 ifstream file(filename.c_str()); 00055 if(!file) 00056 { 00058 cerr << "The file couldn't be opened" << endl; 00059 return; 00060 } 00061 00062 // Clear the triangle mesh 00063 mesh->clear(); 00064 00065 00066 Material *green = mesh->addMaterial(); 00067 green->setAmbient(Color(0.1f, 0.6f, 0.08f)); 00068 green->setDiffuse(Color(0.08f, 0.65f, 0.1f)); 00069 green->setSpecular(Color(0.04f, 0.3f, 0.05f)); 00070 green->setShininess(30.0f); 00071 00072 00073 // Fill in the mesh 00074 char buff[256]; 00075 string line; 00076 int dummy; 00077 unsigned int numVertices, numFaces; 00078 float x, y, z; 00079 unsigned int a, b, c; 00080 Vertex *v1, *v2, *v3; 00081 00082 while(!file.eof()) 00083 { 00084 file.getline(buff, 256); 00085 line = buff; 00086 if(line.find("Tri-Mesh") == 0) 00087 { 00088 sscanf(line.c_str(), "Tri-Mesh, Vertices: %d Faces: %d", &numVertices, &numFaces); 00089 mesh->reserveVertices(numVertices); 00090 mesh->reserveTriangles(numFaces); 00091 } 00092 if(line.find("Vertex") == 0 && line.find("Vertex List") != 0) 00093 { 00094 sscanf(line.c_str(), "Vertex %d: X:%f Y:%f Z:%f", &dummy, &x, &y, &z); 00095 mesh->addVertex(Vertex(Vector3(x, y, z) * scale)); 00096 } 00097 else if(line.find("Face") == 0 && line.find("Face list") != 0) 00098 { 00099 sscanf(line.c_str(), "Face %d: A:%d B:%d C:%d AB:%d BC:%d CA:%d", &dummy, &a, &b, &c, &dummy, &dummy, &dummy); 00100 v1 = mesh->getVertex(a); 00101 v2 = mesh->getVertex(b); 00102 v3 = mesh->getVertex(c); 00103 Triangle *triangle = mesh->addTriangle(Triangle(v1, v2, v3)); 00104 triangle->setMaterial(green); 00105 triangle->calculateNormal(); 00106 } 00107 } 00108 00109 00110 // Close the file 00111 file.close(); 00112 00113 // Calculate the vertex normals 00114 mesh->calculateVertexNormals(); 00115 00116 cout << "Done parsing " << mesh->numVertices() << " vertices and " << mesh->numTriangles() << " triangles." << endl; 00117 } 00118 00119 00120 /********************** 00121 * Protected functions * 00122 **********************/

Generated on Sun Aug 15 19:19:22 2004 for OpenCAL: Open Computer Animation Library by doxygen 1.3.8