原文地址:https://blog.csdn.net/github_39611196/article/details/78416902
着色器是运行在GPU上的小程序,着色器是一种C风格语言——GLSL。
一、GLSL
GLSL是为图形量身定制的,它为向量和矩阵运算提供了很大帮助。
1、着色器的开头总是要声明版本,接着是输入和输出变量、uniform和main函数。main函数时每个着色器的入口,用于处理所有输入变量,并用输出变量输出处理结果。
2、类型:着色器的类型包括:int、float、double、uint、bool。GLSL的两种容器类型:向量和矩阵。
向量是有1、2、3或4个元素的基本类型容器。vecn:包含n个默认为float元素的向量;其他还有bvecn、ivecn、uvecn、dvecn,可以使用vec.x、.y、.z、.w来获取它们的1、2、3、4号元素,GLSL也允许使用rgba来获取颜色的元素,或者stpq获取纹理坐标元素。
3、**in和out:**着色器是各自独立的小程序,每个着色器都是用这两个关键字定义输入和输出。使用layout元数据制定输入变量,这样才可以在CPU上配置顶点属性。
4、定点着色器:
#version 330 core
layout (location = 0) in vec3 position; // 位置变量的属性position为0
out vec4 vertexColor; // 为像素着色器指定一个颜色输出
void main()
{
gl_Position = vec4(position, 1.0); // 把一个vec3作为vec4的构造器的参数
vertexColor = vec4(0.5f, 0.0f, 0.0f, 1.0f); // 把输出颜色设置为暗红色
}
1
2
3
4
5
6
7
8
9
10
5、像素着色器:
#version 330 core
in vec4 vertexColor; // 和顶点着色器的vertexColor变量类型相同、名称相同
out vec4 color; //像素着色器输出的变量名可以任意命名,类型必须是vec4
void main()
{
color = vertexColor;
}
1
2
3
4
5
6
7
8
9
6、uniform
uniform 是另一种从CPU应用向GPU着色器发送数据的方式;
uniform和顶点属性的不同:
1)uniform是全局的:在所有着色器程序对象中都是独一无二的,可以在任何着色器程序的任何阶段使用,并且无论吧uniform值设置成什么,uniform都会一直保存着它们的数据,直到它们被重置或更新;
2)uniform是constant的:不能在着色器中修改,任何修改都会报错;
4
5
6
7
8
9
#version 330 core
out vec4 color;
uniform vec4 ourColor; // 在C++代码中设置
void main()
{
color = ourColor;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
绘制简单三角形
代码:
#define GLEW_STATIC
#include
#include
#include
using namespace std;
//函数原型
void key_callback(GLFWwindow* window, int key,int scancode,
int action,int mode);
//窗口大小
const GLuint WIDTH = 800,HEIGHT = 600;
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x,position.y,position.z,1.0);\n"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f,0.5f,0.2f,1.0f);\n"
"}\n\0";
int main(){
//初始化 GLFW
glfwInit();
//设置GLFW需要的选项
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
//创建一个窗口对象
GLFWwindow* window = glfwCreateWindow(WIDTH,HEIGHT,"test2",nullptr,nullptr);
glfwMakeContextCurrent(window);
//设置徐亚的回调函数
glfwSetKeyCallback(window,key_callback);
glewExperimental = GL_TRUE;
glewInit();
int width,height;
glfwGetFramebufferSize(window,&width,&height);
glViewport(0,0,width,height);
//定点着色器
GLuint vertextShader;
vertextShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertextShader,1,&vertexShaderSource,NULL);
glCompileShader(vertextShader);
//用于判断一个着色器是否编译成功
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertextShader,GL_COMPILE_STATUS,&success);
if(!success){
glGetShaderInfoLog(vertextShader,512,NULL,infoLog);
cout<<"vertextShader COMPILE FAILED"<
//像素着色器
GLuint fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
glCompileShader(fragmentShader);
//用于判断一个着色器是否编译成功
glGetShaderiv(fragmentShader,GL_COMPILE_STATUS,&success);
if(!success){
glGetShaderInfoLog(fragmentShader,512,NULL,infoLog);
cout<<"fragmentShader COMPILE FAILED"<
//创建一个着色器程序对象:多个着色器最后链接的版本
GLuint shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram,vertextShader);
glAttachShader(shaderProgram,fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram,GL_LINK_STATUS,&success);
if(!success){
glGetProgramInfoLog(shaderProgram,512,NULL,infoLog);
}
glDeleteShader(vertextShader);
glDeleteShader(fragmentShader);
//创建定点数据
GLfloat vertices[] = {
-0.5f,-0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
//创建 VBO 顶点缓冲数据,并把数据赋值到内存中
GLuint VBO;
glGenBuffers(1,&VBO);
//VAO顶点数组对象
GLuint VAO;
glGenVertexArrays(1,&VAO);
//绑定VAO
glBindVertexArray(VAO);
//复制顶点数据到缓冲中提供给OpenGL使用
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
//设置顶点属性指针
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(GLfloat),(GLvoid *)0);
glEnableVertexAttribArray(0);
//解绑VBO
glBindBuffer(GL_ARRAY_BUFFER,0);
//解绑VAO
glBindVertexArray(0);
while(!glfwWindowShouldClose(window)){
glfwPollEvents();
//释放颜色缓存
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//绘制物体
//当打算渲染一个物体使用使用着色器程序
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
//绘制函数
glDrawArrays(GL_TRIANGLES,0,3);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1,&VAO);
glDeleteBuffers(1,&VBO);
glfwTerminate();
return 0;
}
void key_callback(GLFWwindow* window, int key,int scancode,
int action,int mode){
if(key == GLFW_KEY_L && action == GLFW_PRESS){
glfwSetWindowShouldClose(window,GL_TRUE);
}
}
绘制结果:
使用uniform绘制三角形示例代码:
#define GLEW_STATIC
#include
#include
#include
#include
using namespace std;
//函数原型
void key_callback(GLFWwindow* window, int key,int scancode,
int action,int mode);
//窗口大小
const GLuint WIDTH = 800,HEIGHT = 600;
//着色器
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"layout (location = 1) in vec3 color;\n"
"out vec3 ourColor;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position, 1.0);\n"
"ourColor = color;\n"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"uniform vec4 ourColor;\n"
"void main()\n"
"{\n"
"color = ourColor;\n"
"}\n\0";
int main(){
//初始化 GLFW
glfwInit();
//设置GLFW需要的选项
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
//创建一个窗口对象
GLFWwindow* window = glfwCreateWindow(WIDTH,HEIGHT,"test2",nullptr,nullptr);
glfwMakeContextCurrent(window);
//设置徐亚的回调函数
glfwSetKeyCallback(window,key_callback);
glewExperimental = GL_TRUE;
glewInit();
//定义 vierport 维度
glViewport(0,0,WIDTH,HEIGHT);
//定义点着色器
GLuint vertextShader;
vertextShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertextShader,1,&vertexShaderSource,NULL);
glCompileShader(vertextShader);
//用于判断一个着色器是否编译成功
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertextShader,GL_COMPILE_STATUS,&success);
if(!success){
glGetShaderInfoLog(vertextShader,512,NULL,infoLog);
cout<<"vertextShader COMPILE FAILED"<
//像素着色器
GLuint fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
glCompileShader(fragmentShader);
//用于判断一个着色器是否编译成功
glGetShaderiv(fragmentShader,GL_COMPILE_STATUS,&success);
if(!success){
glGetShaderInfoLog(fragmentShader,512,NULL,infoLog);
cout<<"fragmentShader COMPILE FAILED"<
//创建一个着色器程序对象:多个着色器最后链接的版本
GLuint shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram,vertextShader);
glAttachShader(shaderProgram,fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram,GL_LINK_STATUS,&success);
if(!success){
glGetProgramInfoLog(shaderProgram,512,NULL,infoLog);
}
glDeleteShader(vertextShader);
glDeleteShader(fragmentShader);
//创建定点数据
GLfloat vertices[] = {
-0.5f,-0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
//创建 VBO 顶点缓冲数据,并把数据赋值到内存中
GLuint VBO;
glGenBuffers(1,&VBO);
//VAO顶点数组对象
GLuint VAO;
glGenVertexArrays(1,&VAO);
//绑定VAO
glBindVertexArray(VAO);
//复制顶点数据到缓冲中提供给OpenGL使用
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
//设置顶点属性指针
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(GLfloat),(GLvoid *)0);
glEnableVertexAttribArray(0);
//解绑VBO
glBindBuffer(GL_ARRAY_BUFFER,0);
//解绑VAO
glBindVertexArray(0);
while(!glfwWindowShouldClose(window)){
glfwPollEvents();
//释放颜色缓存
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//绘制物体
//当打算渲染一个物体使用使用着色器程序
glUseProgram(shaderProgram);
//更新uniform颜色
GLfloat timeValue = glfwGetTime();
GLfloat greenValue = (sin(timeValue) /2 )+0.5;
GLint vertexColorLocation = glGetUniformLocation(shaderProgram,"ourColor");
glUniform4f(vertexColorLocation,0.0f,greenValue,0.0f,1.0f);
//绘制函数--->用于绘制三角形
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,3);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1,&VAO);
glDeleteBuffers(1,&VBO);
glfwTerminate();
return 0;
}
void key_callback(GLFWwindow* window, int key,int scancode,
int action,int mode){
if(key == GLFW_KEY_L && action == GLFW_PRESS){
glfwSetWindowShouldClose(window,GL_TRUE);
}
}
效果:可以看到一个颜色随着时间发生变化的三角形;
绘制渐变色三角形:
代码:
#define GLEW_STATIC
#include
#include
#include
#include
using namespace std;
//函数原型
void key_callback(GLFWwindow* window, int key,int scancode,
int action,int mode);
//窗口大小
const GLuint WIDTH = 800,HEIGHT = 600;
//着色器
const GLchar* vertexShaderSource ="#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"layout (location = 1) in vec3 color;\n"
"out vec3 ourColor;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position, 1.0);\n"
"ourColor = color;\n"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core\n"
"in vec3 ourColor;\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(ourColor, 1.0f);\n"
"}\n\0";
int main(){
//初始化 GLFW
glfwInit();
//设置GLFW需要的选项
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
//创建一个窗口对象
GLFWwindow* window = glfwCreateWindow(WIDTH,HEIGHT,"test2",nullptr,nullptr);
glfwMakeContextCurrent(window);
//设置徐亚的回调函数
glfwSetKeyCallback(window,key_callback);
glewExperimental = GL_TRUE;
glewInit();
//定义 vierport 维度
glViewport(0,0,WIDTH,HEIGHT);
//定义点着色器
GLuint vertextShader;
vertextShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertextShader,1,&vertexShaderSource,NULL);
glCompileShader(vertextShader);
//用于判断一个着色器是否编译成功
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertextShader,GL_COMPILE_STATUS,&success);
if(!success){
glGetShaderInfoLog(vertextShader,512,NULL,infoLog);
cout<<"vertextShader COMPILE FAILED"<
//像素着色器
GLuint fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
glCompileShader(fragmentShader);
//用于判断一个着色器是否编译成功
glGetShaderiv(fragmentShader,GL_COMPILE_STATUS,&success);
if(!success){
glGetShaderInfoLog(fragmentShader,512,NULL,infoLog);
cout<<"fragmentShader COMPILE FAILED"<
//创建一个着色器程序对象:多个着色器最后链接的版本
GLuint shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram,vertextShader);
glAttachShader(shaderProgram,fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram,GL_LINK_STATUS,&success);
if(!success){
glGetProgramInfoLog(shaderProgram,512,NULL,infoLog);
}
glDeleteShader(vertextShader);
glDeleteShader(fragmentShader);
//创建顶点数据
GLfloat vertices[] = {
// Positions // Colors
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // Bottom Left
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // Top
};
//创建 VBO 顶点缓冲数据,并把数据赋值到内存中
GLuint VBO;
glGenBuffers(1,&VBO);
//VAO顶点数组对象
GLuint VAO;
glGenVertexArrays(1,&VAO);
//绑定VAO
glBindVertexArray(VAO);
//复制顶点数据到缓冲中提供给OpenGL使用
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
//设置顶点属性指针
//位置属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
//颜色属性
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//解绑VAO
glBindVertexArray(0);
while(!glfwWindowShouldClose(window)){
glfwPollEvents();
//释放颜色缓存
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//绘制物体
//当打算渲染一个物体使用使用着色器程序
glUseProgram(shaderProgram);
//绘制函数--->用于绘制三角形
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,3);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1,&VAO);
glDeleteBuffers(1,&VBO);
glfwTerminate();
return 0;
}
void key_callback(GLFWwindow* window, int key,int scancode,
int action,int mode){
if(key == GLFW_KEY_L && action == GLFW_PRESS){
glfwSetWindowShouldClose(window,GL_TRUE);
}
}
效果: