I'm trying to draw two triangles in a window in OpenGL 3.3. I'm using the GLFW library for the windowing system.
From what I understand, I should have two VBOs (one for each triangle) and one VAO containing these two VBOs. That's what I did.
However, I can't figure out what calls I should make to render these two VBOs. Indeed, whatever I do, only the first VBO (first triangle) gets drawn. The second one never shows up.
int main()
{
GLFWwindow *window = setupWindow();
GLfloat triangle1Vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
GLfloat triangle2Vertices[] = {
0.f, -0.5f, 0.0f,
0.8f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
GLuint vao, vbo[2];
glGenVertexArrays(1, &vao);
glGenBuffers(2, vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle1Vertices), triangle1Vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle2Vertices), triangle2Vertices, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.3f, 0.3f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(2, vbo);
glfwTerminate();
return 0;
}
I'm clearly missing something, but what?
glVertexAttribPointer
in the render loop look extremely suspicious. Part of the whole point of using VAOs is that you set up the inputs of the pipeline only once, then you dobind vao; draw; bind another vao; draw;
. Why do you keep re-setting the attributes (to the same buffers/formats, nonetheless)? – Selie