目录
13.3.3 简单的景深效果(Depth of Field)
在本章中,我们将创建一个完整的渲染场景,涵盖从基础的场景设置到高级的渲染效果和后期处理。我们将使用OpenGL和GLSL来实现场景的渲染,并逐步引入光照、材质、纹理、阴影映射、环境光遮蔽(AO)以及景深效果等技术。
13.1 项目规划和设计
13.1.1 项目目标
本项目的目标是创建一个包含地面、墙壁和一个简单3D模型(如立方体)的基本渲染场景。除此之外,我们还将实现以下高级渲染效果:
- 阴影映射(Shadow Mapping)
- 环境光遮蔽(Ambient Occlusion,AO)
- 简单的景深效果(Depth of Field)
13.1.2 设计要求
- 场景内容:
- 地面
- 墙壁
- 3D模型(立方体)
- 光照效果:
- 点光源
- 环境光
- 材质效果:
- 漫反射和高光
- 高级渲染效果:
- 阴影映射
- 环境光遮蔽(AO)
- 简单的景深效果
13.2 实现场景中的光照、材质和纹理
13.2.1 创建基础场景
首先,我们将设置基础场景,包括地面、墙壁和一个简单的3D模型(立方体)。我们使用OpenGL创建这些基本几何体,并为每个对象定义适当的顶点和片段着色器。
顶点着色器 (shader.vert
):
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aTexCoord;
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
vec4 worldPosition = model * vec4(aPos, 1.0);
FragPos = worldPosition.xyz;
Normal = mat3(transpose(inverse(model))) * aNormal;
TexCoord = aTexCoord;
gl_Position = projection * view * worldPosition;
}
片段着色器 (shader.frag
):
#version 330 core
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoord;
out vec4 FragColor;
struct Material {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
struct Light {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform Material material;
uniform Light light;
uniform vec3 viewPos;
uniform sampler2D diffuseTexture;
void main() {
// Ambient
vec3 ambient = material.ambient * light.ambient;
// Diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(light.position - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = material.diffuse * diff * light.diffuse;
// Specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = material.specular * spec * light.specular;
vec3 result = ambient + diffuse + specular;
FragColor = vec4(result, 1.0) * texture(diffuseTexture, TexCoord);
}
13.2.2 应用材质和纹理
接下来,我们为场景中的物体应用基础的材质和纹理。材质的主要属性包括漫反射、镜面反射和环境光成分。纹理则用于为物体添加更多的细节和颜色。
纹理加载和绑定 (texture.cpp
):
#include "texture.hpp"
#include <iostream>
Texture::Texture(const char* imagePath) {
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_2D, ID);
// 设置纹理参数
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// 加载并生成纹理
int width, height, nrChannels;
unsigned char* data = stbi_load(imagePath, &width, &height, &nrChannels, 0);
if (data) {
GLenum format;
if (nrChannels == 1)
format = GL_RED;
else if (nrChannels == 3)
format = GL_RGB;
else if (nrChannels == 4)
format = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cerr << "Failed to load texture: " << imagePath << std::endl;
}
stbi_image_free(data);
}
void Texture::bind() const {
glBindTexture(GL_TEXTURE_2D, ID);
}
场景对象加载 (scene_object.cpp
):
#include "scene_object.hpp"
#include <glm/gtc/matrix_transform.hpp>
SceneObject::SceneObject(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices, const char* texturePath)
: vertices(vertices), indices(indices), texture(texturePath) {
setupMesh();
}
void SceneObject::setupMesh() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
// 顶点位置
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
// 法线
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
// 纹理坐标
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
glBindVertexArray(0);
}
void SceneObject::draw(const Shader& shader) {
shader.use();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
13.3 集成高级渲染效果和后期处理
13.3.1 阴影映射(Shadow Mapping)
阴影映射用于在场景中生成逼真的阴影效果。我们将使用深度贴图来计算阴影,并在片段着色器中进行深度比较。
阴影映射Shader (shadow_mapping_shader.vert
and shadow_mapping_shader.frag
):
顶点着色器 (shadow_mapping_shader.vert
):
#version 330 core
layout(location = 0) in vec3 aPos;
uniform mat4 lightSpaceMatrix;
uniform mat4 model;
void main() {
gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0);
}
片段着色器 (shadow_mapping_shader.frag
):
#version 330 core
void main() {
// 这里不需要输出颜色,深度缓冲会自动存储深度值
}
<
标签:GLSL,教程,场景,void,TEXTURE,uniform,vec3,第十三章,GL
From: https://blog.csdn.net/qq_54098120/article/details/140840047