2013-04-30 85 views
2

我已經寫了一個C++ Obj文件加載器,但我無法正常工作。問題是,當解析一個簡單的OBJ文件,如下所示:使用Wavefront Obj瞭解法線指數

# Blender v2.62 (sub 0) OBJ File: '' 
# www.blender.org 
mtllib cube.mtl 
o Cube 
v 1.000000 -1.000000 -1.000000 
v 1.000000 -1.000000 1.000000 
v -1.000000 -1.000000 1.000000 
v -1.000000 -1.000000 -1.000000 
v 1.000000 1.000000 -0.999999 
v 0.999999 1.000000 1.000001 
v -1.000000 1.000000 1.000000 
v -1.000000 1.000000 -1.000000 
vn 0.000000 0.000000 -1.000000 
vn -1.000000 -0.000000 -0.000000 
vn -0.000000 -0.000000 1.000000 
vn 1.000000 -0.000000 0.000000 
vn 1.000000 0.000000 0.000001 
vn -0.000000 1.000000 0.000000 
vn 0.000000 -1.000000 0.000000 
usemtl Material 
s off 
f 5//1 1//1 4//1 
f 5//1 4//1 8//1 
f 3//2 7//2 8//2 
f 3//2 8//2 4//2 
f 2//3 6//3 3//3 
f 6//3 7//3 3//3 
f 1//4 5//4 2//4 
f 5//5 6//5 2//5 
f 5//6 8//6 6//6 
f 8//6 7//6 6//6 
f 1//7 2//7 3//7 
f 1//7 3//7 4//7 

我無法理解正常傳遞給OpenGL的正確方法。我總是得到的結果是這樣的:enter image description here

ObjLoader.h

#include <Eigen/Core> 

class ObjLoader 
{ 
public: 
    ObjLoader(); 
    bool load(const std::string &filename); 

    void draw(); 
private: 
    bool loadFace(const std::string &line,int lineNumber); 


    std::vector< Eigen::Vector3d> verticesCoord,verticesNormals; 
    std::vector< Eigen::Vector2d> textureCoords; 
    std::vector<GLuint> vertexIndices,normalIndices,textureIndices; 
    Eigen::Vector3d calculateNormal(const Eigen::Vector3d &coord1, const Eigen::Vector3d &coord2, const Eigen::Vector3d &coord3); 
    std::string mtlFile; 
    unsigned int nVerticesPerFace; 

}; 

ObjLoader.cpp

#include <iostream> 
#include <fstream> 
#include <string> 
#include <sstream> 
#include <Eigen/Core> 
#include "ObjLoader.h" 

using namespace std; 
using namespace Eigen; 

ObjLoader::ObjLoader() 
{ 

} 

bool ObjLoader::load(const string &filename) 
{ 
    ifstream is(filename.c_str()); 
    if (is.is_open()) 
    { 
    cerr << "File " + filename + " loaded successfully" << endl; 
    } 

    std::vector<Vector3d> temporaryNormals; // a vector to contain the normals as they are read from the obj 

    string line; 
    unsigned int lineNumber=0; 
    while (getline(is,line)) 
    { 
    lineNumber++; 
    if (line.empty() || line.at(0)=='#') 
     continue; 
    if (line.substr(0,6)=="mtllib") 
    { 
     this->mtlFile = line.substr(7,line.size()-1); 
     cerr << "MTLIB support file= " << mtlFile << endl; 
    } 
    stringstream stream(line); 
    char identifier ; 
    stream >> std::skipws >> identifier; 

    char specifier; 
    stream >> specifier; 
    if (specifier != 't' && specifier != 'n' && specifier!='p') 
    { 
     stream.seekg(0); 
     specifier=0; 
    } 

    switch (identifier) 
    { 
    case 'v': //is a vertex specification 
    { 
     switch (specifier) // if there is a space then is a simple vertex coordinates 
     { 
     case 0: 
     { 
      char tmp; stream >> tmp; 
      Eigen::Vector3d vertex(0.0,0.0,0.0); 
      stream >> vertex[0] >> vertex[1] >> vertex[2]; 
      this->verticesCoord.push_back(vertex); 
     } 
      break; 
     case 't': 
     { 
      Eigen::Vector2d textures(0,0); 
      stream >> textures[0] >> textures[1]; 
      this->textureCoords.push_back(textures); 
     } 
      break; 
     case 'n': 
     { 
      Eigen::Vector3d vertexNormal(0,0,0); 
      stream >> vertexNormal[0] >> vertexNormal[1] >> vertexNormal[2]; 
      temporaryNormals.push_back(vertexNormal); 
     } 
      break; 
     } 
    } 
     break; 
    case 'f': // is a face specification 
    { 
     this->loadFace(line,lineNumber); 
    } 
     break; 
    } 
    } 

    // Rearrange the normals 
    verticesNormals.resize(temporaryNormals.size(),Vector3d(1,1,1)); 
    for(unsigned int i=0;i<vertexIndices.size();i++) 
    { 
    GLuint nI = normalIndices.at(i); 
    GLuint vI = vertexIndices.at(i); 
    if(nI!=vI) 
    { 
     this->verticesNormals.at(vI) = temporaryNormals.at(nI); 
     std::cerr<< "Normal index doesn't match vertex index: " << vertexIndices[i] << " " << normalIndices[i] << std::endl; 
    } 
    else 
    { 
     this->verticesNormals.at(vI) = temporaryNormals.at(vI); 
    } 

    cerr << "Vertices=" << this->verticesCoord.size() << endl; 
    cerr << "Normals=" << this->verticesNormals.size() << endl; 
    cerr << "Textures=" << this->textureCoords.size() << endl; 
    cerr << "NVertices per face= " << this->nVerticesPerFace << endl; 
    cerr << "Faces= " << this->vertexIndices.size()/nVerticesPerFace << endl; 

    return 0; 
    } 
} 

bool BothAreSpaces(char lhs, char rhs) 
{ 
    return (lhs == rhs) && (lhs == ' '); 
} 

bool ObjLoader::loadFace(const string &_line, int lineNumber) 
{ 
    std::string line = _line; 
    std::string::iterator new_end = std::unique(line.begin(), line.end(), BothAreSpaces); 
    line.erase(new_end, line.end()); 

    stringstream stream(line),countVerticesStream(line); 
    string val; 
    stream >> val; 
    if (val!="f") 
    { 
    string lineString= static_cast<ostringstream*>(&(ostringstream() << lineNumber))->str(); 
    throw std::logic_error("Error loading face at line " + lineString); 
    } 

    // Count the number of vertices per face by counting the/
    int nVertices = 0; 
    while (countVerticesStream.good()) 
    { 
    if (countVerticesStream.get()==' ' && countVerticesStream.good()) 
     nVertices++; 
    } 

    if (nVerticesPerFace !=0 && nVerticesPerFace != nVertices) 
    { 
    string lineString= static_cast<ostringstream*>(&(ostringstream() << lineNumber))->str(); 
    throw std::logic_error("Can't support non uniform faces definitions. You must use the same number of vertices for every faces. Check line "+lineString); 
    } 
    this->nVerticesPerFace = nVertices; 

    GLuint indexPosition = 0, indexTexture = 0, indexNormal = 0; 

    // Compute the normal 
    Vector3d faceVertices[nVerticesPerFace]; 
    for (unsigned int iFace = 0; iFace < nVerticesPerFace; iFace++) 
    { 
    stream >> indexPosition; 
    faceVertices[iFace] = verticesCoord.at(indexPosition-1); 
    if('/' == stream.peek()) 
    { 
     stream.ignore(); 
     if('/' != stream.peek()) 
     { 
      stream >> indexTexture; 
     } 
     if('/' == stream.peek()) 
     { 
      stream.ignore(); 
      // Optional vertex normal 
      stream >> indexNormal; 
     } 
    } 
    this->vertexIndices.push_back(indexPosition-1); // that's because Obj format starts counting from 1 
    this->textureIndices.push_back(indexTexture-1); // that's because Obj format starts counting from 1 
    this->normalIndices.push_back(indexNormal-1); // that's because Obj format starts counting from 1 

    } 
} 

void ObjLoader::draw() 
{ 
    double *pVerticesCoords = &this->verticesCoord.at(0)[0]; 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glVertexPointer(3,GL_DOUBLE, 0,pVerticesCoords); 
    glDrawArrays(GL_POINTS, 0, this->verticesCoord.size()); 
    glDisableClientState(GL_VERTEX_ARRAY); 

    GLint coordsPerVertex=3; 
    GLint stride=0; // Our coords are tightly packed into their arrays so we set this to 0 
    //double *pVerticesCoords = &this->verticesCoord.at(0)[0]; 
    double *pNormalCoords = &this->verticesNormals.at(0)[0]; 

    glEnableClientState(GL_NORMAL_ARRAY); 
    glEnableClientState(GL_VERTEX_ARRAY); 

    glNormalPointer(GL_DOUBLE, 0, pNormalCoords); // Normal pointer to normal array 
    glVertexPointer(coordsPerVertex,GL_DOUBLE, stride,pVerticesCoords); 

    switch (nVerticesPerFace) 
    { 
    case 3: 
    glDrawElements(GL_TRIANGLES, vertexIndices.size(), GL_UNSIGNED_INT, this->vertexIndices.data()); 
    break; 
    case 4: 
    glDrawElements(GL_QUADS, vertexIndices.size(), GL_UNSIGNED_INT, this->vertexIndices.data()); 
    break; 
    default: 
    glDrawElements(GL_POLYGON, vertexIndices.size(), GL_UNSIGNED_INT, this->vertexIndices.data()); 
    } 
    glDisableClientState(GL_VERTEX_ARRAY); 
    glDisableClientState(GL_NORMAL_ARRAY); 

} 

我應該如何重組法線,使得它們反映的相同順序頂點?

+0

提出你的問題是否真的需要那麼多的代碼?乍一看,我認爲不是。你能否簡單地縮小你提供的代碼,以便我們能夠(更好)更好地理解你的問題,因此我們可以給出更好的答案。 – 2013-04-30 09:20:50

+0

我提供了這個代碼來爲你提供一個可編譯的例子。問題(我認爲)是在加載文件時,按照遇到的順序在「std :: vector normals」中推入法線。我想提供具有相同索引的法線和頂點,但不知道如何。 – linello 2013-04-30 09:25:34

回答

4

你的問題是在數據結構中。至少在加載您需要將您的面孔加載到類似的OBJ:如果你想法線數組(可能texturecoords)

struct Vertex 
{ 
    unsigned int vertex; 
    unsigned int normal; 
    unsigned int texturecoord; 
}; 

struct Face 
{ 
    // not dynamic, but you get the idea. 
    Vertex vertexes[N]; 
}; 

然後你的頂點匹配,你需要創建一個新的數組火柴。 OBJ格式針對存儲進行了優化,而不是渲染。

這兩個步驟過程的額外好處是,您可以通過將每個非三角形拼成三角形來消除同質面上的限制。

+0

謝謝,但應該如何使用'vertexes'數據結構來繪製座標,法線和紋理?我的意思是,索引不是搞亂了嗎? – linello 2013-05-02 10:07:08

+0

您計算一組符合所需VBO結構的新數組。加載步驟應該與渲染步驟完全分離。將來您可能會想要加載其他格式以及其他怪癖,請勿使渲染取決於加載代碼。 – rioki 2013-05-02 10:44:53

-1

我知道這是一個古老的問題,但我在構建Obj解析器時,今天早些時候遇到了同樣的問題。我的代碼中存在兩個問題,導致光線與您所顯示的非常相似: 1-第一個錯誤地使用索引號(1,2,3,...,n)I從Obj文件獲得引用我的數組中的正常。 2-另一個問題是採用vt值而不是vn值。

這是使用在glBegin/glEnd你是如何傳遞正常值OpenGL的:

glNormal3f(....); 
glVertex3f(....); 

你設置繪製每個頂點之前正常值。無論何時設置正常,所有後續頂點都會受到影響。

+0

即時模式?你出去 – RecursiveExceptionException 2016-07-25 05:16:09