From 7ae9635ce7e49c0444e4c9697b54460deff8400a Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 15 Jan 2015 15:27:18 -0800 Subject: [PATCH] Refactoring the code from directional into DeferredLighting.slh --- libraries/render-utils/src/DeferredBuffer.slh | 9 ++ .../render-utils/src/DeferredLighting.slh | 89 +++++++++++++++++++ .../src/DeferredLightingEffect.cpp | 6 ++ .../render-utils/src/DeferredLightingEffect.h | 19 ++++ .../render-utils/src/directional_light.slf | 11 +++ 5 files changed, 134 insertions(+) create mode 100755 libraries/render-utils/src/DeferredLighting.slh diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index da02ef750e..885fa96543 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -42,6 +42,10 @@ struct DeferredFragment { vec4 specularVal; vec4 position; vec3 normal; + vec3 diffuse; + float opacity; + vec3 specular; + float gloss; }; DeferredFragment unpackDeferredFragment(vec2 texcoord) { @@ -58,6 +62,11 @@ DeferredFragment unpackDeferredFragment(vec2 texcoord) { // Unpack the normal from the map frag.normal = normalize(frag.normalVal.xyz * 2.0 - vec3(1.0)); + frag.diffuse = frag.diffuseVal.xyz; + frag.opacity = frag.diffuseVal.w; + frag.specular = frag.specularVal.xyz; + frag.gloss = frag.specularVal.w; + return frag; } diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh new file mode 100755 index 0000000000..71021255dc --- /dev/null +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -0,0 +1,89 @@ + +<@if not DEFERRED_LIGHTING_SLH@> +<@def DEFERRED_LIGHTING_SLH@> + +struct SphericalHarmonics { + vec4 L00; + vec4 L1m1; + vec4 L10; + vec4 L11; + vec4 L2m2; + vec4 L2m1; + vec4 L20; + vec4 L21; + vec4 L22; +}; + +vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) { + + const float C1 = 0.429043; + const float C2 = 0.511664; + const float C3 = 0.743125; + const float C4 = 0.886227; + const float C5 = 0.247708; + + vec4 value = C1 * sh.L22 * (direction.x * direction.x - direction.y * direction.y) + + C3 * sh.L20 * direction.z * direction.z + + C4 * sh.L00 - C5 * sh.L20 + + 2.0 * C1 * ( sh.L2m2 * direction.x * direction.y + + sh.L21 * direction.x * direction.z + + sh.L2m1 * direction.y * direction.z ) + + 2.0 * C2 * ( sh.L11 * direction.x + + sh.L1m1 * direction.y + + sh.L10 * direction.z ) ; + return value; +} + +uniform SphericalHarmonics ambientSphere; + +vec3 evalAmbientSphereAndDirectionalColor(vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + // Ambient lighting + vec3 ambientLight = evalSphericalLight(ambientSphere, normal).xyz; + if (gl_FragCoord.x > 1024) { + ambientLight = gl_FrontLightProduct[0].ambient.rgb; + } + vec3 ambientColor = diffuseVal.rgb * ambientLight; + + // Diffuse Lighting + float diffuseDot = dot(frag.normal, gl_LightSource[0].position.xyz); + float facingLight = step(0.0, diffuseDot); + vec3 diffuseColor = diffuse * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuseDot * facingLight)); + + // compute the specular multiplier (sans exponent) + float specularPower = facingLight * max(0.0, + dot(normalize(gl_LightSource[0].position.xyz - normalize(position)), normal)); + vec3 specularColor = pow(specularPower, gloss * 128.0) * specular; + + // add specular contribution + return vec3(ambientColor + diffuseColor + specularColor); +} + + +vec3 evalAmbientAndDirectionalColor(vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + // Ambient lighting + vec3 ambientLight = gl_FrontLightProduct[0].ambient.rgb; + vec3 ambientColor = diffuseVal.rgb * ambientLight; + + // Diffuse + float diffuseDot = dot(frag.normal, gl_LightSource[0].position.xyz); + float facingLight = step(0.0, diffuseDot); + vec3 diffuseColor = diffuse * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuseDot * facingLight)); + + // compute the specular multiplier (sans exponent) + float specularPower = facingLight * max(0.0, + dot(normalize(gl_LightSource[0].position.xyz - normalize(position)), normal)); + vec3 specularColor = pow(specularPower, gloss * 128.0) * specular; + + // add specular contribution + return vec3(ambientColor + diffuseColor + specularColor); +} +<@endif@> \ No newline at end of file diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 3ce7c42a74..2d44e2c6bd 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -435,3 +435,9 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit locations.radius = program.uniformLocation("radius"); program.release(); } + +void DeferredLightingEffect::setAmbientLightMode(int preset) { + if ((preset >= -1) && (preset < NUM_PRESET)) { + _ambientLightMode = preset; + } +} \ No newline at end of file diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 810aebd508..2c5087c5cb 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -71,6 +71,23 @@ public: void prepare(); void render(); + enum AmbientLightPreset { + OLD_TOWN_SQUARE = 0, + GRACE_CATHEDRAL, + EUCALYPTUS_GROVE, + ST_PETERS_BASILICA, + UFFIZI_GALLERY, + GALILEOS_TOMB, + VINE_STREET_KITCHEN, + BREEZEWAY, + CAMPUS_SUNSET, + FUNSTON_BEACH_SUNSET, + + NUM_PRESET, + }; + + void setAmbientLightMode(int preset); + private: DeferredLightingEffect() { } virtual ~DeferredLightingEffect() { } @@ -126,6 +143,8 @@ private: QVector _postLightingRenderables; AbstractViewStateInterface* _viewState; + + int _ambientLightMode = -1; }; /// Simple interface for objects that require something to be rendered after deferred lighting. diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf index db963be913..733da47f8b 100644 --- a/libraries/render-utils/src/directional_light.slf +++ b/libraries/render-utils/src/directional_light.slf @@ -15,6 +15,8 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> +<@include DeferredLighting.slh@> + void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); @@ -26,6 +28,14 @@ void main(void) { if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { gl_FragColor = vec4(diffuseVal.rgb * specularVal.rgb, 1.0); } else { + glFragColor = vec4( evalAmbientAndDirectionalColor(frag.position.xyz, + frag.normal, + frag.diffuse, + frag.specular, + frag.gloss), + normalVal.a); + +/* // compute the base color based on OpenGL lighting model float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz); float facingLight = step(0.0, diffuse); @@ -39,5 +49,6 @@ void main(void) { // add specular contribution vec4 specularColor = specularVal; gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); +*/ } }