{ 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 },
{ 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
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);
depth_buf.storage(DEPTH_COMPONENT, size, size);
fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
+ set_darkness(0.7);
set_texture_unit(3);
}
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;
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);
Texture2D depth_buf;
Vector3 target;
float radius;
+ float depth_bias;
ProgramData shdata;
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;