Original Post
removed [Edited by - JoshWalters on December 20, 2007 2:56:38 AM]
class CObj{ CObj( const std::string &fileName ); ...}CObj::CObj( const std::string &fileName ) : enableTexture( false ) , enableNormal( false ){ string line, buf; Vertex tempVertices, tempNormals; TextureCoord tempTextureCoord; Face tempFace; ifstream fin( fileName.c_str() ); if(!fin.is_open()) { throw std::exception("Error: Could not open "); } ....CObj o;o.Load("filename.txt");o.Load("filename.txt");o.Load("filename.txt");o.Load("filename.txt");o.Load("filename.txt");typedef std::vector< std::string > TokenizedLine;// all public, make it a structstruct Vertex{ // RAII again Vertex( const TokenizedLine &tokens ) { if(tokens.size() < 4) throw std::exception("Error reading vertex"); x = atof(tokens[1].c_str()); y = atof(tokens[2].c_str()); z = atof(tokens[3].c_str()); } float x, y, z;};struct TextureCoord{ TextureCoord( const TokenizedLine &tokens ) { if(tokens.size() < 3) throw std::exception("Error creating texture, need 2 values"); u = atof(tokens[1].c_str()); v = atof(tokens[2].c_str()); } float u, v;};class CObj{public: CObj( const std::string &filename );private: void scanForFlags( std::ifstream &file ); void parseContents( std::ifstream &file );}CObj::CObj( const std::string &filename ){ std::ifstream f( filename ); scanForFlags( f ); f.seekp(0, std::ios_base::beg); parseContents( f ); f.close();}CObj::parseContents( std::ifstream &file ){ std::string line; while(!file.eof()) { getline(fin,line); stringstream ss(line); TokenizedLine tokens; while(ss >> buf) tokens.push_back(buf.c_str()); if(line.compare(0,2,"v ",0,2) == 0) { Vertices.push_back( Vertex( tokens) ); } else if(line.compare(0,2,"vt",0,2) == 0) { TextureCoords.push_back( TextureCoords( tokens ) ); } else ... }}class Face{ Vertex *v1, *v2, *v3; Normal *n1, *n2, *n3; TextureCoord *t1, *t2, *t3;};void renderFace( Face &f ){ if(model->enableTexture ) glTexCoord2f( f.t1->u, f.t1->v); if(model->enableNormal ) glNormal3f( f.n1->x, f.n1->y, f.n1->z); glVertex3f( f.v1->x, f.v1->y, f.v1->z); if(model->enableTexture ) glTexCoord2f( f.t2->u, f.t2->v); if(model->enableNormal ) glNormal3f( f.n2->x, f.n2->y, f.n2->z); glVertex3f( f.v2->x, f.v2->y, f.v2->z); if(model->enableTexture ) glTexCoord2f( f.t3->u, f.t3->v ); if(model->enableNormal ) glNormal3f( f.n3->x, f.n3->y, f.n3->z); glVertex3f( f.v3->x, f.v3->y, f.v3->z); }}void renderFaces(){ glBegin(GL_TRIANGLES); for(size_t size = 0; size < model->Faces.size(); size+=1) { renderFace( model->Faces[size] ); } glEnd();}This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more