Visually upgraded deferred rendering example with moving lights, multiple meshes and normal mapping

This commit is contained in:
saschawillems 2016-07-03 21:32:35 +02:00
parent f06ffae58e
commit 18013c44e0
12 changed files with 247 additions and 196 deletions

View file

@ -12,12 +12,9 @@ layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outFragcolor;
struct Light {
vec4 position;
vec4 color;
vec4 position;
vec3 color;
float radius;
float quadraticFalloff;
float linearFalloff;
float _pad;
};
layout (binding = 4) uniform UBO
@ -29,40 +26,50 @@ layout (binding = 4) uniform UBO
void main()
{
// Get G-Buffer values
vec3 fragPos = texture(samplerposition, inUV).rgb;
vec3 normal = texture(samplerNormal, inUV).rgb;
vec4 albedo = texture(samplerAlbedo, inUV);
#define lightCount 5
#define ambient 0.05
#define specularStrength 0.15
// Get G-Buffer values
vec3 fragPos = texture(samplerposition, inUV).rgb;
vec3 normal = texture(samplerNormal, inUV).rgb;
vec4 albedo = texture(samplerAlbedo, inUV);
#define lightCount 6
#define ambient 0.0
// Ambient part
vec3 fragcolor = albedo.rgb * ambient;
vec3 fragcolor = albedo.rgb * ambient;
vec3 viewVec = normalize(ubo.viewPos.xyz - fragPos);
for(int i = 0; i < lightCount; ++i)
{
// Distance from light to fragment position
float dist = length(ubo.lights[i].position.xyz - fragPos);
for(int i = 0; i < lightCount; ++i)
{
// Vector to light
vec3 L = ubo.lights[i].position.xyz - fragPos;
// Distance from light to fragment position
float dist = length(L);
// Viewer to fragment
vec3 V = ubo.viewPos.xyz - fragPos;
V = normalize(V);
if(dist < ubo.lights[i].radius)
{
// Get vector from current light source to fragment position
vec3 lightVec = normalize(ubo.lights[i].position.xyz - fragPos);
// Diffuse part
vec3 diffuse = max(dot(normal, lightVec), 0.0) * albedo.rgb * ubo.lights[i].color.rgb;
// Specular part (specular texture part stored in albedo alpha channel)
vec3 halfVec = normalize(lightVec + viewVec);
vec3 specular = ubo.lights[i].color.rgb * pow(max(dot(normal, halfVec), 0.0), 16.0) * albedo.a * specularStrength;
// Attenuation with linearFalloff and quadraticFalloff falloff
float attenuation = 1.0 / (1.0 + ubo.lights[i].linearFalloff * dist + ubo.lights[i].quadraticFalloff * dist * dist);
fragcolor += (diffuse + specular) * attenuation;
}
}
//if(dist < ubo.lights[i].radius)
{
// Light to fragment
L = normalize(L);
// Attenuation
float atten = ubo.lights[i].radius / (pow(dist, 2.0) + 1.0);
// Diffuse part
vec3 N = normalize(normal);
float NdotL = max(0.0, dot(N, L));
vec3 diff = ubo.lights[i].color * albedo.rgb * NdotL * atten;
// Specular part
// Specular map values are stored in alpha of albedo mrt
vec3 R = reflect(-L, N);
float NdotR = max(0.0, dot(R, V));
vec3 spec = ubo.lights[i].color * albedo.a * pow(NdotR, 16.0) * atten;
fragcolor += diff + spec;
}
}
outFragcolor = vec4(fragcolor, 1.0);
}