WIP Shadow Mapping

This commit is contained in:
2014-03-12 21:40:19 +01:00
parent f48cb32008
commit 77aff3c078
15 changed files with 395 additions and 103 deletions
+45 -32
View File
@@ -3,7 +3,8 @@
uniform mat4 model;
uniform mat4 view;
layout(binding=0) uniform sampler2D texture;
layout(binding=0) uniform sampler2D texture0;
layout(binding=1) uniform sampler2D shadowMap;
const int numberOfLights = 3;
@@ -15,20 +16,21 @@ uniform float linearAttenuation[numberOfLights];
uniform float quadraticAttenuation[numberOfLights];
uniform float spotExponent[numberOfLights];
in VertexData {
in VertexData {
vec3 Position;
vec3 Normal;
vec2 TextureCoord;
vec3 ShadowCoord;
} Input;
vec3 scene_ambient = vec3(0.5, 0.5, 0.5);
vec3 scene_ambient = vec3(0.2);
out vec4 fragmentColor;
void main() {
// Texture
vec4 texel = texture2D(texture, Input.TextureCoord);
vec4 texel = texture2D(texture0, Input.TextureCoord);
//vec4 texel = (blend.x * texel0) + (blend.y * texel1) + (blend.z * texel2);
//
@@ -43,48 +45,59 @@ void main() {
vec3 Is;
vec3 Id;
vec3 totalLighting = La * Ka;
// Shadows
//float cosTheta = clamp(dot(Input.Normal, vec3(0, 1, 0)), 0.0, 1.0);
//float bias = 0.001 * tan(acos(cosTheta)); // cosTheta is dot( n,l ), clamped between 0 and 1
//bias = clamp(bias, 0.0, 0.01);
float bias = 0.003;
float visibility = 1.0;
vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy);
if (shadowMapValue.z < Input.ShadowCoord.z - bias) {
visibility = 0.5;
}
vec3 totalLighting = La * Ka * visibility;
float attenuation;
for(int i = 0; i < numberOfLights; i++)
{
// Light
//vec3 lightPosition = vec3(0, 0, 2);
vec3 Ls = specular[i]; // Specular light
vec3 Ld = diffuse[i]; // Diffuse light
// Light
//vec3 lightPosition = vec3(0, 0, 2);
vec3 Ls = specular[i]; // Specular light
vec3 Ld = diffuse[i]; // Diffuse light
vec3 lightPosView = vec3(view * vec4(position[i], 1.0));
vec3 surfacePosition = vec3(model * vec4(Input.Position, 1.0));
vec3 surfacePosView = vec3(view * vec4(surfacePosition, 1.0));
vec3 surfaceToLight = normalize(lightPosView - surfacePosView);
mat3 normalMatrix = transpose(inverse(mat3(view * model)));
vec3 surfaceNormal = normalize(normalMatrix * Input.Normal);
vec3 lightPosView = vec3(view * vec4(position[i], 1.0));
vec3 surfacePosition = vec3(model * vec4(Input.Position, 1.0));
vec3 surfacePosView = vec3(view * vec4(surfacePosition, 1.0));
vec3 surfaceToLight = normalize(lightPosView - surfacePosView);
mat3 normalMatrix = transpose(inverse(mat3(view * model)));
vec3 surfaceNormal = normalize(normalMatrix * Input.Normal);
float dist = length(position[i] - surfacePosition);
float dist = length(position[i] - surfacePosition);
attenuation = 1.0 / (constantAttenuation[i]
+ linearAttenuation[i] * dist
+ quadraticAttenuation[i] * pow(dist, 2.0));
//attenuation = attenuation * pow(clampedCosine, spotExponent[i]);
attenuation = 1.0 / (constantAttenuation[i]
+ linearAttenuation[i] * dist
+ quadraticAttenuation[i] * pow(dist, 2.0));
//attenuation = attenuation * pow(clampedCosine, spotExponent[i]);
// Diffuse light
float dotProd = dot(surfaceToLight, surfaceNormal);
dotProd = max(dotProd, 0.0);
// Diffuse light
float dotProd = dot(surfaceToLight, surfaceNormal);
dotProd = max(dotProd, 0.0);
Id = Ld * Kd * abs(dotProd) * attenuation;
Id = Ld * Kd * abs(dotProd) * attenuation;
// Specular light
vec3 reflection = reflect(-surfaceToLight, surfaceNormal);
float dotSpecular = dot(reflection, normalize(-surfacePosView));
dotSpecular = max(dotSpecular, 0.0);
float specularFactor = pow(dotSpecular, 30.0); // Specular factor
// Specular light
vec3 reflection = reflect(-surfaceToLight, surfaceNormal);
float dotSpecular = dot(reflection, normalize(-surfacePosView));
dotSpecular = max(dotSpecular, 0.0);
float specularFactor = pow(dotSpecular, 30.0); // Specular factor
Is = attenuation * Ls * Ks * specularFactor;
Is = attenuation * Ls * Ks * specularFactor;
totalLighting = totalLighting + Id + Is;
totalLighting = totalLighting + Id + Is;
}
fragmentColor = vec4(totalLighting, 1.0) * texel;
@@ -0,0 +1,9 @@
#version 430
uniform mat4 MVP;
layout(location = 0) out float FragmentDepth;
void main() {
FragmentDepth = gl_FragCoord.z;
}
@@ -0,0 +1,22 @@
#version 430
uniform mat4 MVP;
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
layout(location = 2) in vec2 TextureCoord;
out VertexData {
vec3 Position;
vec3 Normal;
vec2 TextureCoord;
} Output;
void main()
{
gl_Position = MVP * vec4(Position, 1.0);
Output.Position = Position;
Output.Normal = Normal;
Output.TextureCoord = TextureCoord;
}
+3
View File
@@ -1,6 +1,7 @@
#version 430
uniform mat4 MVP;
uniform mat4 DepthMVP;
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
@@ -10,6 +11,7 @@ out VertexData {
vec3 Position;
vec3 Normal;
vec2 TextureCoord;
vec3 ShadowCoord;
} Output;
void main()
@@ -19,4 +21,5 @@ void main()
Output.Position = Position;
Output.Normal = Normal;
Output.TextureCoord = TextureCoord;
Output.ShadowCoord = vec3(DepthMVP * vec4(Position, 1.0));
}
@@ -0,0 +1,24 @@
#version 430
layout(binding = 0) uniform sampler2D DepthTexture;
in VertexData {
vec3 Position;
vec2 TextureCoord;
} Input;
out vec4 FragmentColor;
float LinearizeDepth(float z)
{
float n = 0.1; // camera z near
float f = 10.0; // camera z far
return (2.0 * n) / (f + n - z * (f - n));
}
void main() {
float z = texture(DepthTexture, Input.TextureCoord).x;
vec4 color = vec4(z, z, z, 0);
FragmentColor = color;
}
@@ -0,0 +1,17 @@
#version 430
layout(location = 0) in vec3 Position;
layout(location = 2) in vec2 TextureCoord;
out VertexData {
vec3 Position;
vec2 TextureCoord;
} Output;
void main()
{
gl_Position = vec4(Position, 1.0);
Output.Position = Position;
Output.TextureCoord = TextureCoord;
}