Adding ambient sphere in the lighting equation and menu to control te presets

This commit is contained in:
Sam Gateau 2015-01-16 09:38:53 -08:00
parent 0fdc9c1855
commit d1fb071208
13 changed files with 469 additions and 181 deletions

View file

@ -2797,7 +2797,7 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs
}
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
DependencyManager::get<DeferredLightingEffect>()->prepare();
if (!selfAvatarOnly) {
@ -2848,6 +2848,8 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs
{
DependencyManager::get<DeferredLightingEffect>()->setAmbientLightMode(getRenderAmbientLight());
PROFILE_RANGE("DeferredLighting");
PerformanceTimer perfTimer("lighting");
DependencyManager::get<DeferredLightingEffect>()->render();
@ -3973,3 +3975,31 @@ float Application::getRenderResolutionScale() const {
return 1.0f;
}
}
int Application::getRenderAmbientLight() const {
if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLightGlobal)) {
return -1;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight0)) {
return 0;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight1)) {
return 1;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight2)) {
return 2;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight3)) {
return 3;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight4)) {
return 4;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight5)) {
return 5;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight6)) {
return 6;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight7)) {
return 7;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight8)) {
return 8;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight9)) {
return 9;
} else {
return -1;
}
}

View file

@ -287,6 +287,7 @@ public:
bool isLookingAtMyAvatar(Avatar* avatar);
float getRenderResolutionScale() const;
int getRenderAmbientLight() const;
unsigned int getRenderTargetFramerate() const;
bool isVSyncOn() const;

View file

@ -343,6 +343,21 @@ Menu::Menu() :
addCheckableActionToQMenuAndActionHash(renderOptionsMenu, MenuOption::AmbientOcclusion);
addCheckableActionToQMenuAndActionHash(renderOptionsMenu, MenuOption::DontFadeOnOctreeServerChanges);
addCheckableActionToQMenuAndActionHash(renderOptionsMenu, MenuOption::DisableAutoAdjustLOD);
QMenu* ambientLightMenu = renderOptionsMenu->addMenu(MenuOption::RenderAmbientLight);
QActionGroup* ambientLightGroup = new QActionGroup(ambientLightMenu);
ambientLightGroup->setExclusive(true);
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLightGlobal, 0, true));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight0, 0, false));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight1, 0, false));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight2, 0, false));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight3, 0, false));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight4, 0, false));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight5, 0, false));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight6, 0, false));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight7, 0, false));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight8, 0, false));
ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight9, 0, false));
QMenu* shadowMenu = renderOptionsMenu->addMenu("Shadows");
QActionGroup* shadowGroup = new QActionGroup(shadowMenu);

View file

@ -440,6 +440,18 @@ namespace MenuOption {
const QString RenderResolutionHalf = "1/2";
const QString RenderResolutionThird = "1/3";
const QString RenderResolutionQuarter = "1/4";
const QString RenderAmbientLight = "Ambient Light";
const QString RenderAmbientLightGlobal = "Global";
const QString RenderAmbientLight0 = "OLD_TOWN_SQUARE";
const QString RenderAmbientLight1 = "GRACE_CATHEDRAL";
const QString RenderAmbientLight2 = "EUCALYPTUS_GROVE";
const QString RenderAmbientLight3 = "ST_PETERS_BASILICA";
const QString RenderAmbientLight4 = "UFFIZI_GALLERY";
const QString RenderAmbientLight5 = "GALILEOS_TOMB";
const QString RenderAmbientLight6 = "VINE_STREET_KITCHEN";
const QString RenderAmbientLight7 = "BREEZEWAY";
const QString RenderAmbientLight8 = "CAMPUS_SUNSET";
const QString RenderAmbientLight9 = "FUNSTON_BEACH_SUNSET";
const QString ResetAvatarSize = "Reset Avatar Size";
const QString ResetSensors = "Reset Sensors";
const QString RunningScripts = "Running Scripts";

View file

@ -45,17 +45,20 @@ vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) {
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;
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) {
// Diffuse Lighting
float diffuseDot = dot(frag.normal, gl_LightSource[0].position.xyz);
float facingLight = step(0.0, diffuseDot);
float diffuseDot = dot(normal, gl_LightSource[0].position.xyz);
float facingLight = step(0.0, diffuseDot) * shadowAttenuation;
vec3 diffuseColor = diffuse * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuseDot * facingLight));
// compute the specular multiplier (sans exponent)
@ -64,26 +67,30 @@ vec3 evalAmbientSphereAndDirectionalColor(vec3 position, vec3 normal, vec3 diffu
vec3 specularColor = pow(specularPower, gloss * 128.0) * specular;
// add specular contribution
return vec3(ambientColor + diffuseColor + specularColor);
return vec3(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));
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);
// 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;
// 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;
// add specular contribution
return vec3(ambientColor + diffuseColor + specularColor);
// 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

@ -35,9 +35,150 @@
#include "directional_light_shadow_map_frag.h"
#include "directional_light_cascaded_shadow_map_frag.h"
#include "directional_ambient_light_frag.h"
#include "directional_ambient_light_shadow_map_frag.h"
#include "directional_ambient_light_cascaded_shadow_map_frag.h"
#include "point_light_frag.h"
#include "spot_light_frag.h"
class SphericalHarmonics {
public:
glm::vec3 L00 ; float spare0;
glm::vec3 L1m1 ; float spare1;
glm::vec3 L10 ; float spare2;
glm::vec3 L11 ; float spare3;
glm::vec3 L2m2 ; float spare4;
glm::vec3 L2m1 ; float spare5;
glm::vec3 L20 ; float spare6;
glm::vec3 L21 ; float spare7;
glm::vec3 L22 ; float spare8;
void assignPreset(int p) {
switch (p) {
case DeferredLightingEffect::OLD_TOWN_SQUARE: {
L00 = glm::vec3( 0.871297f, 0.875222f, 0.864470f);
L1m1 = glm::vec3( 0.175058f, 0.245335f, 0.312891f);
L10 = glm::vec3( 0.034675f, 0.036107f, 0.037362f);
L11 = glm::vec3(-0.004629f,-0.029448f,-0.048028f);
L2m2 = glm::vec3(-0.120535f,-0.121160f,-0.117507f);
L2m1 = glm::vec3( 0.003242f, 0.003624f, 0.007511f);
L20 = glm::vec3(-0.028667f,-0.024926f,-0.020998f);
L21 = glm::vec3(-0.077539f,-0.086325f,-0.091591f);
L22 = glm::vec3(-0.161784f,-0.191783f,-0.219152f);
}
break;
case DeferredLightingEffect::GRACE_CATHEDRAL: {
L00 = glm::vec3( 0.79f, 0.44f, 0.54f);
L1m1 = glm::vec3( 0.39f, 0.35f, 0.60f);
L10 = glm::vec3(-0.34f, -0.18f, -0.27f);
L11 = glm::vec3(-0.29f, -0.06f, 0.01f);
L2m2 = glm::vec3(-0.11f, -0.05f, -0.12f);
L2m1 = glm::vec3(-0.26f, -0.22f, -0.47f);
L20 = glm::vec3(-0.16f, -0.09f, -0.15f);
L21 = glm::vec3( 0.56f, 0.21f, 0.14f);
L22 = glm::vec3( 0.21f, -0.05f, -0.30f);
}
break;
case DeferredLightingEffect::EUCALYPTUS_GROVE: {
L00 = glm::vec3( 0.38f, 0.43f, 0.45f);
L1m1 = glm::vec3( 0.29f, 0.36f, 0.41f);
L10 = glm::vec3( 0.04f, 0.03f, 0.01f);
L11 = glm::vec3(-0.10f, -0.10f, -0.09f);
L2m2 = glm::vec3(-0.06f, -0.06f, -0.04f);
L2m1 = glm::vec3( 0.01f, -0.01f, -0.05f);
L20 = glm::vec3(-0.09f, -0.13f, -0.15f);
L21 = glm::vec3(-0.06f, -0.05f, -0.04f);
L22 = glm::vec3( 0.02f, 0.00f, -0.05f);
}
break;
case DeferredLightingEffect::ST_PETERS_BASILICA: {
L00 = glm::vec3( 0.36f, 0.26f, 0.23f);
L1m1 = glm::vec3( 0.18f, 0.14f, 0.13f);
L10 = glm::vec3(-0.02f, -0.01f, 0.00f);
L11 = glm::vec3( 0.03f, 0.02f, -0.00f);
L2m2 = glm::vec3( 0.02f, 0.01f, -0.00f);
L2m1 = glm::vec3(-0.05f, -0.03f, -0.01f);
L20 = glm::vec3(-0.09f, -0.08f, -0.07f);
L21 = glm::vec3( 0.01f, 0.00f, 0.00f);
L22 = glm::vec3(-0.08f, -0.03f, -0.00f);
}
break;
case DeferredLightingEffect::UFFIZI_GALLERY: {
L00 = glm::vec3( 0.32f, 0.31f, 0.35f);
L1m1 = glm::vec3( 0.37f, 0.37f, 0.43f);
L10 = glm::vec3( 0.00f, 0.00f, 0.00f);
L11 = glm::vec3(-0.01f, -0.01f, -0.01f);
L2m2 = glm::vec3(-0.02f, -0.02f, -0.03f);
L2m1 = glm::vec3(-0.01f, -0.01f, -0.01f);
L20 = glm::vec3(-0.28f, -0.28f, -0.32f);
L21 = glm::vec3( 0.00f, 0.00f, 0.00f);
L22 = glm::vec3(-0.24f, -0.24f, -0.28f);
}
break;
case DeferredLightingEffect::GALILEOS_TOMB: {
L00 = glm::vec3( 1.04f, 0.76f, 0.71f);
L1m1 = glm::vec3( 0.44f, 0.34f, 0.34f);
L10 = glm::vec3(-0.22f, -0.18f, -0.17f);
L11 = glm::vec3( 0.71f, 0.54f, 0.56f);
L2m2 = glm::vec3( 0.64f, 0.50f, 0.52f);
L2m1 = glm::vec3(-0.12f, -0.09f, -0.08f);
L20 = glm::vec3(-0.37f, -0.28f, -0.32f);
L21 = glm::vec3(-0.17f, -0.13f, -0.13f);
L22 = glm::vec3( 0.55f, 0.42f, 0.42f);
}
break;
case DeferredLightingEffect::VINE_STREET_KITCHEN: {
L00 = glm::vec3( 0.64f, 0.67f, 0.73f);
L1m1 = glm::vec3( 0.28f, 0.32f, 0.33f);
L10 = glm::vec3( 0.42f, 0.60f, 0.77f);
L11 = glm::vec3(-0.05f, -0.04f, -0.02f);
L2m2 = glm::vec3(-0.10f, -0.08f, -0.05f);
L2m1 = glm::vec3( 0.25f, 0.39f, 0.53f);
L20 = glm::vec3( 0.38f, 0.54f, 0.71f);
L21 = glm::vec3( 0.06f, 0.01f, -0.02f);
L22 = glm::vec3(-0.03f, -0.02f, -0.03f);
}
break;
case DeferredLightingEffect::BREEZEWAY: {
L00 = glm::vec3( 0.32f, 0.36f, 0.38f);
L1m1 = glm::vec3( 0.37f, 0.41f, 0.45f);
L10 = glm::vec3(-0.01f, -0.01f, -0.01f);
L11 = glm::vec3(-0.10f, -0.12f, -0.12f);
L2m2 = glm::vec3(-0.13f, -0.15f, -0.17f);
L2m1 = glm::vec3(-0.01f, -0.02f, 0.02f);
L20 = glm::vec3(-0.07f, -0.08f, -0.09f);
L21 = glm::vec3( 0.02f, 0.03f, 0.03f);
L22 = glm::vec3(-0.29f, -0.32f, -0.36f);
}
break;
case DeferredLightingEffect::CAMPUS_SUNSET: {
L00 = glm::vec3( 0.79f, 0.94f, 0.98f);
L1m1 = glm::vec3( 0.44f, 0.56f, 0.70f);
L10 = glm::vec3(-0.10f, -0.18f, -0.27f);
L11 = glm::vec3( 0.45f, 0.38f, 0.20f);
L2m2 = glm::vec3( 0.18f, 0.14f, 0.05f);
L2m1 = glm::vec3(-0.14f, -0.22f, -0.31f);
L20 = glm::vec3(-0.39f, -0.40f, -0.36f);
L21 = glm::vec3( 0.09f, 0.07f, 0.04f);
L22 = glm::vec3( 0.67f, 0.67f, 0.52f);
}
break;
case DeferredLightingEffect::FUNSTON_BEACH_SUNSET: {
L00 = glm::vec3( 0.68f, 0.69f, 0.70f);
L1m1 = glm::vec3( 0.32f, 0.37f, 0.44f);
L10 = glm::vec3(-0.17f, -0.17f, -0.17f);
L11 = glm::vec3(-0.45f, -0.42f, -0.34f);
L2m2 = glm::vec3(-0.17f, -0.17f, -0.15f);
L2m1 = glm::vec3(-0.08f, -0.09f, -0.10f);
L20 = glm::vec3(-0.03f, -0.02f, -0.01f);
L21 = glm::vec3( 0.16f, 0.14f, 0.10f);
L22 = glm::vec3( 0.37f, 0.31f, 0.20f);
}
break;
}
}
};
void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) {
_viewState = viewState;
@ -54,6 +195,13 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) {
_directionalLightShadowMapLocations);
loadLightProgram(directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap,
_directionalLightCascadedShadowMapLocations);
loadLightProgram(directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations);
loadLightProgram(directional_ambient_light_shadow_map_frag, false, _directionalAmbientSphereLightShadowMap,
_directionalAmbientSphereLightShadowMapLocations);
loadLightProgram(directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap,
_directionalAmbientSphereLightCascadedShadowMapLocations);
loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations);
loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations);
}
@ -206,18 +354,43 @@ void DeferredLightingEffect::render() {
if (_viewState->getCascadeShadowsEnabled()) {
program = &_directionalLightCascadedShadowMap;
locations = &_directionalLightCascadedShadowMapLocations;
_directionalLightCascadedShadowMap.bind();
_directionalLightCascadedShadowMap.setUniform(locations->shadowDistances, _viewState->getShadowDistances());
if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLightCascadedShadowMap;
locations = &_directionalAmbientSphereLightCascadedShadowMapLocations;
}
program->bind();
program->setUniform(locations->shadowDistances, _viewState->getShadowDistances());
} else {
if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLightShadowMap;
locations = &_directionalAmbientSphereLightShadowMapLocations;
}
program->bind();
}
program->setUniformValue(locations->shadowScale,
1.0f / textureCache->getShadowFramebufferObject()->width());
} else {
if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLight;
locations = &_directionalAmbientSphereLightLocations;
}
program->bind();
}
if (locations->ambientSphere >= 0) {
SphericalHarmonics sh;
if (_ambientLightMode < NUM_PRESET) {
sh.assignPreset(_ambientLightMode);
} else {
sh.assignPreset(0);
}
for (int i =0; i <9; i++) {
program->setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i));
}
}
float left, right, bottom, top, nearVal, farVal;
glm::vec4 nearClipPlane, farClipPlane;
@ -433,6 +606,7 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit
locations.depthTexCoordOffset = program.uniformLocation("depthTexCoordOffset");
locations.depthTexCoordScale = program.uniformLocation("depthTexCoordScale");
locations.radius = program.uniformLocation("radius");
locations.ambientSphere = program.uniformLocation("ambientSphere.L00");
program.release();
}

View file

@ -101,6 +101,7 @@ private:
int depthTexCoordOffset;
int depthTexCoordScale;
int radius;
int ambientSphere;
};
static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations);
@ -108,12 +109,20 @@ private:
ProgramObject _simpleProgram;
int _glowIntensityLocation;
ProgramObject _directionalAmbientSphereLight;
LightLocations _directionalAmbientSphereLightLocations;
ProgramObject _directionalAmbientSphereLightShadowMap;
LightLocations _directionalAmbientSphereLightShadowMapLocations;
ProgramObject _directionalAmbientSphereLightCascadedShadowMap;
LightLocations _directionalAmbientSphereLightCascadedShadowMapLocations;
ProgramObject _directionalLight;
LightLocations _directionalLightLocations;
ProgramObject _directionalLightShadowMap;
LightLocations _directionalLightShadowMapLocations;
ProgramObject _directionalLightCascadedShadowMap;
LightLocations _directionalLightCascadedShadowMapLocations;
ProgramObject _pointLight;
LightLocations _pointLightLocations;
ProgramObject _spotLight;
@ -144,7 +153,7 @@ private:
AbstractViewStateInterface* _viewState;
int _ambientLightMode = -1;
int _ambientLightMode = 0;
};
/// Simple interface for objects that require something to be rendered after deferred lighting.

View file

@ -0,0 +1,42 @@
<@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@>
<@include DeferredLighting.slh@>
void main(void) {
DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st);
// Light mapped or not ?
if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) {
gl_FragColor = vec4( evalLightmappedColor(
1.0,
frag.normal,
frag.diffuse,
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(1.0,
frag.position.xyz,
frag.normal,
frag.diffuse,
frag.specular,
frag.gloss);
gl_FragColor = vec4(color, frag.normalVal.a);
}
}

View file

@ -0,0 +1,49 @@
<@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@>
<@include DeferredLighting.slh@>
// Everything about shadow
<@include Shadow.slh@>
void main(void) {
DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st);
// Eval shadow Texcoord and then Attenuation
vec4 shadowTexcoord = evalCascadedShadowTexcoord(frag.position);
float shadowAttenuation = evalShadowAttenuation(shadowTexcoord);
// Light mapped or not ?
if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) {
gl_FragColor = vec4(evalLightmappedColor(
shadowAttenuation,
frag.normal,
frag.diffuse,
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(shadowAttenuation,
frag.position.xyz,
frag.normal,
frag.diffuse,
frag.specular,
frag.gloss);
gl_FragColor = vec4(color, frag.normalVal.a);
}
}

View file

@ -0,0 +1,50 @@
<@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@>
<@include DeferredLighting.slh@>
// Everything about shadow
<@include Shadow.slh@>
void main(void) {
DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st);
// Eval shadow Texcoord and then Attenuation
vec4 shadowTexcoord = evalShadowTexcoord(frag.position);
float shadowAttenuation = evalShadowAttenuation(shadowTexcoord);
// Light mapped or not ?
if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) {
gl_FragColor = vec4(evalLightmappedColor(
shadowAttenuation,
frag.normal,
frag.diffuse,
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(shadowAttenuation,
frag.position.xyz,
frag.normal,
frag.diffuse,
frag.specular,
frag.gloss);
gl_FragColor = vec4(color, frag.normalVal.a);
}
}

View file

@ -20,35 +20,23 @@
void main(void) {
DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st);
vec4 normalVal = frag.normalVal;
vec4 diffuseVal = frag.diffuseVal;
vec4 specularVal = frag.specularVal;
// Light mapped or not ?
if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) {
gl_FragColor = vec4(diffuseVal.rgb * specularVal.rgb, 1.0);
if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) {
gl_FragColor = vec4( evalLightmappedColor(
1.0,
frag.normal,
frag.diffuse,
frag.specularVal.xyz),
1.0);
} else {
glFragColor = vec4( evalAmbientAndDirectionalColor(frag.position.xyz,
frag.normal,
frag.diffuse,
frag.specular,
frag.gloss),
normalVal.a);
vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(1.0,
frag.position.xyz,
frag.normal,
frag.diffuse,
frag.specular,
frag.gloss);
/*
// 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);
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);
*/
gl_FragColor = vec4(color, frag.normalVal.a);
}
}

View file

@ -15,55 +15,35 @@
// Everything about deferred buffer
<@include DeferredBuffer.slh@>
<@include DeferredLighting.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 = evalCascadedShadowTexcoord(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 = 1.0;
// 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, 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);
if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) {
gl_FragColor = vec4(evalLightmappedColor(
shadowAttenuation,
frag.normal,
frag.diffuse,
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(shadowAttenuation,
frag.position.xyz,
frag.normal,
frag.diffuse,
frag.specular,
frag.gloss);
// 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);
gl_FragColor = vec4(color, frag.normalVal.a);
}
}

View file

@ -15,105 +15,36 @@
// Everything about deferred buffer
<@include DeferredBuffer.slh@>
<@include DeferredLighting.slh@>
// Everything about shadow
<@include Shadow.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;
}
<@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);
SphericalHarmonics sh;
sh.L00 = vec4( 0.79, 0.44, 0.54, 1.0);
sh.L1m1 = vec4( 0.39, 0.35, 0.60, 1.0);
sh.L10 = vec4(-0.34, -0.18, -0.27, 1.0);
sh.L11 = vec4(-0.29, -0.06, 0.01, 1.0);
sh.L2m2 = vec4(-0.11, -0.05, -0.12, 1.0);
sh.L2m1 = vec4(-0.26, -0.22, -0.47, 1.0);
sh.L20 = vec4(-0.16, -0.09, -0.15, 1.0);
sh.L21 = vec4( 0.56, 0.21, 0.14, 1.0);
sh.L22 = vec4( 0.21, -0.05, -0.30, 1.0);
// Light mapped or not ?
if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) {
normalVal.a = 0.0;
if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) {
gl_FragColor = vec4(evalLightmappedColor(
shadowAttenuation,
frag.normal,
frag.diffuse,
frag.specularVal.xyz),
1.0);
} else {
vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss)
+ evalDirectionalColor(shadowAttenuation,
frag.position.xyz,
frag.normal,
frag.diffuse,
frag.specular,
frag.gloss);
// 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;
vec4 ambienTerm = 0.5 * evalSphericalLight(sh, frag.normal);
if (gl_FragCoord.x > 1024) {
ambienTerm = gl_FrontLightProduct[0].ambient.rgba;
}
// compute the base color based on OpenGL lighting model
vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb +
ambienTerm.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);
gl_FragColor = vec4(color, frag.normalVal.a);
}
}