1

我的对象阅读器旨在从 obj 文件中读取顶点、法线纹理坐标值,然后读取面值并从顶点、法线和纹理的向量中检索相应的数据。我遇到的问题是,有时它似乎忽略了从它读取的值中减去 -1:

using namespace std;

class Model
{

string name;

int verticesAmount;
int indicesAmount;
int normalAmount;
int textureAmount;

vector<XMFLOAT3> vertices;
vector<short> indices;
vector<SimpleVertex> simplevertex;
vector<XMFLOAT3> normals;
vector<XMFLOAT3> textures;

public:
    Model(const string OBJname)
    {
LoadOBJ(OBJname);
    }

 vector<XMFLOAT3> GetVertices()
 {

     return vertices;
 }

 vector<SimpleVertex> GetVertexData()
 {
     return simplevertex;
 }

 vector<short> GetIndices()
 {

     return indices;
 }

 int GetNumberOfVertices()
 {
     return simplevertex.size();
 }

  int GetNumberOfIndices()
 {
     return indices.size();
 }

void LoadOBJ(const string fileName)
{
    verticesAmount = 0;
    indicesAmount = 0;
    normalAmount = 0;
    textureAmount = 0;

    ifstream inFile;
    inFile.open(fileName.c_str());


    if (!inFile.good())
    {
        cerr  << "Can't open file" << fileName << endl; // checks if you can open the file or not
    }
    else cout<<fileName<< " opened successfully \n";

    string line;
    while(!inFile.eof())
    {

        inFile >> line;
        XMFLOAT3 temp;
            if (line[0] =='v')
            {
                if (line.length() >1)
                {
                    if(line[1] == 'n')
                    {
                        inFile >> temp.x;
                        inFile >> temp.y;
                        inFile >> temp.z;
                        normals.push_back(temp);
                        normalAmount++;
                    }
                    else if (line[1] == 't')
                    {
                        inFile >> temp.x;
                        inFile >> temp.y;
                        inFile >> temp.z;
                        textures.push_back(temp);
                        textureAmount++;
                    }
                }
                else
                {
                    inFile >> temp.x;
                    inFile >> temp.y;
                    inFile >> temp.z;
                    vertices.push_back(temp);
                    indicesAmount++;
                }
            }
            else if (line[0] == 'f')
            {
                SimpleVertex temp2;
                char bin;
                int tempindice;

                inFile >> tempindice;                           // inputs data
                indices.push_back(tempindice-1);
                temp2.Pos.x = vertices[(tempindice-1)].x;
                inFile >> bin;                                  // gets rid of the /
                inFile >> tempindice;                           // inputs data
                temp2.Texture.x = textures[(tempindice-1)].x;
                inFile >> bin;                                  // gets rid of the /
                inFile >> tempindice;                           // inputs data
                temp2.Normal.x = normals[(tempindice-1)].x;

                inFile >> tempindice;                           // inputs data
                indices.push_back(tempindice-1);
                temp2.Pos.y = vertices[(tempindice-1)].y;
                inFile >> bin;                                  // gets rid of the /
                inFile >> tempindice;                           // inputs data
                temp2.Texture.y = textures[(tempindice-1)].y;
                inFile >> bin;                                  // gets rid of the /
                inFile >> tempindice;                           // inputs data
                temp2.Normal.y = normals[(tempindice-1)].y;

                inFile >> tempindice;                           // inputs data
                indices.push_back(tempindice);
                temp2.Pos.z = vertices[(tempindice-1)].z;
                inFile >> bin;                                  // gets rid of the /
                inFile >> tempindice;                           // inputs data
                temp2.Texture.z = textures[(tempindice-1)].z;
                inFile >> bin;                                  // gets rid of the /
                inFile >> tempindice;                           // inputs data
                temp2.Normal.z = normals[(tempindice-1)].z;

                simplevertex.push_back(temp2);
                indicesAmount++;
            }
            else
            {
                inFile.ignore(1000,'\n');
            }
        }

    inFile.close();

}

};

这是我制作的整个模型类,它是我遇到问题的 loadOBj 函数。这是它使用的 SimpleVertex 结构:

struct SimpleVertex
{
    XMFLOAT3 Pos;
    XMFLOAT3 Normal;
    XMFLOAT3 Texture;
};

我不知道它为什么会这样,而且我的课前夜似乎很困惑。

4

0 回答 0