adding Light and replace glLights also for the first deferred pass

This commit is contained in:
Sam Gateau 2015-02-05 14:10:06 -08:00
parent dd0d45cdc5
commit 9b0036d010
15 changed files with 202 additions and 149 deletions

View file

@ -48,7 +48,7 @@ void Light::setOrientation(const glm::quat& orientation) {
}
void Light::setDirection(const Vec3& direction) {
editSchema()._direction = direction;
editSchema()._direction = glm::normalize(direction);
}
const Vec3& Light::getDirection() const {
@ -56,7 +56,7 @@ const Vec3& Light::getDirection() const {
}
void Light::setColor(const Color& color) {
editSchema()._color = color;
editSchema()._color = glm::normalize(color);
}
void Light::setIntensity(float intensity) {
@ -89,3 +89,9 @@ void Light::setSpotExponent(float exponent) {
editSchema()._spot.w = exponent;
}
void Light::setShowVolumeContour(float show) {
if (show <= 0.f) {
show = 0.0f;
}
editSchema()._control.w = show;
}

View file

@ -84,6 +84,10 @@ public:
void setSpotExponent(float exponent);
float getSpotExponent() const { return getSchema()._spot.w; }
// For editing purpose, show the light volume contour
void setShowVolumeContour(float show);
float getShowVolumeContour() const { return getSchema()._control.w; }
// Schema to access the attribute values of the light
class Schema {
public:

View file

@ -0,0 +1,122 @@
<!
// DeferredGlobalLight.slh
// libraries/render-utils/src
//
// Created by Sam Gateau on 2/5/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_GLOBAL_LIGHT_SLH@>
<@def DEFERRED_GLOBAL_LIGHT_SLH@>
<@include DeferredLighting.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;
}
// Need one SH
uniform SphericalHarmonics ambientSphere;
// Everything about light
<@include Light.slh@>
// The view Matrix
uniform mat4 invViewMat;
vec3 evalAmbientColor(vec3 normal, vec3 diffuse, vec3 specular, float gloss) {
return diffuse.rgb * gl_FrontLightProduct[0].ambient.rgb;
}
vec3 evalAmbientSphereColor(vec3 normal, vec3 diffuse, vec3 specular, float gloss) {
vec3 ambientLight = 0.5 * evalSphericalLight(ambientSphere, normal).xyz;
return diffuse.rgb * ambientLight;
}
vec3 evalDirectionalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) {
// Need the light now
Light light = getLight();
vec4 shading = evalFragShading(normal, getLightDirection(light), normalize(position), specular, gloss);
return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light);
// return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * gl_FrontLightProduct[0].diffuse.rgb;
}
vec3 evalAmbienGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) {
vec3 color = evalAmbientColor(normal, diffuse, specular, gloss)
+ evalDirectionalColor(shadowAttenuation,
position,
normal,
diffuse,
specular,
gloss);
return color;
}
vec3 evalAmbienSphereGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) {
vec3 color = evalAmbientSphereColor(normal, diffuse, specular, gloss)
+ evalDirectionalColor(shadowAttenuation,
position,
normal,
diffuse,
specular,
gloss);
return color;
}
vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, vec3 lightmap) {
Light light = getLight();
float diffuseDot = dot(normal, getLightDirection(light));
// need to catch normals perpendicular to the projection plane hence the magic number for the threshold
// it should be just 0, but we have innacurracy so we need to overshoot
const float PERPENDICULAR_THRESHOLD = -0.005;
float facingLight = step(PERPENDICULAR_THRESHOLD, diffuseDot);
// 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 * lightmap;
// ambient is a tiny percentage of the lightmap and only when in the shadow
vec3 ambientLight = (1 - lightAttenuation) * 0.5 * lightmap;
return diffuse * (ambientLight + diffuseLight);
}
<@endif@>

View file

@ -11,53 +11,6 @@
<@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;
// Everything about light
<@include Light.slh@>
vec3 evalAmbientColor(vec3 normal, vec3 diffuse, vec3 specular, float gloss) {
return diffuse.rgb * gl_FrontLightProduct[0].ambient.rgb;
}
vec3 evalAmbientSphereColor(vec3 normal, vec3 diffuse, vec3 specular, float gloss) {
vec3 ambientLight = 0.5 * evalSphericalLight(ambientSphere, normal).xyz;
return diffuse.rgb * ambientLight;
}
// Frag Shading returns the diffuse amount as W and the specular rgb as xyz
vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) {
// Diffuse Lighting
@ -74,33 +27,4 @@ vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 s
return vec4(reflect, diffuse);
}
vec3 evalDirectionalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) {
vec4 shading = evalFragShading(normal, gl_LightSource[0].position.xyz, normalize(position), specular, gloss);
return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * gl_FrontLightProduct[0].diffuse.rgb;
}
vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, vec3 lightmap) {
float diffuseDot = dot(normal, gl_LightSource[0].position.xyz);
// need to catch normals perpendicular to the projection plane hence the magic number for the threshold
// it should be just 0, but we have innacurracy so we need to overshoot
const float PERPENDICULAR_THRESHOLD = -0.005;
float facingLight = step(PERPENDICULAR_THRESHOLD, diffuseDot);
// 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 * lightmap;
// ambient is a tiny percentage of the lightmap and only when in the shadow
vec3 ambientLight = (1 - lightAttenuation) * 0.5 * lightmap;
return diffuse * (ambientLight + diffuseLight);
}
<@endif@>

View file

@ -209,6 +209,17 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) {
loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations);
loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations);
// Allocate 2 global lights representing the GLobal Directional light casting shadow (the sun) and the ambient light
_globalLights.push_back(0);
_allocatedLights.push_back(model::LightPointer(new model::Light()));
model::LightPointer lp = _allocatedLights[0];
lp->setDirection(-glm::vec3(1.0f, 1.0f, 1.0f));
lp->setColor(glm::vec3(1.0f));
lp->setIntensity(1.0f);
lp->setType(model::Light::SUN);
}
void DeferredLightingEffect::bindSimpleProgram() {
@ -264,51 +275,27 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu
const glm::vec3& diffuse, const glm::vec3& specular, float constantAttenuation, float linearAttenuation,
float quadraticAttenuation, const glm::vec3& direction, float exponent, float cutoff) {
int lightID = _pointLights.size() + _spotLights.size();
int lightID = _pointLights.size() + _spotLights.size() + _globalLights.size();
if (lightID >= _allocatedLights.size()) {
_allocatedLights.push_back(model::LightPointer(new model::Light()));
}
model::LightPointer lp = _allocatedLights[lightID];
if (exponent == 0.0f && cutoff == PI) {
PointLight light;
light.position = glm::vec4(position, 1.0f);
light.radius = radius;
light.ambient = glm::vec4(ambient, 1.0f);
light.diffuse = glm::vec4(diffuse, 1.0f);
light.specular = glm::vec4(specular, 1.0f);
light.constantAttenuation = constantAttenuation;
light.linearAttenuation = linearAttenuation;
lp->setPosition(position);
lp->setMaximumRadius(radius);
lp->setColor(diffuse);
lp->setIntensity(1.0f);
lp->setShowVolumeContour(quadraticAttenuation);
lp->setPosition(position);
lp->setMaximumRadius(radius);
lp->setColor(diffuse);
lp->setIntensity(1.0f);
if (exponent == 0.0f && cutoff == PI) {
lp->setType(model::Light::POINT);
_pointLights.push_back(lightID);
} else {
SpotLight light;
light.position = glm::vec4(position, 1.0f);
light.radius = radius;
light.ambient = glm::vec4(ambient, 1.0f);
light.diffuse = glm::vec4(diffuse, 1.0f);
light.specular = glm::vec4(specular, 1.0f);
light.constantAttenuation = constantAttenuation;
light.linearAttenuation = linearAttenuation;
light.direction = direction;
light.exponent = exponent;
light.cutoff = cutoff;
lp->setPosition(position);
lp->setDirection(direction);
lp->setMaximumRadius(radius);
lp->setSpotAngle(cutoff);
lp->setSpotExponent(exponent);
lp->setColor(diffuse);
lp->setIntensity(1.0f);
lp->setType(model::Light::SPOT);
_spotLights.push_back(lightID);
}
}
@ -368,6 +355,10 @@ void DeferredLightingEffect::render() {
float tMin = viewport[VIEWPORT_Y_INDEX] / (float)primaryFBO->height();
float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)primaryFBO->height();
// Fetch the ViewMatrix;
glm::mat4 invViewMat;
_viewState->getViewTransform().getMatrix(invViewMat);
ProgramObject* program = &_directionalLight;
const LightLocations* locations = &_directionalLightLocations;
bool shadowsEnabled = _viewState->getShadowsEnabled();
@ -418,6 +409,17 @@ void DeferredLightingEffect::render() {
}
}
{
auto light = _allocatedLights[_globalLights.front()];
if (locations->lightBufferUnit >= 0) {
gpu::Batch batch;
batch.setUniformBuffer(locations->lightBufferUnit, light->getSchemaBuffer());
gpu::GLBackend::renderBatch(batch);
}
glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
}
float left, right, bottom, top, nearVal, farVal;
glm::vec4 nearClipPlane, farClipPlane;
_viewState->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane);
@ -457,8 +459,6 @@ void DeferredLightingEffect::render() {
const glm::vec3& eyePoint = _viewState->getCurrentViewFrustum()->getPosition();
float nearRadius = glm::distance(eyePoint, _viewState->getCurrentViewFrustum()->getNearTopLeft());
glm::mat4 invViewMat;
_viewState->getViewTransform().getMatrix(invViewMat);
auto geometryCache = DependencyManager::get<GeometryCache>();

View file

@ -91,7 +91,7 @@ public:
void setAmbientLightMode(int preset);
private:
DeferredLightingEffect() { }
DeferredLightingEffect() {}
virtual ~DeferredLightingEffect() { }
class LightLocations {
@ -154,10 +154,9 @@ private:
typedef std::vector< model::LightPointer > Lights;
Lights _allocatedLights;
std::vector<int> _globalLights;
std::vector<int> _pointLights;
std::vector<int> _spotLights;
// QVector<PointLight> _pointLights;
// QVector<SpotLight> _spotLights;
QVector<PostLightingRenderable*> _postLightingRenderables;
AbstractViewStateInterface* _viewState;

View file

@ -61,6 +61,10 @@ float getLightAttenuationCutoff(Light l) {
return l._attenuation.z;
}
float getLightShowContour(Light l) {
return l._control.w;
}
<@if GLPROFILE == PC_GL@>
uniform lightBuffer {
Light light;

View file

@ -15,7 +15,7 @@
// Everything about deferred buffer
<@include DeferredBuffer.slh@>
<@include DeferredLighting.slh@>
<@include DeferredGlobalLight.slh@>
void main(void) {
DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st);
@ -31,8 +31,7 @@ void main(void) {
gl_FragColor = vec4(color, 1.0);
} else {
vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(1.0,
vec3 color = evalAmbienSphereGlobalColor(1.0,
frag.position.xyz,
frag.normal,
frag.diffuse,

View file

@ -15,7 +15,7 @@
// Everything about deferred buffer
<@include DeferredBuffer.slh@>
<@include DeferredLighting.slh@>
<@include DeferredGlobalLight.slh@>
// Everything about shadow
<@include Shadow.slh@>
@ -36,8 +36,7 @@ void main(void) {
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(shadowAttenuation,
vec3 color = evalAmbienSphereGlobalColor(shadowAttenuation,
frag.position.xyz,
frag.normal,
frag.diffuse,

View file

@ -15,7 +15,7 @@
// Everything about deferred buffer
<@include DeferredBuffer.slh@>
<@include DeferredLighting.slh@>
<@include DeferredGlobalLight.slh@>
// Everything about shadow
<@include Shadow.slh@>
@ -37,8 +37,7 @@ void main(void) {
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(shadowAttenuation,
vec3 color = evalAmbienSphereGlobalColor(shadowAttenuation,
frag.position.xyz,
frag.normal,
frag.diffuse,

View file

@ -15,7 +15,7 @@
// Everything about deferred buffer
<@include DeferredBuffer.slh@>
<@include DeferredLighting.slh@>
<@include DeferredGlobalLight.slh@>
void main(void) {
DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st);
@ -29,8 +29,7 @@ void main(void) {
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(1.0,
vec3 color = evalAmbienGlobalColor(1.0,
frag.position.xyz,
frag.normal,
frag.diffuse,

View file

@ -15,7 +15,7 @@
// Everything about deferred buffer
<@include DeferredBuffer.slh@>
<@include DeferredLighting.slh@>
<@include DeferredGlobalLight.slh@>
// Everything about shadow
<@include Shadow.slh@>
@ -36,8 +36,7 @@ void main(void) {
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(shadowAttenuation,
vec3 color = evalAmbienGlobalColor(shadowAttenuation,
frag.position.xyz,
frag.normal,
frag.diffuse,

View file

@ -15,7 +15,7 @@
// Everything about deferred buffer
<@include DeferredBuffer.slh@>
<@include DeferredLighting.slh@>
<@include DeferredGlobalLight.slh@>
// Everything about shadow
<@include Shadow.slh@>
@ -37,8 +37,7 @@ void main(void) {
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(shadowAttenuation,
vec3 color = evalAmbienGlobalColor(shadowAttenuation,
frag.position.xyz,
frag.normal,
frag.diffuse,

View file

@ -64,12 +64,12 @@ void main(void) {
vec3 fragColor = shading.w * (frag.diffuse + shading.xyz);
gl_FragColor = vec4(fragColor * radialAttenuation * getLightColor(light) * getLightIntensity(light), 0.0);
<@if SHOW_LIGHT_CONTOUR@>
// Show edge
float edge = abs(2.0 * ((getLightRadius(light) - fragLightDistance) / (0.1)) - 1.0);
if (edge < 1) {
float edgeCoord = exp2(-8.0*edge*edge);
gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0);
if (getLightShowContour(light) > 0.0) {
// Show edge
float edge = abs(2.0 * ((getLightRadius(light) - fragLightDistance) / (0.1)) - 1.0);
if (edge < 1) {
float edgeCoord = exp2(-8.0*edge*edge);
gl_FragColor = vec4(edgeCoord * edgeCoord * getLightShowContour(light) * getLightColor(light), 0.0);
}
}
<@endif@>
}

View file

@ -72,16 +72,16 @@ void main(void) {
vec3 fragColor = shading.w * (frag.diffuse + shading.xyz);
gl_FragColor = vec4(fragColor * angularAttenuation * radialAttenuation * getLightColor(light) * getLightIntensity(light), 0.0);
<@if SHOW_LIGHT_CONTOUR@>
// Show edges
float edgeDistR = (getLightRadius(light) - fragLightDistance);
float edgeDistS = dot(fragLightDistance * vec2(cosSpotAngle, sqrt(1.0 - cosSpotAngle * cosSpotAngle)), -getLightSpotOutsideNormal2(light));
float edgeDist = min(edgeDistR, edgeDistS);
float edge = abs(2.0 * (edgeDist / (0.1)) - 1.0);
if (edge < 1) {
float edgeCoord = exp2(-8.0*edge*edge);
gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0);
if (getLightShowContour(light) > 0.0) {
// Show edges
float edgeDistR = (getLightRadius(light) - fragLightDistance);
float edgeDistS = dot(fragLightDistance * vec2(cosSpotAngle, sqrt(1.0 - cosSpotAngle * cosSpotAngle)), -getLightSpotOutsideNormal2(light));
float edgeDist = min(edgeDistR, edgeDistS);
float edge = abs(2.0 * (edgeDist / (0.1)) - 1.0);
if (edge < 1) {
float edgeCoord = exp2(-8.0*edge*edge);
gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0);
}
}
<@endif@>
}