From 099488106ad44c7fcf574f0eb6119a4985671b9b Mon Sep 17 00:00:00 2001 From: Jonathas Costa Date: Tue, 26 Nov 2019 16:13:46 -0500 Subject: [PATCH] Fixed issues in Shadows and improved quality. --- .../solarsystem/planets/earth/earth.asset | 9 +- .../globebrowsing/shaders/renderer_fs.glsl | 2 +- modules/globebrowsing/shaders/rings_fs.glsl | 2 +- .../globebrowsing/shaders/smviewer_fs.glsl | 4 +- modules/globebrowsing/src/renderableglobe.cpp | 5 +- modules/globebrowsing/src/shadowcomponent.cpp | 141 +++++++++++------- modules/globebrowsing/src/shadowcomponent.h | 2 + 7 files changed, 103 insertions(+), 62 deletions(-) diff --git a/data/assets/scene/solarsystem/planets/earth/earth.asset b/data/assets/scene/solarsystem/planets/earth/earth.asset index c1482969d0..44150fd439 100644 --- a/data/assets/scene/solarsystem/planets/earth/earth.asset +++ b/data/assets/scene/solarsystem/planets/earth/earth.asset @@ -263,10 +263,11 @@ local Earth = { } --Caster2 = { Name = "Independency Day Ship", Radius = 0.0, } }, - Shadows = { - Enabled = true, - DistanceFraction = 40.0 - }, + -- Shadows = { + -- Enabled = true, + -- DistanceFraction = 70.0, + -- DepthMapSize = {7680.0, 4320.0} + -- }, Labels = { Enable = false, FileName = labelsPath .. "/Earth.labels", diff --git a/modules/globebrowsing/shaders/renderer_fs.glsl b/modules/globebrowsing/shaders/renderer_fs.glsl index d418937e77..e3281fcedd 100644 --- a/modules/globebrowsing/shaders/renderer_fs.glsl +++ b/modules/globebrowsing/shaders/renderer_fs.glsl @@ -267,7 +267,7 @@ Fragment getFragment() { float shadow = 1.0; if ( shadowCoords.w > 1 ) { vec4 normalizedShadowCoords = shadowCoords; - normalizedShadowCoords.z = normalizeFloat(normalizedShadowCoords.w - 0.3); + normalizedShadowCoords.z = normalizeFloat(normalizedShadowCoords.w - 0.005 * normalizedShadowCoords.w); normalizedShadowCoords.xy = normalizedShadowCoords.xy / normalizedShadowCoords.w; normalizedShadowCoords.w = 1.0; diff --git a/modules/globebrowsing/shaders/rings_fs.glsl b/modules/globebrowsing/shaders/rings_fs.glsl index 1dfd5df308..d1be63cef7 100644 --- a/modules/globebrowsing/shaders/rings_fs.glsl +++ b/modules/globebrowsing/shaders/rings_fs.glsl @@ -73,7 +73,7 @@ Fragment getFragment() { float shadow = 1.0; if ( shadowCoords.z >= 0 ) { vec4 normalizedShadowCoords = shadowCoords; - normalizedShadowCoords.z = normalizeFloat(normalizedShadowCoords.w - 0.3); + normalizedShadowCoords.z = normalizeFloat(normalizedShadowCoords.w - 0.005 * normalizedShadowCoords.w); normalizedShadowCoords.xy = normalizedShadowCoords.xy / normalizedShadowCoords.w; normalizedShadowCoords.w = 1.0; diff --git a/modules/globebrowsing/shaders/smviewer_fs.glsl b/modules/globebrowsing/shaders/smviewer_fs.glsl index be189229f1..0d58b4e8aa 100644 --- a/modules/globebrowsing/shaders/smviewer_fs.glsl +++ b/modules/globebrowsing/shaders/smviewer_fs.glsl @@ -25,9 +25,11 @@ #include "PowerScaling/powerScaling_fs.hglsl" #include "fragment.glsl" +precision highp float; + in vec2 texCoord; -uniform sampler2D shadowMapTexture; +uniform highp sampler2D shadowMapTexture; Fragment getFragment() { Fragment frag; diff --git a/modules/globebrowsing/src/renderableglobe.cpp b/modules/globebrowsing/src/renderableglobe.cpp index 792bfa3257..191b351738 100644 --- a/modules/globebrowsing/src/renderableglobe.cpp +++ b/modules/globebrowsing/src/renderableglobe.cpp @@ -574,7 +574,6 @@ RenderableGlobe::RenderableGlobe(const ghoul::Dictionary& dictionary) _generalProperties.eclipseShadowsEnabled.onChange(notifyShaderRecompilation); _generalProperties.eclipseHardShadows.onChange(notifyShaderRecompilation); _generalProperties.performShading.onChange(notifyShaderRecompilation); - _generalProperties.shadowMapping.onChange(notifyShaderRecompilation); _debugProperties.showChunkEdges.onChange(notifyShaderRecompilation); _debugProperties.showHeightResolution.onChange(notifyShaderRecompilation); _debugProperties.showHeightIntensities.onChange(notifyShaderRecompilation); @@ -681,7 +680,9 @@ RenderableGlobe::RenderableGlobe(const ghoul::Dictionary& dictionary) _shadowComponent.initialize(); addPropertySubOwner(_shadowComponent); _hasShadows = true; + _generalProperties.shadowMapping = true; } + _generalProperties.shadowMapping.onChange(notifyShaderRecompilation); #ifdef OPENSPACE_MODULE_GLOBEBROWSING_INSTRUMENTATION _module = global::moduleEngine.module(); @@ -775,7 +776,7 @@ void RenderableGlobe::render(const RenderData& data, RendererTasks& rendererTask glEnable(GL_BLEND); - _shadowComponent.setViewDepthMap(true); + _shadowComponent.setViewDepthMap(false); _shadowComponent.end(); diff --git a/modules/globebrowsing/src/shadowcomponent.cpp b/modules/globebrowsing/src/shadowcomponent.cpp index f0fcb217b8..f857588011 100644 --- a/modules/globebrowsing/src/shadowcomponent.cpp +++ b/modules/globebrowsing/src/shadowcomponent.cpp @@ -79,6 +79,12 @@ namespace { "considered as the new light source distance." }; + constexpr openspace::properties::Property::PropertyInfo DepthMapSizeInfo = { + "DepthMapSize", + "Depth Map Size", + "The depth map size in pixels. You must entry the width and height values." + }; + void checkFrameBufferState(const std::string& codePosition) { if (glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { @@ -201,6 +207,23 @@ namespace openspace { _executeDepthTextureSave = true; }); + + if (_shadowMapDictionary.hasKey(DepthMapSizeInfo.identifier)) { + glm::vec2 depthMapSize = + _shadowMapDictionary.value(DepthMapSizeInfo.identifier); + _shadowDepthTextureWidth = depthMapSize.x; + _shadowDepthTextureHeight = depthMapSize.y; + _dynamicDepthTextureRes = false; + } + else { + glm::ivec2 renderingResolution = global::renderEngine.renderingResolution(); + _shadowDepthTextureWidth = renderingResolution.x * 2; + _shadowDepthTextureHeight = renderingResolution.y * 2; + _dynamicDepthTextureRes = true; + } + + _viewDepthMap = false; + addProperty(_enabled); addProperty(_saveDepthTexture); addProperty(_distanceFraction); @@ -420,12 +443,12 @@ namespace openspace { glGenVertexArrays(1, &_quadVAO); } + _renderDMProgram->activate(); + ghoul::opengl::TextureUnit shadowMapUnit; shadowMapUnit.activate(); glBindTexture(GL_TEXTURE_2D, _shadowDepthTexture); - _renderDMProgram->activate(); - _renderDMProgram->setUniform("shadowMapTexture", shadowMapUnit); glBindVertexArray(_quadVAO); @@ -437,61 +460,22 @@ namespace openspace { void ShadowComponent::update(const UpdateData& /*data*/) { _sunPosition = global::renderEngine.scene()->sceneGraphNode("Sun")->worldPosition(); + + glm::ivec2 renderingResolution = global::renderEngine.renderingResolution(); + if (_dynamicDepthTextureRes && ((_shadowDepthTextureWidth != renderingResolution.x) || + (_shadowDepthTextureHeight != renderingResolution.y))) { + _shadowDepthTextureWidth = renderingResolution.x * 2; + _shadowDepthTextureHeight = renderingResolution.y * 2; + updateDepthTexture(); + } } void ShadowComponent::createDepthTexture() { glGenTextures(1, &_shadowDepthTexture); - glBindTexture(GL_TEXTURE_2D, _shadowDepthTexture); - glTexStorage2D( - GL_TEXTURE_2D, - 1, - GL_DEPTH_COMPONENT32F, - _shadowDepthTextureWidth, - _shadowDepthTextureHeight - ); - - /*glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_DEPTH_COMPONENT32F, - _shadowDepthTextureWidth, - _shadowDepthTextureHeight, - 0, - GL_DEPTH_COMPONENT, - GL_FLOAT, - 0 - );*/ + updateDepthTexture(); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, ShadowBorder); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); + checkGLError("createDepthTexture() -- Depth texture created"); - checkGLError("createDepthTexture() -- Depth testure created"); - - /*glGenTextures(1, &_positionInLightSpaceTexture); - glBindTexture(GL_TEXTURE_2D, _positionInLightSpaceTexture); - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RGB32F, - _shadowDepthTextureWidth, - _shadowDepthTextureHeight, - 0, - GL_RGBA, - GL_FLOAT, - nullptr - ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);*/ - - glBindTexture(GL_TEXTURE_2D, 0); - _shadowData.shadowDepthTexture = _shadowDepthTexture; //_shadowData.positionInLightSpaceTexture = _positionInLightSpaceTexture; } @@ -499,8 +483,7 @@ namespace openspace { void ShadowComponent::createShadowFBO() { // Saves current FBO first glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_defaultFBO); - - + glGenFramebuffers(1, &_shadowFBO); glBindFramebuffer(GL_FRAMEBUFFER, _shadowFBO); glFramebufferTexture( @@ -528,6 +511,58 @@ namespace openspace { glBindFramebuffer(GL_FRAMEBUFFER, _defaultFBO); } + void ShadowComponent::updateDepthTexture() { + glBindTexture(GL_TEXTURE_2D, _shadowDepthTexture); + /* + glTexStorage2D( + GL_TEXTURE_2D, + 1, + GL_DEPTH_COMPONENT32F, + _shadowDepthTextureWidth, + _shadowDepthTextureHeight + ); + */ + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_DEPTH_COMPONENT32F, + _shadowDepthTextureWidth, + _shadowDepthTextureHeight, + 0, + GL_DEPTH_COMPONENT, + GL_FLOAT, + 0 + ); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, ShadowBorder); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); + + /*glGenTextures(1, &_positionInLightSpaceTexture); + glBindTexture(GL_TEXTURE_2D, _positionInLightSpaceTexture); + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_RGB32F, + _shadowDepthTextureWidth, + _shadowDepthTextureHeight, + 0, + GL_RGBA, + GL_FLOAT, + nullptr + ); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);*/ + + glBindTexture(GL_TEXTURE_2D, 0); + } + void ShadowComponent::saveDepthBuffer() { int size = _shadowDepthTextureWidth * _shadowDepthTextureHeight; GLubyte * buffer = new GLubyte[size]; diff --git a/modules/globebrowsing/src/shadowcomponent.h b/modules/globebrowsing/src/shadowcomponent.h index 5391e7714a..602a480f00 100644 --- a/modules/globebrowsing/src/shadowcomponent.h +++ b/modules/globebrowsing/src/shadowcomponent.h @@ -93,6 +93,7 @@ namespace openspace { private: void createDepthTexture(); void createShadowFBO(); + void updateDepthTexture(); // Debug void saveDepthBuffer(); @@ -119,6 +120,7 @@ namespace openspace { int _shadowDepthTextureHeight; int _shadowDepthTextureWidth; + bool _dynamicDepthTextureRes = true; GLuint _shadowDepthTexture; GLuint _positionInLightSpaceTexture;