質問

How to parse .obj file and load it in opengl.I have the .obj downloaded from Blendswap.com

役に立ちましたか?

解決

Actually there are many solutions if you search in Google. Here is one simple solution as below.

First define one model containing face and vertex info:

class obj3dmodel
{
   struct vertex{
        double x;
        double y;
        double z;
   };
   struct face{
         unsigned int v1,v2,v3;
   };
   std::vector<vertex> vetexes;
   std::vector<face> faces;

public:
  void readfile(const char* filename);
  void draw();
};

Then read file in readfile():

void obj3dmodel::readfile(const char *filename) 
{
   string s;
   ifstream fin(filename);
   if(!fin)
         return;
   while(fin>>s)
   {
         switch(*s.c_str())
         {
         case 'v':
              {
                    vertex v;
                    fin>>v.x>>v.y>>v.z;
                    this->vetexes.push_back(v);
              }
              break;            
         case 'f':
              {
                    face f;
                    fin>>f.v1>>f.v2>>f.v3;
                    faces.push_back(f);
              }
              break;
        }
   }     
}

Now you could use vertex and face information to do draw():

void obj3dmodel::draw()
{
   glBegin(GL_TRIANGLES);
   for(int i=0;i<faces.size();i++)
   {                         
      vertex v1= vetexes[faces[i].v1-1];
      vertex v2=vetexes[faces[i].v2-1];
      vertex v3=vetexes[faces[i].v3-1];

      glVertex3f(v1.x,v1.y,v1.z);
      glVertex3f(v2.x,v2.y,v2.z);
      glVertex3f(v3.x,v3.y,v3.z);
   }
   glEnd(GL_TRIANGLES);
 }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top