Refactoring the code from directional into DeferredLighting.slh

This commit is contained in:
Sam Gateau 2015-01-15 15:27:18 -08:00
parent d759f4c255
commit 7ae9635ce7
5 changed files with 134 additions and 0 deletions

View file

@ -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;
}

View file

@ -0,0 +1,89 @@
<!
// DeferredLighting.slh
// libraries/render-utils/src
//
// Created by Sam Gateau on 1/15/15.
// Copyright 2013 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
!>
<@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@>

View file

@ -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;
}
}

View file

@ -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<PostLightingRenderable*> _postLightingRenderables;
AbstractViewStateInterface* _viewState;
int _ambientLightMode = -1;
};
/// Simple interface for objects that require something to be rendered after deferred lighting.

View file

@ -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);
*/
}
}