-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
651 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#version 330 core | ||
|
||
void main() | ||
{ | ||
// gl_FragDepth = gl_FragCoord.z; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.