00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "TriangleMesh.h"
00013 #include "xcplusplus.h"
00014
00015 #include <fstream>
00016 #include <iostream>
00017 #include <cassert>
00018 #include <string>
00019 #include <GL/glut.h>
00020 #include <cfloat>
00021
00022
00023 using namespace std;
00024
00025
00026 TriangleMesh :: TriangleMesh(const string obj_fname) : texture(0)
00027 {
00028 ifstream is(obj_fname.c_str());
00029
00030 if(is)
00031 {
00032 bool no_normal = true;
00033 bool no_tex = true;
00034
00035 float
00036 minx = FLT_MAX, maxx = FLT_MIN,
00037 miny = FLT_MAX, maxy = FLT_MIN,
00038 minz = FLT_MAX, maxz = FLT_MIN;
00039
00040 while( skip_comment_lines(is, '#') )
00041 {
00042 string ele_id;
00043 float x, y, z;
00044
00045 if( ! (is >> ele_id) ) break;
00046
00047 if(ele_id == "v")
00048 {
00049 is >> x >> y >> z;
00050 V.push_back( Point(x, y, z) );
00051
00052 if(x < minx) minx = x;
00053 else if(x > maxx) maxx = x;
00054
00055 if(y < miny) miny = y;
00056 else if(y > maxy) maxy = y;
00057
00058 if(z < minz) minz = z;
00059 else if(z > maxz) maxz = z;
00060 }
00061 else if(ele_id == "vt")
00062 {
00063 no_tex = false;
00064 is >> x >> y >> z;
00065 is.clear();
00066 T.push_back( RGB(x, y, z) );
00067 }
00068 else if(ele_id == "vn")
00069 {
00070 no_normal = false;
00071 is >> x >> y >> z;
00072 if(! is.good())
00073 {
00074 x = y = z = 0.0;
00075 is.clear();
00076 skip_line(is);
00077 }
00078 N.push_back( Vector(x, y, z) );
00079 }
00080 else if(ele_id == "f")
00081 {
00082 int vi[10];
00083 int ni[10] = { -1, -1, -1, -1, };
00084 int ti[10] = { -1, -1, -1, -1, };
00085 int i = 0;
00086 for(char c; i<10; ++i)
00087 {
00088 if(no_tex && no_normal) is >> vi[i];
00089 else if(no_tex) is >> vi[i] >> c >> c >> ni[i];
00090 else if(no_normal) is >> vi[i] >> c >> ti[i];
00091 else is >> vi[i] >> c >> ti[i] >> c >> ni[i];
00092
00093 if( ! is.good() ) break;
00094 }
00095 for(int k=0; k<=i-3; k++)
00096 {
00097 F.push_back( TriangleFace(vi[0]-1, vi[k+1]-1, vi[k+2]-1, ti[0]-1, ti[k+1]-1, ti[k+2]-1, ni[0]-1, ni[k+1]-1, ni[k+2]-1) );
00098 }
00099 is.clear();
00100 }
00101 else
00102 skip_line(is);
00103 }
00104 is.close();
00105
00106 Point
00107 min(minx, miny, minz),
00108 max(maxx, maxy, maxz),
00109 center( (min + max) / 2 );
00110
00111 for(vector<Point> :: iterator itr = V.begin(); itr != V.end(); ++itr)
00112 {
00113 *itr -= center;
00114 }
00115
00116 has_normal = N.size();
00117 has_texture = T.size();
00118 total_triangles = F.size();
00119
00120 cout << "total vertices " << V.size() << endl;
00121 cout << "total normals " << N.size() << endl;
00122 cout << "total texture coordinates " << T.size() << endl;
00123 cout << "total triangles " << F.size() << endl;
00124 cout << "min pnt is " << min << endl;
00125 cout << "max pnt is " << max << endl;
00126 }
00127 else
00128 cerr << "Can't open input file " << obj_fname << endl;
00129 }
00130
00131