1.通过循环改变fragment_shader中的uniform颜色值来改变颜色,uniform全局变量,全局唯一,所以可以直接放在片段着色器中,而不用通过顶点着色器进行传递
2.C++的程序在运行时,如果访问文件,则是以它当前的exe文件所在路径来访问的,所以使用相对路径会有风险
3.当上层给fragment_shader中传数据的时候,会发现下面接收的数据是一个向量类型的,所以需要它特有的数据进行整合 glUniform4f()即可做到
opengl_shader_glsl.cpp
#include <iostream>
#include <cmath>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "utils/Shader.h"
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void processInput(GLFWwindow *window);
/**
* 顶点数组对象:Vertex Array Object,VAO
* 顶点缓冲对象:Vertex Buffer Object,VBO
* 索引缓冲对象:Element Buffer Object,EBO或Index Buffer Object,IBO
* @return
*/
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_OPENGL_CORE_PROFILE);
//第三个参数是标题
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpengl", NULL, NULL);
if (window == NULL) {
std::cout << "Fail to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//奇特的写法,本应是个函数,写出来却像个变量,连参数都不需要传了
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cout << "File to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);//指定视口大小,跟java一样
//设置窗口尺寸改变的回调,C++原来是直接把一个函数作为回调传进去
//原来它们的函数名是可以直接使用的,也解释了上面的现象
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//以此方式创建新对象?
//这里要注意,当程序运行的时候,它是去它运行的目录下去找这个文件的,shit
//怪不得一直报错,
// Shader shader("shader/vertex_shader_one.glsl", "shader/fragment_shader_one.glsl");
Shader shader("D:\\cl_workspace\\TestOpengl\\shader\\vertex_shader_one.glsl",
"D:\\cl_workspace\\TestOpengl\\shader\\fragment_shader_one.glsl");
//矩形
float vertices[] = {
0.5f, 0.5f, 0.0f, // 右上角
0.5f, -0.5f, 0.0f, // 右下角
-0.5f, -0.5f, 0.0f, // 左下角
-0.5f, 0.5f, 0.0f // 左上角
};
unsigned int indices[] = { // 注意索引从0开始!
0, 1, 3, // 第一个三角形
1, 2, 3 // 第二个三角形
};
unsigned int VBO;//顶点缓冲对象
glGenBuffers(1, &VBO);//&符号应该是传引用对象的意思,不然会传值进去
//设置索引数据
unsigned int EBO;//索引缓冲对象
glGenBuffers(1, &EBO);
//创建一个VAO,看起来像是对上面的固定过程的封装,毕竟链接着色器程序,指定数据,都是一些固定操作
//且VAO是必须的
unsigned int VAO;
glGenVertexArrays(1, &VAO);
//初始化代码,一般只运行一次
//1.绑定VAP
glBindVertexArray(VAO);
//2.把顶点数组复制到缓冲中供OpenGl使用,GL_ARRAY_BUFFER这个是个特殊的东西
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//2.5 把索引数组复制到一个索引缓冲中,供opengl使用
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//3.设置顶点属性指针,第一个参数0,其实是Location
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//循环绘制
while (!glfwWindowShouldClose(window)) {//判断界面是否关闭,没关闭就循环绘制
processInput(window);
//同java
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shader.use();//必须先激活
// 更新uniform颜色
float timeValue = (float) glfwGetTime();
float greenValue = (float) (sin(timeValue) / 2.0f + 0.5f);
shader.setFloat("ourColor",0.0f, greenValue, 0.0f, 1.0f);
glBindVertexArray(VAO);
// glDrawArrays(GL_TRIANGLES,0,3);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
//双缓冲机制,前缓冲保存着最终输出的图像,后缓冲则进行绘制,绘制完成以后与前缓冲交换,就会立即显示
//单缓冲会存在闪烁问题
glfwSwapBuffers(window);//交换颜色缓冲
glfwPollEvents();//检查有没有什么触发事件,鼠标键盘等,并调用相关的回调
}
//回收数据
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glfwTerminate();//结束绘制
return 0;
}
//窗口尺寸改变的回调
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window) {
//按下esc键的意思
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);//关闭窗户
}
}
Shader.h
//
// Created by dell on 2018/3/22.
//
#ifndef TESTOPENGL_SHADER_H
#define TESTOPENGL_SHADER_H
#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader {
public:
//程序ID
unsigned int ID;
//构造器读取并构建着色器
Shader(const GLchar *vertexPath, const GLchar *fragmentPath);
//使用程序
void use();
//工具函数
void setBool(const std::string &name, bool value) const;
void setInt(const std::string &name, int value) const;
void setFloat(const std::string &name, float value) const;
void setFloat(const std::string &name, float value1, float value2, float value3, float value4) const;
};
#endif //TESTOPENGL_SHADER_H
Shader.cpp
//
// Created by dell on 2018/3/22.
//
#include "Shader.h"
Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath) {
//1.从文件路径中获取顶点/片段着色器
//这四个是定义啊
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// 保证ifstream 对象可以抛出异常:
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
//打开文件
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
//读取文件的缓冲内容到数据流中
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
//关闭
vShaderFile.close();
fShaderFile.close();
//把数据流的字符放到String中
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
} catch (std::ifstream::failure e) {
std::cout << "ERROR::SHADER::fILE_NOT_SUCCESSFULLY_READ" << std::endl;
}
//最后取出来的着色器数据
const char *vShaderCode = vertexCode.c_str();
const char *fShaderCode = fragmentCode.c_str();
//2.编译着色器
unsigned int vertex, fragment;
int success;
char infoLog[512];
//顶点着色器
vertex = glCreateShader(GL_VERTEX_SHADER);
//把着色器源码附加到着色器对象上,然后编译它
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
//检测是否编译成功
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::cOMPILATION_FAILED\n" << infoLog << std::endl;
}
//片段着色器
fragment = glCreateShader(GL_FRAGMENT_SHADER);
//把着色器源码附加到着色器对象上,然后编译它
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
//检测是否编译成功
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::cOMPILATION_FAILED\n" << infoLog << std::endl;
}
//着色器程序
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
// 打印连接错误(如果有的话)
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(ID, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertex);
glDeleteShader(fragment);
}
void Shader::use() {
glUseProgram(ID);
}
void Shader::setBool(const std::string &name, bool value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value);
}
void Shader::setInt(const std::string &name, int value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
void Shader::setFloat(const std::string &name, float value) const {
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
void Shader::setFloat(const std::string &name, float value1, float value2, float value3, float value4) const {
glUniform4f(glGetUniformLocation(ID, name.c_str()), value1, value2, value3, value4);
}
fragment_shader_one.glsl
#version 330 core
out vec4 FragColor;
uniform vec4 ourColor;
void main() {
FragColor = ourColor;
}
vertex_shader_one.glsl
#version 330 core
layout (location = 0) in vec3 aPos;//位置变量的属性位置值为0
layout (location = 1) in vec3 aColor;//位置变量的属性位置值为0
//out vec3 ourColor; //向片段着色器输出一个颜色
void main() {
gl_Position = vec4(aPos,1.0);
// ourColor = aColor;
}
网友评论