Using the skybox cubemap for ambient lighting

This commit is contained in:
Sam Gateau 2015-05-09 11:40:46 -07:00
parent 0e56b49b28
commit 5a11143a5d
11 changed files with 230 additions and 11 deletions

View file

@ -3190,6 +3190,7 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs
// Background rendering decision
auto skyStage = DependencyManager::get<SceneScriptingInterface>()->getSkyStage();
auto skybox = model::SkyboxPointer();
if (skyStage->getBackgroundMode() == model::SunSkyStage::NO_BACKGROUND) {
} else if (skyStage->getBackgroundMode() == model::SunSkyStage::SKY_DOME) {
if (!selfAvatarOnly && Menu::getInstance()->isOptionChecked(MenuOption::Stars)) {
@ -3237,7 +3238,7 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs
}
} else if (skyStage->getBackgroundMode() == model::SunSkyStage::SKY_BOX) {
auto skybox = skyStage->getSkybox();
skybox = skyStage->getSkybox();
if (skybox) {
gpu::Batch batch;
model::Skybox::render(batch, _viewFrustum, *skybox);
@ -3316,6 +3317,7 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs
auto skyStage = DependencyManager::get<SceneScriptingInterface>()->getSkyStage();
DependencyManager::get<DeferredLightingEffect>()->setGlobalLight(skyStage->getSunLight()->getDirection(), skyStage->getSunLight()->getColor(), skyStage->getSunLight()->getIntensity(), skyStage->getSunLight()->getAmbientIntensity());
DependencyManager::get<DeferredLightingEffect>()->setGlobalAtmosphere(skyStage->getAtmosphere());
DependencyManager::get<DeferredLightingEffect>()->setGlobalSkybox(skybox);
PROFILE_RANGE("DeferredLighting");
PerformanceTimer perfTimer("lighting");

View file

@ -14,7 +14,7 @@
<@if GLPROFILE == PC_GL @>
<@def GPU_FEATURE_PROFILE GPU_CORE@>
<@def GPU_TRANSFORM_PROFILE GPU_CORE@>
<@def VERSION_HEADER #version 330 compatibility@>
<@def VERSION_HEADER #version 430 compatibility@>
<@elif GLPROFILE == MAC_GL @>
<@def GPU_FEATURE_PROFILE GPU_LEGACY@>
<@def GPU_TRANSFORM_PROFILE GPU_LEGACY@>

View file

@ -370,9 +370,21 @@ GLBackend::GLTexture* GLBackend::syncGPUObject(const Texture& texture) {
glBindTexture(GL_TEXTURE_CUBE_MAP, object->_texture);
const int NUM_FACES = 6;
const GLenum FACE_LAYOUT[] = {
GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
/* GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_X };
*/
GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
/* GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
*/
/* GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };*/
if (needUpdate) {
if (texture.isStoredMipAvailable(0)) {
Texture::PixelsPointer mip = texture.accessStoredMip(0);

View file

@ -167,7 +167,7 @@ VectorOfMotionStates& PhysicalEntitySimulation::getObjectsToAdd() {
_tempVector.push_back(motionState);
entityItr = _pendingAdds.erase(entityItr);
} else {
qDebug() << "Warning! Failed to generate new shape for entity." << entity->getName();
// qDebug() << "Warning! Failed to generate new shape for entity." << entity->getName();
++entityItr;
}
} else {

View file

@ -13,6 +13,14 @@
<@include DeferredLighting.slh@>
uniform samplerCube skyboxMap;
vec4 evalSkyboxLight(vec3 direction, float lod) {
// vec4 skytexel = textureCube(skyboxMap, direction);
vec4 skytexel = textureCubeLod(skyboxMap, direction, lod * textureQueryLevels(skyboxMap));
return skytexel;
}
struct SphericalHarmonics {
vec4 L00;
@ -92,6 +100,23 @@ vec3 evalAmbienSphereGlobalColor(float shadowAttenuation, vec3 position, vec3 no
return color;
}
vec3 evalSkyboxGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) {
// Need the light now
Light light = getLight();
vec3 fragNormal = normalize(vec3(invViewMat * vec4(normal, 0.0)));
vec4 fragEyeVector = invViewMat * vec4(-position, 0.0);
vec3 fragEyeDir = normalize(fragEyeVector.xyz);
vec3 color = diffuse.rgb * evalSkyboxLight(fragNormal, 0.75).xyz * getLightAmbientIntensity(light);
vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss);
color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light);
return color;
}
vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, vec3 lightmap) {
Light light = getLight();

View file

@ -41,6 +41,10 @@
#include "directional_ambient_light_shadow_map_frag.h"
#include "directional_ambient_light_cascaded_shadow_map_frag.h"
#include "directional_skybox_light_frag.h"
#include "directional_skybox_light_shadow_map_frag.h"
#include "directional_skybox_light_cascaded_shadow_map_frag.h"
#include "point_light_frag.h"
#include "spot_light_frag.h"
@ -66,6 +70,12 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) {
loadLightProgram(directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap,
_directionalAmbientSphereLightCascadedShadowMapLocations);
loadLightProgram(directional_skybox_light_frag, false, _directionalSkyboxLight, _directionalSkyboxLightLocations);
loadLightProgram(directional_skybox_light_shadow_map_frag, false, _directionalSkyboxLightShadowMap,
_directionalSkyboxLightShadowMapLocations);
loadLightProgram(directional_skybox_light_cascaded_shadow_map_frag, false, _directionalSkyboxLightCascadedShadowMap,
_directionalSkyboxLightCascadedShadowMapLocations);
loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations);
loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations);
@ -217,6 +227,7 @@ void DeferredLightingEffect::render() {
float tMin = viewport[VIEWPORT_Y_INDEX] / (float)framebufferSize.height();
float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)framebufferSize.height();
bool useSkyboxCubemap = (_skybox) && (_skybox->getCubemap());
// Fetch the ViewMatrix;
glm::mat4 invViewMat;
@ -234,7 +245,10 @@ void DeferredLightingEffect::render() {
if (_viewState->getCascadeShadowsEnabled()) {
program = &_directionalLightCascadedShadowMap;
locations = &_directionalLightCascadedShadowMapLocations;
if (_ambientLightMode > -1) {
if (useSkyboxCubemap) {
program = &_directionalSkyboxLightCascadedShadowMap;
locations = &_directionalSkyboxLightCascadedShadowMapLocations;
} else if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLightCascadedShadowMap;
locations = &_directionalAmbientSphereLightCascadedShadowMapLocations;
}
@ -242,7 +256,10 @@ void DeferredLightingEffect::render() {
program->setUniform(locations->shadowDistances, _viewState->getShadowDistances());
} else {
if (_ambientLightMode > -1) {
if (useSkyboxCubemap) {
program = &_directionalSkyboxLightShadowMap;
locations = &_directionalSkyboxLightShadowMapLocations;
} else if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLightShadowMap;
locations = &_directionalAmbientSphereLightShadowMapLocations;
}
@ -252,7 +269,10 @@ void DeferredLightingEffect::render() {
1.0f / textureCache->getShadowFramebuffer()->getWidth());
} else {
if (_ambientLightMode > -1) {
if (useSkyboxCubemap) {
program = &_directionalSkyboxLight;
locations = &_directionalSkyboxLightLocations;
} else if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLight;
locations = &_directionalAmbientSphereLightLocations;
}
@ -269,6 +289,11 @@ void DeferredLightingEffect::render() {
}
}
if (useSkyboxCubemap) {
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_CUBE_MAP, gpu::GLBackend::getTextureID(_skybox->getCubemap()));
}
if (locations->lightBufferUnit >= 0) {
gpu::Batch batch;
batch.setUniformBuffer(locations->lightBufferUnit, globalLight->getSchemaBuffer());
@ -300,7 +325,14 @@ void DeferredLightingEffect::render() {
renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight);
program->release();
if (useSkyboxCubemap) {
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
if (!shadowsEnabled) {
glActiveTexture(GL_TEXTURE3);
}
}
if (shadowsEnabled) {
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE3);
@ -492,6 +524,7 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit
program.setUniformValue("specularMap", 2);
program.setUniformValue("depthMap", 3);
program.setUniformValue("shadowMap", 4);
program.setUniformValue("skyboxMap", 5);
locations.shadowDistances = program.uniformLocation("shadowDistances");
locations.shadowScale = program.uniformLocation("shadowScale");
locations.nearLocation = program.uniformLocation("near");

View file

@ -101,6 +101,13 @@ private:
ProgramObject _simpleProgram;
int _glowIntensityLocation;
ProgramObject _directionalSkyboxLight;
LightLocations _directionalSkyboxLightLocations;
ProgramObject _directionalSkyboxLightShadowMap;
LightLocations _directionalSkyboxLightShadowMapLocations;
ProgramObject _directionalSkyboxLightCascadedShadowMap;
LightLocations _directionalSkyboxLightCascadedShadowMapLocations;
ProgramObject _directionalAmbientSphereLight;
LightLocations _directionalAmbientSphereLightLocations;
ProgramObject _directionalAmbientSphereLightShadowMap;

View file

@ -513,7 +513,7 @@ void NetworkTexture::setImage(const QImage& image, bool translucent, const QColo
imageLoaded(image);
if ((_width > 0) && (_height > 0)) {
bool isLinearRGB = true; //(_type == NORMAL_TEXTURE) || (_type == EMISSIVE_TEXTURE);
gpu::Element formatGPU = gpu::Element(gpu::VEC3, gpu::UINT8, (isLinearRGB ? gpu::RGB : gpu::SRGB));
@ -525,7 +525,7 @@ void NetworkTexture::setImage(const QImage& image, bool translucent, const QColo
if (_type == CUBE_TEXTURE) {
if (_height >= (6 * _width)) {
_gpuTexture = gpu::TexturePointer(gpu::Texture::createCube(formatGPU, image.width(), gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_LINEAR, gpu::Sampler::WRAP_CLAMP)));
_gpuTexture = gpu::TexturePointer(gpu::Texture::createCube(formatGPU, image.width(), gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR, gpu::Sampler::WRAP_CLAMP)));
_gpuTexture->assignStoredMip(0, formatMip, image.byteCount(), image.constBits());
_gpuTexture->autoGenerateMips(-1);
}

View file

@ -0,0 +1,43 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// directional_light.frag
// fragment shader
//
// Created by Sam Gateau on 5/8/2015.
// 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 DeferredGlobalLight.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)) {
vec3 color = evalLightmappedColor(
1.0,
frag.normal,
frag.diffuse,
frag.specularVal.xyz);
gl_FragColor = vec4(color, 1.0);
} else {
vec3 color = evalSkyboxGlobalColor(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,48 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// directional_light.frag
// fragment shader
//
// Created by Sam Gateau on 5/8/2015.
// 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 DeferredGlobalLight.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 = evalSkyboxGlobalColor(shadowAttenuation,
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 gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// directional_light.frag
// fragment shader
//
// Created by Sam Gateau on 5/8/2015.
// 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 DeferredGlobalLight.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 = evalSkyboxGlobalColor(shadowAttenuation,
frag.position.xyz,
frag.normal,
frag.diffuse,
frag.specular,
frag.gloss);
gl_FragColor = vec4(color, frag.normalVal.a);
}
}