Fixed issues in Shadows and improved quality.

This commit is contained in:
Jonathas Costa
2019-11-26 16:13:46 -05:00
parent 74c754d923
commit 099488106a
7 changed files with 103 additions and 62 deletions

View File

@@ -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",

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<GlobeBrowsingModule>();
@@ -775,7 +776,7 @@ void RenderableGlobe::render(const RenderData& data, RendererTasks& rendererTask
glEnable(GL_BLEND);
_shadowComponent.setViewDepthMap(true);
_shadowComponent.setViewDepthMap(false);
_shadowComponent.end();

View File

@@ -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<glm::vec2>(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];

View File

@@ -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;