#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glfw3.h>
GLuint VBO[2], VAO[2];
void init()
{
static const GLfloat vertices[] =
{
// First triangle
-0.9f, -0.5f, 0.0f, // Left
-0.0f, -0.5f, 0.0f, // Right
-0.45f, 0.5f, 0.0f, // Top
};
static const GLfloat vertices2[] =
{
// Second triangle
0.0f, -0.5f, 0.0f, // Left
0.9f, -0.5f, 0.0f, // Right
0.45f, 0.5f, 0.0f // Top
};
glGenVertexArrays(2, VAO);
glBindVertexArray(VAO[0]);
glGenBuffers(2, VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindVertexArray(VAO[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
static void render()
{
glClearColor(0, 0, 0.2, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO[0]);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(VAO[1]);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
int main()
{
glfwInit();
GLFWwindow *window = glfwCreateWindow(640, 480, "VAO", nullptr, nullptr);
glfwMakeContextCurrent(window);
glewInit();
init();
while (!glfwWindowShouldClose(window))
{
render();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
When I run this program, it works fine: [ATTACH=CONFIG]1904[/ATTACH]
But when I use
VAO[0] = 1; VAO[1] = 2;
instead of
glGenVertexArrays(2, VAO);
I get the following result: [ATTACH=CONFIG]1905[/ATTACH]
How does glGenVertexArrays actually work?