openGL shader学习总结

着色器是运行在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); // 把输出颜色设置为暗红色
}

5、像素着色器:

#version 330 core
in vec4 vertexColor; // 和顶点着色器的vertexColor变量类型相同、名称相同
 
out vec4 color; //像素着色器输出的变量名可以任意命名,类型必须是vec4
 
void main()
{
    color = vertexColor;
}

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;
}

绘制简单三角形
代码:

#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"<

绘制结果:
openGL shader学习总结_第1张图片

使用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"<用于绘制三角形
		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"<用于绘制三角形
		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);
					  }
}

效果:

openGL shader学习总结_第2张图片

你可能感兴趣的:(OpenGL)