重点部分:
在顶点着色器中定义一个out扔到片段着色器中(in),片段着色器将顶点着色器中vertexColor的值赋给每一个像素;
定义uniform(global 全局变量),uniform变量在每个着色器程序对象中都是独一无二的,而且它可以被着色器程序的任意着色器在任意阶段访问。无论把uniform值设置成什么,uniform会一直保存它们的数据,直到它们被重置或更新
//GLSL类C语言 shader的结构如顶点着色器和片段着色器
//顶点着色器
const char* vertexShadersource =
"#version 330 core \n"
"layout (location = 7) in vec3 aPos; \n"
"out vec4 vertexColor; \n" //在 vertexShadersource 中定义并输出到fragmentShadersource中的FragColor来使用
"void main(){ \n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); \n"
" vertexColor = vec4(1.0,0,0,1.0); \n"
"} \n";
//片段着色器
const char* fragmentShadersource =
"#version 330 core \n "
"in vec4 vertexColor; \n "
"uniform vec4 ourColor; \n"
"out vec4 FragColor; \n "
"void main(){ \n "
" FragColor = ourColor;} \n ";
//闪烁
float timeValue = glfwGetTime();
float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
glUseProgram(shaderProgram);
glUniform4f(vertexColorLocation, 0, greenValue, 0, 1.0f);
代码:
#define GLEW_STATIC
#include<iostream>
#include<GL/glew.h>
//#include<glad/glad.h>
#include<GLFW/glfw3.h>
float vertices[] = {
-0.5f, -0.5f, 0.0f,//右上角0
0.5f, -0.5f, 0.0f,//右下角1
0.0f, 0.5f, 0.0f, //左下角2
//0.5f,0.5f,0.0f;
//0.0f,0.5f,0.0f;
0.8f, 0.8f,0.0f左上角3
};
unsigned int indices[] = {0,1,2,2,1,3};//按照顶点顺序绘制
/*
const char* vertexShadersource =
"#version 330 core \n"
"layout (location = 7) in vec3 aPos; \n"
"void main(){ \n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);} \n";
const char* fragmentShadersource =
"#version 330 core \n"
"out vec4 FragColor; \n"
"void main(){ \n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);} \n";
*/
//shader的编译
//顶点着色器
const char* vertexShadersource =
"#version 330 core \n"
"layout (location = 7) in vec3 aPos; \n"
"out vec4 vertexColor; \n" //在 vertexShadersource 中定义并输出到fragmentShadersource中的FragColor来使用
"void main(){ \n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); \n"
" vertexColor = vec4(1.0,0,0,1.0); \n"
"} \n";
//片段着色器
const char* fragmentShadersource =
"#version 330 core \n "
"in vec4 vertexColor; \n "
"uniform vec4 ourColor; \n"
"out vec4 FragColor; \n "
"void main(){ \n "
" FragColor = ourColor;} \n ";
//定义一个键盘输入
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)//如果键盘输入为esc,则设置关闭功能为真
glfwSetWindowShouldClose(window, true);
}
int main() {
glfwInit();
//告诉版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "My Opengl", NULL, NULL);
if (window == NULL)
{
printf("OPen Window failed");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);//通知GLFW将窗口的上下文设置为当前线程的主上下文了。
//init glew 得初始化glew 不然会出现异常问题
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
printf("init glew failed");
glfwTerminate();
return -1;
}
glViewport(0, 0, 800, 600);//glViewport函数前两个参数控制 程序 (不是opengl的窗口)窗口左下角的位置。第三个和第四个参数控制渲染窗口的宽度和高度(像素)
//进行面的剔除
//glEnable(GL_CULL_FACE);//打开面剔除功能
//glCullFace(GL_BACK);//进行背面的剔除,(猜测:如果三角形的法向量是朝屏幕里面的 应该就是背面)
//线框模式绘制
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
//把需要创建的着色器类型以参数形式提供给glCreateShader。由于我们正在创建一个顶点着色器,传递的参数是GL_VERTEX_SHADER
glShaderSource(vertexShader, 1, &vertexShadersource, NULL);
glCompileShader(vertexShader);
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShadersource, NULL);
glCompileShader(fragmentShader);
//glShaderSource函数把要编译的着色器对象作为第一个参数。第二参数指定了传递的源码字符串数量,这里只有一个。第三个参数是顶点着色器真正的源码,第四个参数我们先设置为NULL
//创建一个程序对象
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
//把之前编译的着色器附加到程序对象上
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
//用glLinkProgram链接它们
glLinkProgram(shaderProgram);
//使用glVertexAttribPointer函数告诉OpenGL该如何解析顶点数据
glVertexAttribPointer(7, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);//第一个参数是layout(location = 0)中的0
glEnableVertexAttribArray(7);//layout(location = 0)中的0
while (!glfwWindowShouldClose(window))
{
//输入
processInput(window);
//渲染部分
//设置opengl的窗口背景的颜色
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
//闪烁
float timeValue = glfwGetTime();
float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
glUseProgram(shaderProgram);
glUniform4f(vertexColorLocation, 0, greenValue, 0, 1.0f);
glUseProgram(shaderProgram);
//glDrawArrays(GL_TRIANGLES, 0, 3);
//使用ebo进行绘制
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);//glfwWindowShouldClose函数在我们每次循环的开始前检查一次GLFW是否被要求退出,如果是的话该函数返回true然后渲染循环便结束了,之后为我们就可以关闭应用程序了
glfwPollEvents();//glfwPollEvents函数检查有没有触发什么事件(比如键盘输入、鼠标移动等)、更新窗口状态,并调用对应的回调函数
}
glfwTerminate();//清除释放分配的资源
return 0;
}