shadow works

This commit is contained in:
VB
2017-07-02 00:10:45 +02:00
parent 4b3d038c2b
commit 3a02a65ada
10 changed files with 170 additions and 33 deletions

View File

@@ -23,10 +23,12 @@ struct light_s
in vec3 f_normal;
in vec2 f_coord;
in vec3 f_pos;
in vec4 f_light_pos;
out vec4 color;
uniform sampler2D tex;
uniform sampler2D shadowmap;
uniform vec3 ambient;
uniform vec3 emission;
@@ -37,6 +39,16 @@ uniform float specular;
uniform light_s lights[8];
uniform uint lights_count;
float calc_shadow()
{
vec3 coords = f_light_pos.xyz / f_light_pos.w;
coords = coords * 0.5 + 0.5;
float closest_depth = texture(shadowmap, coords.xy).r;
float current_depth = coords.z;
float shadow = current_depth > closest_depth ? 0.0 : 1.0;
return shadow;
}
vec3 apply_fog(vec3 color)
{
float sun_amount = 0.0;
@@ -90,6 +102,7 @@ float calc_dir_light(light_s light)
void main()
{
float shadow = calc_shadow();
vec3 result = ambient * 0.3 + emission;
for (uint i = 0U; i < lights_count; i++)
{
@@ -103,7 +116,7 @@ void main()
else if (light.type == LIGHT_DIR)
part = calc_dir_light(light);
result += light.color * part;
result += light.color * part * shadow;
}
vec4 tex_color = texture(tex, f_coord);

7
shaders/empty.frag Normal file
View File

@@ -0,0 +1,7 @@
#version 330
in vec3 f_pos;
void main()
{
}

View File

@@ -7,7 +7,9 @@ layout (location = 2) in vec2 v_coord;
out vec3 f_normal;
out vec2 f_coord;
out vec3 f_pos;
out vec4 f_light_pos;
uniform mat4 lightview;
uniform mat4 modelview;
uniform mat3 modelviewnormal;
uniform mat4 projection;
@@ -18,4 +20,6 @@ void main()
f_normal = modelviewnormal * v_normal;
f_coord = v_coord;
f_pos = vec3(modelview * vec4(v_vert, 1.0f));
//f_light_pos = lightview * vec4(f_pos, 1.0f);
f_light_pos = lightview * vec4(f_pos, 1.0f);
}

13
shaders/shadowmap.vert Normal file
View File

@@ -0,0 +1,13 @@
#version 330
layout (location = 0) in vec3 v_vert;
out vec3 f_pos;
uniform mat4 modelview;
uniform mat4 projection;
void main()
{
gl_Position = (projection * modelview) * vec4(v_vert, 1.0f);
}