Skip to content

Commit

Permalink
Shadows.
Browse files Browse the repository at this point in the history
  • Loading branch information
thezzw committed Sep 8, 2022
1 parent 7dfd402 commit 6f474f3
Show file tree
Hide file tree
Showing 12 changed files with 651 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,22 @@ target_link_libraries(${EXE_7_NAME} assimp glfw ${GLAD_LIBRARIES})
# 设置运行时项目输出目录位置
set_target_properties(${EXE_7_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${EXE_7_NAME})
#----------------------------------------------------------------------------------------------
#可执行文件八-----------------------------------------------------------------------------------
set(EXE_8_NAME exe_shadow_mapping)
file(
GLOB EXE_8_SOURCES
src/shadow_mapping.cpp
)

# 链接可执行文件
add_executable(${EXE_8_NAME} ${EXE_8_SOURCES} ${PROJECT_HEADERS}
${PROJECT_SHADERS} ${VENDORS_SOURCES})

# 链接库
target_link_libraries(${EXE_8_NAME} assimp glfw ${GLAD_LIBRARIES})

# 设置运行时项目输出目录位置
set_target_properties(${EXE_8_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${EXE_8_NAME})
#----------------------------------------------------------------------------------------------
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ cmake ..
![](assets/readme/instance_100000_belt.png)
13. [Test Blinn-Phong shader model.]()
![](assets/readme/blinn_phong.png)
14. [Shadows.]
![acne](assets/readme/shadow_acne.png)
![bias](assets/readme/shadow_bias.png)
![pcf](assets/readme/shadow_pcf.png)

## __Useful Link__
* [__OpenGL/VRML Materials__](http://devernay.free.fr/cours/opengl/materials.html)
Expand Down
3 changes: 3 additions & 0 deletions assets/readme/shadow_acne.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/readme/shadow_bias.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/readme/shadow_pcf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions assets/shader/shadow_mapping.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#version 330 core
out vec4 FragColor;

in VS_OUT {
vec3 FragPos;
vec3 Normal;
vec2 TexCoords;
vec4 FragPosLightSpace;
} fs_in;

uniform sampler2D diffuseTexture;
uniform sampler2D shadowMap;

uniform vec3 lightPos;
uniform vec3 viewPos;

float ShadowCalculation(vec4 fragPosLightSpace)
{
// perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(shadowMap, projCoords.xy).r;
// get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
// check whether current frag pos is in shadow
float bias = max(0.05 * (1.0 - dot(normalize(fs_in.Normal), normalize(lightPos - fs_in.FragPos))), 0.005);

float shadow = 0.0;
vec2 texelSize = 1.0 / textureSize(shadowMap, 0);
for(int x = -2; x <= 2; ++x)
{
for(int y = -2; y <= 2; ++y)
{
float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
}
}
shadow /= 25.0;

if(projCoords.z > 1.0) shadow = 0.0;

return shadow;
}

void main()
{
vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb;
vec3 normal = normalize(fs_in.Normal);
vec3 lightColor = vec3(0.3);
// ambient
vec3 ambient = 0.3 * lightColor;
// diffuse
vec3 lightDir = normalize(lightPos - fs_in.FragPos);
float diff = max(dot(lightDir, normal), 0.0);
vec3 diffuse = diff * lightColor;
// specular
vec3 viewDir = normalize(viewPos - fs_in.FragPos);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = 0.0;
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0);
vec3 specular = spec * lightColor;
// calculate shadow
float shadow = ShadowCalculation(fs_in.FragPosLightSpace);
vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color;

FragColor = vec4(lighting, 1.0);
}
27 changes: 27 additions & 0 deletions assets/shader/shadow_mapping.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;

out vec2 TexCoords;

out VS_OUT {
vec3 FragPos;
vec3 Normal;
vec2 TexCoords;
vec4 FragPosLightSpace;
} vs_out;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat4 lightSpaceMatrix;

void main()
{
vs_out.FragPos = vec3(model * vec4(aPos, 1.0));
vs_out.Normal = transpose(inverse(mat3(model))) * aNormal;
vs_out.TexCoords = aTexCoords;
vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0);
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
6 changes: 6 additions & 0 deletions assets/shader/shadow_mapping_depth.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#version 330 core

void main()
{
// gl_FragDepth = gl_FragCoord.z;
}
10 changes: 10 additions & 0 deletions assets/shader/shadow_mapping_depth.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#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);
}
22 changes: 22 additions & 0 deletions assets/shader/shadow_mapping_quad.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#version 330 core
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D depthMap;
uniform float near_plane;
uniform float far_plane;

// required when using a perspective projection matrix
float LinearizeDepth(float depth)
{
float z = depth * 2.0 - 1.0; // Back to NDC
return (2.0 * near_plane * far_plane) / (far_plane + near_plane - z * (far_plane - near_plane));
}

void main()
{
float depthValue = texture(depthMap, TexCoords).r;
// FragColor = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective
FragColor = vec4(vec3(depthValue), 1.0); // orthographic
}
11 changes: 11 additions & 0 deletions assets/shader/shadow_mapping_quad.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords;

out vec2 TexCoords;

void main()
{
TexCoords = aTexCoords;
gl_Position = vec4(aPos, 1.0);
}
Loading

0 comments on commit 6f474f3

Please sign in to comment.