Compare commits

..

No commits in common. "163d413f9c4c751ade24f0fb3017b51a9158cf3a" and "2a661daf3ea80c5971a2433c07af29adf5c394c3" have entirely different histories.

5 changed files with 34 additions and 19 deletions

View File

@ -59,11 +59,6 @@ vec2 get_parallax_offset_uv(vec2 uv, vec3 view_direction) {
return current_uv;
}
// Return a linear interpolation (0..1) of value between low and high, or 0.0 / 1.0 if value is below / above the bounds
float linear_step(float low, float high, float value) {
return clamp((value - low) / (high - low), 0.0, 1.0);
}
float get_shadow(vec4 fragPosLightSpace, vec3 normal) {
// perform perspective divide
mediump vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
@ -77,20 +72,17 @@ float get_shadow(vec4 fragPosLightSpace, vec3 normal) {
vec2 moments = texture(shadowMap, projCoords.xy).rg;
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
mediump float closestDepth = moments.r;
mediump float closestDepth = texture(shadowMap, projCoords.xy).r;
// get depth of current fragment from light's perspective
mediump float currentDepth = projCoords.z;
// check whether current frag pos is in shadow
float p = step(projCoords.z, moments.x);
// We divide by this later, so make sure it's not exactly 0
// It seems like it should always be 0.0, but due to interpolation it's not -- it increases with the deviation!
// A larger second parameter to max() means more blur (but also more light bleeding)
float variance = max(moments.y - moments.x * moments.x, (-0.01 + (currentDepth - closestDepth) * 0.7) * 0.1);
float variance = max(moments.y - moments.x * moments.x, 0.00002);
float d = projCoords.z - moments.x * 1.0;
// Use linear_step to prevent light bleeding
float p_max = linear_step(0.2, 1.0, variance / (variance + d * d));
float d = projCoords.z - moments.x * 1.0; // bias should be "compare", what is that?
float p_max = variance / (variance + d * d);
// If this pixel is exactly in the light, p is 1, so make sure we return that in that case
// min() to make sure that it doesn't get greater than 1.0

View File

@ -35,7 +35,7 @@ void main() {
vec3 N = normalize(mat3(model) * aNormal);
mat3 TBN = transpose(mat3(T, B, N));
vs_out.TangentLightDir = TBN * -light_direction;
vs_out.TangentLightDir = light_direction;
vs_out.TangentViewPos = TBN * viewPos;
vs_out.TangentFragPos = TBN * vs_out.FragPos;
}

View File

@ -1,9 +1,7 @@
#version 430
out vec4 FragColor;
void main()
{
// The mean and the mean squared, which we need to calculate the variance
FragColor = vec4(gl_FragCoord.z, gl_FragCoord.z * gl_FragCoord.z, 0.0, 0.0);
gl_FragColor = vec4(gl_FragCoord.z, gl_FragCoord.z * gl_FragCoord.z, 0.0, 0.0);
}

View File

@ -10,6 +10,28 @@ class DirectionalLight {
public:
glm::vec3 direction;
DirectionalLight() : shadow_shader(Gedeng::Shader("Shader/shadow.vs", "Shader/shadow.fs")) {
// Configure depth map
glGenFramebuffers(1, &depth_map_fbo);
// Create depth texture
glGenTextures(1, &depth_map);
glBindTexture(GL_TEXTURE_2D, depth_map);
// R and G with 32 bit floats: stores mean and variance
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Attach depth texture as FBO's depth buffer
glBindFramebuffer(GL_FRAMEBUFFER, depth_map_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, depth_map, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
explicit DirectionalLight(glm::vec3 direction)
// TODO: Avoid all this duplication
: direction(direction), shadow_shader(Gedeng::Shader("Shader/shadow.vs", "Shader/shadow.fs")) {
@ -19,7 +41,8 @@ class DirectionalLight {
// Create depth texture
glGenTextures(1, &depth_map);
glBindTexture(GL_TEXTURE_2D, depth_map);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@ -27,7 +50,9 @@ class DirectionalLight {
// Attach depth texture as FBO's depth buffer
glBindFramebuffer(GL_FRAMEBUFFER, depth_map_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, depth_map, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_map, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

View File

@ -92,10 +92,10 @@ class ShadowDemo : public Gedeng::Application {
void dynamic_update(double delta) override {
// Shadows
light.clear_shadows();
light.render_shadow(quad_mesh);
light.render_shadow(mesh1);
light.render_shadow(mesh2);
light.render_shadow(mesh3);
light.render_shadow(quad_mesh);
glViewport(0, 0, 1920, 1080);
glClearColor(0.1, 0.1f, 0.1, 1.0f);