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

MLoader.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 "MLoader.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 using namespace std; 00022 00023 00024 /****************************** 00025 * Constructors and destructor * 00026 ******************************/ 00027 00028 MLoader::MLoader() 00029 { 00030 } 00031 00032 MLoader::~MLoader() 00033 { 00034 } 00035 00036 00037 /************************ 00038 * Get and set functions * 00039 ************************/ 00040 00041 00042 /****************** 00043 * Other functions * 00044 ******************/ 00045 00046 void MLoader::loadMesh(TriangleMesh *mesh, const string &filename) 00047 { 00048 const float scale = 0.02f; 00049 00050 cout << "Let's parse this M file (" << filename << ")" << endl; 00051 00052 // Open the file 00053 ifstream file(filename.c_str()); 00054 if(!file) 00055 { 00057 cerr << "The file couldn't be opened" << endl; 00058 return; 00059 } 00060 00061 // Clear the triangle mesh 00062 mesh->clear(); 00063 00064 00065 Material *green = mesh->addMaterial(); 00066 green->setAmbient(Color(0.1f, 0.6f, 0.08f)); 00067 green->setDiffuse(Color(0.08f, 0.65f, 0.1f)); 00068 green->setSpecular(Color(0.04f, 0.3f, 0.05f)); 00069 green->setShininess(30.0f); 00070 00071 00072 // Fill in the mesh 00073 char buff[256]; 00074 string line; 00075 int dummy; 00076 float x, y, z; 00077 unsigned int a, b, c; 00078 Vertex *v1, *v2, *v3; 00079 00080 while(!file.eof()) 00081 { 00082 file.getline(buff, 256); 00083 line = buff; 00084 if(line.find("Vertex") == 0) 00085 { 00086 sscanf(line.c_str(), "Vertex %d %f %f %f", &dummy, &x, &y, &z); 00087 mesh->addVertex(Vertex(Vector3(x, y, z) * scale)); 00088 } 00089 else if(line.find("Face") == 0) 00090 { 00091 sscanf(line.c_str(), "Face %d %d %d %d", &dummy, &a, &b, &c); 00092 v1 = mesh->getVertex(a); 00093 v2 = mesh->getVertex(b); 00094 v3 = mesh->getVertex(c); 00095 Triangle *triangle = mesh->addTriangle(Triangle(v1, v2, v3)); 00096 triangle->calculateNormal(); 00097 } 00098 } 00099 00100 00101 // Close the file 00102 file.close(); 00103 00104 // Calculate the vertex normals 00105 mesh->calculateVertexNormals(); 00106 00107 cout << "Done parsing " << mesh->numVertices() << " vertices and " << mesh->numTriangles() << " triangles." << endl; 00108 } 00109 00110 00111 /********************** 00112 * Protected functions * 00113 **********************/

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