From 223e33164d0706d357efc03da1dc31a653f6afcf Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 30 Aug 2012 09:03:39 +0300 Subject: [PATCH] Improvements for shadow mapping Depth bias is now configurable, and a darkness parameter was added. --- source/programbuilder.cpp | 4 +++- source/shadowmap.cpp | 22 ++++++++++++++++++++-- source/shadowmap.h | 15 ++++++++++++++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/source/programbuilder.cpp b/source/programbuilder.cpp index 46f7c153..1e07c1e5 100644 --- a/source/programbuilder.cpp +++ b/source/programbuilder.cpp @@ -53,7 +53,8 @@ const ProgramBuilder::StandardVariable ProgramBuilder::standard_variables[] = { FRAGMENT, "rgb_light", "vec3", "vec3(l_diffuse+l_specular)", "!mp" }, { FRAGMENT, "rgb_light", "vec3", "l_diffuse*gl_FrontLightProduct[0].diffuse.rgb", "m!p" }, { FRAGMENT, "rgb_light", "vec3", "l_diffuse*gl_FrontLightProduct[0].diffuse.rgb+l_specular*gl_FrontLightProduct[0].specular.rgb", "mp" }, - { FRAGMENT, "l_shadow", "float", "shadow2D(shadow, shd_vertex).r", 0 }, + { FRAGMENT, "l_shadow", "float", "mix(1.0, shadow_sample, shadow_darkness)", 0 }, + { FRAGMENT, "shadow_sample", "float", "shadow2D(shadow, shd_vertex).r", 0 }, { FRAGMENT, "l_diffuse", "float", "max(dot(n_zzz_normal, n_zzz_light_dir), 0.0)", 0 }, { FRAGMENT, "l_specular", "float", "pow(max(dot(n_zzz_half_vec, n_zzz_normal), 0.0), gl_FrontMaterial.shininess)", 0 }, { FRAGMENT, "n_zzz_half_vec", "vec3", "normalize(zzz_light_dir-zzz_incident_dir)", 0 }, @@ -87,6 +88,7 @@ const ProgramBuilder::StandardVariable ProgramBuilder::standard_variables[] = { UNIFORM, "shadow_unit", "int", 0, 0 }, { UNIFORM, "texture", "sampler2D", 0, 0 }, { UNIFORM, "shadow", "sampler2DShadow", 0, 0 }, + { UNIFORM, "shadow_darkness", "float", 0, 0 }, { UNIFORM, "normalmap", "sampler2D", 0, 0 }, // Terminator entry diff --git a/source/shadowmap.cpp b/source/shadowmap.cpp index ea4982fe..b930315f 100644 --- a/source/shadowmap.cpp +++ b/source/shadowmap.cpp @@ -18,7 +18,8 @@ ShadowMap::ShadowMap(unsigned s, const Renderable &r, const Light &l): Effect(r), size(s), light(l), - radius(1) + radius(1), + depth_bias(4) { depth_buf.set_min_filter(LINEAR); depth_buf.set_compare_enabled(true); @@ -27,6 +28,7 @@ ShadowMap::ShadowMap(unsigned s, const Renderable &r, const Light &l): depth_buf.storage(DEPTH_COMPONENT, size, size); fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0); + set_darkness(0.7); set_texture_unit(3); } @@ -36,6 +38,22 @@ void ShadowMap::set_target(const Vector3 &t, float r) radius = r; } +void ShadowMap::set_darkness(float d) +{ + if(d<0.0f || d>1.0f) + throw invalid_argument("ShadowMap::set_darkness"); + + shdata.uniform("shadow_darkness", d); +} + +void ShadowMap::set_depth_bias(float b) +{ + if(b<0.0f) + throw invalid_argument("ShadowMap::set_depth_bias"); + + depth_bias = b; +} + void ShadowMap::set_texture_unit(unsigned u) { unit = u; @@ -118,7 +136,7 @@ void ShadowMap::render(Renderer &renderer, const Tag &tag) const TexGen tg_s, tg_t, tg_r; tg_s.set_plane(Vector4(matrix[0]/diam, matrix[4]/diam, matrix[8]/diam, matrix[12]/diam+0.5f)); tg_t.set_plane(Vector4(matrix[1]/diam, matrix[5]/diam, matrix[9]/diam, matrix[13]/diam+0.5f)); - tg_r.set_plane(Vector4(-matrix[2]/diam, -matrix[6]/diam, -matrix[10]/diam, 0.5f-matrix[14]/diam-4.0f/size)); + tg_r.set_plane(Vector4(-matrix[2]/diam, -matrix[6]/diam, -matrix[10]/diam, 0.5f-matrix[14]/diam-depth_bias/size)); tg_s.bind_to(SCOORD); tg_t.bind_to(TCOORD); tg_r.bind_to(RCOORD); diff --git a/source/shadowmap.h b/source/shadowmap.h index af3cf6f7..986e0d96 100644 --- a/source/shadowmap.h +++ b/source/shadowmap.h @@ -29,6 +29,7 @@ private: Texture2D depth_buf; Vector3 target; float radius; + float depth_bias; ProgramData shdata; public: @@ -39,8 +40,20 @@ public: covered by the ShadowMap. */ void set_target(const Vector3 &, float); + /** Sets the darkness of shadows. Must be in the range between 0.0 and 1.0, + inclusive. Only usable with shaders, and provided through the + shadow_darkness uniform. */ + void set_darkness(float); + + /** Sets a distance beyond objects from which the shadow starts. Expressed + in pixel-sized units. Must be positive; values less than 1.0 are not + recommended. Larger values produce less depth artifacts, but may prevent + thin objects from casting shadows on nearby sufraces. */ + void set_depth_bias(float); + /** Sets the texture unit to bind the shadow map to during the rendering - phase. The default is texture unit 3. */ + phase. Provided to shaders through the shadow and shadow_unit uniforms. + The default is texture unit 3. */ void set_texture_unit(unsigned); virtual void render(Renderer &, const Tag &) const; -- 2.45.2