MeshLoader.cpp
Go to the documentation of this file.00001 /**<!--------------------------------------------------------------------> 00002 @file MeshLoader.cpp 00003 @author Travis Fischer (fisch0920@gmail.com) 00004 @author Matthew Jacobs (jacobs.mh@gmail.com) 00005 @date Spring 2008 00006 00007 @brief 00008 static class which loads mesh data from external files in either OBJ 00009 or PLY formats (file extensions '.obj' and '.ply' respectively) 00010 <!-------------------------------------------------------------------->**/ 00011 00012 #include "MeshLoader.h" 00013 #include "Mesh.h" 00014 00015 #include "MeshLoaderOBJ.h" 00016 #include "MeshLoaderPLY.h" 00017 00018 #include <fstream> 00019 #include <objparser/obj.h> 00020 #include <plyparser/ply.h> 00021 00022 Mesh *MeshLoader::load(const std::string &fileName) { 00023 const std::string &suffix = fileName.substr(fileName.rfind('.') + 1); 00024 MeshData data; 00025 00026 if (suffix == "obj" || suffix == "OBJ") { 00027 MeshLoaderOBJ loader(data); 00028 00029 if (!loader.load(fileName, data)) 00030 return false; 00031 } else if (suffix == "ply" || suffix == "PLY") { 00032 MeshLoaderPLY loader(data); 00033 00034 if (!loader.load(fileName, data)) 00035 return false; 00036 } else { 00037 cerr << "unable to infer mesh type '" << suffix << "' from file '" 00038 << fileName << "'" << endl; 00039 00040 return NULL; 00041 } 00042 00043 return new Mesh(data); 00044 } 00045 00046 bool MeshLoader::load(const std::string &fileName, MeshData &outData) { 00047 std::ifstream fileStream; 00048 fileStream.open(fileName.c_str()); 00049 00050 if (!fileStream.is_open()) { 00051 cerr << "error opening file '" << fileName << "'" << endl; 00052 return NULL; 00053 } 00054 00055 outData.fileName = fileName; 00056 return _load(fileStream, outData); 00057 } 00058 00059 bool MeshLoader::save(Mesh *mesh, const std::string &fileName) { 00060 std::ofstream out; 00061 ASSERT(mesh); 00062 00063 { // attempt to open the given file 00064 const std::string &suffix = fileName.substr(fileName.rfind('.') + 1); 00065 if (suffix != "obj" && suffix != "OBJ") { 00066 cerr << "warning: saving mesh out to file in OBJ format, but requested filename '" << fileName << "' does not end with '.obj'" << endl; 00067 } 00068 00069 out.open(fileName.c_str()); 00070 00071 if (!out.is_open()) { 00072 cerr << "error opening file '" << fileName << "'" << endl; 00073 return false; 00074 } 00075 } 00076 00077 const unsigned noVertices = mesh->getNoVertices(); 00078 const unsigned noNormals = mesh->getNoNormals(); 00079 const unsigned noTriangles = mesh->getNoTriangles(); 00080 00081 const Vector3 *vertices = mesh->getVertices(); 00082 const Vector3 *normals = mesh->getNormals(); 00083 const MeshTriangle *triangles = mesh->getTriangles(); 00084 00085 // print out a header, followed by vertices, normals, and faces 00086 out << "# OBJ mesh exported by the Milton rendering framework" << endl << endl; 00087 00088 out << "# vertices" << endl; 00089 for(unsigned i = 0; i < noVertices; ++i) 00090 out << "v " << vertices[i][0] << " " << vertices[i][1] << " " << vertices[i][2] << endl; 00091 out << endl; 00092 00093 if (noNormals > 0) { 00094 out << "# normals" << endl; 00095 for(unsigned i = 0; i < noNormals; ++i) 00096 out << "vn " << normals[i][0] << " " << normals[i][1] << " " << normals[i][2] << endl; 00097 out << endl; 00098 } 00099 00100 out << "# faces (all polygons have been triangulated for ease-of-use)" << endl; 00101 for(unsigned i = 0; i < noTriangles; ++i) { 00102 const MeshTriangle &t = triangles[i]; 00103 // note: OBJ indexing starts at one, not zero 00104 00105 out << "f "; 00106 for(unsigned j = 0; j < 3; ++j) 00107 out << (t.data[j] + 1) << "//" << (t.nData[j] + 1) << " "; 00108 out << endl; 00109 } 00110 out << endl; 00111 00112 return true; 00113 } 00114
Generated on 28 Feb 2009 for Milton by
1.5.6