2012-11-05 20 views
2

我試圖加載OBJ模型:我試圖加載模型,但它未顯示

# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware 
# File Created: 05.11.2012 20:07:48 

mtllib test.mtl 

# 
# object Box001 
# 

v -12.6264 0.0000 -58.6877 
v -12.6264 0.0000 6.8357 
v 43.9557 0.0000 6.8357 
v 43.9557 0.0000 -58.6877 
v 43.9557 -51.9219 6.8357 
v -12.6264 -51.9219 6.8357 
v -12.6264 -51.9219 -58.6877 
v 43.9557 -51.9219 -58.6877 
# 8 vertices 

vn 0.0000 1.0000 -0.0000 
vn 0.0000 -1.0000 -0.0000 
vn 0.0000 0.0000 1.0000 
vn 1.0000 0.0000 -0.0000 
vn 0.0000 0.0000 -1.0000 
vn -1.0000 0.0000 -0.0000 
# 6 vertex normals 

vt 0.0000 65.5234 0.0000 
vt 0.0000 0.0000 0.0000 
vt 56.5821 0.0000 0.0000 
vt 56.5821 65.5234 0.0000 
vt 0.0000 -51.9219 0.0000 
vt 56.5821 -51.9219 0.0000 
vt 65.5234 0.0000 0.0000 
vt 65.5234 -51.9219 0.0000 
# 8 texture coords 

g Box001 
usemtl wire_229154215 
s 2 
f 1/1/1 2/2/1 3/3/1 4/4/1 
s 4 
f 5/2/2 6/3/2 7/4/2 8/1/2 
s 8 
f 3/3/3 2/2/3 6/5/3 5/6/3 
s 16 
f 4/7/4 3/2/4 5/5/4 8/8/4 
s 32 
f 1/3/5 4/2/5 8/5/5 7/6/5 
s 64 
f 2/7/6 1/2/6 7/5/6 6/8/6 
# 6 polygons 

我有一個名爲模型類:

#ifndef _Model_H_ 
#define _Model_H_ 

#include <vector> 

struct Coordinate{ 
    float X,Y,Z; 
    Coordinate(float x,float y,float z); 
}; 

struct TextureCoordinate{ 
    float U,V; 
    TextureCoordinate(float u,float v); 
}; 

struct Face 
{ 
    int VertexIndex,TextureIndex,NormalIndex; 

    Face(int vi, int ti, int ni); 

}; 

class Model 
{ 
public: 
    Model(void); 
    ~Model(void); 
    bool IsTriangle; 
    std::string Name; 
    std::vector<Coordinate> Vertices; 
    std::vector<Coordinate> VerticesNormals; 
    std::vector<Coordinate> VerticesTexture; 
    std::vector<Face  > Faces; 
    int ModelNumber; 
}; 

#endif 

,我試圖加載它像這樣: -

Model ModelsLoader::LoadModel(const char * ModelName){ 
    std::ifstream fs(ModelName); 
    char temp[256]; 
    std::string * line; 
    Model m; 
    float tempX,tempY,tempZ; 
    int tempA1,tempA2,tempA3,tempA4; 

    if(fs.is_open()){ 

     while (!fs.eof()) 
     { 
      fs.getline(temp,256); 
      Lines.push_back(new std::string(temp)); 
     } 
     int c,slashes; 
     for (int i = 0; i < Lines.size(); i++) 
     { 
      line = Lines[i]; 

      if(line->size() == 0 ||(*line)[0] == '#') continue; 
      else if((*line)[0] == 'v' && (*line)[1] == ' ') 
      { 
       sscanf(line->c_str(),"v %f %f %f",&tempX,&tempY,&tempZ); 
       m.Vertices.push_back(Coordinate(tempX,tempY,tempZ)); 
      } 
      else if((*line)[0] == 'v' && (*line)[1] == 't'){ 
       sscanf(line->c_str(),"vn %f %f %f",&tempX,&tempY,&tempZ); 
       m.VerticesTexture.push_back(Coordinate(tempX,tempY,tempZ)); 
      } 
      else if((*line)[0] == 'v' && (*line)[1] == 'n'){ 
       sscanf(line->c_str(),"vn %f %f %f",&tempX,&tempY,&tempZ); 
       m.VerticesNormals.push_back(Coordinate(tempX,tempY,tempZ)); 
      } 
      else if((*line)[0] == 'f' && (*line)[1] == ' ') 
      { 
       c = std::count(line->begin(),line->end(),' ') - 1; 
       slashes = std::count(line->begin(),line->end(),'/'); 

       if(slashes/c == 2){ 
        int i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12; 
        sscanf(line->c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d",&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8,&i9,&i10,&i11,&i12); 
        m.Faces.push_back(Face(i1-1,i2-1,i3-1)); 
        m.Faces.push_back(Face(i4-1,i5-1,i6-1)); 
        m.Faces.push_back(Face(i7-1,i8-1,i9-1)); 
        m.Faces.push_back(Face(i10-1,i11-1,i12-1)); 
        m.IsTriangle = false; 
       } 
       else if (slashes/c == 3) 
       { 
        int i1,i2,i3,i4,i5,i6,i7,i8,i9; 
        sscanf(line->c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d",&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8,&i9); 
        m.Faces.push_back(Face(i1-1,i2-1,i3-1)); 
        m.Faces.push_back(Face(i4-1,i5-1,i6-1)); 
        m.Faces.push_back(Face(i7-1,i8-1,i9-1)); 
        m.IsTriangle = true; 
       } 

      } 
     } 
     fs.close(); 
     m.ModelNumber = glGenLists(1); 
     glNewList(m.ModelNumber, GL_COMPILE); 
     for (int i = 0; i < m.Faces.size(); i++) 
     { 
      if(m.IsTriangle){ 
      } 
      else 
      { 
       glBegin(GL_QUADS); 
       for (int i = 0; i < m.Faces.size(); i++) 
       { 
        glVertex3f(m.VerticesNormals[m.Faces[i].NormalIndex].X,m.VerticesNormals[m.Faces[i].NormalIndex].Y,m.VerticesNormals[m.Faces[i].NormalIndex].Z); 
        glNormal3f(m.Vertices[m.Faces[i].VertexIndex].X,m.Vertices[m.Faces[i].VertexIndex].Y,m.Vertices[m.Faces[i].VertexIndex].Z); 
       } 
       glEnd(); 
      } 
     } 
     glEndList(); 
    } 

    Clear(); 
    return m; 
} 

現在當我打電話列表它沒有顯示任何東西: -

void drawScene() { 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective 
    glLoadIdentity(); //Reset the drawing perspective 

    camera(); 

    glCallList(model.ModelNumber); 

    glutSwapBuffers(); //Send the 3D scene to the screen 
} 

什麼在這裏做worng!?

+1

你是否正在使用當前GL上下文調用'ModelsLoader :: LoadModel()'? – genpfault

+0

抱歉沒有得到你的意思:\ – Abanoub

+1

'glutCreateWindow()'創建一個[OpenGL上下文](http://www.opengl.org/wiki/OpenGL_Context)並使其成爲最新。如果你在*'glutCreateWindow()'之前調用'ModelsLoader :: LoadModel()'*,那麼所有GL調用將失敗。 – genpfault

回答

0

請確保您在之後通過ModelsLoader::LoadModel()調用glutCreateWindow()創建OpenGL上下文。

如果您在之前致電LoadModel()您有一個當前GL上下文,它內部的所有GL調用都將失敗。

相關問題