0

非常に基本的な .obj ファイル ローダー (頂点の座標と三角形の面のインデックスを読み込むだけ) とレンダラーを作成しました。一度に 1 つのモデルのみをロードすると、うまく機能します。2 つ目 (またはそれ以上) のモデルをロードすると、それぞれが自分自身と他のモデルの一部を描画します。これがなぜなのかを理解しようとして頭を悩ませました。

これは、ロードされて描画される単純な円柱です

これは、立方体と円柱 (最初に立方体) をロードするとどうなるかです。

関連するコード スニペット:

ここで .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