3

I'm a newbie to OpenGL and I was following a series by "thebennybox" on Youtube. Specifically his series on modern opengl programming. I thought I would try to implement his Mesh wrapper using the vector in C++ rather than using arrays of data and passing that to OpenGL. It works, but I cant' seem to render multiple meshes?

Am I making an obvious mistake somewhere?

This is I do to setup my mesh:

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(BUFFER_COUNT, vbo);

// vertices buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo[BUFFER_POSITION]);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(positions.front()), &positions.front(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

// texture buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo[BUFFER_TEXCOORD]);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(texes.front()), &texes.front(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);

// index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[BUFFER_INDEX]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(indices.front()), &indices.front(), GL_STATIC_DRAW);

glBindVertexArray(0);

Then to render it I bind the VAO and draw the elements:

glBindVertexArray(vao);
glDrawElementsBaseVertex(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0, 0);
glBindVertexArray(0);

And finally I can send it some data:

std::vector<Vertex> vertices = {
    Vertex(glm::vec3(0, 0, 0), glm::vec2(0, 0)),
    Vertex(glm::vec3(0, 1, 0), glm::vec2(1, 0)),
    Vertex(glm::vec3(0, 1, 1), glm::vec2(1, 1)),
    Vertex(glm::vec3(0, 0, 1), glm::vec2(0, 1)),
};

std::vector<unsigned int> indices = {
    0, 1, 2, 2, 3, 0
};

shape = new Mesh(vertices, indices);

The weird thing is that this works perfectly, but as soon as I try render another mesh it wont render the previous mesh. The weird thing is that say I had mesh A and mesh B. I can render mesh A, then I setup mesh B, and even if I don't render it, nothing renders at all. Not even mesh A. I assume this is due to the state-based-ness of OpenGL.

  • 4
    Maybe a silly question, but you're setting up two separate VAOs and two separate arrays of VBOs for the two meshes, right? If you're accidentally re-using them, that would explain why setting-up B screws up the rendering of A. – Nathan Reed Apr 19 '16 at 22:46
  • 1
    Can you tell how you are calling the draw function ? and what functionality does Vertex class perform. I remember i once experienced the same problem when i was overwriting two VBOs. Maybe you are doing the same, I can't tell without seeing how you are drawing –  Jun 16 '16 at 12:22

0 Answers0