序言
以glew、glfw库
OpenGL学习网站
glfw官网
OpenGL-API文档
glew官网
投光物
我们目前使用的所有光照都来自于一个单独的光源,这是空间中的一个点。它的效果不错,但是在真实世界,我们有多种类型的光,它们每个表现都不同。一个光源把光投射到物体上,叫做投光。
定向光
当一个光源很远的时候,来自光源的每条光线接近于平行。这看起来就像所有的光线来自于同一个方向,无论物体和观察者在哪儿。当一个光源被设置为无限远时,它被称为定向光(Directional Light),因为所有的光线都有着同一个方向;它会独立于光源的位置。
我们知道的定向光源
的一个好例子是,太阳。太阳和我们不是无限远,但它也足够远了,在计算光照的时候,我们感觉它就像无限远。在下面的图片里,来自于太阳的所有的光线都被定义为平行光:
因为所有的光线都是平行的,对于场景中的每个物体光的方向都保持一致,物体和光源的位置保持怎样的关系都无所谓。由于光的方向向量保持一致,光照计算会和场景中的其他物体相似。
我们可以通过定义一个光的方向向量
,来模拟这样一个定向光,而不是使用光的位置向量。着色器计算保持大致相同的要求,这次我们直接使用光的方向向量来代替用lightDir
向量和position
向量的计算:
片段着色器
struct Light
{
// vec3 position; //现在不在需要光源位置了,因为它是无限远的
vec3 direction; //光的方向向量
vec3 ambient; //环境光强度
vec3 diffuse; //漫反射强度
vec3 specular; //镜面反射强度
};
...
void main()
{
vec3 lightDir = normalize(-light.direction);
...
}
注意,我们首先对light.direction
向量取反。目前我们使用的光照计算需要光的方向作为一个来自片段朝向的光源(或者是光源朝向片段)的方向(和OpenGL笔记7-摄像机一样的原理朝向和指向是相反的,所以方向向量是片段指向光源),但是人们通常更习惯定义一个定向光作为一个全局方向,它从光源发出。所以我们必须对全局光的方向向量取反来改变它的方向;它现在是一个方向向量指向光源。同时,确保对向量进行标准化处理,因为假定输入的向量就是一个单位向量是不明智的。
作为结果的lightDir
向量被使用在diffuse
和specular
计算之前。
为了清晰地强调一个定向光对所有物体都有同样的影响,我们再次访问坐标系教程结尾部分的箱子场景。例子里我们先定义10个不同的箱子位置,为每个箱子生成不同的模型矩阵,每个模型矩阵包含相应的本地到世界变换:
for(GLuint i = 0; i < 10; i++)
{
model = glm::mat4();
model = glm::translate(model, cubePositions[i]);
GLfloat angle = 20.0f * i;
model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
同时,不要忘记定义光源的方向(注意,我们把方向定义为:从光源处发出的方向;在下面,你可以快速看到光的方向的指向):
GLint lightDirPos = glGetUniformLocation(shaderProgram, "light.direction");
glUniform3f(lightDirPos, -0.2f, -1.0f, -0.3f);
我们已经把光的位置和方向向量传递为vec3,但是有些人去想更喜欢把所有的向量设置为vec4.当定义位置向量为vec4的时候,把w元素设置为1.0非常重要,这样平移和投影才会合理的被应用。然而,当定义一个方向向量为vec4时,我们并不想让平移发挥作用(因为它们除了代表方向,其他什么也不是)所以我们把w元素设置为0.0。
方向向量被表示为:vec4(0.2f, 1.0f, 0.3f, 0.0f)。这可以作为简单检查光的类型的方法:你可以检查w元素是否等于1.0,查看我们现在所拥有的光的位置向量,w是否等于0.0,我们有一个光的方向向量,所以根据那个调整计算方法:
if(lightVector.w == 0.0) // 请留意浮点数错误 // 执行定向光照计算
else if(lightVector.w == 1.0) // 像上一个教程一样执行顶点光照计算
有趣的事实:这就是旧OpenGL(固定函数式)决定一个光源是一个定向光还是位置光源,更具这个修改它的光照。
如果你现在编译应用,飞跃场景,它看起来像有一个太阳一样的光源,把光抛到物体身上。你可以看到diffuse和specular元素都对该光源进行反射了,就像天空上有一个光源吗?看起来就像这样:
未使用定向光前
使用定向光后
完整代码
#include <glew.h>
#include <glfw3.h>
#include <iostream>
#include <SOIL2.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//顶点着色器
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"layout (location = 1) in vec3 normal;\n"
"layout (location = 2) in vec2 texCoords;\n"
"out vec3 Normal;\n" //法向量
"out vec3 FragPos;\n" //片段位置(如立方体六个面,每个面两个三角形,三角形形成的那个片段)
"out vec2 TexCoords;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
" FragPos = vec3(model * vec4(position, 1.0f));\n" //片段位置,将片段位置转化为世界坐标
" Normal = normal;\n"
" TexCoords = texCoords;\n"
"}\n";
//片段着色器
const char* fragmentShaderSource = "#version 330 core\n"
"in vec3 Normal;\n"
"in vec3 FragPos;\n"
"in vec2 TexCoords;\n"
"out vec4 color;\n"
"uniform vec3 objectColor;\n"
"uniform vec3 lightColor;\n"
"uniform vec3 lightPos;\n"
"uniform vec3 viewPos;\n" //摄像机位置
//材质属性
"struct Material\n"
"{\n"
" sampler2D diffuse;\n" //在漫反射光照下物体的颜色
" sampler2D specular;\n" //受到镜面反射的颜色
" float shininess;\n" //反射半径
"};\n"
"uniform Material material;\n"
//光强度
"struct Light\n"
"{\n"
//" vec3 position;\n" //现在无需光源位置,因为无限远
" vec3 direction;" //光的方向向量
" vec3 ambient;\n" //环境光强度
" vec3 diffuse;\n" //漫反射强度
" vec3 specular;\n" //镜面反射强度
"};\n"
"uniform Light light;\n"
"void main()\n"
"{\n"
//光的方向向量
" vec3 direction = normalize(-light.direction);\n" //和摄像机一样,朝向和指向是相反的
//环境光
" vec3 ambient = vec3(texture(material.diffuse, TexCoords)) * light.ambient;\n" //环境光
//漫反射
" vec3 norm = normalize(Normal);\n" //法向量单位化
//" vec3 lightDir = normalize(lightPos - FragPos);\n" //计算光的方向,即方向向量 指向光源方向lightPos, 朝向片段方向FragPos
" float diff = max(dot(norm, direction), 0.0);\n" //计算光对当前片段的实际散射影响
" vec3 diffuse = diff * vec3(texture(material.diffuse, TexCoords)) * light.diffuse;\n" //得到散射因子
//镜面高光
" vec3 viewDir = normalize(viewPos - FragPos);\n" //指向摄像机viewPos,朝向FragPos
" vec3 reflectDir = reflect(-direction, norm);\n" //反射的方向
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
" vec3 specular = vec3(texture(material.specular, TexCoords)) * spec * light.specular;\n" //计算镜面分量
//光照
" vec3 result = ambient + diffuse + specular;\n" //冯氏光照模型 = 环境光 + 漫反射 + 镜面反射
" color = vec4(result, 1.0f) ;\n"
"}\n";
//光源-顶点着色器
const char* lightVertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
"}\n";
//光源-片段着色器
const char* lightFragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(1.0f);\n"
"}\n";
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void do_movement();
const GLuint WIDTH = 800, HEIGHT = 600;
//摄像机信息
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); //摄像机位置
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); //摄像机朝向
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); //上向量
GLfloat deltaTime = 0.0f; //当前帧遇上一帧的时间差
GLfloat lastFrame = 0.0f; //上一帧的时间
GLfloat lastX = WIDTH / 2, lastY = HEIGHT / 2; //鼠标上一次位置,默认屏幕中心
GLfloat pitch = 0.0f; //俯仰角
GLfloat yaw = -90.0f; //偏航角
GLfloat aspect = 45.0f; //视角大小
glm::vec3 lightPos(1.0f, 1.0f, 2.0f);
bool keys[1024];
int main()
{
glfwInit(); //必须要将glfw初始化
//告诉GLFW使用OpenGL版本
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //主版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //次版本号
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //使用的是OpenGL核心模式
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //不允许调整窗口大小
//创建窗口
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW Window" << std::endl;
glfwTerminate(); //销毁窗口与数据
return -1;
}
glfwMakeContextCurrent(window); //将OpenGL指向为当前窗口
glewExperimental = GL_TRUE; //用于告知GLEW使用现化OpenGL技术
//glew初始化
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
//视口
int width = 800, height = 600;
glfwGetFramebufferSize(window, &width, &height); //设置OpenGL渲染窗口的尺寸
glViewport(0, 0, width, height); //设置窗口的维度 前两个参数控制窗口左下角的位置, 第三、四个参数控制渲染窗口的宽度和高度
glfwSetKeyCallback(window, key_callback); //注册按键回调事件
glfwSetCursorPosCallback(window, mouse_callback); //注册鼠标回调事件
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); //隐藏鼠标,并捕获他
glfwSetScrollCallback(window, scroll_callback); //鼠标滚动回调事件
glEnable(GL_DEPTH_TEST); //深度缓存区
//顶点着色器
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
GLint vertexSuccess;
GLchar vertexInfoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(vertexShader, 512, nullptr, vertexInfoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << vertexInfoLog << std::endl;
}
//片段着色器
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
GLint fragmentSuccess;
GLchar fragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, fragmentInfoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << fragmentInfoLog << std::endl;
}
//着色器链接程序
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
GLint programSuccess;
GLchar programInfoLog[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &programSuccess);
if (!programSuccess) {
glGetProgramInfoLog(shaderProgram, 512, nullptr, programInfoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << programInfoLog << std::endl;
}
//光源-顶点着色器
GLuint lightVertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(lightVertexShader, 1, &lightVertexShaderSource, nullptr);
glCompileShader(lightVertexShader);
GLint lightVertexSuccess;
GLchar lightVertexInfoLog[512];
glGetShaderiv(lightVertexShader, GL_COMPILE_STATUS, &lightVertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(lightVertexShader, 512, nullptr, lightVertexInfoLog);
std::cout << "ERROR::SHADER::LIGHT::VERTEX::COMPILATION_FAILED\n" << lightVertexInfoLog << std::endl;
}
//片段着色器
GLuint lightFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(lightFragmentShader, 1, &lightFragmentShaderSource, nullptr);
glCompileShader(lightFragmentShader);
GLint lightFragmentSuccess;
GLchar lightFragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &lightFragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, lightFragmentInfoLog);
std::cout << "ERROR::SHADER::LIGHT::FRAGMENT::COMPILATION_FAILED\n" << lightFragmentInfoLog << std::endl;
}
//光源着色器链接程序
GLuint lightShaderProgram = glCreateProgram();
glAttachShader(lightShaderProgram, lightVertexShader);
glAttachShader(lightShaderProgram, lightFragmentShader);
glLinkProgram(lightShaderProgram);
GLint lightProgramSuccess;
GLchar lightProgramInfoLog[512];
glGetProgramiv(lightShaderProgram, GL_LINK_STATUS, &lightProgramSuccess);
if (!programSuccess) {
glGetProgramInfoLog(lightShaderProgram, 512, nullptr, lightProgramInfoLog);
std::cout << "ERROR::SHADER::LIGHT::PROGRAM::LINKING_FAILED\n" << lightProgramInfoLog << std::endl;
}
GLfloat vertices[] = {
//-----位置 //法向量 //纹理坐标
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
//十个立方体
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
//顶点数据
GLuint VBO, VAO;
glGenBuffers(1, &VBO); //顶点缓冲对象
glGenVertexArrays(1, &VAO); //顶点数组对象
glBindVertexArray(VAO);
//顶点数据
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
//法向量
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//纹理坐标
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//光源
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(lightVAO);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//纹理
GLuint texture;
int textureWidht, textureHeight;
//加载纹理
unsigned char* image = SOIL_load_image("container2.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
GLuint texture2;
//加载纹理
image = SOIL_load_image("container2_specular.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
while (!glfwWindowShouldClose(window)) {
//检查GLFW是否退出,即窗口是否关闭了,true代表结束了
glfwPollEvents(); //检查有没有事件发生(键盘输入、鼠标移动),如发生调用对应的回调函数 键盘事件:glfwSetKeyCallback(window, key_callback); key_callback即设定的回调函数
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); //清空屏幕所用的颜色,即清除颜色缓冲之后,整个颜色缓冲都会被填充为glClearColor里所设置的颜色。
//glClear(GL_COLOR_BUFFER_BIT); //清空屏幕缓冲,这里是颜色缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
do_movement();
//渲染指令
glUseProgram(shaderProgram);
glm::mat4 model; //模型矩阵
glm::mat4 view; //观察矩阵
glm::mat4 projection; //投影矩阵-透视投影
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
projection = glm::perspective(aspect, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f); //缩放效果
//物体颜色和光颜色
GLint objectColorLoc = glGetUniformLocation(shaderProgram, "objectColor");
GLint lightColorLoc = glGetUniformLocation(shaderProgram, "lightColor");
glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f);// 我们所熟悉的珊瑚红
glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f); // 依旧把光源设置为白色
//材质
GLint matDiffuseLoc = glGetUniformLocation(shaderProgram, "material.diffuse");
GLint matSpecularLoc = glGetUniformLocation(shaderProgram, "material.specular");
GLint matShineLoc = glGetUniformLocation(shaderProgram, "material.shininess");
glUniform1i(matDiffuseLoc, 0);
glUniform1i(matSpecularLoc, 1);
glUniform1f(matShineLoc, 64.0f);
//光强度
GLint lightAmbientLoc = glGetUniformLocation(shaderProgram, "light.ambient");
GLint lightDiffuseLoc = glGetUniformLocation(shaderProgram, "light.diffuse");
GLint lightSpecularLoc = glGetUniformLocation(shaderProgram, "light.specular");
GLint lightDirPos = glGetUniformLocation(shaderProgram, "light.direction");
glUniform3f(lightAmbientLoc, 0.2f, 0.2f, 0.2f);
glUniform3f(lightDiffuseLoc, 0.5f, 0.5f, 0.5f);
glUniform3f(lightSpecularLoc, 1.0f, 1.0f, 1.0f);
glUniform3f(lightDirPos, -0.2f, -1.0f, -0.3f);
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
GLint lightPosLoc = glGetUniformLocation(shaderProgram, "lightPos");
GLint viewPosLoc = glGetUniformLocation(shaderProgram, "viewPos");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z);
glUniform3f(viewPosLoc, cameraPos.x, cameraPos.y, cameraPos.z);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(VAO);
//glDrawArrays(GL_TRIANGLES, 0, 36);
for (GLuint i = 0; i < 10; i++)
{
model = glm::mat4();
model = glm::translate(model, cubePositions[i]);
GLfloat angle = 20.0f * i;
model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0);
//光源
glUseProgram(lightShaderProgram); //光源
modelLoc = glGetUniformLocation(lightShaderProgram, "model");
viewLoc = glGetUniformLocation(lightShaderProgram, "view");
projLoc = glGetUniformLocation(lightShaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(lightVAO);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glfwSwapBuffers(window); //交换颜色缓冲,用来绘制,输出显示在屏幕上
}
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
glfwTerminate();
return 0;
}
/*
* 按键回调事件
* @param window 窗口
* @param key 按键
* @param scancode 扫描码
* @param action 表示这个按键是被按下还是释放
* @param mode 是否有Ctrl、Shift、Alt、Super等按钮的操作
*/
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
//esc键按下,关闭窗口
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (action == GLFW_PRESS) //按下
keys[key] = true;
else if (action == GLFW_RELEASE) //松开
keys[key] = false;
}
void do_movement() {
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
GLfloat cameraSpeed = 5.0f * deltaTime; //速度
if (keys[GLFW_KEY_W])
cameraPos += cameraSpeed * cameraFront;
if (keys[GLFW_KEY_S])
cameraPos -= cameraSpeed * cameraFront;
if (keys[GLFW_KEY_A])
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_D])
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_SPACE]) //向上
cameraPos += cameraSpeed * cameraUp;
}
bool firstMouse = true; //除了第一次鼠标移动外,其他不能移动
/*
* 鼠标回调事件
* @param window 窗口
* @param xpos 鼠标x坐标
* @param ypos 鼠标y坐标
*/
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
GLfloat xoffset = xpos - lastX;
GLfloat yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
GLfloat sensitivity = 0.05; //鼠标转向速度
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
/*
* 鼠标滚轮回调事件
*/
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
if (aspect >= 1.0f && aspect <= 45.0f)
aspect -= yoffset;
if (aspect <= 1.0f)
aspect = 1.0f;
if (aspect >= 45.0f)
aspect = 45.0f;
}
点光源
定向光作为全局光可以照亮整个场景,这非常棒,但是另一方面除了定向光,我们通常也需要几个点光源(Point Light),在场景里发亮。点光是一个在时间里有位置的光源,它向所有方向发光,光线随距离增加逐渐变暗。想象灯泡和火炬作为投光物,它们可以扮演点光的角色。
之前的教程我们已经使用了(最简单的)点光。我们指定了一个光源以及其所在的位置,它从这个位置向所有方向发散光线。然而,我们定义的光源所模拟光线的强度却不会因为距离变远而衰减,这使得看起来像是光源亮度极强。在大多数3D仿真场景中,我们更希望去模拟一个仅仅能照亮靠近光源点附近场景的光源,而不是照亮整个场景的光源。
如果你把10个箱子添加到之前教程的光照场景中,你会注意到黑暗中的每个箱子都会有同样的亮度,就像箱子在光照的前面;没有公式定义光的距离衰减。我们想让黑暗中与光源比较近的箱子被轻微地照亮。
衰减
随着光线穿越距离的变远使得亮度也相应地减少的现象,通常称之为衰减(Attenuation)。一种随着距离减少亮度的方式是使用线性等式。这样的一个随着距离减少亮度的线性方程,可以使远处的物体更暗。然而,这样的线性方程效果会有点假。在真实世界,通常光在近处时非常亮,但是一个光源的亮度,开始的时候减少的非常快,之后随着距离的增加,减少的速度会慢下来。我们需要一种不同的方程来减少光的亮度。
幸运的是一些聪明人已经早就把它想到了。下面的方程把一个片段的光的亮度除以一个已经计算出来的衰减值,这个值根据光源的远近得到:
$$
F_ = \frac{1.0}{K_c + K_l * d + K_q * d^2}
$$
在这里$d$代表片段到光源的距离。为了计算衰减值,我们定义3个(可配置)项:常数项$K_c$,一次项$K_l$和二次项$K_q$。
- 常数项通常是1.0,它的作用是保证分母永远不会比1小,因为它可以利用一定的距离增加亮度,这个结果不会影响到我们所寻找的。
- 一次项用于与距离值相乘,这会以线性的方式减少亮度。
- 二次项用于与距离的平方相乘,为光源设置一个亮度的二次递减。二次项在距离比较近的时候相比一次项会比一次项更小,但是当距离更远的时候比一次项更大。
由于二次项的光会以线性方式减少,指导距离足够大的时候,就会超过一次项,之后,光的亮度会减少的更快。最后的效果就是光在近距离时,非常量,但是距离变远亮度迅速降低,最后亮度降低速度再次变慢。下面的图展示了在100以内的范围,这样的衰减效果。
你可以看到当距离很近的时候光有最强的亮度,但是随着距离增大,亮度明显减弱,大约接近100的时候,就会慢下来。这就是我们想要的。
选择正确的值
但是,我们把这三个项设置为什么值呢?正确的值的设置由很多因素决定:环境、你希望光所覆盖的距离范围、光的类型等。大多数场合,这是经验的问题,也要适度调整。下面的表格展示一些各项的值,它们模拟现实(某种类型的)光源,覆盖特定的半径(距离)。第一栏定义一个光的距离,它覆盖所给定的项。这些值是大多数光的良好开始,它是来自Ogre3D的礼物:
距离 | 常数项 | 一次项 | 二次项 |
---|---|---|---|
7 | 1.0 | 0.7 | 1.8 |
13 | 1.0 | 0.35 | 0.44 |
20 | 1.0 | 0.22 | 0.20 |
32 | 1.0 | 0.14 | 0.07 |
50 | 1.0 | 0.09 | 0.032 |
65 | 1.0 | 0.07 | 0.017 |
100 | 1.0 | 0.045 | 0.0075 |
160 | 1.0 | 0.027 | 0.0028 |
200 | 1.0 | 0.022 | 0.0019 |
325 | 1.0 | 0.014 | 0.0007 |
600 | 1.0 | 0.007 | 0.0002 |
3250 | 1.0 | 0.0014 | 0.000007 |
就像你所看到的,常数项$K_c$一直都是1.0。一次项$K_l$为了覆盖更远的距离通常很小,二次项$K_q$就更小了。尝试用这些值进行实验,看看它们在你的实现中各自的效果。我们的环境中,32
到100
的距离对大多数光通常就足够了。
实现衰减
为了实现衰减,在着色器中我们会需要三个额外数值:也就是公式的常量、一次项和二次项。最好把它们储存在之前定义的Light结构体
中。要注意的是我们计算lightDir
,就是在前面的教程中我们所做的,不是像之前的定向光的那部分。
struct Light
{
vec3 position; //光位置
vec3 ambient; //环境光强度
vec3 diffuse; //漫反射光强度
vec3 specular; //镜面方式光强度
float constant; //常量
float linear; //一次项
float quadratic; //二次项
};
然后,我们在OpenGL中设置这些项:我们希望光覆盖50的距离,所以我们会使用上面的表格中合适的常数项、一次项和二次项
:
在片段着色器中实现衰减很直接:我们根据公式简单的计算衰减值,在乘以ambient、diffuse和specular
元素。
我们需要将光源的距离提供给公式;还记得我们是怎样计算向量的长度吗?我们可以通过获取片段和光源之间的不同向量把向量的长度结果作为距离项。我们可以使用GLSL
的内建length
函数做这件事:
float distance = length(light.position - FragPos);
float attenuation = 1.0f / (light.constant + light.linear*distance +light.quadratic*(distance*distance));
然后,我们在光照计算中,通过把衰减值乘以ambient、diffuse和specular
颜色,包含这个衰减值。
我们可以可以把ambient
元素留着不变,这样amient
光照就不会随着距离减少,但是如果我们使用多余1个的光源,所有的ambient
元素会开始叠加,因此这种情况,我们希望ambient
光照也衰减。简单的调试出对于你的环境来说最好的效果。
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
如果你运行应用后获得这样的效果:
你可以看到现在只有最近处的箱子的前面被照得最亮。后面的箱子一点都没被照亮,因为它们距离光源太远了。
完整代码
#include <glew.h>
#include <glfw3.h>
#include <iostream>
#include <SOIL2.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//顶点着色器
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"layout (location = 1) in vec3 normal;\n"
"layout (location = 2) in vec2 texCoords;\n"
"out vec3 Normal;\n" //法向量
"out vec3 FragPos;\n" //片段位置(如立方体六个面,每个面两个三角形,三角形形成的那个片段)
"out vec2 TexCoords;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
" FragPos = vec3(model * vec4(position, 1.0f));\n" //片段位置,将片段位置转化为世界坐标
" Normal = normal;\n"
" TexCoords = texCoords;\n"
"}\n";
//片段着色器
const char* fragmentShaderSource = "#version 330 core\n"
"in vec3 Normal;\n"
"in vec3 FragPos;\n"
"in vec2 TexCoords;\n"
"out vec4 color;\n"
"uniform vec3 objectColor;\n"
"uniform vec3 lightColor;\n"
"uniform vec3 lightPos;\n"
"uniform vec3 viewPos;\n" //摄像机位置
//材质属性
"struct Material\n"
"{\n"
" sampler2D diffuse;\n" //在漫反射光照下物体的颜色
" sampler2D specular;\n" //受到镜面反射的颜色
" float shininess;\n" //反射半径
"};\n"
"uniform Material material;\n"
//光强度
"struct Light\n"
"{\n"
" vec3 position;\n" //现在无需光源位置,因为无限远
" vec3 ambient;\n" //环境光强度
" vec3 diffuse;\n" //漫反射强度
" vec3 specular;\n" //镜面反射强度
" float constant;\n" //常量
" float linear;\n" //一次项
" float quadratic;\n" //二次项
"};\n"
"uniform Light light;\n"
"void main()\n"
"{\n"
//环境光
" vec3 ambient = vec3(texture(material.diffuse, TexCoords)) * light.ambient;\n" //环境光
//漫反射
" vec3 norm = normalize(Normal);\n" //法向量单位化
" vec3 lightDir = normalize(lightPos - FragPos);\n" //计算光的方向,即方向向量 指向光源方向lightPos, 朝向片段方向FragPos
" float diff = max(dot(norm, lightDir), 0.0);\n" //计算光对当前片段的实际散射影响
" vec3 diffuse = diff * vec3(texture(material.diffuse, TexCoords)) * light.diffuse;\n" //得到散射因子
//镜面高光
" vec3 viewDir = normalize(viewPos - FragPos);\n" //指向摄像机viewPos,朝向FragPos
" vec3 reflectDir = reflect(-lightDir, norm);\n" //反射的方向
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
" vec3 specular = vec3(texture(material.specular, TexCoords)) * spec * light.specular;\n" //计算镜面分量
//衰减
" float distance = length(light.position - FragPos);\n" //光源距离
" float attenuation = 1.0f / (light.constant + light.linear*distance +light.quadratic*(distance*distance));\n" //衰减
" ambient *= attenuation;\n"
" diffuse *= attenuation;\n"
" specular *= attenuation;\n"
//光照
" vec3 result = ambient + diffuse + specular;\n" //冯氏光照模型 = 环境光 + 漫反射 + 镜面反射
" color = vec4(result, 1.0f) ;\n"
"}\n";
//光源-顶点着色器
const char* lightVertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
"}\n";
//光源-片段着色器
const char* lightFragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(1.0f);\n"
"}\n";
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void do_movement();
const GLuint WIDTH = 800, HEIGHT = 600;
//摄像机信息
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); //摄像机位置
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); //摄像机朝向
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); //上向量
GLfloat deltaTime = 0.0f; //当前帧遇上一帧的时间差
GLfloat lastFrame = 0.0f; //上一帧的时间
GLfloat lastX = WIDTH / 2, lastY = HEIGHT / 2; //鼠标上一次位置,默认屏幕中心
GLfloat pitch = 0.0f; //俯仰角
GLfloat yaw = -90.0f; //偏航角
GLfloat aspect = 45.0f; //视角大小
glm::vec3 lightPos(1.0f, 1.0f, 2.0f);
bool keys[1024];
int main()
{
glfwInit(); //必须要将glfw初始化
//告诉GLFW使用OpenGL版本
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //主版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //次版本号
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //使用的是OpenGL核心模式
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //不允许调整窗口大小
//创建窗口
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW Window" << std::endl;
glfwTerminate(); //销毁窗口与数据
return -1;
}
glfwMakeContextCurrent(window); //将OpenGL指向为当前窗口
glewExperimental = GL_TRUE; //用于告知GLEW使用现化OpenGL技术
//glew初始化
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
//视口
int width = 800, height = 600;
glfwGetFramebufferSize(window, &width, &height); //设置OpenGL渲染窗口的尺寸
glViewport(0, 0, width, height); //设置窗口的维度 前两个参数控制窗口左下角的位置, 第三、四个参数控制渲染窗口的宽度和高度
glfwSetKeyCallback(window, key_callback); //注册按键回调事件
glfwSetCursorPosCallback(window, mouse_callback); //注册鼠标回调事件
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); //隐藏鼠标,并捕获他
glfwSetScrollCallback(window, scroll_callback); //鼠标滚动回调事件
glEnable(GL_DEPTH_TEST); //深度缓存区
//顶点着色器
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
GLint vertexSuccess;
GLchar vertexInfoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(vertexShader, 512, nullptr, vertexInfoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << vertexInfoLog << std::endl;
}
//片段着色器
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
GLint fragmentSuccess;
GLchar fragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, fragmentInfoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << fragmentInfoLog << std::endl;
}
//着色器链接程序
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
GLint programSuccess;
GLchar programInfoLog[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &programSuccess);
if (!programSuccess) {
glGetProgramInfoLog(shaderProgram, 512, nullptr, programInfoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << programInfoLog << std::endl;
}
//光源-顶点着色器
GLuint lightVertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(lightVertexShader, 1, &lightVertexShaderSource, nullptr);
glCompileShader(lightVertexShader);
GLint lightVertexSuccess;
GLchar lightVertexInfoLog[512];
glGetShaderiv(lightVertexShader, GL_COMPILE_STATUS, &lightVertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(lightVertexShader, 512, nullptr, lightVertexInfoLog);
std::cout << "ERROR::SHADER::LIGHT::VERTEX::COMPILATION_FAILED\n" << lightVertexInfoLog << std::endl;
}
//片段着色器
GLuint lightFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(lightFragmentShader, 1, &lightFragmentShaderSource, nullptr);
glCompileShader(lightFragmentShader);
GLint lightFragmentSuccess;
GLchar lightFragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &lightFragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, lightFragmentInfoLog);
std::cout << "ERROR::SHADER::LIGHT::FRAGMENT::COMPILATION_FAILED\n" << lightFragmentInfoLog << std::endl;
}
//光源着色器链接程序
GLuint lightShaderProgram = glCreateProgram();
glAttachShader(lightShaderProgram, lightVertexShader);
glAttachShader(lightShaderProgram, lightFragmentShader);
glLinkProgram(lightShaderProgram);
GLint lightProgramSuccess;
GLchar lightProgramInfoLog[512];
glGetProgramiv(lightShaderProgram, GL_LINK_STATUS, &lightProgramSuccess);
if (!programSuccess) {
glGetProgramInfoLog(lightShaderProgram, 512, nullptr, lightProgramInfoLog);
std::cout << "ERROR::SHADER::LIGHT::PROGRAM::LINKING_FAILED\n" << lightProgramInfoLog << std::endl;
}
GLfloat vertices[] = {
//-----位置 //法向量 //纹理坐标
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
//十个立方体
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
//顶点数据
GLuint VBO, VAO;
glGenBuffers(1, &VBO); //顶点缓冲对象
glGenVertexArrays(1, &VAO); //顶点数组对象
glBindVertexArray(VAO);
//顶点数据
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
//法向量
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//纹理坐标
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//光源
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(lightVAO);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//纹理
GLuint texture;
int textureWidht, textureHeight;
//加载纹理
unsigned char* image = SOIL_load_image("container2.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
GLuint texture2;
//加载纹理
image = SOIL_load_image("container2_specular.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
while (!glfwWindowShouldClose(window)) {
//检查GLFW是否退出,即窗口是否关闭了,true代表结束了
glfwPollEvents(); //检查有没有事件发生(键盘输入、鼠标移动),如发生调用对应的回调函数 键盘事件:glfwSetKeyCallback(window, key_callback); key_callback即设定的回调函数
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); //清空屏幕所用的颜色,即清除颜色缓冲之后,整个颜色缓冲都会被填充为glClearColor里所设置的颜色。
//glClear(GL_COLOR_BUFFER_BIT); //清空屏幕缓冲,这里是颜色缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
do_movement();
//渲染指令
glUseProgram(shaderProgram);
glm::mat4 model; //模型矩阵
glm::mat4 view; //观察矩阵
glm::mat4 projection; //投影矩阵-透视投影
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
projection = glm::perspective(aspect, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f); //缩放效果
//物体颜色和光颜色
GLint objectColorLoc = glGetUniformLocation(shaderProgram, "objectColor");
GLint lightColorLoc = glGetUniformLocation(shaderProgram, "lightColor");
glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f);// 我们所熟悉的珊瑚红
glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f); // 依旧把光源设置为白色
//材质
GLint matDiffuseLoc = glGetUniformLocation(shaderProgram, "material.diffuse");
GLint matSpecularLoc = glGetUniformLocation(shaderProgram, "material.specular");
GLint matShineLoc = glGetUniformLocation(shaderProgram, "material.shininess");
glUniform1i(matDiffuseLoc, 0);
glUniform1i(matSpecularLoc, 1);
glUniform1f(matShineLoc, 64.0f);
//光强度
GLint lightAmbientLoc = glGetUniformLocation(shaderProgram, "light.ambient");
GLint lightDiffuseLoc = glGetUniformLocation(shaderProgram, "light.diffuse");
GLint lightSpecularLoc = glGetUniformLocation(shaderProgram, "light.specular");
//GLint lightDirPos = glGetUniformLocation(shaderProgram, "light.direction");
glUniform3f(lightAmbientLoc, 0.2f, 0.2f, 0.2f);
glUniform3f(lightDiffuseLoc, 0.5f, 0.5f, 0.5f);
glUniform3f(lightSpecularLoc, 1.0f, 1.0f, 1.0f);
//glUniform3f(lightDirPos, -0.2f, -1.0f, -0.3f);
//点光源
glUniform1f(glGetUniformLocation(shaderProgram, "light.constant"), 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "light.linear"), 0.09);
glUniform1f(glGetUniformLocation(shaderProgram, "light.quadratic"), 0.032);
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
GLint lightPosLoc = glGetUniformLocation(shaderProgram, "lightPos");
GLint viewPosLoc = glGetUniformLocation(shaderProgram, "viewPos");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z);
glUniform3f(viewPosLoc, cameraPos.x, cameraPos.y, cameraPos.z);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(VAO);
//glDrawArrays(GL_TRIANGLES, 0, 36);
for (GLuint i = 0; i < 10; i++)
{
model = glm::mat4();
model = glm::translate(model, cubePositions[i]);
GLfloat angle = 20.0f * i;
model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0);
//光源
glUseProgram(lightShaderProgram); //光源
modelLoc = glGetUniformLocation(lightShaderProgram, "model");
viewLoc = glGetUniformLocation(lightShaderProgram, "view");
projLoc = glGetUniformLocation(lightShaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(lightVAO);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glfwSwapBuffers(window); //交换颜色缓冲,用来绘制,输出显示在屏幕上
}
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
glfwTerminate();
return 0;
}
/*
* 按键回调事件
* @param window 窗口
* @param key 按键
* @param scancode 扫描码
* @param action 表示这个按键是被按下还是释放
* @param mode 是否有Ctrl、Shift、Alt、Super等按钮的操作
*/
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
//esc键按下,关闭窗口
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (action == GLFW_PRESS) //按下
keys[key] = true;
else if (action == GLFW_RELEASE) //松开
keys[key] = false;
}
void do_movement() {
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
GLfloat cameraSpeed = 5.0f * deltaTime; //速度
if (keys[GLFW_KEY_W])
cameraPos += cameraSpeed * cameraFront;
if (keys[GLFW_KEY_S])
cameraPos -= cameraSpeed * cameraFront;
if (keys[GLFW_KEY_A])
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_D])
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_SPACE]) //向上
cameraPos += cameraSpeed * cameraUp;
}
bool firstMouse = true; //除了第一次鼠标移动外,其他不能移动
/*
* 鼠标回调事件
* @param window 窗口
* @param xpos 鼠标x坐标
* @param ypos 鼠标y坐标
*/
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
GLfloat xoffset = xpos - lastX;
GLfloat yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
GLfloat sensitivity = 0.05; //鼠标转向速度
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
/*
* 鼠标滚轮回调事件
*/
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
if (aspect >= 1.0f && aspect <= 45.0f)
aspect -= yoffset;
if (aspect <= 1.0f)
aspect = 1.0f;
if (aspect >= 45.0f)
aspect = 45.0f;
}
聚光
我们要讨论的最后一种类型光是聚光(Spotlight)。聚光是一种位于环境中某处的光源,它不是向所有方向照射,而是只朝某个方向照射。结果是只有一个聚光照射方向的确定半径内的物体才会被照亮,其他的都保持黑暗。聚光的好例子是路灯或手电筒。
OpenGL中的聚光用世界空间位置,一个方向和一个指定了聚光半径的切光角来表示。我们计算的每个片段,如果片段在聚光的切光方向之间(就是在圆锥体内),我们就会把片段照亮。下面的图可以让你明白聚光是如何工作的:
- LightDir:从片段指向光源的向量。
- SpotDir:聚光所指向的方向。
- $\phi$:定义聚光半径的切光角。每个落在这个角度之外的,聚光都不会照亮。
- $\theta$:
LightDir
向量和SpotDir
向量之间的角度。$\theta$值应该比$\phi$值小,这样才会在聚光内。
所以我们大致要做的是,计算LightDir
向量和SpotDir
向量的点乘
(返回两个单位向量的点乘,还记得吗?),然后在和切光角$\phi$对比。现在你应该明白聚光是我们下面将创建的手电筒的范例。
手电筒
手电筒(Flashlight) 是一个坐落在观察者位置的聚光,通常瞄准玩家透视图的前面。基本上说,一个手电筒是一个普通的聚光,但是根据玩家的位置和方向持续的更新它的位置和方向。
所以我们需要为片段着色器
提供的值,是聚光的位置向量(来计算光的方向坐标)
,聚光的方向向量
和切光角
。我们可以把这些值储存在Light结构体中:
struct Light
{
vec3 position;
vec3 direction;
float cutOff;
...
};
下面我们把这些适当的值传给着色器:
glUniform3f(lightPosLoc, camera.Position.x, camera.Position.y, camera.Position.z);
glUniform3f(lightSpotdirLoc, camera.Front.x, camera.Front.y, camera.Front.z); //摄像机朝向
glUniform1f(lightSpotCutOffLoc, glm::cos(glm::radians(12.5f)));
你可以看到,我们为切光角设置一个角度,但是我们根据一个角度计算了余弦值
,把这个余弦结果传给了片段着色器。这么做的原因是在片段着色器中,我们计算LightDir
和SpotDir
向量的点乘,而点乘返回一个余弦值
,不是一个角度,所以我们不能直接把一个角度和余弦值对比。为了获得这个角度,我们必须计算点乘结果的反余弦
,这个操作开销是很大的。所以为了节约一些性能,我们先计算给定切光角的余弦值,然后把结果传递给片段着色器。由于每个角度都被表示为余弦了,我们可以直接对比它们,而不用进行任何开销高昂的操作。
现在剩下要做的是计算$\theta$值,用它和$\phi$值对比,以决定我们是否在或不在聚光的内部:
float theta = dot(lightDir, normalize(-light.direction));
if(theta > light.cutOff)
{
// 执行光照计算
}
else // 否则使用环境光,使得场景不至于完全黑暗
color = vec4(light.ambient*vec3(texture(material.diffuse,TexCoords)), 1.0f);
我们首先计算lightDir
和取反的direction
向量的点乘(它是取反过的因为我们想要向量指向光源,而不是从光源作为指向出发点。前面的specular
教程中作者却用了相反的表示方法,这里读者可以选择喜欢的表达方式)。确保对所有相关向量进行了标准化处理。
你可能奇怪为什么if
条件中使用>
符号而不是<
符号。为了在聚光以内,theta
不是应该比光的切光值更小吗?这没错,但是不要忘了,角度值是以余弦值来表示的,一个0
度的角表示为1.0
的余弦值,当一个角是90
度的时候被表示为0.0
的余弦值,你可以在这里看到:
现在你可以看到,余弦越是接近1.0
,角度就越小。这就解释了为什么θ
需要比切光值更大了。切光值当前被设置为12.5
的余弦,它等于0.997
8,所以θ的余弦值在0.9979和1.0之
间,片段会在聚光内,被照亮。
运行应用,在聚光内的片段才会被照亮。这看起来像这样:
完整代码
#include <glew.h>
#include <glfw3.h>
#include <iostream>
#include <SOIL2.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//顶点着色器
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"layout (location = 1) in vec3 normal;\n"
"layout (location = 2) in vec2 texCoords;\n"
"out vec3 Normal;\n" //法向量
"out vec3 FragPos;\n" //片段位置(如立方体六个面,每个面两个三角形,三角形形成的那个片段)
"out vec2 TexCoords;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
" FragPos = vec3(model * vec4(position, 1.0f));\n" //片段位置,将片段位置转化为世界坐标
" Normal = normal;\n"
" TexCoords = texCoords;\n"
"}\n";
//片段着色器
const char* fragmentShaderSource = "#version 330 core\n"
"in vec3 Normal;\n"
"in vec3 FragPos;\n"
"in vec2 TexCoords;\n"
"out vec4 color;\n"
"uniform vec3 objectColor;\n"
"uniform vec3 lightColor;\n"
"uniform vec3 lightPos;\n"
"uniform vec3 viewPos;\n" //摄像机位置
//材质属性
"struct Material\n"
"{\n"
" sampler2D diffuse;\n" //在漫反射光照下物体的颜色
" sampler2D specular;\n" //受到镜面反射的颜色
" float shininess;\n" //反射半径
"};\n"
"uniform Material material;\n"
//光属性
"struct Light\n"
"{\n"
" vec3 position;\n" //现在无需光源位置,因为无限远
" vec3 direction;\n" //光的方向向量
" float cutOff;\n" //切光角
" vec3 ambient;\n" //环境光强度
" vec3 diffuse;\n" //漫反射强度
" vec3 specular;\n" //镜面反射强度
" float constant;\n" //常量
" float linear;\n" //一次项
" float quadratic;\n" //二次项
"};\n"
"uniform Light light;\n"
"void main()\n"
"{\n"
//光的方向
" vec3 lightDir = normalize(light.position - FragPos);\n" //计算光的方向,即方向向量 指向光源方向lightPos, 朝向片段方向FragPos
" float theta = dot(lightDir, normalize(-light.direction));\n" //切光角
" if(theta > light.cutOff)"
" {\n"
//环境光
" vec3 ambient = vec3(texture(material.diffuse, TexCoords)) * light.ambient;\n" //环境光
//漫反射
" vec3 norm = normalize(Normal);\n" //法向量单位化
" float diff = max(dot(norm, lightDir), 0.0);\n" //计算光对当前片段的实际散射影响
" vec3 diffuse = diff * vec3(texture(material.diffuse, TexCoords)) * light.diffuse;\n" //得到散射因子
//镜面高光
" vec3 viewDir = normalize(viewPos - FragPos);\n" //指向摄像机viewPos,朝向FragPos
" vec3 reflectDir = reflect(-lightDir, norm);\n" //反射的方向
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
" vec3 specular = vec3(texture(material.specular, TexCoords)) * spec * light.specular;\n" //计算镜面分量
//衰减
" float distance = length(light.position - FragPos);\n" //光源距离
" float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));\n" //衰减
//" ambient *= attenuation;\n"
" diffuse *= attenuation;\n"
" specular *= attenuation;\n"
//光照
" vec3 result = ambient + diffuse + specular;\n" //冯氏光照模型 = 环境光 + 漫反射 + 镜面反射
" color = vec4(result, 1.0f) ;\n"
" }\n"
" else \n"
" {\n"
//否则使用环境光,使得场景不至于完全黑暗
" color = vec4(light.ambient*vec3(texture(material.diffuse,TexCoords)), 1.0f);"
" };\n"
"}\n";
//光源-顶点着色器
const char* lightVertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
"}\n";
//光源-片段着色器
const char* lightFragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(1.0f);\n"
"}\n";
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void do_movement();
const GLuint WIDTH = 800, HEIGHT = 600;
//摄像机信息
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); //摄像机位置
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); //摄像机朝向
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); //上向量
GLfloat deltaTime = 0.0f; //当前帧遇上一帧的时间差
GLfloat lastFrame = 0.0f; //上一帧的时间
GLfloat lastX = WIDTH / 2, lastY = HEIGHT / 2; //鼠标上一次位置,默认屏幕中心
GLfloat pitch = 0.0f; //俯仰角
GLfloat yaw = -90.0f; //偏航角
GLfloat aspect = 45.0f; //视角大小
glm::vec3 lightPos(1.0f, 1.0f, 2.0f);
bool keys[1024];
int main()
{
glfwInit(); //必须要将glfw初始化
//告诉GLFW使用OpenGL版本
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //主版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //次版本号
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //使用的是OpenGL核心模式
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //不允许调整窗口大小
//创建窗口
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW Window" << std::endl;
glfwTerminate(); //销毁窗口与数据
return -1;
}
glfwMakeContextCurrent(window); //将OpenGL指向为当前窗口
glewExperimental = GL_TRUE; //用于告知GLEW使用现化OpenGL技术
//glew初始化
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
//视口
int width = 800, height = 600;
glfwGetFramebufferSize(window, &width, &height); //设置OpenGL渲染窗口的尺寸
glViewport(0, 0, width, height); //设置窗口的维度 前两个参数控制窗口左下角的位置, 第三、四个参数控制渲染窗口的宽度和高度
glfwSetKeyCallback(window, key_callback); //注册按键回调事件
glfwSetCursorPosCallback(window, mouse_callback); //注册鼠标回调事件
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); //隐藏鼠标,并捕获他
glfwSetScrollCallback(window, scroll_callback); //鼠标滚动回调事件
glEnable(GL_DEPTH_TEST); //深度缓存区
//顶点着色器
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
GLint vertexSuccess;
GLchar vertexInfoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(vertexShader, 512, nullptr, vertexInfoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << vertexInfoLog << std::endl;
}
//片段着色器
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
GLint fragmentSuccess;
GLchar fragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, fragmentInfoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << fragmentInfoLog << std::endl;
}
//着色器链接程序
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
GLint programSuccess;
GLchar programInfoLog[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &programSuccess);
if (!programSuccess) {
glGetProgramInfoLog(shaderProgram, 512, nullptr, programInfoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << programInfoLog << std::endl;
}
//光源-顶点着色器
GLuint lightVertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(lightVertexShader, 1, &lightVertexShaderSource, nullptr);
glCompileShader(lightVertexShader);
GLint lightVertexSuccess;
GLchar lightVertexInfoLog[512];
glGetShaderiv(lightVertexShader, GL_COMPILE_STATUS, &lightVertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(lightVertexShader, 512, nullptr, lightVertexInfoLog);
std::cout << "ERROR::SHADER::LIGHT::VERTEX::COMPILATION_FAILED\n" << lightVertexInfoLog << std::endl;
}
//片段着色器
GLuint lightFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(lightFragmentShader, 1, &lightFragmentShaderSource, nullptr);
glCompileShader(lightFragmentShader);
GLint lightFragmentSuccess;
GLchar lightFragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &lightFragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, lightFragmentInfoLog);
std::cout << "ERROR::SHADER::LIGHT::FRAGMENT::COMPILATION_FAILED\n" << lightFragmentInfoLog << std::endl;
}
//光源着色器链接程序
GLuint lightShaderProgram = glCreateProgram();
glAttachShader(lightShaderProgram, lightVertexShader);
glAttachShader(lightShaderProgram, lightFragmentShader);
glLinkProgram(lightShaderProgram);
GLint lightProgramSuccess;
GLchar lightProgramInfoLog[512];
glGetProgramiv(lightShaderProgram, GL_LINK_STATUS, &lightProgramSuccess);
if (!programSuccess) {
glGetProgramInfoLog(lightShaderProgram, 512, nullptr, lightProgramInfoLog);
std::cout << "ERROR::SHADER::LIGHT::PROGRAM::LINKING_FAILED\n" << lightProgramInfoLog << std::endl;
}
GLfloat vertices[] = {
//-----位置 //法向量 //纹理坐标
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
//十个立方体
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
//顶点数据
GLuint VBO, VAO;
glGenBuffers(1, &VBO); //顶点缓冲对象
glGenVertexArrays(1, &VAO); //顶点数组对象
glBindVertexArray(VAO);
//顶点数据
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
//法向量
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//纹理坐标
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//光源
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(lightVAO);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//纹理
GLuint texture;
int textureWidht, textureHeight;
//加载纹理
unsigned char* image = SOIL_load_image("container2.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
GLuint texture2;
//加载纹理
image = SOIL_load_image("container2_specular.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
while (!glfwWindowShouldClose(window)) {
//检查GLFW是否退出,即窗口是否关闭了,true代表结束了
glfwPollEvents(); //检查有没有事件发生(键盘输入、鼠标移动),如发生调用对应的回调函数 键盘事件:glfwSetKeyCallback(window, key_callback); key_callback即设定的回调函数
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); //清空屏幕所用的颜色,即清除颜色缓冲之后,整个颜色缓冲都会被填充为glClearColor里所设置的颜色。
//glClear(GL_COLOR_BUFFER_BIT); //清空屏幕缓冲,这里是颜色缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
do_movement();
//渲染指令
glUseProgram(shaderProgram);
glm::mat4 model; //模型矩阵
glm::mat4 view; //观察矩阵
glm::mat4 projection; //投影矩阵-透视投影
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
projection = glm::perspective(aspect, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f); //缩放效果
//物体颜色和光颜色
GLint objectColorLoc = glGetUniformLocation(shaderProgram, "objectColor");
GLint lightColorLoc = glGetUniformLocation(shaderProgram, "lightColor");
glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f);// 我们所熟悉的珊瑚红
glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f); // 依旧把光源设置为白色
//材质
GLint matDiffuseLoc = glGetUniformLocation(shaderProgram, "material.diffuse");
GLint matSpecularLoc = glGetUniformLocation(shaderProgram, "material.specular");
GLint matShineLoc = glGetUniformLocation(shaderProgram, "material.shininess");
glUniform1i(matDiffuseLoc, 0);
glUniform1i(matSpecularLoc, 1);
glUniform1f(matShineLoc, 32.0f);
GLint lightStructPosLoc = glGetUniformLocation(shaderProgram, "light.position");
GLint lightStructSpotdirLoc = glGetUniformLocation(shaderProgram, "light.direction");
GLint lightStructSpotCutOffLoc = glGetUniformLocation(shaderProgram, "light.cutOff");
glUniform3f(lightStructPosLoc, cameraPos.x, cameraPos.y, cameraPos.z);
glUniform3f(lightStructSpotdirLoc, cameraFront.x, cameraFront.y, cameraFront.z);
glUniform1f(lightStructSpotCutOffLoc, glm::cos(glm::radians(12.5f)));
//光照
glUniform3f(glGetUniformLocation(shaderProgram, "light.ambient"), 0.1f, 0.1f, 0.1f);
glUniform3f(glGetUniformLocation(shaderProgram, "light.diffuse"), 0.8f, 0.8f, 0.8f);
glUniform3f(glGetUniformLocation(shaderProgram, "light.specular"), 1.0f, 1.0f, 1.0f);
//衰减
glUniform1f(glGetUniformLocation(shaderProgram, "light.constant"), 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "light.linear"), 0.09);
glUniform1f(glGetUniformLocation(shaderProgram, "light.quadratic"), 0.032);
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
GLint lightPosLoc = glGetUniformLocation(shaderProgram, "lightPos");
GLint viewPosLoc = glGetUniformLocation(shaderProgram, "viewPos");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z);
glUniform3f(viewPosLoc, cameraPos.x, cameraPos.y, cameraPos.z);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(VAO);
//glDrawArrays(GL_TRIANGLES, 0, 36);
for (GLuint i = 0; i < 10; i++)
{
model = glm::mat4();
model = glm::translate(model, cubePositions[i]);
GLfloat angle = 20.0f * i;
model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0);
//光源
glUseProgram(lightShaderProgram); //光源
modelLoc = glGetUniformLocation(lightShaderProgram, "model");
viewLoc = glGetUniformLocation(lightShaderProgram, "view");
projLoc = glGetUniformLocation(lightShaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(lightVAO);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glfwSwapBuffers(window); //交换颜色缓冲,用来绘制,输出显示在屏幕上
}
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
glfwTerminate();
return 0;
}
/*
* 按键回调事件
* @param window 窗口
* @param key 按键
* @param scancode 扫描码
* @param action 表示这个按键是被按下还是释放
* @param mode 是否有Ctrl、Shift、Alt、Super等按钮的操作
*/
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
//esc键按下,关闭窗口
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (action == GLFW_PRESS) //按下
keys[key] = true;
else if (action == GLFW_RELEASE) //松开
keys[key] = false;
}
void do_movement() {
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
GLfloat cameraSpeed = 5.0f * deltaTime; //速度
if (keys[GLFW_KEY_W])
cameraPos += cameraSpeed * cameraFront;
if (keys[GLFW_KEY_S])
cameraPos -= cameraSpeed * cameraFront;
if (keys[GLFW_KEY_A])
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_D])
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_SPACE]) //向上
cameraPos += cameraSpeed * cameraUp;
}
bool firstMouse = true; //除了第一次鼠标移动外,其他不能移动
/*
* 鼠标回调事件
* @param window 窗口
* @param xpos 鼠标x坐标
* @param ypos 鼠标y坐标
*/
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
GLfloat xoffset = xpos - lastX;
GLfloat yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
GLfloat sensitivity = 0.05; //鼠标转向速度
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
/*
* 鼠标滚轮回调事件
*/
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
if (aspect >= 1.0f && aspect <= 45.0f)
aspect -= yoffset;
if (aspect <= 1.0f)
aspect = 1.0f;
if (aspect >= 45.0f)
aspect = 45.0f;
}
它看起来仍然有点假,原因是聚光有了一个硬边。片段着色器一旦到达了聚光的圆锥边缘,它就立刻黑了下来,却没有任何平滑减弱的过度。一个真实的聚光的光会在它的边界处平滑减弱的。
平滑/软化边缘
为创建聚光的平滑边,我们希望去模拟的聚光有一个内圆锥和外圆锥。我们可以把内圆锥设置为前面部分定义的圆锥,我们希望外圆锥从内边到外边逐步的变暗。
为创建外圆锥,我们简单定义另一个余弦值,它代表聚光的方向向量和外圆锥的向量(等于它的半径)的角度。然后,如果片段在内圆锥和外圆锥之间,就会给它计算出一个0.0
到1.0
之间的亮度。如果片段在内圆锥以内这个亮度就等于1.0
,如果在外面就是0.0
。
我们可以使用下面的公式计算这样的值:
$$
I = \frac{\theta - \gamma}{\epsilon}
$$
这里$\epsilon$是内部($\phi$)和外部圆锥($\gamma$)($\epsilon = \phi - \gamma$)的差。结果$I$的值是聚光在当前片段的亮度。
很难用图画描述出这个公式是怎样工作的,所以我们尝试使用一个例子:
$\theta$ | $\theta$(角度) | $\phi$(内切) | $\phi$(角度) | $\gamma$(外切) | $\gamma$(角度) | $\epsilon$ | $I$ |
---|---|---|---|---|---|---|---|
0.87 | 30 | 0.91 | 25 | 0.82 | 35 | 0.91 - 0.82 = 0.09 | 0.87 - 0.82 / 0.09 = 0.56 |
0.9 | 26 | 0.91 | 25 | 0.82 | 35 | 0.91 - 0.82 = 0.09 | 0.9 - 0.82 / 0.09 = 0.89 |
0.97 | 14 | 0.91 | 25 | 0.82 | 35 | 0.91 - 0.82 = 0.09 | 0.97 - 0.82 / 0.09 = 1.67 |
0.97 | 14 | 0.91 | 25 | 0.82 | 35 | 0.91 - 0.82 = 0.09 | 0.97 - 0.82 / 0.09 = 1.67 |
0.83 | 34 | 0.91 | 25 | 0.82 | 35 | 0.91 - 0.82 = 0.09 | 0.83 - 0.82 / 0.09 = 0.11 |
0.64 | 50 | 0.91 | 25 | 0.82 | 35 | 0.91 - 0.82 = 0.09 | 0.64 - 0.82 / 0.09 = -2.0 |
0.966 | 15 | 0.9978 | 12.5 | 0.953 | 17.5 | 0.966 - 0.953 = 0.0448 | 0.966 - 0.953 / 0.0448 = 0.29 |
就像你看到的那样我们基本是根据$\theta$在外余弦和内余弦之间插值。如果你仍然不明白怎么继续,不要担心。你可以简单的使用这个公式计算,当你更加老道和明白的时候再来看。
由于我们现在有了一个亮度值,当在聚光外的时候是个负的,当在内部圆锥以内大于1
。如果我们适当地把这个值固定,我们在片段着色器中就再不需要if-else
了,我们可以简单地用计算出的亮度值乘以光的元素:
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.cutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon,0.0, 1.0);
...
// We’ll leave ambient unaffected so we always have a little
light.diffuse* = intensity;
specular* = intensity;
...
注意,我们使用了clamp
函数,它把第一个参数固定在0.0和1.0
之间。这保证了亮度值不会超出[0, 1]
以外。
确定你把outerCutOff
值添加到了Light
结构体,并在应用中设置了它的uniform
值。对于下面的图片,内部切光角12.5f
,外部切光角是17.5f
:
#include <glew.h>
#include <glfw3.h>
#include <iostream>
#include <SOIL2.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//顶点着色器
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"layout (location = 1) in vec3 normal;\n"
"layout (location = 2) in vec2 texCoords;\n"
"out vec3 Normal;\n" //法向量
"out vec3 FragPos;\n" //片段位置(如立方体六个面,每个面两个三角形,三角形形成的那个片段)
"out vec2 TexCoords;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
" FragPos = vec3(model * vec4(position, 1.0f));\n" //片段位置,将片段位置转化为世界坐标
" Normal = normal;\n"
" TexCoords = texCoords;\n"
"}\n";
//片段着色器
const char* fragmentShaderSource = "#version 330 core\n"
"in vec3 Normal;\n"
"in vec3 FragPos;\n"
"in vec2 TexCoords;\n"
"out vec4 color;\n"
"uniform vec3 objectColor;\n"
"uniform vec3 lightColor;\n"
"uniform vec3 lightPos;\n"
"uniform vec3 viewPos;\n" //摄像机位置
//材质属性
"struct Material\n"
"{\n"
" sampler2D diffuse;\n" //在漫反射光照下物体的颜色
" sampler2D specular;\n" //受到镜面反射的颜色
" float shininess;\n" //反射半径
"};\n"
"uniform Material material;\n"
//光属性
"struct Light\n"
"{\n"
" vec3 position;\n" //现在无需光源位置,因为无限远
" vec3 direction;\n" //光的方向向量
" float cutOff;\n" //切光角
" float outerCutOff;\n" //外切
" vec3 ambient;\n" //环境光强度
" vec3 diffuse;\n" //漫反射强度
" vec3 specular;\n" //镜面反射强度
" float constant;\n" //常量
" float linear;\n" //一次项
" float quadratic;\n" //二次项
"};\n"
"uniform Light light;\n"
"void main()\n"
"{\n"
//光的方向
" vec3 lightDir = normalize(light.position - FragPos);\n" //计算光的方向,即方向向量 指向光源方向lightPos, 朝向片段方向FragPos
" float theta = dot(lightDir, normalize(-light.direction));\n" //切光角
" float epsilon = light.cutOff - light.outerCutOff;\n"
" float intensity = clamp((theta - light.outerCutOff) / epsilon,0.0, 1.0);\n"
//环境光
" vec3 ambient = vec3(texture(material.diffuse, TexCoords)) * light.ambient;\n" //环境光
//漫反射
" vec3 norm = normalize(Normal);\n" //法向量单位化
" float diff = max(dot(norm, lightDir), 0.0);\n" //计算光对当前片段的实际散射影响
" vec3 diffuse = diff * vec3(texture(material.diffuse, TexCoords)) * light.diffuse;\n" //得到散射因子
" diffuse *= intensity;\n"
//镜面高光
" vec3 viewDir = normalize(viewPos - FragPos);\n" //指向摄像机viewPos,朝向FragPos
" vec3 reflectDir = reflect(-lightDir, norm);\n" //反射的方向
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
" vec3 specular = vec3(texture(material.specular, TexCoords)) * spec * light.specular;\n" //计算镜面分量
" specular *= intensity;\n"
//衰减
" float distance = length(light.position - FragPos);\n" //光源距离
" float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));\n" //衰减
// " ambient *= attenuation;\n"
" diffuse *= attenuation;\n"
" specular *= attenuation;\n"
//光照
" vec3 result = ambient + diffuse + specular;\n" //冯氏光照模型 = 环境光 + 漫反射 + 镜面反射
" color = vec4(result, 1.0f) ;\n"
"}\n";
//光源-顶点着色器
const char* lightVertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
"}\n";
//光源-片段着色器
const char* lightFragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(1.0f);\n"
"}\n";
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void do_movement();
const GLuint WIDTH = 800, HEIGHT = 600;
//摄像机信息
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); //摄像机位置
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); //摄像机朝向
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); //上向量
GLfloat deltaTime = 0.0f; //当前帧遇上一帧的时间差
GLfloat lastFrame = 0.0f; //上一帧的时间
GLfloat lastX = WIDTH / 2, lastY = HEIGHT / 2; //鼠标上一次位置,默认屏幕中心
GLfloat pitch = 0.0f; //俯仰角
GLfloat yaw = -90.0f; //偏航角
GLfloat aspect = 45.0f; //视角大小
glm::vec3 lightPos(1.0f, 1.0f, 2.0f);
bool keys[1024];
int main()
{
glfwInit(); //必须要将glfw初始化
//告诉GLFW使用OpenGL版本
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //主版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //次版本号
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //使用的是OpenGL核心模式
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //不允许调整窗口大小
//创建窗口
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW Window" << std::endl;
glfwTerminate(); //销毁窗口与数据
return -1;
}
glfwMakeContextCurrent(window); //将OpenGL指向为当前窗口
glewExperimental = GL_TRUE; //用于告知GLEW使用现化OpenGL技术
//glew初始化
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
//视口
int width = 800, height = 600;
glfwGetFramebufferSize(window, &width, &height); //设置OpenGL渲染窗口的尺寸
glViewport(0, 0, width, height); //设置窗口的维度 前两个参数控制窗口左下角的位置, 第三、四个参数控制渲染窗口的宽度和高度
glfwSetKeyCallback(window, key_callback); //注册按键回调事件
glfwSetCursorPosCallback(window, mouse_callback); //注册鼠标回调事件
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); //隐藏鼠标,并捕获他
glfwSetScrollCallback(window, scroll_callback); //鼠标滚动回调事件
glEnable(GL_DEPTH_TEST); //深度缓存区
//顶点着色器
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
GLint vertexSuccess;
GLchar vertexInfoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(vertexShader, 512, nullptr, vertexInfoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << vertexInfoLog << std::endl;
}
//片段着色器
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
GLint fragmentSuccess;
GLchar fragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, fragmentInfoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << fragmentInfoLog << std::endl;
}
//着色器链接程序
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
GLint programSuccess;
GLchar programInfoLog[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &programSuccess);
if (!programSuccess) {
glGetProgramInfoLog(shaderProgram, 512, nullptr, programInfoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << programInfoLog << std::endl;
}
//光源-顶点着色器
GLuint lightVertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(lightVertexShader, 1, &lightVertexShaderSource, nullptr);
glCompileShader(lightVertexShader);
GLint lightVertexSuccess;
GLchar lightVertexInfoLog[512];
glGetShaderiv(lightVertexShader, GL_COMPILE_STATUS, &lightVertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(lightVertexShader, 512, nullptr, lightVertexInfoLog);
std::cout << "ERROR::SHADER::LIGHT::VERTEX::COMPILATION_FAILED\n" << lightVertexInfoLog << std::endl;
}
//片段着色器
GLuint lightFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(lightFragmentShader, 1, &lightFragmentShaderSource, nullptr);
glCompileShader(lightFragmentShader);
GLint lightFragmentSuccess;
GLchar lightFragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &lightFragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, lightFragmentInfoLog);
std::cout << "ERROR::SHADER::LIGHT::FRAGMENT::COMPILATION_FAILED\n" << lightFragmentInfoLog << std::endl;
}
//光源着色器链接程序
GLuint lightShaderProgram = glCreateProgram();
glAttachShader(lightShaderProgram, lightVertexShader);
glAttachShader(lightShaderProgram, lightFragmentShader);
glLinkProgram(lightShaderProgram);
GLint lightProgramSuccess;
GLchar lightProgramInfoLog[512];
glGetProgramiv(lightShaderProgram, GL_LINK_STATUS, &lightProgramSuccess);
if (!programSuccess) {
glGetProgramInfoLog(lightShaderProgram, 512, nullptr, lightProgramInfoLog);
std::cout << "ERROR::SHADER::LIGHT::PROGRAM::LINKING_FAILED\n" << lightProgramInfoLog << std::endl;
}
GLfloat vertices[] = {
//-----位置 //法向量 //纹理坐标
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
//十个立方体
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
//顶点数据
GLuint VBO, VAO;
glGenBuffers(1, &VBO); //顶点缓冲对象
glGenVertexArrays(1, &VAO); //顶点数组对象
glBindVertexArray(VAO);
//顶点数据
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
//法向量
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//纹理坐标
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//光源
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(lightVAO);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//纹理
GLuint texture;
int textureWidht, textureHeight;
//加载纹理
unsigned char* image = SOIL_load_image("container2.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
GLuint texture2;
//加载纹理
image = SOIL_load_image("container2_specular.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
while (!glfwWindowShouldClose(window)) {
//检查GLFW是否退出,即窗口是否关闭了,true代表结束了
glfwPollEvents(); //检查有没有事件发生(键盘输入、鼠标移动),如发生调用对应的回调函数 键盘事件:glfwSetKeyCallback(window, key_callback); key_callback即设定的回调函数
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); //清空屏幕所用的颜色,即清除颜色缓冲之后,整个颜色缓冲都会被填充为glClearColor里所设置的颜色。
//glClear(GL_COLOR_BUFFER_BIT); //清空屏幕缓冲,这里是颜色缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
do_movement();
//渲染指令
glUseProgram(shaderProgram);
glm::mat4 model; //模型矩阵
glm::mat4 view; //观察矩阵
glm::mat4 projection; //投影矩阵-透视投影
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
projection = glm::perspective(aspect, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f); //缩放效果
//物体颜色和光颜色
GLint objectColorLoc = glGetUniformLocation(shaderProgram, "objectColor");
GLint lightColorLoc = glGetUniformLocation(shaderProgram, "lightColor");
glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f);// 我们所熟悉的珊瑚红
glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f); // 依旧把光源设置为白色
//材质
GLint matDiffuseLoc = glGetUniformLocation(shaderProgram, "material.diffuse");
GLint matSpecularLoc = glGetUniformLocation(shaderProgram, "material.specular");
GLint matShineLoc = glGetUniformLocation(shaderProgram, "material.shininess");
glUniform1i(matDiffuseLoc, 0);
glUniform1i(matSpecularLoc, 1);
glUniform1f(matShineLoc, 32.0f);
//聚光
GLint lightStructPosLoc = glGetUniformLocation(shaderProgram, "light.position");
GLint lightStructSpotdirLoc = glGetUniformLocation(shaderProgram, "light.direction");
GLint lightStructSpotCutOffLoc = glGetUniformLocation(shaderProgram, "light.cutOff");
GLint lightStructOuterCutOff = glGetUniformLocation(shaderProgram, "light.outerCutOff");
glUniform3f(lightStructPosLoc, cameraPos.x, cameraPos.y, cameraPos.z);
glUniform3f(lightStructSpotdirLoc, cameraFront.x, cameraFront.y, cameraFront.z);
glUniform1f(lightStructSpotCutOffLoc, glm::cos(glm::radians(12.5f)));
glUniform1f(lightStructOuterCutOff, glm::cos(glm::radians(17.5f)));
//光照
glUniform3f(glGetUniformLocation(shaderProgram, "light.ambient"), 0.1f, 0.1f, 0.1f);
glUniform3f(glGetUniformLocation(shaderProgram, "light.diffuse"), 0.8f, 0.8f, 0.8f);
glUniform3f(glGetUniformLocation(shaderProgram, "light.specular"), 1.0f, 1.0f, 1.0f);
//衰减
glUniform1f(glGetUniformLocation(shaderProgram, "light.constant"), 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "light.linear"), 0.09);
glUniform1f(glGetUniformLocation(shaderProgram, "light.quadratic"), 0.032);
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
GLint lightPosLoc = glGetUniformLocation(shaderProgram, "lightPos");
GLint viewPosLoc = glGetUniformLocation(shaderProgram, "viewPos");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z);
glUniform3f(viewPosLoc, cameraPos.x, cameraPos.y, cameraPos.z);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(VAO);
//glDrawArrays(GL_TRIANGLES, 0, 36);
for (GLuint i = 0; i < 10; i++)
{
model = glm::mat4();
model = glm::translate(model, cubePositions[i]);
GLfloat angle = 20.0f * i;
model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0);
//光源
glUseProgram(lightShaderProgram); //光源
modelLoc = glGetUniformLocation(lightShaderProgram, "model");
viewLoc = glGetUniformLocation(lightShaderProgram, "view");
projLoc = glGetUniformLocation(lightShaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(lightVAO);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glfwSwapBuffers(window); //交换颜色缓冲,用来绘制,输出显示在屏幕上
}
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
glfwTerminate();
return 0;
}
/*
* 按键回调事件
* @param window 窗口
* @param key 按键
* @param scancode 扫描码
* @param action 表示这个按键是被按下还是释放
* @param mode 是否有Ctrl、Shift、Alt、Super等按钮的操作
*/
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
//esc键按下,关闭窗口
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (action == GLFW_PRESS) //按下
keys[key] = true;
else if (action == GLFW_RELEASE) //松开
keys[key] = false;
}
void do_movement() {
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
GLfloat cameraSpeed = 5.0f * deltaTime; //速度
if (keys[GLFW_KEY_W])
cameraPos += cameraSpeed * cameraFront;
if (keys[GLFW_KEY_S])
cameraPos -= cameraSpeed * cameraFront;
if (keys[GLFW_KEY_A])
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_D])
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_SPACE]) //向上
cameraPos += cameraSpeed * cameraUp;
}
bool firstMouse = true; //除了第一次鼠标移动外,其他不能移动
/*
* 鼠标回调事件
* @param window 窗口
* @param xpos 鼠标x坐标
* @param ypos 鼠标y坐标
*/
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
GLfloat xoffset = xpos - lastX;
GLfloat yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
GLfloat sensitivity = 0.05; //鼠标转向速度
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
/*
* 鼠标滚轮回调事件
*/
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
if (aspect >= 1.0f && aspect <= 45.0f)
aspect -= yoffset;
if (aspect <= 1.0f)
aspect = 1.0f;
if (aspect >= 45.0f)
aspect = 45.0f;
}
多光源
我们在前面的教程中已经学习了许多关于OpenGL光照
的知识,其中包括冯氏照明模型(Phong shading,OpenGL笔记8-光照)、光照材质(Materials,OpenGL笔记9-材质)、光照图(Lighting maps,OpenGL笔记10-光照贴图)以及各种投光物(Light casters,OpenGL笔记11-投光物)。
本教程将结合上述所学的知识,创建一个包含六个光源的场景。我们将模拟一个类似阳光的平行光(Directional light)
和4个定点光(Point lights)
以及一个手电筒(Flashlight)
。
要在场景中使用多光源我们需要封装一些GLSL函数用来计算光照。如果我们对每个光源都去写一遍光照计算的代码,这将是一件令人恶心的事情,并且这些放在main
函数中的代码将难以理解,所以我们将一些操作封装为函数。
GLSL中的函数与C语言的非常相似,它需要一个函数名、一个返回值类型
。并且在调用前必须提前声明
。接下来我们将为下面的每一种光照来写一个函数。
当我们在场景中使用多个光源时一般使用以下途径:创建一个代表输出颜色的向量。每一个光源都对输出颜色贡献一些颜色 。因此,场景中的每个光源将进行独立运算,并且运算结果都对最终的输出颜色有一定影响。下面是使用这种方式进行多光源运算的一般结构:
out vec4 color;
void main()
{
// 定义输出颜色
vec3 output;
// 将平行光的运算结果颜色添加到输出颜色
output += someFunctionToCalculateDirectionalLight();
// 同样,将定点光的运算结果颜色添加到输出颜色
for(int i = 0; i < nr_of_point_lights; i++)
output += someFunctionToCalculatePointLight();
// 添加其他光源的计算结果颜色(如投射光)
output += someFunctionToCalculateSpotLight();
color = vec4(output, 1.0);
}
即使对每一种光源的运算实现不同,但此算法的结构一般是与上述出入不大的。我们将定义几个用于计算各个光源的函数,并将这些函数的结算结果(返回颜色)添加到输出颜色向量中。例如,靠近被照射物体的光源计算结果将返回比远离背照射物体的光源更明亮的颜色。
平行光
我们要在片段着色器中定义一个函数用来计算平行光(Directional light)
在对应的照射点上的光照颜色,这个函数需要几个参数并返回一个计算平行光照结果的颜色。
首先我们需要设置一系列用于表示平行光的变量,正如上一节中所讲过的,我们可以将这些变量定义在一个叫做DirLight
的结构体中,并定义一个这个结构体类型的uniform
变量。
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform DirLight dirLight;
之后我们可以将dirLight
这个uniform
变量作为下面这个函数原型的参数。
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
和C/C++
一样,我们调用一个函数的前提是这个函数在调用前已经被声明过(此例中我们是在main函数中调用)。通常情况下我们都将函数定义在main
函数之后,为了能在main
函数中调用这些函数,我们就必须在main
函数之前声明这些函数的原型,这就和我们写C语言是一样的 。
你已经知道,这个函数需要一个DirLight
和两个其他的向量作为参数来计算光照。如果你看过之前的教程的话,你会觉得下面的函数定义得一点也不意外:
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction);
// 计算漫反射强度
float diff = max(dot(normal, lightDir), 0.0);
// 计算镜面反射强度
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// 合并各个光照分量
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
return (ambient + diffuse + specular);
}
我们从之前的教程中复制了代码,并用两个向量来作为函数参数来计算出平行光的光照颜色向量,该结果是一个由该平行光的环境反射、漫反射和镜面反射 的各个分量组成的一个向量。
点光源
和计算平行光一样,我们同样需要定义一个函数用于计算点光源(Point Light)
。同样的,我们定义一个包含点光源所需属性的结构体:
struct PointLight {
vec3 position;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
#define NR_POINT_LIGHTS 4
uniform PointLight pointLights[NR_POINT_LIGHTS];
如你所见,我们在GLSL中使用预处理器指令来定义点光源
的数目。之后我们使用这个NR_POINT_LIGHTS
常量来创建一个PointLight
结构体的数组。和C
语言一样,GLSL
也是用一对中括号来创建数组的。现在我们有了4个PointLight
结构体对象了。
我们同样可以简单粗暴地定义一个大号的结构体(而不是为每一种类型的光源定义一个结构体),它包含所有类型光源所需要属性变量。并且将这个结构体应用与所有的光照计算函数,在各个光照结算时忽略不需要的属性变量。然而,就我个人来说更喜欢分开定义,这样可以省下一些内存,因为定义一个大号的光源结构体在计算过程中会有用不到的变量 。
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
这个函数将所有用得到的数据作为它的参数并使用一个vec3
作为它的返回值类表示一个顶点光的结算结果。我们再一次聪明地从之前的教程中复制代码来把它定义成下面的样子:
// 计算定点光在确定位置的光照颜色
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
// 计算漫反射强度
float diff = max(dot(normal, lightDir), 0.0);
// 计算镜面反射
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// 计算衰减
float distance = length(light.position - fragPos);
float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
// 将各个分量合并
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
有了这个函数我们就可以在main函数中调用它来代替写很多个计算点光源的代码了。通过循环调用此函数就能实现同样的效果,当然代码更简洁。
把它们放在一起
我们现在定义了用于计算平行光和点光源的函数,现在我们把这些代码放到一起,写入文开始的一般结构中:
void main()
{
// 一些属性
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);
// 第一步,计算平行光照
vec3 result = CalcDirLight(dirLight, norm, viewDir);
// 第二步,计算顶点光照
for(int i = 0; i < NR_POINT_LIGHTS; i++)
result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
// 第三部,计算 Spot light
//result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
color = vec4(result, 1.0);
}
每一个光源的运算结果都添加到了输出颜色上,输出颜色包含了此场景中的所有光源的影响。如果你想实现手电筒的光照效果,同样的把计算结果添加到输出颜色上。
设置平行光结构体的uniform
值和之前所讲过的方式没什么两样,但是你可能想知道如何设置场景中PointLight
结构体的uniforms
变量数组。我们之前并未讨论过如何做这件事。
庆幸的是,这并不是什么难题。设置uniform
变量数组和设置单个uniform
变量值是相似的,只需要用一个合适的下标就能够检索到数组中我们想要的uniform
变量了。
glUniform1f(glGetUniformLocation(lightingShader.Program, "pointLights[0].constant"), 1.0f);
这样我们检索到pointLights
数组中的第一个PointLight
结构体元素,同时也可以获取到该结构体中的各个属性变量。不幸的是这一位置我们还需要手动对这个四个光源的每一个属性都进行设置,这样手动设置这28个uniform
变量是相当乏味的工作。你可以尝试去定义个光源类来为你设置这些uniform
属性来减少你的工作,但这依旧不能改变去设置每个uniform
属性的事实。
别忘了,我们还需要为每个光源设置它们的位置。这里,我们定义一个glm::vec3
类的数组来包含这些点光源的坐标:
glm::vec3 pointLightPositions[] = {
glm::vec3( 0.7f, 0.2f, 2.0f),
glm::vec3( 2.3f, -3.3f, -4.0f),
glm::vec3(-4.0f, 2.0f, -12.0f),
glm::vec3( 0.0f, 0.0f, -3.0f)
};
同时我们还需要根据这些光源的位置在场景中绘制4个表示光源的立方体,这样的工作我们在之前的教程中已经做过了。
如果你在还是用了手电筒的话,将所有的光源结合起来看上去应该和下图差不多:
完整代码
#include <glew.h>
#include <glfw3.h>
#include <iostream>
#include <SOIL2.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//顶点着色器
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"layout (location = 1) in vec3 normal;\n"
"layout (location = 2) in vec2 texCoords;\n"
"out vec3 Normal;\n" //法向量
"out vec3 FragPos;\n" //片段位置(如立方体六个面,每个面两个三角形,三角形形成的那个片段)
"out vec2 TexCoords;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
" FragPos = vec3(model * vec4(position, 1.0f));\n" //片段位置,将片段位置转化为世界坐标
" Normal = normal;\n"
" TexCoords = texCoords;\n"
"}\n";
//片段着色器
const char* fragmentShaderSource = "#version 330 core\n"
"in vec3 Normal;\n"
"in vec3 FragPos;\n"
"in vec2 TexCoords;\n"
"out vec4 color;\n"
"uniform vec3 objectColor;\n"
"uniform vec3 lightColor;\n"
"uniform vec3 lightPos;\n"
"uniform vec3 viewPos;\n" //摄像机位置
//材质属性
"struct Material\n"
"{\n"
" sampler2D diffuse;\n" //在漫反射光照下物体的颜色
" sampler2D specular;\n" //受到镜面反射的颜色
" float shininess;\n" //反射半径
"};\n"
"uniform Material material;\n"
//平行光属性
"struct DirLight\n"
"{\n"
" vec3 direction;\n"
" vec3 ambient;\n"
" vec3 diffuse;\n"
" vec3 specular;\n"
"};\n"
"uniform DirLight dirLight;\n"
"vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);\n"
//点光源属性
"struct PointLight {\n"
" vec3 position;\n"
" float constant;\n"
" float linear;\n"
" float quadratic;\n"
" vec3 ambient;\n"
" vec3 diffuse;\n"
" vec3 specular;\n"
"};\n"
"#define NR_POINT_LIGHTS 4\n"
"uniform PointLight pointLights[NR_POINT_LIGHTS];\n"
"vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);\n"
"void main()\n"
"{\n"
//一些属性
" vec3 norm = normalize(Normal);\n" //法向量
" vec3 viewDir = normalize(viewPos - FragPos);\n" //摄像机方向向量
// 第一步,计算平行光照
" vec3 result = CalcDirLight(dirLight, norm, viewDir);\n"
// 第二步,计算顶点光照
" for(int i = 0; i < NR_POINT_LIGHTS; i++) {\n"
" result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);\n"
" }\n"
// 第三步,计算点光照
//" result += CalcSpotLight(spotLight, norm, FragPos, viewDir);\n"
" color = vec4(result, 1.0f) ;\n"
"}\n"
//平行光
"vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)\n"
"{\n"
" vec3 lightDir = normalize(-light.direction);\n"
// 计算漫反射强度
" float diff = max(dot(normal, lightDir), 0.0);\n"
// 计算镜面反射强度
" vec3 reflectDir = reflect(-lightDir, normal);\n"
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
// 合并各个光照分量
" vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));\n"
" vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));\n"
" vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));\n"
" return (ambient + diffuse + specular);\n"
"}\n"
//点光源
"vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)\n"
"{\n"
" vec3 lightDir = normalize(light.position - fragPos);\n"
// 计算漫反射强度
" float diff = max(dot(normal, lightDir), 0.0);\n"
// 计算镜面反射
" vec3 reflectDir = reflect(-lightDir, normal);\n"
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
// 计算衰减
" float distance = length(light.position - fragPos);\n"
" float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));\n"
// 将各个分量合并
" vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));\n"
" vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));\n"
" vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));\n"
" ambient *= attenuation;\n"
" diffuse *= attenuation;\n"
" specular *= attenuation;\n"
" return (ambient + diffuse + specular);\n"
"}\n";
//光源-顶点着色器
const char* lightVertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(position, 1.0f);\n" //数学库矩阵转换
"}\n";
//光源-片段着色器
const char* lightFragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(1.0f);\n"
"}\n";
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void do_movement();
const GLuint WIDTH = 800, HEIGHT = 600;
//摄像机信息
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); //摄像机位置
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); //摄像机朝向
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); //上向量
GLfloat deltaTime = 0.0f; //当前帧遇上一帧的时间差
GLfloat lastFrame = 0.0f; //上一帧的时间
GLfloat lastX = WIDTH / 2, lastY = HEIGHT / 2; //鼠标上一次位置,默认屏幕中心
GLfloat pitch = 0.0f; //俯仰角
GLfloat yaw = -90.0f; //偏航角
GLfloat aspect = 45.0f; //视角大小
glm::vec3 lightPos(1.0f, 1.0f, 2.0f);
bool keys[1024];
int main()
{
glfwInit(); //必须要将glfw初始化
//告诉GLFW使用OpenGL版本
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //主版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //次版本号
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //使用的是OpenGL核心模式
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //不允许调整窗口大小
//创建窗口
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW Window" << std::endl;
glfwTerminate(); //销毁窗口与数据
return -1;
}
glfwMakeContextCurrent(window); //将OpenGL指向为当前窗口
glewExperimental = GL_TRUE; //用于告知GLEW使用现化OpenGL技术
//glew初始化
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
//视口
int width = 800, height = 600;
glfwGetFramebufferSize(window, &width, &height); //设置OpenGL渲染窗口的尺寸
glViewport(0, 0, width, height); //设置窗口的维度 前两个参数控制窗口左下角的位置, 第三、四个参数控制渲染窗口的宽度和高度
glfwSetKeyCallback(window, key_callback); //注册按键回调事件
glfwSetCursorPosCallback(window, mouse_callback); //注册鼠标回调事件
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); //隐藏鼠标,并捕获他
glfwSetScrollCallback(window, scroll_callback); //鼠标滚动回调事件
glEnable(GL_DEPTH_TEST); //深度缓存区
//顶点着色器
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
GLint vertexSuccess;
GLchar vertexInfoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(vertexShader, 512, nullptr, vertexInfoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << vertexInfoLog << std::endl;
}
//片段着色器
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
GLint fragmentSuccess;
GLchar fragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, fragmentInfoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << fragmentInfoLog << std::endl;
}
//着色器链接程序
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
GLint programSuccess;
GLchar programInfoLog[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &programSuccess);
if (!programSuccess) {
glGetProgramInfoLog(shaderProgram, 512, nullptr, programInfoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << programInfoLog << std::endl;
}
//光源-顶点着色器
GLuint lightVertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(lightVertexShader, 1, &lightVertexShaderSource, nullptr);
glCompileShader(lightVertexShader);
GLint lightVertexSuccess;
GLchar lightVertexInfoLog[512];
glGetShaderiv(lightVertexShader, GL_COMPILE_STATUS, &lightVertexSuccess);
if (!vertexSuccess) {
glGetShaderInfoLog(lightVertexShader, 512, nullptr, lightVertexInfoLog);
std::cout << "ERROR::SHADER::LIGHT::VERTEX::COMPILATION_FAILED\n" << lightVertexInfoLog << std::endl;
}
//片段着色器
GLuint lightFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(lightFragmentShader, 1, &lightFragmentShaderSource, nullptr);
glCompileShader(lightFragmentShader);
GLint lightFragmentSuccess;
GLchar lightFragmentInfoLog[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &lightFragmentSuccess);
if (!fragmentSuccess) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, lightFragmentInfoLog);
std::cout << "ERROR::SHADER::LIGHT::FRAGMENT::COMPILATION_FAILED\n" << lightFragmentInfoLog << std::endl;
}
//光源着色器链接程序
GLuint lightShaderProgram = glCreateProgram();
glAttachShader(lightShaderProgram, lightVertexShader);
glAttachShader(lightShaderProgram, lightFragmentShader);
glLinkProgram(lightShaderProgram);
GLint lightProgramSuccess;
GLchar lightProgramInfoLog[512];
glGetProgramiv(lightShaderProgram, GL_LINK_STATUS, &lightProgramSuccess);
if (!programSuccess) {
glGetProgramInfoLog(lightShaderProgram, 512, nullptr, lightProgramInfoLog);
std::cout << "ERROR::SHADER::LIGHT::PROGRAM::LINKING_FAILED\n" << lightProgramInfoLog << std::endl;
}
GLfloat vertices[] = {
//-----位置 //法向量 //纹理坐标
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
//十个立方体
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
//光源坐标
glm::vec3 pointLightPositions[] = {
glm::vec3(0.7f, 0.2f, 2.0f),
glm::vec3(2.3f, -3.3f, -4.0f),
glm::vec3(-4.0f, 2.0f, -12.0f),
glm::vec3(0.0f, 0.0f, -3.0f)
};
//顶点数据
GLuint VBO, VAO;
glGenBuffers(1, &VBO); //顶点缓冲对象
glGenVertexArrays(1, &VAO); //顶点数组对象
glBindVertexArray(VAO);
//顶点数据
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
//法向量
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//纹理坐标
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//光源
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(lightVAO);
//顶点属性
//顶点坐标
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//纹理
GLuint texture;
int textureWidht, textureHeight;
//加载纹理
unsigned char* image = SOIL_load_image("container2.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
GLuint texture2;
//加载纹理
image = SOIL_load_image("container2_specular.png", &textureWidht, &textureHeight, 0, SOIL_LOAD_RGB);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
//环绕方式-WRAP,默认环绕方式-重复纹理图形GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //对应X轴
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //对应Y轴
//过滤方式。缩小(GL_TEXTURE_MIN_FILTER)和放大(GL_TEXTURE_MAG_FILTER)都采用线性过滤(GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大
//生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidht, textureWidht, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); //生成所有需要的多级渐远纹理
//解绑
SOIL_free_image_data(image); //释放图像内存
glBindTexture(GL_TEXTURE_2D, 0);
while (!glfwWindowShouldClose(window)) {
//检查GLFW是否退出,即窗口是否关闭了,true代表结束了
glfwPollEvents(); //检查有没有事件发生(键盘输入、鼠标移动),如发生调用对应的回调函数 键盘事件:glfwSetKeyCallback(window, key_callback); key_callback即设定的回调函数
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); //清空屏幕所用的颜色,即清除颜色缓冲之后,整个颜色缓冲都会被填充为glClearColor里所设置的颜色。
//glClear(GL_COLOR_BUFFER_BIT); //清空屏幕缓冲,这里是颜色缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
do_movement();
//渲染指令
glUseProgram(shaderProgram);
glm::mat4 model; //模型矩阵
glm::mat4 view; //观察矩阵
glm::mat4 projection; //投影矩阵-透视投影
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
projection = glm::perspective(aspect, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f); //缩放效果
//物体颜色和光颜色
GLint objectColorLoc = glGetUniformLocation(shaderProgram, "objectColor");
GLint lightColorLoc = glGetUniformLocation(shaderProgram, "lightColor");
glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f);// 我们所熟悉的珊瑚红
glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f); // 依旧把光源设置为白色
//材质
GLint matDiffuseLoc = glGetUniformLocation(shaderProgram, "material.diffuse");
GLint matSpecularLoc = glGetUniformLocation(shaderProgram, "material.specular");
GLint matShineLoc = glGetUniformLocation(shaderProgram, "material.shininess");
glUniform1i(matDiffuseLoc, 0);
glUniform1i(matSpecularLoc, 1);
glUniform1f(matShineLoc, 32.0f);
//平行光
glUniform3f(glGetUniformLocation(shaderProgram, "dirLight.direction"), -0.2f, -1.0f, -0.3f);
glUniform3f(glGetUniformLocation(shaderProgram, "dirLight.ambient"), 0.05f, 0.05f, 0.05f);
glUniform3f(glGetUniformLocation(shaderProgram, "dirLight.diffuse"), 0.4f, 0.4f, 0.4f);
glUniform3f(glGetUniformLocation(shaderProgram, "dirLight.specular"), 0.5f, 0.5f, 0.5f);
// 点光源 1
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[0].position"), pointLightPositions[0].x, pointLightPositions[0].y, pointLightPositions[0].z);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[0].ambient"), 0.05f, 0.05f, 0.05f);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[0].diffuse"), 0.8f, 0.8f, 0.8f);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[0].specular"), 1.0f, 1.0f, 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[0].constant"), 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[0].linear"), 0.09);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[0].quadratic"), 0.032);
// 点光源 2
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[1].position"), pointLightPositions[1].x, pointLightPositions[1].y, pointLightPositions[1].z);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[1].ambient"), 0.05f, 0.05f, 0.05f);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[1].diffuse"), 0.8f, 0.8f, 0.8f);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[1].specular"), 1.0f, 1.0f, 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[1].constant"), 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[1].linear"), 0.09);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[1].quadratic"), 0.032);
// 点光源 3
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[2].position"), pointLightPositions[2].x, pointLightPositions[2].y, pointLightPositions[2].z);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[2].ambient"), 0.05f, 0.05f, 0.05f);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[2].diffuse"), 0.8f, 0.8f, 0.8f);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[2].specular"), 1.0f, 1.0f, 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[2].constant"), 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[2].linear"), 0.09);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[2].quadratic"), 0.032);
// 点光源 4
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[3].position"), pointLightPositions[3].x, pointLightPositions[3].y, pointLightPositions[3].z);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[3].ambient"), 0.05f, 0.05f, 0.05f);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[3].diffuse"), 0.8f, 0.8f, 0.8f);
glUniform3f(glGetUniformLocation(shaderProgram, "pointLights[3].specular"), 1.0f, 1.0f, 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[3].constant"), 1.0f);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[3].linear"), 0.09);
glUniform1f(glGetUniformLocation(shaderProgram, "pointLights[3].quadratic"), 0.032);
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
GLint lightPosLoc = glGetUniformLocation(shaderProgram, "lightPos");
GLint viewPosLoc = glGetUniformLocation(shaderProgram, "viewPos");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z);
glUniform3f(viewPosLoc, cameraPos.x, cameraPos.y, cameraPos.z);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(VAO);
for (GLuint i = 0; i < 10; i++)
{
model = glm::mat4();
model = glm::translate(model, cubePositions[i]);
GLfloat angle = 20.0f * i;
model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0);
//光源
glUseProgram(lightShaderProgram); //光源
modelLoc = glGetUniformLocation(lightShaderProgram, "model");
viewLoc = glGetUniformLocation(lightShaderProgram, "view");
projLoc = glGetUniformLocation(lightShaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(lightVAO);
/* model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);*/
for (GLuint i = 0; i < 4; i++)
{
model = glm::mat4();
model = glm::translate(model, pointLightPositions[i]);
model = glm::scale(model, glm::vec3(0.2f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0);
glfwSwapBuffers(window); //交换颜色缓冲,用来绘制,输出显示在屏幕上
}
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
glfwTerminate();
return 0;
}
/*
* 按键回调事件
* @param window 窗口
* @param key 按键
* @param scancode 扫描码
* @param action 表示这个按键是被按下还是释放
* @param mode 是否有Ctrl、Shift、Alt、Super等按钮的操作
*/
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
//esc键按下,关闭窗口
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (action == GLFW_PRESS) //按下
keys[key] = true;
else if (action == GLFW_RELEASE) //松开
keys[key] = false;
}
void do_movement() {
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
GLfloat cameraSpeed = 5.0f * deltaTime; //速度
if (keys[GLFW_KEY_W])
cameraPos += cameraSpeed * cameraFront;
if (keys[GLFW_KEY_S])
cameraPos -= cameraSpeed * cameraFront;
if (keys[GLFW_KEY_A])
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_D])
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_SPACE]) //向上
cameraPos += cameraSpeed * cameraUp;
}
bool firstMouse = true; //除了第一次鼠标移动外,其他不能移动
/*
* 鼠标回调事件
* @param window 窗口
* @param xpos 鼠标x坐标
* @param ypos 鼠标y坐标
*/
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
GLfloat xoffset = xpos - lastX;
GLfloat yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
GLfloat sensitivity = 0.05; //鼠标转向速度
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
/*
* 鼠标滚轮回调事件
*/
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
if (aspect >= 1.0f && aspect <= 45.0f)
aspect -= yoffset;
if (aspect <= 1.0f)
aspect = 1.0f;
if (aspect >= 45.0f)
aspect = 45.0f;
}