Shadow mapping

This commit is contained in:
2014-11-18 19:00:51 +01:00
parent 5f388a1723
commit 701ccce857
9 changed files with 258 additions and 17 deletions

5
shaders/shadow.fs Normal file
View File

@@ -0,0 +1,5 @@
#version 330 core
void main(void) {
// Only standard depth calc.
}

12
shaders/shadow.vs Normal file
View File

@@ -0,0 +1,12 @@
#version 330 core
#extension GL_ARB_shading_language_420pack : enable
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
layout(location = 0) in vec3 vertex;
void main(void) {
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertex, 1.0);
}

View File

@@ -1,16 +1,52 @@
#version 330 core
const uint lights = 2u;
uniform sampler2D texBase;
uniform samplerCubeShadow texShadowMaps[lights];
float VectorToDepth (vec3 Vec)
{
vec3 AbsVec = abs(Vec);
float LocalZcomp = max(AbsVec.x, max(AbsVec.y, AbsVec.z));
// Replace f and n with the far and near plane values you used when
// you drew your cube map.
const float f = 128.0;
const float n = 1.0;
float NormZComp = (f+n) / (f-n) - (2*f*n)/(f-n)/LocalZcomp;
return (NormZComp + 1.0) * 0.5;
}
in vec2 fragTC;
//in vec3 fragNorm;
//in vec3 light0Vect;
in vec3 light;
in vec3 lightColors[lights];
in vec3 lightVecs[lights];
out vec4 color;
const float bias = 0.00;
void main(void) {
vec4 texColor = texture(texBase, fragTC);
color = texColor*vec4(light, 1.0)+texColor*0.05;
vec4 col = vec4(0.0, 0.0, 0.0, 0.0);
{
float depth = VectorToDepth(lightVecs[0])-bias;
vec3 nlv = normalize(lightVecs[0]);
float depth_compare = texture(texShadowMaps[0], vec4(nlv, depth));
// color = vec4(depth_compare, 0.0, 0.0, 1.0);
col += texColor*vec4(lightColors[0], 1.0)*depth_compare;
}
{
float depth = VectorToDepth(lightVecs[1])-bias;
vec3 nlv = normalize(lightVecs[1]);
float depth_compare = texture(texShadowMaps[1], vec4(nlv, depth));
// color = vec4(depth_compare, 0.0, 0.0, 1.0);
col += texColor*vec4(lightColors[1], 1.0)*depth_compare;
}
color = col+texColor*0.05;
}

View File

@@ -19,7 +19,8 @@ layout(location = 1) in vec2 vertexTC;
layout(location = 2) in vec3 vertexNorm;
out vec2 fragTC;
out vec3 light;
out vec3 lightColors[lights];
out vec3 lightVecs[lights];
void main(void) {
vec4 vertex_Pos = model_matrix * vec4(vertex, 1.0);
@@ -28,12 +29,13 @@ void main(void) {
fragTC = vertexTC;
vec3 vertexNorm_trans = (model_matrix*vec4(vertexNorm, 0.0)).xyz;
light = vec3(0.0, 0.0, 0.0);
for (uint i = 0u;i < lights;++i) {
vec3 lightVec = lightPos[i]-vertex_Pos.xyz;
lightVecs[i] = -lightVec;
vec3 lightNorm = normalize(lightVec);
float lightDist = length(lightVec);
light += lightColor[i]*clamp(dot(vertexNorm_trans, lightNorm), 0.0, 1.0)*
clamp(lightIntensity[i]/(lightDist*lightDist), 0.0, 1.0);
lightColors[i] = lightColor[i]*clamp(dot(vertexNorm_trans, lightNorm), 0.0, 1.0)*
clamp(lightIntensity[i]/(lightDist*lightDist), 0.0, 1.0);
}
}