OBJ模型解析器无法正常工作OpenGL

时间:2015-06-05 20:35:38

标签: c++ opengl .obj

我正在为我的引擎开发一个OBJ解析器,并且模型显示错误。

以下是代码:

//Variables
vector<float> pVertexData;
vector<float> pNormalData;
vector<float> pTexCoordData;

LoadModel函数

string line;
ifstream objFile(filename);
if (objFile.is_open())
{
    vector<vec3> vertices;
    vector<vec3> normals;
    vector<vec2> texcoords;


    while (!objFile.eof())                                          
    {
        getline(objFile, line);                                         

        //Vertex--------------------------------------------------------------
        if (line.c_str()[0] == 'v' && line.c_str()[1] == ' ')                                       
        {
            line[0] = ' ';                                              

            float x, y, z;

            x = 0;
            y = 0;
            z = 0;

            sscanf_s(line.c_str(), "%f %f %f",                          
                &x,
                &y,
                &z);

            vertices.push_back(vec3(x, y, z));
        }

        //Texture Coord--------------------------------------------------------
        if (line.c_str()[0] == 'v' && line.c_str()[1] == 't')                                       
        {
            line[0] = ' ';                                              
            line[1] = ' ';                                              

            float s, t;

            s = 0;
            t = 0;

            sscanf_s(line.c_str(), "%f %f",
                &s,
                &t);

            texcoords.push_back(vec2(s, t));

        }
        //Normal---------------------------------------------------------------
        if (line.c_str()[0] == 'v' && line.c_str()[1] == 'n')                                       
        {
            line[0] = ' ';                                              
            line[1] = ' ';                                              

            float x, y, z;

            x = 0;
            y = 0;
            z = 0;

            sscanf_s(line.c_str(), "%f %f %f",
                &x,
                &y,
                &z);

            normals.push_back(vec3(x, y, z));
        }
        //Triangle-----------------------------------------------------------
        if (line.c_str()[0] == 'f')                                 
        {
            line[0] = ' ';

            int vIndices[3];
            int tIndices[3];
            int nIndices[3];

            sscanf_s(line.c_str(), "%i/%i/%i %i/%i/%i %i/%i/%i",
                &vIndices[0],
                &tIndices[0],
                &nIndices[0],

                &vIndices[1],
                &tIndices[1],
                &nIndices[1],

                &vIndices[2],
                &tIndices[2],
                &nIndices[2]
                );

            pVertexData.push_back(vertices[vIndices[0]][0]);
            pVertexData.push_back(vertices[vIndices[0]][1]);
            pVertexData.push_back(vertices[vIndices[0]][2]);

            pTexCoordData.push_back(texcoords[tIndices[0]][0]);
            pTexCoordData.push_back(texcoords[tIndices[0]][1]);
            pTexCoordData.push_back(texcoords[tIndices[0]][2]);

            pNormalData.push_back(normals[nIndices[0]][0]);
            pNormalData.push_back(normals[nIndices[0]][1]);
            pNormalData.push_back(normals[nIndices[0]][2]);


            pVertexData.push_back(vertices[vIndices[1]][0]);
            pVertexData.push_back(vertices[vIndices[1]][1]);
            pVertexData.push_back(vertices[vIndices[1]][2]);

            pTexCoordData.push_back(texcoords[tIndices[1]][0]);
            pTexCoordData.push_back(texcoords[tIndices[1]][1]);
            pTexCoordData.push_back(texcoords[tIndices[1]][2]);

            pNormalData.push_back(normals[nIndices[1]][0]);
            pNormalData.push_back(normals[nIndices[1]][1]);
            pNormalData.push_back(normals[nIndices[1]][2]);


            pVertexData.push_back(vertices[vIndices[2]][0]);
            pVertexData.push_back(vertices[vIndices[2]][1]);
            pVertexData.push_back(vertices[vIndices[2]][2]);

            pTexCoordData.push_back(texcoords[tIndices[2]][0]);
            pTexCoordData.push_back(texcoords[tIndices[2]][1]);
            pTexCoordData.push_back(texcoords[tIndices[2]][2]);

            pNormalData.push_back(normals[nIndices[2]][0]);
            pNormalData.push_back(normals[nIndices[2]][1]);
            pNormalData.push_back(normals[nIndices[2]][2]);
        }
    }
    objFile.close();                

    vBuff->SetData<float>(&pVertexData[0], 0, pVertexData.size());
    vBuff->SetData<float>(&pNormalData[0], 1, pNormalData.size());
    vBuff->SetData<float>(&pTexCoordData[0], 2, pTexCoordData.size());

    vBuff->CreateVertexArrayObject();
}
else
{
    cout << "Unable to open file";
}

return 0;

以下是一些意思:

vBuff->SetData - a command I use for my custom buffer object to put in all data. Translates to:

glBindBuffer(GL_ARRAY_BUFFER, bufferID-second argumet)
glBufferData(GL_ARRAY_BUFFER, data-1st argument)

我试着和Suzanne一起看起来很乱。 我尝试使用立方体,顶点也是错误的!

我错过了什么?

0 个答案:

没有答案
相关问题