Minor cleanup for shadows

This commit is contained in:
karl 2021-05-23 19:34:41 +02:00
parent b4e0cefbe7
commit 8c5b04ad97
2 changed files with 3 additions and 25 deletions

View File

@ -77,7 +77,7 @@ float get_shadow(vec4 fragPosLightSpace, vec3 normal) {
vec2 moments = texture(shadowMap, projCoords.xy).rg; vec2 moments = texture(shadowMap, projCoords.xy).rg;
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
mediump float closestDepth = texture(shadowMap, projCoords.xy).r; mediump float closestDepth = moments.r;
// get depth of current fragment from light's perspective // get depth of current fragment from light's perspective
mediump float currentDepth = projCoords.z; mediump float currentDepth = projCoords.z;
// check whether current frag pos is in shadow // check whether current frag pos is in shadow
@ -85,9 +85,9 @@ float get_shadow(vec4 fragPosLightSpace, vec3 normal) {
// We divide by this later, so make sure it's not exactly 0 // 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! // 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) // A larger second parameter to max() means more blur (but also more light bleeding)
float variance = max(moments.y - moments.x * moments.x, 0.0005); float variance = max(moments.y - moments.x * moments.x, (currentDepth - closestDepth) * 0.05);
float d = projCoords.z - moments.x * 1.0; // bias should be "compare", what is that? float d = projCoords.z - moments.x * 1.0;
// Use linear_step to prevent light bleeding // Use linear_step to prevent light bleeding
float p_max = linear_step(0.2, 1.0, variance / (variance + d * d)); float p_max = linear_step(0.2, 1.0, variance / (variance + d * d));

View File

@ -10,28 +10,6 @@ class DirectionalLight {
public: public:
glm::vec3 direction; 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) explicit DirectionalLight(glm::vec3 direction)
// TODO: Avoid all this duplication // TODO: Avoid all this duplication
: direction(direction), shadow_shader(Gedeng::Shader("Shader/shadow.vs", "Shader/shadow.fs")) { : direction(direction), shadow_shader(Gedeng::Shader("Shader/shadow.vs", "Shader/shadow.fs")) {