Add specular component
[opengl.git] / pbrfrag.glsl
index 16a9fa7a1ba6128257bff2175ee0b24a8868d407..99523ec604f99b0fd7673e56848d14a837328c13 100644 (file)
@@ -13,6 +13,8 @@ uniform sampler2D metallicMap;
 uniform sampler2D roughnessMap;
 uniform sampler2D aoMap;
 uniform samplerCube irradianceMap;
+uniform samplerCube prefilterMap;
+uniform sampler2D brdfMap;
 
 out vec4 fragColor;
 
@@ -76,6 +78,8 @@ void main() {
 
        vec3 F0 = mix(vec3(0.04), albedo, metallic);
 
+       vec3 R = reflect(-V, N);
+
        // reflectance
        vec3 Lo = vec3(0.f);
        for (int i = 0; i < lightPositions.length(); i++) {
@@ -102,9 +106,20 @@ void main() {
                Lo += (kD * albedo / PI + specular) * radiance * mdot(N, L);
        }
 
-       vec3 kD = 1.f - fresnelSchlickRoughness(mdot(N, V), F0, roughness);
-       vec3 diffuse = texture(irradianceMap, N).rgb * albedo;
-       vec3 ambient = (kD * diffuse) * ao;
+       vec3 F = fresnelSchlickRoughness(mdot(N, V), F0, roughness);
+       
+       vec3 kS = F;
+       vec3 kD = (1.f - kS) * (1.f - metallic);
+
+       vec3 irradiance = texture(irradianceMap, N).rgb;
+       vec3 diffuse = irradiance * albedo;
+
+       const float maxReflectionLoD = 4.f;
+       vec3 prefilteredColor = textureLod(prefilterMap, R, roughness * maxReflectionLoD).rgb;
+       vec2 envBRDF = texture(brdfMap, vec2(mdot(N, V), roughness)).rg;
+       vec3 specular = prefilteredColor * (F * envBRDF.x + envBRDF.y);
+
+       vec3 ambient = (kD * diffuse + specular) * ao;
        vec3 color = ambient + Lo;
 
        color = color / (color + vec3(1.f)); // map to HDR