重要知识点:
1,顶点数组对象:VAO
2,顶点缓冲对象
总结:
使用顶点数据初始化 顶点缓冲对象,然后 顶点数组对象通过 顶点缓冲对象 获取 顶点数据(通过位置layout (location = 0)),把数据存放在 顶点数组对象的好处是 当配置好了顶点属性指针的时候,你只需要使用VAO即可 不需要再次去配置 顶点属性指针
/**
1,顶点数组对象: VAO
2,顶点缓冲对象:CBO
3,索引缓冲对象:EBO
图形渲染管线:就是把数据
流程:
1,顶点着色器:接受一组3D数据,转换成另外一个3D 数据
2,图元着色器:接受顶点着色器的输出,然后装配成指定的图元形状
3,几何着色器:接受图元着色器的输出,在这可以产生新的图元
4,光栅化着色器:可以叫栅格化或像素化,就是把图元映射到屏幕上,变成像素,供片段着色器使用的片段。
在运行片段着色器之前,会把屏幕之外的数据删除,以提高运行效率。这里说的片段是指 openGL 渲染一个像素 所需要的所有数据
5,片段着色器:接受关栅化阶段的数据,计算出最终在屏幕像素上显示的颜色,包括关照、光影、光的颜色
6,Alpha测试和混合(Blending)阶段:测片段的对应的深度(和模板(Stencil))值(后面会讲),用它们来判断这个像素是其它物体的前面还是后面,决定是否应该丢弃。这个阶段也会检查alpha值(alpha值定义了一个物体的透明度)并对物体进行混合(Blend
顶点输入:
第一:坐标
1,标准化设备坐标:所有的坐标都是 3D,范围在 (-1,1)超过这个范围都会被舍弃
2,屏幕空间坐标:标准化设备坐标是通过 glViewport 函数 进行视口变换,转换成屏幕空间坐标。屏幕空间坐标又会变换成片段 输入到片段着色器中
第二:顶点输入步骤:
1,创建用于存储的顶点数据
2,配置OpenGL如何解释这些内存
3,指定如何发送给显卡
第三:顶点缓冲对象
优势:可以缓冲大量数据,因cpu吧数据发送给显卡相对较慢,所以尽可能一次性发送尽可能多的数据,发送到显卡内存之后,顶点着色器可非常快的访问顶点数据
*/
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
//
/**
1,着色器的版本号 必须与 openGL 的版本号一至
2, gl_Position : 预定义变量
*/
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()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
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: 加载所有的函数指针
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// build and compile our shader program
// ------------------------------------
// 创建一个顶点着色器 对象
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
//创建片段着色器
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
//创建一个 着色器程序对象,用于链接所有的着色器程序
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 vertices[] = {
-0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // top
};
unsigned int VBO, VAO;
//使用一个缓冲ID生成一个顶点数组对象
glGenVertexArrays(1, &VAO);
//使用glGenBuffers函数和一个缓冲ID生成一个VBO对象:
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
//绑定顶点数组对象
glBindVertexArray(VAO);
//绑定顶点缓冲对象
glBindBuffer(GL_ARRAY_BUFFER, VBO);
/**
使用顶点数据初始化 顶点缓冲对象
GL_STATIC_DRAW :数据不会或几乎不会改变。
GL_DYNAMIC_DRAW:数据会被改变很多。
GL_STREAM_DRAW :数据每次绘制时都会改变。
*/
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
/**
设置顶点属性指针 =》 从顶点缓冲对象中 获取 顶点数据
1,第一个参数,与顶点着色器 layout location = 0 相对应 所以为 0
2,第二参数:顶点数组每个数据都是一个vec3 总共三个 所以 是 3
3,第三个参数:每个数据都是 float 类型 所以是 GL_FLOAT
4,第四个参数: 数据是否需要标准化,标准化的时候 是:(-1,1)这里不需要标准化 (0,1) 所以是:GL_FALSE
5,第五个参数:步长(Stride),它告诉我们在连续的顶点属性组之间的间隔,因为下组位置是在 3个float 之后 所以是 3 * sizeof(float)
6,第六个参数:类型是void*,所以需要我们进行这个奇怪的强制类型转换。它表示位置数据在缓冲中起始位置的偏移量(Offset)。由于位置数据在数组的开头,所以这里是0。我们会在后面详细解释这个参数。
获取数据规则是:每个顶点属性从一个VBO管理的内存中获得它的数据,而具体是从哪个VBO(程序中可以有多个VBO)获取则是通过在调用glVertexAttribPointer时绑定到GL_ARRAY_BUFFER的VBO决定的。由于在调用glVertexAttribPointer之前绑定的是先前定义的VBO对象,顶点属性0现在会链接到它的顶点数据。
*/
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
//启用顶点数据,因起始位置是0 所以传 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
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.
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);
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
/**
1,第一个参数:图元的类型 三角形
2,第二参数:顶点数组的起始索引,为 0
3,第三个参数:总共绘制的顶点数:为3
*/
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(1, &VAO);
//删除顶点缓冲对象
glDeleteBuffers(1, &VBO);
//删除着色器程序
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);
}
上一篇:openGL 第三篇:创建窗口