<@include Config.slh@> <$VERSION_HEADER$> // Generated on <$_SCRIBE_DATE$> // // directional_light.frag // fragment shader // // Created by Andrzej Kapolka on 9/3/14. // Copyright 2014 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // // Everything about deferred buffer <@include DeferredBuffer.slh@> // Everything about shadow <@include Shadow.slh@> void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); vec4 normalVal = frag.normalVal; vec4 diffuseVal = frag.diffuseVal; vec4 specularVal = frag.specularVal; // Eval shadow Texcoord and then Attenuation vec4 shadowTexcoord = evalShadowTexcoord(frag.position); float shadowAttenuation = evalShadowAttenuation(shadowTexcoord); // how much this fragment faces the light direction float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz); // Light mapped or not ? if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { normalVal.a = 0.0; // need to catch normals perpendicular to the projection plane hence the magic number for the threshold // it should be just 0, be we have innacurracy so we need to overshoot const float PERPENDICULAR_THRESHOLD = -0.005; float facingLight = step(PERPENDICULAR_THRESHOLD, diffuse); // evaluate the shadow test but only relevant for light facing fragments float lightAttenuation = (1 - facingLight) + facingLight * shadowAttenuation; // diffuse light is the lightmap dimmed by shadow vec3 diffuseLight = lightAttenuation * specularVal.rgb; // ambient is a tiny percentage of the lightmap and only when in the shadow vec3 ambientLight = (1 - lightAttenuation) * 0.5 * specularVal.rgb; gl_FragColor = vec4(diffuseVal.rgb * (ambientLight + diffuseLight), 1.0); } else { // average values from the shadow map float facingLight = step(0.0, diffuse) * shadowAttenuation; // compute the base color based on OpenGL lighting model vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); // compute the specular multiplier (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(frag.position.xyz)), frag.normal)); // add specular contribution vec4 specularColor = specularVal; gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); } }