Jump to content

View more

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Shadowmapping issue - Shadows not showing

4: Adsense
  • You cannot reply to this topic
3 replies to this topic

#1 ChobitsTheZero   Members   

117
Like
0Likes
Like

Posted 19 March 2017 - 08:30 AM

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.


Edited by ChobitsTheZero, 19 March 2017 - 08:35 AM.


#2 mzbear   Members   

218
Like
0Likes
Like

Posted 19 March 2017 - 10:39 AM

Short answer: in your vertex shader, FragmentPos is wrong. Instead of copypasting code, please seek to understand exactly what it does and why does it does so.

Long answer:

The shadow map is supposed to be a depth map, but you're taking vectors out of it in the vertex shader... using completely unrelated texture coordinates, no less, this doesn't make any sense whatsoever.

You could think of the shadow map texture as a lookup table for ray tracing the primary light rays, i.e. the ray between the fragment position and the light source. You will basically have to transform the fragment two different ways, for two different "cameras" to get the values you need to do this calculation. One of the cameras represents the main display, and the other represents the light. The light camera was previously used to create the depth map, in which each texel represents a single ray of light, and the depth value is the distance a light ray can travel before it gets obstructed. To perform the shadow calculation, you will test if the distance traveled by the light is shorter than the actual distance to the fragment that's being shaded, to determine if the light is obstructed before it reaches the fragment in question.

Thus, your vertex shader shouldn't do texture lookups, it should just output the position of the vertex in lightspace, i.e. lightSpaceMatrix * vec4(vertexPos, 1)



#3 ChobitsTheZero   Members   

117
Like
0Likes
Like

Posted 19 March 2017 - 02:35 PM

Short answer: in your vertex shader, FragmentPos is wrong. Instead of copypasting code, please seek to understand exactly what it does and why does it does so.

Long answer:

The shadow map is supposed to be a depth map, but you're taking vectors out of it in the vertex shader... using completely unrelated texture coordinates, no less, this doesn't make any sense whatsoever.

You could think of the shadow map texture as a lookup table for ray tracing the primary light rays, i.e. the ray between the fragment position and the light source. You will basically have to transform the fragment two different ways, for two different "cameras" to get the values you need to do this calculation. One of the cameras represents the main display, and the other represents the light. The light camera was previously used to create the depth map, in which each texel represents a single ray of light, and the depth value is the distance a light ray can travel before it gets obstructed. To perform the shadow calculation, you will test if the distance traveled by the light is shorter than the actual distance to the fragment that's being shaded, to determine if the light is obstructed before it reaches the fragment in question.

Thus, your vertex shader shouldn't do texture lookups, it should just output the position of the vertex in lightspace, i.e. lightSpaceMatrix * vec4(vertexPos, 1)

I see! That really helps! I have kinda been short on time and really stressed due to exams so I basically has to rush through it all. But I shouldn't try to make excuses, I guess i'm just bad at understanding how excatly 3D programming works.



#4 ChobitsTheZero   Members   

117
Like
0Likes
Like

Posted 20 March 2017 - 08:27 AM

Okey so, I did what you said but it seems that there is also another problem which I can't really figure out what it is. So now there are shadows, but they dont seem to be at the correct position. Instead of being behind the models they are instead infront of them and slightly above them. I was thinking that maybe I had to multiple the vec4(vertexPos, 1) with model, but that also gave a weird result.

#Edit: Nevermind I was able to solve it, appearantly the lightspacematrix had some issues with it.


Edited by ChobitsTheZero, 21 March 2017 - 04:59 AM.