GAME_OpenGL_Project_2
1 |
|
这里介绍shader相关内容
首先明确在现代OpenGL中我们至少需要配置一个vertex shader和fragment shader。
1 | int main() |
这里将介绍VBO、VAO和EBO三个对象
定义顶点数据后,我们希望将其作为输入发送到graphic pipeline的第一个stage:vertex shader。这是通过在 GPU 上创建存储顶点数据的内存、配置 OpenGL 应如何解释内存并指定如何将数据发送到显卡来完成的。
我们通过vertex buffer objects/VBO(这是一个object,还记得在learn-1中介绍的object这个名词吗?)来管理GPU的内存。使用GPU内存的优点是:可以一次性地将大量verteces发送到GPU暂存,而不必CPU低效地每次传一个。
接下来介绍VBO的工作流程
- 首先使用glGenBuffers()函数为VBO对象创建唯一ID。
- 然后将新申请的VBO对象绑定到OpenGL的context上。OpenGL的context中有多种类型的buffer object。且context中每种buffer object只能绑定一个对象。
- 最后,使用glBufferData()将提前定义好的顶点数据拷贝到buffer中。
- 到此为止,数据已拷贝到buffer。但是,vertex shader并不知道VBO如何管理vertex attributes。因此还应告诉shader如何处理VBO中的数据。
- glVertexAttribPointer()就是告诉OpenGL该如何处理vertex attributes。
- 最后使用glEnableVertexAttribArray()启用顶点属性。默认情况下是未启用的。
注意:自第二步将objectID绑定到context上,之后的state配置都是基于objectID的,意味着这些配置选项都被保存到objectID中了。
那么,我们可以思考一个问题:在绘图过程中,每对一个VBO进行绘图,都需要进行配置。因此,我们可以将这些对象和配置管理起来,需要哪个就绑定哪个。因而引出VAO的概念。
Vertex Array Object
Core OpenGL要求我们使用VAO以便它知道如何处理顶点。如果我们没有绑定VAO,OpenGL不会绘制任何图形。 - 首先使用glGenVertexArrays()为VAO对象创建唯一ID。
- 然后执行上述VBO的工作流程。
- 解绑VAO供之后使用。当绘制时,只需绑定配置好的VAO对象即可。
element buffer objects
OpenGL主要处理三角形。当我们绘制长方形时,实际上就是绘制两个三角形,这意味着我们需要指定6个顶点,但实际上只需4个顶点即可表示长方形。这就产生了冗余。如何处理?使用EBO! - 使用glGenBuffers()为EBO对象创建唯一ID。
- 使用glBindBuffer()将创建好的EBO绑定到context的GL_ELEMENT_ARRAY_BUFFER。
- 使用glBufferData()将索引数据传递到缓冲区中。
- 最后使用glDrawElements()向OpenGL表明使用当前绑定的EBO中的索引数据绘制。
由于VAO也保存了当目标为GL_ELEMENT_ARRAY_BUFFER时glBindBuffer()的配置,这意味着EBO的解绑操作也会记录在VAO中,所以请确保解绑VAO之前没有解绑EBO,否则EBO不会被成功配置。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 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);
glBindVertexArray(VAO); // 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
//glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// 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(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
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);
}