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
00029
00030
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
00043
00044 tgModelLoader::tgModelLoader(){
00045 }
00046
00047 tgModelLoader::~tgModelLoader(){
00048
00049 }
00050
00051
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
00061 PlyProperty **plist;
00062
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
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
00081
00082
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
00090
00091
00092 obj_info = ply_get_obj_info(plyfile, &num_obj_info);
00093 if(num_obj_info < 1){
00094
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
00101 }
00102 }
00103
00104
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
00120 PlyProperty face_props[] = {
00121 {(char*)"vertex_indices", PLY_USHORT, PLY_UINT, offsetof(PlyFace,v),
00122 1, PLY_USHORT, PLY_UINT, offsetof(PlyFace,nverts)},
00123 };
00124
00125
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
00133 elem_name = elist[i];
00134 plist = ply_get_element_description (plyfile, elem_name, &num_elems, &nprops);
00135
00136
00137 if (equal_strings ((char*)"vertex", elem_name)) {
00138
00139 num_vertices = num_elems;
00140 plyvertexlist = (PlyVertex*)malloc(sizeof(PlyVertex) * num_vertices);
00141
00142
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
00149 for(j=0; j<num_elems; j++){
00150 ply_get_element(plyfile, &plyvertexlist[j]);
00151 }
00152
00153 }
00154
00155
00156 if (equal_strings ((char*)"face", elem_name)) {
00157
00158 num_faces = num_elems;
00159 plyfacelist = (PlyFace*)malloc(sizeof(PlyFace)*num_faces);
00160
00161
00162 for(j=0; j<nprops && j<1; j++){
00163 ply_get_property(plyfile, elem_name, &face_props[j]);
00164 }
00165
00166
00167 for(j=0; j<num_elems; j++){
00168 ply_get_element(plyfile, &plyfacelist[j]);
00169
00170 }
00171 }
00172
00173
00174 if (equal_strings ((char*)"edge", elem_name)) {
00175
00176 plyedgelist = (PlyEdge*)malloc(sizeof(PlyEdge)*num_elems);
00177
00178
00179
00180 for(j=0; j<nprops && j<2; j++){
00181 ply_get_property(plyfile, elem_name, &edge_props[j]);
00182 }
00183
00184
00185 for(j=0; j<num_elems; j++){
00186 ply_get_element(plyfile, &plyedgelist[j]);
00187
00188 }
00189 }
00190 }
00191 ply_close(plyfile);
00192
00193
00194
00195
00196
00197
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
00210 }
00211
00212
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
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
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