From cb40c369cdaf759891c5e58cf08d8ddfbce6adf3 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Wed, 8 Mar 2017 01:35:07 +0100 Subject: [PATCH] Added PCF shadows --- wadsrc/static/shaders/glsl/main.fp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/wadsrc/static/shaders/glsl/main.fp b/wadsrc/static/shaders/glsl/main.fp index 1c146510b..c3ef1f5d6 100644 --- a/wadsrc/static/shaders/glsl/main.fp +++ b/wadsrc/static/shaders/glsl/main.fp @@ -139,11 +139,10 @@ float R_DoomLightingEquation(float light) // //=========================================================================== -float shadowmapAttenuation(vec4 lightpos, float shadowIndex) +float sampleShadowmap(vec2 lightpos, vec2 testpos, float v) { float u; - float v = (abs(shadowIndex) + 0.5) / 1024.0; - vec2 dir = (pixelpos.xz - lightpos.xz); + vec2 dir = (testpos - lightpos); if (abs(dir.x) > abs(dir.y)) { if (dir.x >= 0.0) @@ -163,6 +162,28 @@ float shadowmapAttenuation(vec4 lightpos, float shadowIndex) return texture(ShadowMap, vec2(u, v)).x > dist2 ? 1.0 : 0.0; } +//=========================================================================== +// +// Check if light is in shadow using Percentage Closer Filtering (PCF) +// +//=========================================================================== + +#define PCF_FILTER_STEP_COUNT 3 +#define PCF_COUNT (PCF_FILTER_STEP_COUNT * 2 + 1) + +float shadowmapAttenuation(vec4 lightpos, float shadowIndex) +{ + float v = (abs(shadowIndex) + 0.5) / 1024.0; + vec2 dir = (pixelpos.xz - lightpos.xz); + vec2 normal = normalize(vec2(-dir.y, dir.x)); + float sum = 0.0; + for (float x = -PCF_FILTER_STEP_COUNT; x <= PCF_FILTER_STEP_COUNT; x++) + { + sum += sampleShadowmap(lightpos.xz, pixelpos.xz + normal * x, v); + } + return sum / PCF_COUNT; +} + //=========================================================================== // // Standard lambertian diffuse light calculation