Shadowmapping bugfixes and tweaks.

This was SVN commit r12884.
This commit is contained in:
myconid 2012-11-25 18:35:59 +00:00
parent 1052cb9ee0
commit f456112492
7 changed files with 40 additions and 26 deletions

View File

@ -83,6 +83,11 @@ TEX tex, v_tex, texture[0], 2D;
// color = (texdiffuse * sundiffuse + specular) * get_shadow() + texdiffuse * ambient;
// (sundiffuse is 2*fragment.color due to clamp-avoidance in the vertex program)
#if USE_SHADOW && !DISABLE_RECEIVE_SHADOWS
TEMP shadowBias;
TEMP biasedShdw;
MOV shadowBias.x, 0.003;
MOV biasedShdw, v_shadow;
SUB biasedShdw.z, v_shadow.z, shadowBias.x;
#if USE_FP_SHADOW
#if USE_SHADOW_PCF
SUB offset.xy, v_shadow, 0.5;
@ -97,7 +102,7 @@ TEX tex, v_tex, texture[0], 2D;
SUB weight.zw, weight, 1.0;
SUB offset.xy, v_shadow, offset;
MOV offset.z, v_shadow;
MOV offset.z, biasedShdw.z;
ADD weight, weight, offset.xyxy;
MUL weight, weight, shadowScale.zwzw;
@ -114,11 +119,11 @@ TEX tex, v_tex, texture[0], 2D;
DP4 shadow.x, temp, size;
MUL shadow.x, shadow.x, 0.111111;
#else
TEX shadow.x, v_shadow, texture[1], SHADOW2D;
TEX shadow.x, biasedShdw, texture[1], SHADOW2D;
#endif
#else
TEX tex, v_shadow, texture[1], 2D;
MOV_SAT temp.z, v_shadow.z;
MOV_SAT temp.z, biasedShdw.z;
SGE shadow.x, tex.x, temp.z;
#endif

View File

@ -37,6 +37,11 @@ TEX color, fragment.texcoord[0], texture[0], 2D;
// Compute color = texture * (ambient + diffuse*shadow)
// (diffuse is 2*fragment.color due to clamp-avoidance in the vertex program)
#if USE_SHADOW
TEMP shadowBias;
TEMP biasedShdw;
MOV shadowBias.x, 0.0005;
MOV biasedShdw, fragment.texcoord[2];
SUB biasedShdw.z, fragment.texcoord[2].z, shadowBias.x;
#if USE_FP_SHADOW
#if USE_SHADOW_PCF
SUB offset.xy, fragment.texcoord[2], 0.5;
@ -51,7 +56,7 @@ TEX color, fragment.texcoord[0], texture[0], 2D;
SUB weight.zw, weight, 1.0;
SUB offset.xy, fragment.texcoord[2], offset;
MOV offset.z, fragment.texcoord[2];
MOV offset.z, biasedShdw;
ADD weight, weight, offset.xyxy;
MUL weight, weight, shadowScale.zwzw;
@ -68,11 +73,11 @@ TEX color, fragment.texcoord[0], texture[0], 2D;
DP4 temp.x, temp, size;
MUL temp.x, temp.x, 0.111111;
#else
TEX temp.x, fragment.texcoord[2], texture[2], SHADOW2D;
TEX temp.x, biasedShdw, texture[2], SHADOW2D;
#endif
#else
TEX tex, fragment.texcoord[2], texture[2], 2D;
MOV_SAT temp.z, fragment.texcoord[2].z;
MOV_SAT temp.z, biasedShdw.z;
SGE temp.x, tex.x, temp.z;
#endif
MUL diffuse.rgb, fragment.color, 2.0;

View File

@ -65,27 +65,28 @@ varying vec2 v_los;
#endif
#endif
float get_shadow()
{
float shadowBias = 0.003;
#if USE_SHADOW && !DISABLE_RECEIVE_SHADOWS
float biasedShdwZ = v_shadow.z - shadowBias;
#if USE_SHADOW_SAMPLER
#if USE_SHADOW_PCF
vec2 offset = fract(v_shadow.xy - 0.5);
vec4 size = vec4(offset + 1.0, 2.0 - offset);
vec4 weight = (vec4(2.0 - 1.0 / size.xy, 1.0 / size.zw - 1.0) + (v_shadow.xy - offset).xyxy) * shadowScale.zwzw;
return (1.0/9.0)*dot(size.zxzx*size.wwyy,
vec4(shadow2D(shadowTex, vec3(weight.zw, v_shadow.z)).r,
shadow2D(shadowTex, vec3(weight.xw, v_shadow.z)).r,
shadow2D(shadowTex, vec3(weight.zy, v_shadow.z)).r,
shadow2D(shadowTex, vec3(weight.xy, v_shadow.z)).r));
vec4(shadow2D(shadowTex, vec3(weight.zw, biasedShdwZ)).r,
shadow2D(shadowTex, vec3(weight.xw, biasedShdwZ)).r,
shadow2D(shadowTex, vec3(weight.zy, biasedShdwZ)).r,
shadow2D(shadowTex, vec3(weight.xy, biasedShdwZ)).r));
#else
return shadow2D(shadowTex, v_shadow.xyz).r;
return shadow2D(shadowTex, vec3(v_shadow.xy, biasedShdwZ)).r;
#endif
#else
if (v_shadow.z >= 1.0)
if (biasedShdwZ >= 1.0)
return 1.0;
return (v_shadow.z <= texture2D(shadowTex, v_shadow.xy).x ? 1.0 : 0.0);
return (biasedShdwZ < texture2D(shadowTex, v_shadow.xy).x ? 1.0 : 0.0);
#endif
#else
return 1.0;

View File

@ -7,6 +7,7 @@ uniform sampler2D normTex;
uniform sampler2D specTex;
#if USE_SHADOW
uniform float shadowAngle;
#if USE_SHADOW_SAMPLER
uniform sampler2DShadow shadowTex;
#if USE_SHADOW_PCF
@ -68,24 +69,26 @@ varying vec3 v_normal;
float get_shadow()
{
float shadowBias = 0.0005;
#if USE_SHADOW
float biasedShdwZ = v_shadow.z - shadowBias;
#if USE_SHADOW_SAMPLER
#if USE_SHADOW_PCF
vec2 offset = fract(v_shadow.xy - 0.5);
vec4 size = vec4(offset + 1.0, 2.0 - offset);
vec4 weight = (vec4(2.0 - 1.0 / size.xy, 1.0 / size.zw - 1.0) + (v_shadow.xy - offset).xyxy) * shadowScale.zwzw;
return (1.0/9.0)*dot(size.zxzx*size.wwyy,
vec4(shadow2D(shadowTex, vec3(weight.zw, v_shadow.z)).r,
shadow2D(shadowTex, vec3(weight.xw, v_shadow.z)).r,
shadow2D(shadowTex, vec3(weight.zy, v_shadow.z)).r,
shadow2D(shadowTex, vec3(weight.xy, v_shadow.z)).r));
vec4(shadow2D(shadowTex, vec3(weight.zw, biasedShdwZ)).r,
shadow2D(shadowTex, vec3(weight.xw, biasedShdwZ)).r,
shadow2D(shadowTex, vec3(weight.zy, biasedShdwZ)).r,
shadow2D(shadowTex, vec3(weight.xy, biasedShdwZ)).r));
#else
return shadow2D(shadowTex, v_shadow.xyz).r;
return shadow2D(shadowTex, vec3(v_shadow.xy, biasedShdwZ)).r;
#endif
#else
if (v_shadow.z >= 1.0)
if (biasedShdwZ >= 1.0)
return 1.0;
return (v_shadow.z <= texture2D(shadowTex, v_shadow.xy).x ? 1.0 : 0.0);
return (biasedShdwZ < texture2D(shadowTex, v_shadow.xy).x ? 1.0 : 0.0);
#endif
#else
return 1.0;

View File

@ -873,7 +873,10 @@ void CRenderer::RenderShadowMap(const CShaderDefines& context)
{
PROFILE("render patches");
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
m->terrainRenderer.RenderPatches();
glCullFace(GL_BACK);
}
CShaderDefines contextCast = context;

View File

@ -276,7 +276,7 @@ void ShadowMapInternals::CalcShadowMatrices()
LightProjection._22 = scale.Y;
LightProjection._24 = (shift.Y + offsetY) * scale.Y;
LightProjection._33 = scale.Z;
LightProjection._34 = shift.Z * scale.Z + renderer.m_ShadowZBias;
LightProjection._34 = shift.Z * scale.Z;// + renderer.m_ShadowZBias;
LightProjection._44 = 1.0;
// Calculate texture matrix by creating the clip space to texture coordinate matrix

View File

@ -481,16 +481,13 @@ void TerrainRenderer::PrepareShader(const CShaderProgramPtr& shader, ShadowMap*
shader->Uniform("shadowScale", width, height, 1.0f / width, 1.0f / height);
}
shader->Uniform("ambient", lightEnv.m_UnitsAmbientColor);
shader->Uniform("sunDir", lightEnv.GetSunDir());
shader->Uniform("sunColor", lightEnv.m_SunColor);
CLOSTexture& los = g_Renderer.GetScene().GetLOSTexture();
shader->BindTexture("losTex", los.GetTextureSmooth());
shader->Uniform("losTransform", los.GetTextureMatrix()[0], los.GetTextureMatrix()[12], 0.f, 0.f);
shader->Uniform("ambient", lightEnv.m_TerrainAmbientColor);
shader->Uniform("sunColor", lightEnv.m_SunColor);
shader->Uniform("sunDir", lightEnv.GetSunDir());
shader->Uniform("fogColor", lightEnv.m_FogColor);
shader->Uniform("fogParams", lightEnv.m_FogFactor, lightEnv.m_FogMax, 0.f, 0.f);