Bind shadow map texture for main.fp and sample from the shadowmap texture

This commit is contained in:
Magnus Norddahl 2017-03-02 19:10:57 +01:00
parent 538d516c9a
commit 0d1deddae5
3 changed files with 37 additions and 2 deletions

View File

@ -67,6 +67,8 @@ void FShadowMap::Update()
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, 0);
GLRenderer->mBuffers->BindShadowMapTexture(16);
FGLDebug::PopGroup();
}

View File

@ -270,6 +270,9 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char *
if (tempindex > 0) glUniform1i(tempindex, i - 1);
}
int shadowmapindex = glGetUniformLocation(hShader, "ShadowMap");
if (shadowmapindex > 0) glUniform1i(shadowmapindex, 16);
glUseProgram(0);
return !!linked;
}

View File

@ -26,6 +26,7 @@ out vec4 FragNormal;
uniform sampler2D tex;
uniform sampler2D ShadowMap;
vec4 Process(vec4 color);
vec4 ProcessTexel();
@ -132,6 +133,34 @@ float R_DoomLightingEquation(float light)
return lightscale;
}
//===========================================================================
//
// Check if light is in shadow according to its 1D shadow map
//
//===========================================================================
float shadowmapAttenuation(vec4 lightpos, float shadowIndex)
{
float u;
float v = (abs(shadowIndex) + 0.5) / 1024.0;
vec2 dir = (pixelpos.xz - lightpos.xz);
if (abs(dir.x) > abs(dir.y))
{
if (dir.x >= 0.0)
u = dir.y / dir.x * 0.125 + (0.25 + 0.125);
else
u = dir.y / dir.x * 0.125 + (0.75 + 0.125);
}
else
{
if (dir.y >= 0.0)
u = dir.x / dir.y * 0.125 + 0.125;
else
u = dir.x / dir.y * 0.125 + (0.50 + 0.125);
}
return texture(ShadowMap, vec2(u, v)).x < dot(dir, dir) ? 1.0 : 0.0;
}
//===========================================================================
//
// Standard lambertian diffuse light calculation
@ -151,10 +180,11 @@ float diffuseContribution(vec3 lightDirection, vec3 normal)
//
//===========================================================================
float pointLightAttenuation(vec4 lightpos, float attenuate)
float pointLightAttenuation(vec4 lightpos, float shadowIndex)
{
float attenuation = max(lightpos.w - distance(pixelpos.xyz, lightpos.xyz),0.0) / lightpos.w;
if (attenuate >= 0.0) // Sign bit is the attenuate flag
attenuation *= shadowmapAttenuation(lightpos, shadowIndex);
if (shadowIndex >= 0.0) // Sign bit is the attenuated light flag
{
return attenuation;
}