2018-03-11 11:58:50 +01:00
|
|
|
#version 150 core
|
|
|
|
|
|
|
|
uniform vec4 uColor;
|
|
|
|
uniform vec3 uLightDirection;
|
2018-03-11 14:16:23 +01:00
|
|
|
uniform vec3 uViewPosition;
|
2018-03-15 11:01:55 +01:00
|
|
|
uniform sampler2D uAlbedoTexture;
|
|
|
|
uniform sampler2D uShadowMap;
|
2018-03-11 11:58:50 +01:00
|
|
|
|
2018-03-11 14:16:23 +01:00
|
|
|
in vec3 ioFragPosition;
|
2018-03-15 11:01:55 +01:00
|
|
|
in vec3 ioFragNormal;
|
|
|
|
in vec2 ioFragTexCoords;
|
|
|
|
smooth in vec4 ioFragColor;
|
|
|
|
in vec4 ioFragPosLightSpace;
|
2018-03-11 11:58:50 +01:00
|
|
|
|
|
|
|
out vec4 outColor;
|
|
|
|
|
2018-03-15 11:01:55 +01:00
|
|
|
float ShadowCalculation(vec4 frag_pos_light_space) {
|
|
|
|
vec3 projected_coordinates = frag_pos_light_space.xyz / frag_pos_light_space.w;
|
|
|
|
projected_coordinates = projected_coordinates * 0.5 + 0.5;
|
|
|
|
|
|
|
|
float closest_depth = texture(uShadowMap, projected_coordinates.xy).r;
|
|
|
|
float current_depth = projected_coordinates.z;
|
|
|
|
|
2018-03-15 23:00:53 +01:00
|
|
|
float bias = max(0.05 * (1.0 - dot(ioFragNormal, uLightDirection)), 0.005);
|
|
|
|
bias = 0.0;
|
2018-03-15 11:54:00 +01:00
|
|
|
return current_depth - bias > closest_depth ? 1.0 : 0.0;
|
2018-03-15 11:01:55 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 11:58:50 +01:00
|
|
|
void main() {
|
2018-03-15 11:01:55 +01:00
|
|
|
vec4 albedo_color = texture(uAlbedoTexture, ioFragTexCoords) * ioFragColor * uColor;
|
|
|
|
|
2018-03-11 14:16:23 +01:00
|
|
|
// ambient lighting
|
2018-03-15 23:00:53 +01:00
|
|
|
float ambient_strength = 0.2;
|
2018-03-15 11:01:55 +01:00
|
|
|
vec4 ambient = ambient_strength * albedo_color;
|
2018-03-11 11:58:50 +01:00
|
|
|
|
2018-03-11 14:16:23 +01:00
|
|
|
// diffuse lighting
|
2018-03-15 11:01:55 +01:00
|
|
|
vec3 normal_dir = normalize(ioFragNormal);
|
2018-03-11 11:58:50 +01:00
|
|
|
vec3 light_dir = normalize(uLightDirection);
|
2018-03-11 17:30:56 +01:00
|
|
|
float diff = max(dot(normal_dir, light_dir), 0.0);
|
2018-03-15 11:01:55 +01:00
|
|
|
vec4 diffuse = diff * albedo_color;
|
2018-03-11 11:58:50 +01:00
|
|
|
|
2018-03-11 14:16:23 +01:00
|
|
|
// specular lighting
|
|
|
|
vec3 view_dir = normalize(uViewPosition - ioFragPosition);
|
2018-03-11 17:30:56 +01:00
|
|
|
vec3 halfway_dir = normalize(light_dir + view_dir);
|
2018-03-11 14:16:23 +01:00
|
|
|
|
2018-03-11 17:30:56 +01:00
|
|
|
float spec = pow(max(dot(normal_dir, halfway_dir), 0.0), 32);
|
|
|
|
vec4 specular = spec * vec4(0.5);
|
2018-03-11 14:16:23 +01:00
|
|
|
|
2018-03-15 11:01:55 +01:00
|
|
|
// shadow
|
|
|
|
float shadow = ShadowCalculation(ioFragPosLightSpace);
|
|
|
|
outColor = ambient + (1.0 - shadow) * (diffuse + specular);
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// vec3 projected_coordinates = ioFragPosLightSpace.xyz / ioFragPosLightSpace.w;
|
|
|
|
// projected_coordinates = projected_coordinates * 0.5 + 0.5;
|
|
|
|
// float shadow_map_value = texture(uShadowMap, projected_coordinates.xy).r;
|
|
|
|
// outColor = shadow_map_value * vec4(1.0, 1.0, 1.0, 1.0);
|
|
|
|
//
|
|
|
|
// outColor = vec4(vec3(1.0f - shadow_map_value), 1.0);
|
|
|
|
|
|
|
|
// outColor = (shadow) * vec4(1.0, 1.0, 1.0, 1.0);
|
|
|
|
// outColor = ioFragPosLightSpace / ioFragPosLightSpace.w;
|
|
|
|
// outColor = ambient + diffuse + specular;
|
2018-03-11 11:58:50 +01:00
|
|
|
}
|