基础光照
现实世界的光照是极其复杂的,而且会受到诸多因素的影响,这是我们有限的计算能力所无法模拟的。因此OpenGL的光照使用的是简化的模型。
其中一个模型是Phong光照模型,其主要有三个分量组成,环境(ambient)、漫反射(diffuse)和镜面光照(specular)。
环境光照
我们使用一个很小的常量(光照)颜色,添加到物体片段的最终颜色中,这样子的话即便场景中没有直接的光源也能看起来存在有一些发散的光。
把环境光照添加到场景里非常简单。我们用光的颜色乘以一个很小的常量环境因子,再乘以物体的颜色,然后将最终结果作为片段的颜色。
漫反射光照
漫反射光照使物体上与光线方向越接近的片段能从光源处获得更多的亮度。
镜面反射
我们通过反射法向量周围光的方向来计算反射向量。然后我们计算反射向量和视线方向的角度差,如果夹角越小,那么镜面光的影响就会越大。它的作用效果就是,当我们去看光被物体所反射的那个方向的时候,我们会看到一个高光。
code
环境光照
#include "../env/glm/glm.hpp"
#include "../env/glm/gtc/matrix_transform.hpp"
#include "../env/glm/gtc/type_ptr.hpp"
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "../env/std_image.h"
#include "../env/glad.h"
#include "../env/glfw3.h"
#include <fstream>
#include "../tools/shader.h"
#include "../tools/camera.h"
#define WIDTH 800
#define HEIGHT 600
GLFWwindow *initialize(int width, int height);
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
void processInput(GLFWwindow *window);
Camera camera = Camera(glm::vec3(0.0f, 0.0f, 3.0f),
glm::vec3(0.0f, 0.0f, -1.0f),
glm::vec3(0.0f, 1.0f, 0.0f),
0.0f, 0.0f);
float cameraSpeed = 2.5f;
float lastTime;
float lastXPos;
float lastYPos;
bool firstMouse;
float fov = 45.0f;
int main()
{
GLFWwindow *window = initialize(WIDTH, HEIGHT);
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1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VBO;
glGenBuffers(1, &myVBO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GLuint objectVAO;
glGenVertexArrays(1, &objectVAO);
glBindVertexArray(objectVAO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
glm::mat4 model, view, projection;
Shader lampShader("1-vertex.glsl", "1-lamp-fragment.glsl");
Shader lightShader("1-vertex.glsl", "2-fragment.glsl");
lastTime = glfwGetTime();
while (!glfwWindowShouldClose(window))
{
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(1.2f, 1.0f, 2.0f));
model = glm::scale(model, glm::vec3(0.2f));
view = camera.lookAt();
projection = glm::perspective(glm::radians(fov), 1.0f * WIDTH / HEIGHT, 0.1f, 100.0f);
lampShader.use();
lampShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
lampShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
lampShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4(1.0f);
lightShader.use();
lightShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
lightShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
lightShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 objectColor = glm::vec3(1.0f, 0.5f, 0.31f);
lightShader.setv3("lightColor", glm::value_ptr(lightColor));
lightShader.setv3("objectColor", glm::value_ptr(objectColor));
glBindVertexArray(objectVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
processInput(window);
glfwSwapBuffers(window);
glfwPollEvents();
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window)
{
float currentTime = glfwGetTime();
float delta = currentTime - lastTime;
lastTime = currentTime;
float distance = cameraSpeed * delta;
// 移动照相机位置
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
else if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
camera.move(CameraMovement::FORWARD, distance);
}
else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
camera.move(CameraMovement::BACKWARD, distance);
}
else if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
camera.move(CameraMovement::LEFT, distance);
}
else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
camera.move(CameraMovement::RIGHT, distance);
}
else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
{
camera.move(CameraMovement::DOWN, distance);
}
else if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
{
camera.move(CameraMovement::UP, distance);
}
}
GLFWwindow *initialize(int width, int height)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "2", nullptr, nullptr);
if (!window)
{
exit(-1);
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
exit(-1);
}
glViewport(0, 0, width, height);
glfwSetWindowSizeCallback(window, framebuffer_size_callback);
// 光标初始位置
lastXPos = 400;
lastYPos = 300;
firstMouse = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
return window;
}
void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{
if (firstMouse)
{
lastXPos = xpos;
lastYPos = ypos;
firstMouse = false;
}
float sensitivity = 0.05f;
float xOffset = (xpos - lastXPos) * sensitivity;
float yOffset = (lastYPos - ypos) * sensitivity;
lastXPos = xpos;
lastYPos = ypos;
camera.rotate(xOffset, yOffset);
}
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
fov -= yoffset;
if (fov <= 1.0f)
{
fov = 1.0f;
}
if (fov >= 45.0f)
{
fov = 45.0f;
}
}
2-fragment.glsl
#version 330 core
uniform vec3 lightColor;
uniform vec3 objectColor;
out vec4 FragColor;
void main() {
float ambientStrength = 0.1;
FragColor = vec4(ambientStrength * lightColor * objectColor, 1.0f);
}
环境光照+漫反射光照
#include "../env/glm/glm.hpp"
#include "../env/glm/gtc/matrix_transform.hpp"
#include "../env/glm/gtc/type_ptr.hpp"
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "../env/std_image.h"
#include "../env/glad.h"
#include "../env/glfw3.h"
#include <fstream>
#include "../tools/shader.h"
#include "../tools/camera.h"
#define WIDTH 800
#define HEIGHT 600
GLFWwindow *initialize(int width, int height);
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
void processInput(GLFWwindow *window);
Camera camera = Camera(glm::vec3(0.0f, 0.0f, 3.0f),
glm::vec3(0.0f, 0.0f, -1.0f),
glm::vec3(0.0f, 1.0f, 0.0f),
0.0f, 0.0f);
float cameraSpeed = 2.5f;
float lastTime;
float lastXPos;
float lastYPos;
bool firstMouse;
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f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f};
unsigned int myVBO;
glGenBuffers(1, &myVBO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GLuint objectVAO;
glGenVertexArrays(1, &objectVAO);
glBindVertexArray(objectVAO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
glm::mat4 model, view, projection;
Shader lampShader("2.1-vertex.glsl", "1-lamp-fragment.glsl");
Shader lightShader("2.1-vertex.glsl", "2.1-fragment.glsl");
lastTime = glfwGetTime();
glEnable(GL_DEPTH_TEST);
while (!glfwWindowShouldClose(window))
{
model = glm::mat4(1.0f);
glm::vec3 lightPos = glm::vec3(1.2f, 1.0f, 2.0f);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
view = camera.lookAt();
projection = glm::perspective(glm::radians(fov), 1.0f * WIDTH / HEIGHT, 0.1f, 100.0f);
lampShader.use();
lampShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
lampShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
lampShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4(1.0f);
lightShader.use();
lightShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
lightShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
lightShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 objectColor = glm::vec3(1.0f, 0.5f, 0.31f);
lightShader.setv3("lightColor", glm::value_ptr(lightColor));
lightShader.setv3("objectColor", glm::value_ptr(objectColor));
lightShader.setv3("lightPos", glm::value_ptr(lightPos));
glBindVertexArray(objectVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
processInput(window);
glfwSwapBuffers(window);
glfwPollEvents();
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window)
{
float currentTime = glfwGetTime();
float delta = currentTime - lastTime;
lastTime = currentTime;
float distance = cameraSpeed * delta;
// 移动照相机位置
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
else if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
camera.move(CameraMovement::FORWARD, distance);
}
else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
camera.move(CameraMovement::BACKWARD, distance);
}
else if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
camera.move(CameraMovement::LEFT, distance);
}
else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
camera.move(CameraMovement::RIGHT, distance);
}
else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
{
camera.move(CameraMovement::DOWN, distance);
}
else if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
{
camera.move(CameraMovement::UP, distance);
}
}
GLFWwindow *initialize(int width, int height)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "2", nullptr, nullptr);
if (!window)
{
exit(-1);
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
exit(-1);
}
glViewport(0, 0, width, height);
glfwSetWindowSizeCallback(window, framebuffer_size_callback);
// 光标初始位置
lastXPos = 400;
lastYPos = 300;
firstMouse = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
return window;
}
void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{
if (firstMouse)
{
lastXPos = xpos;
lastYPos = ypos;
firstMouse = false;
}
float sensitivity = 0.05f;
float xOffset = (xpos - lastXPos) * sensitivity;
float yOffset = (lastYPos - ypos) * sensitivity;
lastXPos = xpos;
lastYPos = ypos;
camera.rotate(xOffset, yOffset);
}
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
fov -= yoffset;
if (fov <= 1.0f)
{
fov = 1.0f;
}
if (fov >= 45.0f)
{
fov = 45.0f;
}
}
2.1-vertex.glsl
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
out vec3 Normal;
out vec3 fragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0f);
fragPos = vec3(model * vec4(aPos, 1.0f));
// 物体变换到世界坐标时,法向量也应当跟随着变换过去
Normal = mat3(transpose(inverse(model))) * aNormal;
}
2-fragment.glsl
#version 330 core
in vec3 Normal;
in vec3 fragPos;
uniform vec3 lightColor;
uniform vec3 objectColor;
uniform vec3 lightPos;
out vec4 FragColor;
void main() {
float ambientStrength = 0.1;
vec3 lightDir = normalize(lightPos - fragPos);
vec3 normal = normalize(Normal);
float diffuseStrength = max(0.0f, dot(normal, lightDir));
FragColor = vec4((ambientStrength + diffuseStrength)* lightColor * objectColor, 1.0f);
}
环境光照+漫反射光照+镜面反射光照
#include "../env/glm/glm.hpp"
#include "../env/glm/gtc/matrix_transform.hpp"
#include "../env/glm/gtc/type_ptr.hpp"
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "../env/std_image.h"
#include "../env/glad.h"
#include "../env/glfw3.h"
#include <fstream>
#include "../tools/shader.h"
#include "../tools/camera.h"
#define WIDTH 800
#define HEIGHT 600
GLFWwindow *initialize(int width, int height);
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
void processInput(GLFWwindow *window);
Camera camera = Camera(glm::vec3(0.0f, 0.0f, 3.0f),
glm::vec3(0.0f, 0.0f, -1.0f),
glm::vec3(0.0f, 1.0f, 0.0f),
0.0f, 0.0f);
float cameraSpeed = 2.5f;
float lastTime;
float lastXPos;
float lastYPos;
bool firstMouse;
float fov = 45.0f;
int main()
{
GLFWwindow *window = initialize(WIDTH, HEIGHT);
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0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0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f};
unsigned int myVBO;
glGenBuffers(1, &myVBO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GLuint objectVAO;
glGenVertexArrays(1, &objectVAO);
glBindVertexArray(objectVAO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
glm::mat4 model, view, projection;
Shader lampShader("2.2-vertex.glsl", "1-lamp-fragment.glsl");
Shader lightShader("2.2-vertex.glsl", "2.2-fragment.glsl");
lastTime = glfwGetTime();
glEnable(GL_DEPTH_TEST);
while (!glfwWindowShouldClose(window))
{
model = glm::mat4(1.0f);
glm::vec3 lightPos = glm::vec3(1.2f, 1.0f, 2.0f);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
view = camera.lookAt();
projection = glm::perspective(glm::radians(fov), 1.0f * WIDTH / HEIGHT, 0.1f, 100.0f);
lampShader.use();
lampShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
lampShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
lampShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4(1.0f);
lightShader.use();
lightShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
lightShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
lightShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 objectColor = glm::vec3(1.0f, 0.5f, 0.31f);
lightShader.setv3("lightColor", glm::value_ptr(lightColor));
lightShader.setv3("objectColor", glm::value_ptr(objectColor));
glm::vec3 cameraPos = camera.getPosition();
lightShader.setv3("viewPos", glm::value_ptr(cameraPos));
lightShader.setv3("lightPos", glm::value_ptr(lightPos));
glBindVertexArray(objectVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
processInput(window);
glfwSwapBuffers(window);
glfwPollEvents();
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window)
{
float currentTime = glfwGetTime();
float delta = currentTime - lastTime;
lastTime = currentTime;
float distance = cameraSpeed * delta;
// 移动照相机位置
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
else if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
camera.move(CameraMovement::FORWARD, distance);
}
else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
camera.move(CameraMovement::BACKWARD, distance);
}
else if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
camera.move(CameraMovement::LEFT, distance);
}
else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
camera.move(CameraMovement::RIGHT, distance);
}
else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
{
camera.move(CameraMovement::DOWN, distance);
}
else if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
{
camera.move(CameraMovement::UP, distance);
}
}
GLFWwindow *initialize(int width, int height)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "2", nullptr, nullptr);
if (!window)
{
exit(-1);
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
exit(-1);
}
glViewport(0, 0, width, height);
glfwSetWindowSizeCallback(window, framebuffer_size_callback);
// 光标初始位置
lastXPos = 400;
lastYPos = 300;
firstMouse = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
return window;
}
void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{
if (firstMouse)
{
lastXPos = xpos;
lastYPos = ypos;
firstMouse = false;
}
float sensitivity = 0.05f;
float xOffset = (xpos - lastXPos) * sensitivity;
float yOffset = (lastYPos - ypos) * sensitivity;
lastXPos = xpos;
lastYPos = ypos;
camera.rotate(xOffset, yOffset);
}
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
fov -= yoffset;
if (fov <= 1.0f)
{
fov = 1.0f;
}
if (fov >= 45.0f)
{
fov = 45.0f;
}
}
2.2-vertex.glsl
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
out vec3 Normal;
out vec3 fragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0f);
fragPos = vec3(model * vec4(aPos, 1.0f));
// 物体变换到世界坐标时,法向量也应当跟随着变换过去
Normal = mat3(transpose(inverse(model))) * aNormal;
}
2.2-fragment.glsl
#version 330 core
in vec3 Normal;
in vec3 fragPos;
uniform vec3 lightColor;
uniform vec3 objectColor;
uniform vec3 lightPos;
uniform vec3 viewPos;
out vec4 FragColor;
void main() {
float ambientStrength = 0.1;
vec3 lightDir = normalize(lightPos - fragPos);
vec3 normal = normalize(Normal);
float diffuseStrength = max(0.0f, dot(normal, lightDir));
vec3 viewDir = normalize(viewPos - fragPos);
vec3 reflectDir = reflect(-lightDir, normal);
float specularStrength = pow(max(0.0f, dot(viewDir, reflectDir)), 16) * 0.5;
FragColor = vec4((ambientStrength + diffuseStrength + specularStrength)* lightColor * objectColor, 1.0f);
}