//Then normalize those new values so we do not accidentally go above length = 1. Also normalize the normals themselves beforehand, just to be sure calculations are accurate.
//Check if the depth of the vertex is greater than the closest depth.
//If so, we are in shadow.
float shadow;
if(projCoords.z - bias > closestDepth)
{
shadow = 1.0;
}
else
{
shadow = 0.0;
}
//Make sure that if the coordinates are closer than the closest point of the shadowmap can be (1)
//Then they are set to not be in shadow.
if(projCoords.z > 1.0)
{
shadow = 0.0;
}
return shadow;
}
vec3 DirectionalLight(
in vec3 color,
in vec3 direction,
in float shadow
)
{
//Ambient lighting. Ambient light is light that is present even when normally no light should shine upon that part of the object.
//This is the poor mans way of simulating light reflecting from other surfaces in the room. For those that don't want to get into more advanced lighting models.
float ambient_strength = 0.1;
vec3 ambient = ambient_strength * color;
//Diffuse lighting. Light that scatters in all directions after hitting the object.
vec3 dir_to_light = normalize(-direction); //First we get the direction from the object to the lightsource ( which is of course the opposite of the direction of the light)
vec3 diffuse = color * max(0.0, dot(normals, dir_to_light)); //Then we find how strongly the light scatters in different directions, with a minimum of 0.0, via the normals and the direction we just found.
//Specular Lighting. Light that is reflected away from the object it hits with the same angle it hit it. Think of it as light hitting a mirror and bouncing off, if you will.
vec3 viewDirection = normalize(vec3(inverse(u_ViewMat) * vec4(0,0,0,1) - u_TransformationMat * vertexPositions)); //We find the direction from the surface the light hits to our camera.
vec3 reflectionDirection = reflect(dir_to_light,normals); //And then we find the angle between the direction of the light and the direction from surface to camera
float specular_power = pow(max(0.0,dot(viewDirection,reflectionDirection)),32); //The closer together those two vectors are, the more powerful the specular light.
vec3 specular = u_Specularity * specular_power * color; //Finally, multiply with how reflective the surface is and add the color.
//We make sure to mutilply diffuse and specular with how shadowed our vertex is
//The 1-shadow is not really necessary for this, but the values coming from the ShadowCalculation can be updated to give smoother transitions between shadows
//Finally, multiply with the color. Make sure the vector still has the same dimensions. Alpha channel is set to 1 here, because our object is not transparent. Might be different if you use a texture.