<p>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?</p>

<p>Am I making an obvious mistake somewhere?</p>

<p>This is I do to setup my mesh:</p>



<pre><code>glGenVertexArrays(1, &amp;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()), &amp;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()), &amp;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()), &amp;indices.front(), GL_STATIC_DRAW);

glBindVertexArray(0);
</code></pre>

<p>Then to render it I bind the VAO and draw the elements:</p>



<pre><code>glBindVertexArray(vao);
glDrawElementsBaseVertex(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0, 0);
glBindVertexArray(0);
</code></pre>

<p>And finally I can send it some data:</p>

<pre class="lang-cpp prettyprint-override"><code>std::vector&lt;Vertex&gt; 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&lt;unsigned int&gt; indices = {
    0, 1, 2, 2, 3, 0
};

shape = new Mesh(vertices, indices);
</code></pre>

<p>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 <em>setup</em> 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.</p>

CC BY-SA 3.0
2016-04-19T22:46:37.597Z
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
<opengl>
Can't render multiple VAO's?
Jonathon Blow
# Can't render multiple VAO's?

(Asked by: username_0 on 2016-04-19)

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

**Tags:** opengl
> **Comments:**
> **username_1**: 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.  

---

## 0 answers

