tgModelLoader.cpp
Go to the documentation of this file.
00001 
00002 
00003 #include <stdexcept>
00004 #include <wordexp.h>
00005 #include <sstream>
00006 
00007 #include <blort/TomGine/tgModelLoader.h>
00008 
00009 using namespace TomGine;
00010 using namespace std;
00011 
00012 
00013 std::string expandName ( const std::string &fileString ) {
00014   std::stringstream ss;
00015     wordexp_t p;
00016     char** w;
00017     wordexp( fileString.c_str(), &p, 0 );
00018     w = p.we_wordv;
00019     for (size_t i=0; i < p.we_wordc; i++ ) {
00020       ss << w[i];
00021     }
00022     wordfree( &p );
00023     return ss.str();
00024 }
00025 
00026 
00027 
00028 // *** PRIVATE ***
00029 
00030 // Tests if property of file is available in list (data structure)
00031 bool tgModelLoader::propertyIsInList(PlyProperty* prop, PlyProperty* list, int n, int* index){
00032         
00033         for(int i=0; i<n; i++){
00034                 if(equal_strings(prop->name, list[i].name)){
00035                         *index = i;
00036                         return true;
00037                 }
00038         }
00039         return false;
00040 }
00041 
00042 // *** PUBLIC ***
00043 
00044 tgModelLoader::tgModelLoader(){
00045 }
00046 
00047 tgModelLoader::~tgModelLoader(){
00048 
00049 }
00050 
00051 // read ply file
00052 bool tgModelLoader::LoadPly(tgModel &model, const char* filename){
00053         PlyFile* plyfile;
00054         int i,j;
00055         int nelems;
00056         char **elist;
00057         int file_type;
00058         float version;
00059         char *elem_name;
00060         //PlyElement *elem_ptr;
00061         PlyProperty **plist;
00062         //PlyProperty* prop_ptr;
00063         int num_elems;
00064         int nprops;
00065         int index;
00066         char** obj_info;
00067         int num_obj_info;   
00068         
00069         int num_vertices = 0;
00070         int num_faces = 0;
00071         //int num_edges;
00072         PlyVertex* plyvertexlist = 0;
00073         PlyFace* plyfacelist = 0;
00074         PlyEdge* plyedgelist = 0;
00075         vector<string> texFilenames;
00076         string strFilename = expandName(filename);
00077         
00078         
00079         // **********************************************
00080         // Read data from ply file
00081         
00082         // open file
00083         plyfile = ply_open_for_reading(strFilename.c_str(), &nelems, &elist, &file_type, &version);
00084         if(plyfile==0){
00085                 char errmsg[256];
00086                 sprintf(errmsg,"[tgModelLoader::read] Cannot open ply file '%s'\n", strFilename.c_str());
00087                 throw runtime_error(errmsg);
00088         }
00089 //      sprintf(model.m_modelname, "%s", filename);
00090         
00091         // Load texture files from obj_info (=texture-filename)
00092         obj_info = ply_get_obj_info(plyfile, &num_obj_info);
00093         if(num_obj_info < 1){
00094         //printf("[tgModelLoader::read] Warning no texture found in model %s\n", filename);
00095         }else if(num_obj_info >= 1){
00096                 for(i=0; i<num_obj_info; i++){
00097                         texFilenames.push_back("");
00098                         texFilenames[i].append(filename, 0, strFilename.find_last_of("/")+1);
00099                         texFilenames[i].append(obj_info[i]);
00100 //                      printf("[tgModelLoader::read] obj_info: %s\n", texFilenames[i].c_str());
00101                 }
00102         }
00103         
00104         // list of property information for a vertex
00105         PlyProperty vert_props[] = { 
00106                 {(char*)"x", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,x), 0, 0, 0, 0},
00107                 {(char*)"y", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,y), 0, 0, 0, 0},
00108                 {(char*)"z", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,z), 0, 0, 0, 0},
00109                 {(char*)"nx", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,nx), 0, 0, 0, 0},
00110                 {(char*)"ny", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,ny), 0, 0, 0, 0},
00111                 {(char*)"nz", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,nz), 0, 0, 0, 0},
00112                 {(char*)"s", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,s), 0, 0, 0, 0},
00113                 {(char*)"t", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,t), 0, 0, 0, 0},
00114                 {(char*)"red", PLY_UCHAR, PLY_UCHAR, offsetof(PlyVertex,r), 0, 0, 0, 0},
00115                 {(char*)"green", PLY_UCHAR, PLY_UCHAR, offsetof(PlyVertex,g), 0, 0, 0, 0},
00116                 {(char*)"blue", PLY_UCHAR, PLY_UCHAR, offsetof(PlyVertex,b), 0, 0, 0, 0}
00117         };
00118 
00119         // list of property information for a face
00120         PlyProperty face_props[] = { /* list of property information for a vertex */
00121                 {(char*)"vertex_indices", PLY_USHORT, PLY_UINT, offsetof(PlyFace,v),
00122                         1, PLY_USHORT, PLY_UINT, offsetof(PlyFace,nverts)},
00123         };
00124         
00125         // list of property information for an edge
00126         PlyProperty edge_props[] = { 
00127                 {(char*)"start", PLY_USHORT, PLY_USHORT, offsetof(PlyEdge,start), 0, 0, 0, 0},
00128                 {(char*)"end", PLY_USHORT, PLY_USHORT, offsetof(PlyEdge,end), 0, 0, 0, 0}
00129         };
00130         
00131         for(i=0; i<nelems; i++){
00132                 // get description of element
00133                 elem_name = elist[i];
00134                 plist = ply_get_element_description (plyfile, elem_name, &num_elems, &nprops);
00135                         
00136                 // *** Read Vertices ***
00137                 if (equal_strings ((char*)"vertex", elem_name)) {
00138                         // allocate memory for vertices
00139                         num_vertices = num_elems;
00140                         plyvertexlist = (PlyVertex*)malloc(sizeof(PlyVertex) * num_vertices);
00141                                         
00142                         // setup property specification for elements
00143                         for(j=0; j<nprops; j++){
00144                                 if(propertyIsInList(plist[j], vert_props, 11, &index))
00145                                 ply_get_property(plyfile, elem_name, &vert_props[index]);
00146                         }
00147                                                         
00148                         // grab all vertex elements
00149                         for(j=0; j<num_elems; j++){
00150                                 ply_get_element(plyfile, &plyvertexlist[j]); 
00151                         }
00152                                 
00153                 }
00154                                 
00155                 // *** Read Faces ***
00156                 if (equal_strings ((char*)"face", elem_name)) {
00157                 // allocate memory for faces
00158                         num_faces = num_elems;
00159                         plyfacelist = (PlyFace*)malloc(sizeof(PlyFace)*num_faces);
00160                                                                         
00161                         // setup property specification for elements
00162                         for(j=0; j<nprops && j<1; j++){
00163                                 ply_get_property(plyfile, elem_name, &face_props[j]);
00164                         }
00165                         
00166                         // grab all face elements
00167                         for(j=0; j<num_elems; j++){
00168                                 ply_get_element(plyfile, &plyfacelist[j]);
00169                                 //printf ("face: %d %d %d %d\n", m_facelist[j].v[0], m_facelist[j].v[1], m_facelist[j].v[2], m_facelist[j].v[3]);
00170                         }            
00171                 }
00172                                 
00173                 // *** Read Edges ***
00174                 if (equal_strings ((char*)"edge", elem_name)) {
00175                         // allocate memory for edges
00176                         plyedgelist = (PlyEdge*)malloc(sizeof(PlyEdge)*num_elems);
00177                         //num_edges = num_elems;
00178                         
00179                         // setup property specification for elements
00180                         for(j=0; j<nprops && j<2; j++){
00181                                         ply_get_property(plyfile, elem_name, &edge_props[j]);
00182                         }
00183                         
00184                         // grab all edge elements
00185                         for(j=0; j<num_elems; j++){
00186                                         ply_get_element(plyfile, &plyedgelist[j]);
00187                                         //printf("edge: %d %d\n", m_edgelist[j].start, m_edgelist[j].end);
00188                         }
00189                 }
00190         }
00191         ply_close(plyfile);
00192         
00193         
00194         // **********************************************
00195         // Convert ply to model 
00196         
00197         // Parse through vertex list
00198         for(i=0; i<num_vertices; i++){
00199                 tgVertex v;
00200                 v.pos.x = plyvertexlist[i].x;
00201                 v.pos.y = plyvertexlist[i].y;
00202                 v.pos.z = plyvertexlist[i].z;
00203                 v.normal.x = plyvertexlist[i].nx;
00204                 v.normal.y = plyvertexlist[i].ny;
00205                 v.normal.z = plyvertexlist[i].nz;
00206                 v.texCoord.x = plyvertexlist[i].s;
00207                 v.texCoord.y = plyvertexlist[i].t;
00208                 model.m_vertices.push_back(v);
00209 //              printf("v: %f %f %f\n", v.pos.x, v.pos.y, v.pos.z);
00210         }
00211         
00212         // Parse through face list
00213         for(i=0; i<num_faces; i++){
00214                 tgFace f;
00215                 for(j=0; j<plyfacelist[i].nverts; j++){
00216                         f.v.push_back(plyfacelist[i].v[j]);
00217                 }
00218                 model.m_faces.push_back(f);
00219         }
00220         
00221 //      for(i=0; i<num_edges; i++){
00222 //              Model::Edge e;
00223 //              e.start = plyedgelist[i].start;
00224 //              e.end = plyedgelist[i].end;
00225 //              model.m_edgelist.push_back(e);
00226 //      }
00227         
00228 //      model.ComputeFaceNormals();
00229         
00230         // **********************************************
00231         // Clean up
00232         if(plyvertexlist) free(plyvertexlist);
00233         if(plyfacelist) free(plyfacelist);
00234         if(plyedgelist) free(plyedgelist);
00235         
00236         return true;
00237 }
00238 
00239 bool tgModelLoader::SavePly(tgModel &model, const char* filename){
00240         FILE* pFile;
00241         
00242         if(!(pFile=fopen( filename, "w"))){
00243                 printf("[tgModelLoader::SavePly] Warning file not found '%s'\n", filename);
00244                 return false;
00245         }
00246         
00247         fputs("testing\nhallo\n", pFile);
00248         
00249         
00250         fclose(pFile);
00251         return true;    
00252 }
00253 
00254 


blort
Author(s): Thomas Mörwald , Michael Zillich , Andreas Richtsfeld , Johann Prankl , Markus Vincze , Bence Magyar
autogenerated on Wed Aug 26 2015 15:24:12