// // コンピュータグラフィックス特論U // 幾何形状データ(Obj形式)の読み込み&描画のサンプルプログラム // #ifndef _WAVEFRONT_OBJ_H_ #define _WAVEFRONT_OBJ_H_ #include #include #include using namespace std; // // Alias|Wavefront Obj形式 の幾何形状データ // class WavefrontObj { public: /* 構造体 */ // 頂点 struct Vertex { double x, y, z; }; // カラー struct Color { double r, g, b; }; struct Face; // グループ struct Group { string name; vector< int > faces; }; // マテリアル struct Material { string name; Color ambient; Color diffuse; Color specular; string diffuse_map; vector< int > faces; }; // エレメント struct Element { Group * group; Material * material; }; // 頂点 struct Face : public Element { vector< int > data; int NumVertex() const { return data.size() / 3; } int GetVertex( int n ) const { return data[ n*3 ]; } int GetTexture( int n ) const { return data[ n*3+1 ]; } int GetNormal( int n ) const { return data[ n*3+2 ]; } }; private: /* 内部データ */ vector< Group * > groups; vector< Material * > materials; vector< Vertex > vertices; vector< Vertex > normals; vector< Vertex > t_coords; vector< Face * > faces; map< string, Group * > group_index; map< string, Material * > mtl_index; public: /* コンストラクタ・デストラクタ */ WavefrontObj( const char * file_name ); ~WavefrontObj(); protected: // マテリアルファイルの読み込み void LoadMaterialFile( const char * file_name ); public: /* アクセサ */ const int NumGroups() const { return groups.size(); } const Group * GetGroup( int n ) const { return groups[n]; } const Group * GetGroup( const string & s ) const { return ( (group_index.find( s ) != group_index.end()) ? (*group_index.find( s )).second : NULL ); } const int NumMaterials() const { return materials.size(); } const Material * GetMaterial( int n ) const { return materials[n]; } const int NumVertices() const { return vertices.size(); } const Vertex & GetVertex( int n ) const { return vertices[n]; } const int NumNormals() const { return normals.size(); } const Vertex & GetNormal( int n ) const { return normals[n]; } const int NumTextureCoords() const { return t_coords.size(); } const Vertex & GetTextureCoords( int n ) const { return t_coords[n]; } const int NumFaces() const { return faces.size(); } const Face * GetFace( int n ) const { return faces[n]; } // OpenGLを使用してオブジェクトを描画 void Draw(); }; #endif // _WAVEFRONT_OBJ_H_