0

我编写了一个非常基本的 .obj 文件加载器(只加载顶点坐标和三角形面索引)和渲染器。当我一次只加载一个模型时,效果很好。当我加载第二个模型(或更多)时,每个模型都会绘制自己和其他模型的一部分,我绞尽脑汁想弄清楚为什么会这样。

这是一个正在加载和绘制的简单圆柱体

这就是我加载一个立方体和一个圆柱体(首先是立方体)时发生的情况。

一些相关的代码片段:

这是我打开 .obj 文件并从文件中读取顶点坐标和三角形面索引的地方。

bool Model::loadFromFile(string fileName)
{
    vector<GLfloat> vertices;
    vector<GLuint> indices;

    fstream file(fileName);

    string line = "";
    while (getline(file, line))
    {
        istringstream lineReader(line);
        char c;

        //read the first letter of the line
        //The first letter designates the meaning of the rest of the line
        lineReader >> c;

        vec3 vertex;        //if we need to read .x, .y, and .z from the file
        GLuint index[3];    //if we need to read the 3 indices on a triangle

        switch (c)
        {
        case '#':
            //we do nothing with comment lines
            break;

        case 'v': //V means vertex coords
        case 'V': //so Load it into a vec3
            lineReader >> vertex.x >> vertex.y >> vertex.z;
            vertices.push_back(vertex.x);
            vertices.push_back(vertex.y);
            vertices.push_back(vertex.z);
            break;

        case 'vt':
        case 'VT':
            //no tex coords yet either
            break;

        case 'f': //F means indices of a face
        case 'F':
            lineReader >> index[0] >> index[1] >> index[2];

            //we subtract one from each index in the file because
            //openGL indices are 0-based, but .obj has them as
            //1-based
            indices.push_back(index[0] - 1);
            indices.push_back(index[1] - 1);
            indices.push_back(index[2] - 1);

            cout << "Indices: " << index[0] << ", " << index[1] << ", " << index[2] << endl;
            break;
        }
    }

    return this->load(vertices, indices);
}

在 loadFromFile 方法结束时,我们将索引和顶点向量传递给 model::load() 方法,该方法创建并绑定 VAO、VBO 和 IBO。

bool Model::load(vector<float>& vertices, vector<GLuint>& indices)
{
    //create the VAO
    glGenVertexArrays(1, &handle);
    glBindVertexArray(getHandle());
    //and enable vertexPositionAttribute
    glEnableVertexAttribArray(pShader->getPositionAttribIndex());

    //Populate the position portion of the
    //VAO
    GLuint vboID = 0;
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), &vertices[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glVertexAttribPointer(pShader->getPositionAttribIndex(), 3, GL_FLOAT, GL_FALSE, 0, nullptr);

    //Populate the indices portion of the
    //VAO
    GLuint iboID = 0;
    glGenBuffers(1, &iboID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), &indices[0], GL_STATIC_DRAW);

    //bind nothing to avoid any mistakes or loading
    //of random data to my VAO
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    this->numVertices = vertices.size() / 3;
    this->numIndices = indices.size();

    cout << "Num Indices: " << numIndices << endl;

    return true;
}

加载模型后,它的 VAO id 将存储在类变量中。VAO 在 model::bind() 方法中加载。

void Model::bind()
{
    glBindVertexArray(getHandle());
}

并通过调用 glDrawElements() 进行绘制

void Model::draw()
{
    //and draw me, bitchez!
    glDrawElements(GL_TRIANGLES, this->numIndices, GL_UNSIGNED_INT, nullptr);
}

模型在 level::draw() 方法中绘制:

void Level::draw()
{
    pShader->setViewMatrix(camera.getViewMatrix());

    for (int modelIndex = 0; modelIndex < models.size(); modelIndex++)
    {
        models[modelIndex]->bind();

        for (int drawableIndex = 0; drawableIndex < drawables.size(); drawableIndex++)
            drawables[drawableIndex]->draw();
    }
}

没有 OpenGL 错误,当我将其打印到控制台时,文件解析会生成正确的值。

如果有人能指出发生这种情况的方式/原因,我将不胜感激。

4

1 回答 1

0

好吧,经过几个小时的代码挖掘后,我在 level::draw 方法中注意到:

for (int modelIndex = 0; modelIndex < models.size(); modelIndex++)
{
    models[modelIndex]->bind();

    for (int drawableIndex = 0; drawableIndex < drawables.size(); drawableIndex++)
        drawables[drawableIndex]->draw();
}

我为我加载的每个实体绑定了每个模型的 VAO。从字面上看,我正在为屏幕上的所有内容绘制每个加载的模型。抱歉浪费了 OpenGL 部分的屏幕空间!

于 2015-08-05T20:22:32.883 回答