1.添加更多顶点到数据中,使用glDrawArrays,尝试绘制两个彼此相连的三角形
将之前的代码这部分进行修改
float vertices[] = {
-0.5f, 1.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.5f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f
};
unsigned int indices[] = {
// 注意索引从0开始!
// 此例的索引(0,1,2,3)就是顶点数组vertices的下标,
// 这样可以由下标代表顶点组合成矩形
0, 1, 2, // 第一个三角形
2, 3, 4 // 第二个三角形
};
2.创建相同的两个三角形,但对它们的数据使用不同的VAO和VBO
主要改动的地方
float firstTriangle[] = {
-0.9f, -0.5f, 0.0f, // left
-0.0f, -0.5f, 0.0f, // right
-0.45f, 0.5f, 0.0f, // top
};
float secondTriangle[] = {
0.0f, -0.5f, 0.0f, // left
0.9f, -0.5f, 0.0f, // right
0.45f, 0.5f, 0.0f // top
};
unsigned int VBOs[2], VAOs[2];
glGenVertexArrays(2, VAOs);
glGenBuffers(2, VBOs);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
// 绑定顶点数组对象,然后绑定和设置顶点缓冲区,然后配置顶点属性
glBindVertexArray(VAOs[0]); // 绑定VAO
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]); // 绑定VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(firstTriangle), firstTriangle, GL_STATIC_DRAW); // 将顶点数据复制到缓冲区中
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // 解析顶点数据 0:解析的顶点属性的位置值 3:顶点属性的大小 GL_FLOAT:数据类型 GL_FALSE:是否希望数据被标准化 3 * sizeof(float):步长 0:偏移量
glEnableVertexAttribArray(0); // 启用顶点属性
glBindVertexArray(VAOs[1]); // 绑定VAO
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]); // 绑定VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(secondTriangle), secondTriangle, GL_STATIC_DRAW); // 将顶点数据复制到缓冲区中
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // 解析顶点数据 0:解析的顶点属性的位置值 3:顶点属性的大小 GL_FLOAT:数据类型 GL_FALSE:是否希望数据被标准化 3 * sizeof(float):步长 0:偏移量
glEnableVertexAttribArray(0); // 启用顶点属性
glBindVertexArray(VAOs[0]); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
// 绘制三角形 从顶点数组的起始位置开始绘制3个顶点
glDrawArrays(GL_TRIANGLES, 0, 3); // 从顶点数组的起始位置开始绘制3个顶点
glBindVertexArray(VAOs[1]);
glDrawArrays(GL_TRIANGLES, 0, 3);
完整代码
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height); // 窗口大小改变回调函数
void processInput(GLFWwindow *window); // 输入处理函数
// 大小设置
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const char *vertexShaderSource = "#version 330 core\n" // 顶点着色器源码
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n" // 片段着色器源码
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
int main()
{
glfwInit(); // 初始化GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 设置主版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // 设置次版本号
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 设置OpenGL配置文件为核心模式
#ifdef __APPLE__ // Mac OS X系统需要加上这行代码
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// 创建窗口对象
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// 加载所有OpenGL函数指针 glad是用来管理OpenGL的函数指针的,所以在调用任何OpenGL的函数之前我们需要初始化GLAD
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// build and compile our shader program
// 创建并编译着色器程序
// ------------------------------------
// vertex shader
// 顶点着色器是GPU上运行的小程序,它负责处理顶点数据,所以我们必须先定义一个顶点着色器
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
// 检查编译是否成功
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
// 片段着色器同样是GPU上运行的小程序,它负责处理片段着色器的颜色
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
// 检查编译是否成功
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
// 链接着色器 将顶点着色器和片段着色器链接到一个着色器程序中
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
// 检查链接是否成功
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// set up vertex data (and buffer(s)) and configure vertex attributes
// 设置顶点数据(和缓冲区)并配置顶点属性
// ------------------------------------------------------------------
float firstTriangle[] = {
-0.9f, -0.5f, 0.0f, // left
-0.0f, -0.5f, 0.0f, // right
-0.45f, 0.5f, 0.0f, // top
};
float secondTriangle[] = {
0.0f, -0.5f, 0.0f, // left
0.9f, -0.5f, 0.0f, // right
0.45f, 0.5f, 0.0f // top
};
unsigned int VBOs[2], VAOs[2];
glGenVertexArrays(2, VAOs);
glGenBuffers(2, VBOs);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
// 绑定顶点数组对象,然后绑定和设置顶点缓冲区,然后配置顶点属性
glBindVertexArray(VAOs[0]); // 绑定VAO
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]); // 绑定VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(firstTriangle), firstTriangle, GL_STATIC_DRAW); // 将顶点数据复制到缓冲区中
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // 解析顶点数据 0:解析的顶点属性的位置值 3:顶点属性的大小 GL_FLOAT:数据类型 GL_FALSE:是否希望数据被标准化 3 * sizeof(float):步长 0:偏移量
glEnableVertexAttribArray(0); // 启用顶点属性
glBindVertexArray(VAOs[1]); // 绑定VAO
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]); // 绑定VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(secondTriangle), secondTriangle, GL_STATIC_DRAW); // 将顶点数据复制到缓冲区中
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // 解析顶点数据 0:解析的顶点属性的位置值 3:顶点属性的大小 GL_FLOAT:数据类型 GL_FALSE:是否希望数据被标准化 3 * sizeof(float):步长 0:偏移量
glEnableVertexAttribArray(0); // 启用顶点属性
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
// 注意,这是允许的,调用glVertexAttribPointer将VBO注册为顶点属性的绑定顶点缓冲区对象,因此之后我们可以安全地解绑
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
// 之后可以解绑VAO,以防止将来其他的VAO调用错误地修改了这个VAO,但这很少发生。修改其他VAO需要调用glBindVertexArray,所以我们通常不会在不直接需要的时候解绑VAO(也不会解绑VBO)
glBindVertexArray(0);
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
// 绘制第一个三角形
glUseProgram(shaderProgram); // 看到了吗?我们只是在渲染循环的每次迭代中调用一次glUseProgram函数而不是在每次渲染调用中都调用一次。这是因为一旦着色器程序对象被链接到程序对象中,使用程序对象就会变得很简单了:我们只需激活相应的程序对象,它会自动成为当前程序对象。所以一旦我们设置好了程序对象,我们就只需要激活这个程序对象,然后绘制物体就好了。
glBindVertexArray(VAOs[0]); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
// 绘制三角形 从顶点数组的起始位置开始绘制3个顶点
glDrawArrays(GL_TRIANGLES, 0, 3); // 从顶点数组的起始位置开始绘制3个顶点
glBindVertexArray(VAOs[1]);
glDrawArrays(GL_TRIANGLES, 0, 3);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// 可选:一旦它们完成了任务,就释放所有资源:
// ------------------------------------------------------------------------
glDeleteVertexArrays(2, VAOs);
glDeleteBuffers(2, VBOs);
glDeleteProgram(shaderProgram);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
3.创建两个着色器程序,第二个程序使用一个不同的片段着色器,输出黄色;再次绘制这两个三角形,让其中一个输出为黄色
主要修改部分
const char *vertexShaderSource = "#version 330 core\n" // 顶点着色器源码
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShader1Source = "#version 330 core\n" // 片段着色器源码
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
const char *fragmentShader2Source = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);\n"
"}\n\0";
unsigned int fragmentShaders[2];
fragmentShaders[0] = glCreateShader(GL_FRAGMENT_SHADER);
fragmentShaders[1] = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaders[0], 1, &fragmentShader1Source, NULL);
glShaderSource(fragmentShaders[1], 1, &fragmentShader2Source, NULL);
glCompileShader(fragmentShaders[0]);
glCompileShader(fragmentShaders[1]);
unsigned int shaderPrograms[2];
shaderPrograms[0] = glCreateProgram();
shaderPrograms[1] = glCreateProgram();
glAttachShader(shaderPrograms[0], vertexShader);
glAttachShader(shaderPrograms[0], fragmentShaders[0]);
glAttachShader(shaderPrograms[1], vertexShader);
glAttachShader(shaderPrograms[1], fragmentShaders[1]);
glLinkProgram(shaderPrograms[0]);
glLinkProgram(shaderPrograms[1]);
完整代码
// draw our first triangle
// 绘制第一个三角形
glUseProgram(shaderPrograms[0]); // 看到了吗?我们只是在渲染循环的每次迭代中调用一次glUseProgram函数而不是在每次渲染调用中都调用一次。这是因为一旦着色器程序对象被链接到程序对象中,使用程序对象就会变得很简单了:我们只需激活相应的程序对象,它会自动成为当前程序对象。所以一旦我们设置好了程序对象,我们就只需要激活这个程序对象,然后绘制物体就好了。
glBindVertexArray(VAOs[0]); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
// 绘制三角形 从顶点数组的起始位置开始绘制3个顶点
glDrawArrays(GL_TRIANGLES, 0, 3); // 从顶点数组的起始位置开始绘制3个顶点
glUseProgram(shaderPrograms[1]);
glBindVertexArray(VAOs[1]);
glDrawArrays(GL_TRIANGLES, 0, 3);
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height); // 窗口大小改变回调函数
void processInput(GLFWwindow *window); // 输入处理函数
// 大小设置
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const char *vertexShaderSource = "#version 330 core\n" // 顶点着色器源码
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShader1Source = "#version 330 core\n" // 片段着色器源码
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
const char *fragmentShader2Source = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);\n"
"}\n\0";
int main()
{
glfwInit(); // 初始化GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 设置主版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // 设置次版本号
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 设置OpenGL配置文件为核心模式
#ifdef __APPLE__ // Mac OS X系统需要加上这行代码
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// 创建窗口对象
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// 加载所有OpenGL函数指针 glad是用来管理OpenGL的函数指针的,所以在调用任何OpenGL的函数之前我们需要初始化GLAD
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// build and compile our shader program
// 创建并编译着色器程序
// ------------------------------------
// vertex shader
// 顶点着色器是GPU上运行的小程序,它负责处理顶点数据,所以我们必须先定义一个顶点着色器
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
// 检查编译是否成功
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
// 片段着色器同样是GPU上运行的小程序,它负责处理片段着色器的颜色
// unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
// glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
// glCompileShader(fragmentShader);
unsigned int fragmentShaders[2];
fragmentShaders[0] = glCreateShader(GL_FRAGMENT_SHADER);
fragmentShaders[1] = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaders[0], 1, &fragmentShader1Source, NULL);
glShaderSource(fragmentShaders[1], 1, &fragmentShader2Source, NULL);
glCompileShader(fragmentShaders[0]);
glCompileShader(fragmentShaders[1]);
// check for shader compile errors
// 检查编译是否成功
// glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
// if (!success)
// {
// glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
// std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
// }
// link shaders
// 链接着色器 将顶点着色器和片段着色器链接到一个着色器程序中
// unsigned int shaderProgram = glCreateProgram();
// glAttachShader(shaderProgram, vertexShader);
// glAttachShader(shaderProgram, fragmentShader);
// glLinkProgram(shaderProgram);
unsigned int shaderPrograms[2];
shaderPrograms[0] = glCreateProgram();
shaderPrograms[1] = glCreateProgram();
glAttachShader(shaderPrograms[0], vertexShader);
glAttachShader(shaderPrograms[0], fragmentShaders[0]);
glAttachShader(shaderPrograms[1], vertexShader);
glAttachShader(shaderPrograms[1], fragmentShaders[1]);
glLinkProgram(shaderPrograms[0]);
glLinkProgram(shaderPrograms[1]);
// check for linking errors
// 检查链接是否成功
// glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
// if (!success) {
// glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
// std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
// }
// glDeleteShader(vertexShader);
// glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShaders[0]);
glDeleteShader(fragmentShaders[1]);
// set up vertex data (and buffer(s)) and configure vertex attributes
// 设置顶点数据(和缓冲区)并配置顶点属性
// ------------------------------------------------------------------
float firstTriangle[] = {
-0.9f, -0.5f, 0.0f, // left
-0.0f, -0.5f, 0.0f, // right
-0.45f, 0.5f, 0.0f, // top
};
float secondTriangle[] = {
0.0f, -0.5f, 0.0f, // left
0.9f, -0.5f, 0.0f, // right
0.45f, 0.5f, 0.0f // top
};
unsigned int VBOs[2], VAOs[2];
glGenVertexArrays(2, VAOs);
glGenBuffers(2, VBOs);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
// 绑定顶点数组对象,然后绑定和设置顶点缓冲区,然后配置顶点属性
glBindVertexArray(VAOs[0]); // 绑定VAO
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]); // 绑定VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(firstTriangle), firstTriangle, GL_STATIC_DRAW); // 将顶点数据复制到缓冲区中
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // 解析顶点数据 0:解析的顶点属性的位置值 3:顶点属性的大小 GL_FLOAT:数据类型 GL_FALSE:是否希望数据被标准化 3 * sizeof(float):步长 0:偏移量
glEnableVertexAttribArray(0); // 启用顶点属性
glBindVertexArray(VAOs[1]); // 绑定VAO
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]); // 绑定VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(secondTriangle), secondTriangle, GL_STATIC_DRAW); // 将顶点数据复制到缓冲区中
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // 解析顶点数据 0:解析的顶点属性的位置值 3:顶点属性的大小 GL_FLOAT:数据类型 GL_FALSE:是否希望数据被标准化 3 * sizeof(float):步长 0:偏移量
glEnableVertexAttribArray(0); // 启用顶点属性
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
// 注意,这是允许的,调用glVertexAttribPointer将VBO注册为顶点属性的绑定顶点缓冲区对象,因此之后我们可以安全地解绑
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
// 之后可以解绑VAO,以防止将来其他的VAO调用错误地修改了这个VAO,但这很少发生。修改其他VAO需要调用glBindVertexArray,所以我们通常不会在不直接需要的时候解绑VAO(也不会解绑VBO)
glBindVertexArray(0);
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
// 绘制第一个三角形
glUseProgram(shaderPrograms[0]); // 看到了吗?我们只是在渲染循环的每次迭代中调用一次glUseProgram函数而不是在每次渲染调用中都调用一次。这是因为一旦着色器程序对象被链接到程序对象中,使用程序对象就会变得很简单了:我们只需激活相应的程序对象,它会自动成为当前程序对象。所以一旦我们设置好了程序对象,我们就只需要激活这个程序对象,然后绘制物体就好了。
glBindVertexArray(VAOs[0]); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
// 绘制三角形 从顶点数组的起始位置开始绘制3个顶点
glDrawArrays(GL_TRIANGLES, 0, 3); // 从顶点数组的起始位置开始绘制3个顶点
glUseProgram(shaderPrograms[1]);
glBindVertexArray(VAOs[1]);
glDrawArrays(GL_TRIANGLES, 0, 3);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// 可选:一旦它们完成了任务,就释放所有资源:
// ------------------------------------------------------------------------
glDeleteVertexArrays(2, VAOs);
glDeleteBuffers(2, VBOs);
glDeleteProgram(shaderPrograms[0]);
glDeleteProgram(shaderPrograms[1]);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}