OpenGL渲染流程(管线)其实就是OpenGL执行将用户程序(CPU)定义的三维顶点数据,颜色数据,经过一系列转换最终渲染到计算机屏幕上的过程。
程序开发人员想要实现自己的OpenGL程序需要实现几部:
* 定义3D坐标向量并想着色器传输顶点数据
* 定义着色器,初始化渲染程序
* 实现渲染循环
VBO顶点缓冲对象
用一个数组标示3D坐标向量;
//创建顶点点数据
float vertices[] = {
-0.5f,-0.5f,0.0f,
0.5f,-0.5f,0.0f,
0.0f, 0.5f, 0.0f
};
GLuint VBO;
// 1.创建VBO
glGenBuffers(1,&VBO);
// 2.绑定VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// 3.绑定数据到VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
VAO顶点缓冲数组
顶点数组对象,用于向顶点着色器传输数据;
GLuint VAO;
// 1.创建VAO对象
glGenVertexArrays(1, &VAO);
// 2. 绑定VAO
glBindVertexArray(VAO);
// 3. 把顶点数组复制到缓冲中供OpenGL使用
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// 4. 设置顶点属性指针
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
// 5.使用顶点数据localtion = 0
glEnableVertexAttribArray(0);
EBO 索引缓冲对象
是一组让VBO数据重复利用的索引数组对象;
GLuint EBO;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//假设顶点着色器:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 bPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
//顶点数据
float vertices[] = {
//apos //bpos
-0.5f,-0.5f,0.0f, -0.5f,-0.5f,0.0f,
0.5f,-0.5f,0.0f, -0.5f,-0.5f,0.0f,
0.0f, 0.5f, 0.0f, -0.5f,-0.5f,0.0f
};
glVertexAttribPointer函数将每次传入顶点着色器的顶点数据“分段”传输,glEnableVertexAttribArray定义了传输数据的localtion;
例如:
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);
上述将顶点数据每次传输6个,第一个从0开始的3位,对应location = 0(aPos),第二个从第四位开始的三位,对应location = 1(bPos);
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
gl_Position : openGL预定义的位置变量
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
out vec4 FragColor : 定义渲染颜色数据
//编译着色器
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader,1,&vertexShaderSrc, nullptr);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader,GL_COMPILE_STATUS,&success);
if(!success)
{
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
std::cout << "vertex shader error : " <std::endl;
return false;
}
//创建渲染程序
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(program, 512, nullptr, infoLog);
std::cout << "program error : " << infoLog << std::endl;
return false;
}
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
dorending()...