I have been trying to implement shadows with the use of Shadowmapping in OpenGL. I sorta followed this tutorial https://learnopengl.com/#Advanced-Lighting/Shadows/Shadow-Mapping
I have implemented the depthmap and have gotten it to render the depthmap.
The problem I am having is when implementing the shadows. Now since this is part of a bigger project that uses Deferred Rendering, the code does not match the code in the tutorial but I am using the same concept.
LightVertex shader:
#version 440
layout (location = 0) in vec3 vertexPos;
layout (location = 1) in vec2 texCoords;
out vec2 TexCoords;
out vec4 FragPosLightSpace;
uniform mat4 model;
uniform sampler2D gPosition;
uniform mat4 lightSpaceMatrix;
void main()
{
gl_Position = vec4(vertexPos, 1.0f);
TexCoords = texCoords;
vec3 FragmentPos = vec3(model * texture(gPosition, texCoords));
FragPosLightSpace = lightSpaceMatrix * vec4(FragmentPos, 1.0);
}
LightFragment shader:
#version 440
out vec4 FragColor;
in vec2 TexCoords;
in vec4 FragPosLightSpace;
uniform sampler2D gPosition;
uniform sampler2D gNormal;
uniform sampler2D gAlbedoSpec;
uniform sampler2D depthMap;
struct Light {
vec3 Position;
vec3 Color;
float Linear;
float Quadratic;
};
const int NR_LIGHTS = 32;
uniform Light lights[NR_LIGHTS];
uniform vec3 viewPos;
float ShadowCalculation(vec4 fragPosLightSpace)
{
//Perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
//Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
//Get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(depthMap, projCoords.xy).r;
//Get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
//Check wheter current frag pos is in shadow
float shadow = 0.0;
if(currentDepth > closestDepth)
{
shadow = 1.0;
}
return shadow;
}
void main()
{
// Retrieve data from G-buffer
vec3 FragPos = texture(gPosition, TexCoords).rgb;
vec3 Normal = texture(gNormal, TexCoords).rgb;
vec3 color = texture(gAlbedoSpec, TexCoords).rgb;
float Specular = texture(gAlbedoSpec, TexCoords).a;
float shadow = ShadowCalculation(FragPosLightSpace);
vec3 lighting = vec3(0.1 + (1.0-shadow),0.1 + (1.0-shadow),0.1 + (1.0-shadow));
vec3 viewDir = normalize(viewPos - FragPos);
for(int i = 0; i < NR_LIGHTS; ++i)
{
vec3 lightDir = normalize(lights[i].Position - FragPos);
vec3 diffuse = max(dot(Normal, lightDir), 0.0) * color * lights[i].Color;
// Specular
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(Normal, halfwayDir), 0.0), 16.0);
vec3 specular = lights[i].Color * spec * Specular;
// Attenuation
float distance = length(lights[i].Position - FragPos);
float attenuation = 1.0 / (1.0 + lights[i].Linear * distance + lights[i].Quadratic * distance * distance);
diffuse *= attenuation;
specular *= attenuation;
lighting += diffuse + specular;
}
lighting * color;
FragColor = vec4(lighting, 1.0f);
float depthValue = texture(depthMap,TexCoords).r;
// Test depthmap
//FragColor = vec4(vec3(depthValue),1.0);
}
The ShadowCalculation is the function that calculates if a position is in shadows or not. And it pretty much follows the same concept as the tutorial does.
Now if I run all this, all I get is a white screen, I thought it might be because I had the setting of shadow wrong so I tried setting float shadow = 1.0 and then in the if-statement setting shadows to 0.0. Now I dont get a completly white screen but the shadows are not showing. I feel like I am close to a solution but have kinda gotten stuck right now and would appreciate if someone could tell me what the problem is or could be.