0
\$\begingroup\$

I am trying to render a quad with gl render mode GL_TRIANGLES with using element array buffer. Here is some code to explain process. Only the first 3 elements of indices array are drawn as expected. But following indices doesn't drawn at all.

Mesh Creation

glm::vec3 vertices[] = { glm::vec3(-1, 1, 0),
                          glm::vec3(-1, -1, 0),
                          glm::vec3(1, -1, 0),
                          glm::vec3(1, 1, 0)
                        };

glm::vec2 textures[] = { glm::vec2(1, 0),
                          glm::vec2(0, 0),
                          glm::vec2(0, 1),
                          glm::vec2(1, 1)
                        };

unsigned int indices[] = { 0, 1, 3, 3, 1, 2};
Mesh* mesh = new Mesh(vertices, sizeof(vertices) / sizeof(glm::vec3), textures, sizeof(textures) / sizeof(glm::vec2), indices, sizeof(indices) / sizeof(unsigned int), texture);

Mesh Class

Mesh::Mesh(...){
    InitMesh(...);
}

void Mesh::InitMesh(vec3* vertices, unsigned int numVertices, vec2* textureCoords, unsigned int numTextureCoords, unsigned int* indices, unsigned int numIndices) {
     this->numIndices = numIndices;

    // Generate VAO and bind
    glGenVertexArrays(1, &vaoID);
    glBindVertexArray(vaoID);
    // Generate VBOs
    glGenBuffers(3, vboIDs);
    // Bind Position Buffer, Buffer Position Data
    glBindBuffer(GL_ARRAY_BUFFER, vboIDs[0]);
    glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(vec3), vertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    // Buffer TextureCoord Data
    glBindBuffer(GL_ARRAY_BUFFER, vboIDs[1]);
    glBufferData(GL_ARRAY_BUFFER, numTextureCoords * sizeof(vec2), textureCoords, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
    // Buffer Indices Data
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIDs[2]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(unsigned int), indices, GL_STATIC_DRAW);
    // Unbind VAO
    glBindVertexArray(0);
}

void Mesh::Draw() {
    glBindVertexArray(vaoID);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
    glBindVertexArray(0);
}
\$\endgroup\$
  • \$\begingroup\$ Is the index buffer bound when you call drawElements? \$\endgroup\$ – Bálint Nov 6 at 11:56
  • \$\begingroup\$ @Bálint Yes, On InitMesh function(it is called by constructor). Fourth line from bottom of InitMesh. \$\endgroup\$ – Barış Doğa Yavaş Nov 6 at 11:57
  • 1
    \$\begingroup\$ @BarışDoğaYavaş Please post an answer instead of putting the solution in your question, so others can see it has an answer. \$\endgroup\$ – Ian Young Nov 6 at 13:06
2
\$\begingroup\$

SOLUTION

After several hours of searching found the issue on creation of vboIDs[]. I was creating it by size 2, changed it to 3 and fixed the issue.

\$\endgroup\$
  • \$\begingroup\$ If your variable vboIDs was a std::vector<GLuint>, then you could do // Generate VBOs vboIDs.resize(3); glGenBuffers(vboIDs.size(), &vboIDs[0]); to avoid having to update 2 places with the constant 3. \$\endgroup\$ – torbjoernwh Nov 7 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.