From 65e83df214b2996723f2572fb6ce98eca22d4e41 Mon Sep 17 00:00:00 2001 From: Sam Cake Date: Wed, 13 May 2015 11:08:18 -0700 Subject: [PATCH] Generating the SH from the cube map! --- libraries/gpu/src/gpu/Texture.cpp | 2 +- libraries/model/src/model/Skybox.cpp | 5 ++++- libraries/model/src/model/Skybox.slf | 3 ++- libraries/render-utils/src/DeferredGlobalLight.slh | 4 ++-- libraries/render-utils/src/DeferredLightingEffect.cpp | 7 ++++++- libraries/render-utils/src/TextureCache.cpp | 3 ++- 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/libraries/gpu/src/gpu/Texture.cpp b/libraries/gpu/src/gpu/Texture.cpp index 07bb73a375..542a47c915 100755 --- a/libraries/gpu/src/gpu/Texture.cpp +++ b/libraries/gpu/src/gpu/Texture.cpp @@ -57,7 +57,7 @@ const Texture::PixelsPointer Texture::Storage::getMipFace(uint16 level, uint8 fa void Texture::Storage::notifyMipFaceGPULoaded(uint16 level, uint8 face) const { PixelsPointer mipFace = getMipFace(level, face); - if (mipFace) { + if (mipFace && (_type != TEX_CUBE)) { mipFace->_isGPULoaded = true; mipFace->_sysmem.resize(0); } diff --git a/libraries/model/src/model/Skybox.cpp b/libraries/model/src/model/Skybox.cpp index 7ec15eb008..f9d2905864 100755 --- a/libraries/model/src/model/Skybox.cpp +++ b/libraries/model/src/model/Skybox.cpp @@ -39,6 +39,9 @@ void Skybox::setColor(const Color& color) { } void Skybox::setCubemap(const gpu::TexturePointer& cubemap) { + if (_isSHValid && (cubemap != _cubemap)) { + _isSHValid = false; + } _cubemap = cubemap; } @@ -348,7 +351,7 @@ const SphericalHarmonics& Skybox::getAmbientSH() const { if (_cubemap && _cubemap->isDefined()) { std::vector< glm::vec3 > coefs(10, glm::vec3(0.0f)); - sphericalHarmonicsFromTexture(*_cubemap, coefs, 2); + sphericalHarmonicsFromTexture(*_cubemap, coefs, 3); _ambientSH.L00 = coefs[0]; _ambientSH.L1m1 = coefs[1]; diff --git a/libraries/model/src/model/Skybox.slf b/libraries/model/src/model/Skybox.slf index 5739cfda34..abb30bf09c 100755 --- a/libraries/model/src/model/Skybox.slf +++ b/libraries/model/src/model/Skybox.slf @@ -20,5 +20,6 @@ varying vec3 color; void main(void) { vec3 coord = normalize(normal); vec4 texel = textureCube(cubeMap, coord); - gl_FragData[0] = vec4(texel.xyz * color, 0.0); + vec3 pixel = pow(texel.xyz * color, vec3(1.0/2.2)); // manual Gamma correction + gl_FragData[0] = vec4(pixel, 0.0); } diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index d676a9c6b7..2f312e42d8 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -111,8 +111,8 @@ vec3 evalSkyboxGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec3 color = diffuse.rgb * evalSkyboxLight(fragNormal, 0.75).xyz * getLightAmbientIntensity(light); - + vec3 color = diffuse.rgb * evalSphericalLight(ambientSphere, fragNormal).xyz * getLightAmbientIntensity(light); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 9b0c5647ed..d9a4aa0378 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -291,7 +291,12 @@ void DeferredLightingEffect::render() { auto globalLight = _allocatedLights[_globalLights.front()]; if (locations->ambientSphere >= 0) { - auto sh = globalLight->getAmbientSphere(); + model::SphericalHarmonics sh; + if (useSkyboxCubemap) { + sh = _skybox->getAmbientSH(); + } else { + sh = globalLight->getAmbientSphere(); + } for (int i =0; i setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i)); } diff --git a/libraries/render-utils/src/TextureCache.cpp b/libraries/render-utils/src/TextureCache.cpp index c1c8ec030f..a6c932b4d8 100644 --- a/libraries/render-utils/src/TextureCache.cpp +++ b/libraries/render-utils/src/TextureCache.cpp @@ -514,7 +514,8 @@ void NetworkTexture::setImage(const QImage& image, bool translucent, const QColo if ((_width > 0) && (_height > 0)) { - bool isLinearRGB = true; //(_type == NORMAL_TEXTURE) || (_type == EMISSIVE_TEXTURE); + // bool isLinearRGB = true; //(_type == NORMAL_TEXTURE) || (_type == EMISSIVE_TEXTURE); + bool isLinearRGB = !(_type == CUBE_TEXTURE); //(_type == NORMAL_TEXTURE) || (_type == EMISSIVE_TEXTURE); gpu::Element formatGPU = gpu::Element(gpu::VEC3, gpu::UINT8, (isLinearRGB ? gpu::RGB : gpu::SRGB)); gpu::Element formatMip = gpu::Element(gpu::VEC3, gpu::UINT8, (isLinearRGB ? gpu::RGB : gpu::SRGB));