- A Vertex Array Object (VAO) is an OpenGL Object that stores all of the state needed to supply vertex data (with one minor exception noted below). It stores the format of the vertex data.
- A Vertex Buffer Object (VBO) is the common term for a normal Buffer Object when it is used as a source for vertex array data.
const char *vertexShaderSource = "#version 330 core\n"
"out vec3 color;\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"layout (location = 2) in vec3 aPos1;\n"
"layout (location = 3) in vec3 aColor1;\n"
"uniform int sw;\n"
"void main()\n"
"{\n"
"if(sw < 1){\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
" color = aColor;\n"
"}else{\n"
" gl_Position = vec4(aPos1.x, aPos1.y, aPos1.z, 1.0);\n"
" color = aColor1;\n"
"}\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec3 color;\n"
"void main()\n"
"{\n"
" FragColor = vec4(color, 1.0f);\n"
"}\n\0";
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
float vertices[] = {
-0.9f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.0f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.45f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
};
float vertices1[] = {
0.0f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
0.9f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
0.45f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
};
unsigned int VBO[2], VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(2, VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(2);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(3);
glBindVertexArray(0);
unsigned int flag = 0;
while (!glfwWindowShouldClose(window))
{
glUseProgram(shaderProgram);
flag = !flag;
glUniform1i(glGetUniformLocation(shaderProgram, "sw"), flag);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
}