diff --git a/modules/newhorizons/rendering/renderableplanetprojection.cpp b/modules/newhorizons/rendering/renderableplanetprojection.cpp index 13516f1e5f..51523d6d9b 100644 --- a/modules/newhorizons/rendering/renderableplanetprojection.cpp +++ b/modules/newhorizons/rendering/renderableplanetprojection.cpp @@ -364,8 +364,9 @@ void RenderablePlanetProjection::update(const UpdateData& data) { _fboProgramObject->rebuildFromFile(); } - if (_programObject->isDirty()) + if (_programObject->isDirty()) { _programObject->rebuildFromFile(); + } _time = Time::ref().currentTime(); _capture = false; diff --git a/modules/newhorizons/shaders/dilation_fs.glsl b/modules/newhorizons/shaders/dilation_fs.glsl index e9080e13b4..65ce29c64e 100644 --- a/modules/newhorizons/shaders/dilation_fs.glsl +++ b/modules/newhorizons/shaders/dilation_fs.glsl @@ -28,6 +28,7 @@ in vec2 vs_uv; out vec4 color; uniform sampler2D tex; +uniform sampler2D stencil; vec2 offsets[8] = { vec2(-1.0, -1.0), @@ -50,8 +51,9 @@ vec3 gatherColors(vec2 position) { vec4 colors[8]; for (int i = 0; i < 8; i++) { colors[i] = texture(tex, position + h * offsets[i]); + float s = texture(stencil, position + h * offsets[i]).r; - if (colors[i].a != 0.0) { + if (s != 0.0) { totalColor.rgb += colors[i].rgb; nContributions++; } @@ -62,8 +64,9 @@ vec3 gatherColors(vec2 position) { void main() { vec4 c = texture(tex, vs_uv); + float s = texture(stencil, vs_uv).r; - if (c.a == 0.0) { + if (s == 0.0) { // This means that the current fragment/texel we are looking at has not been // projected on and we only want to do the dilation into these texels diff --git a/modules/newhorizons/shaders/renderableModelProjection_fs.glsl b/modules/newhorizons/shaders/renderableModelProjection_fs.glsl index 0d0bcbec89..34d683f98b 100644 --- a/modules/newhorizons/shaders/renderableModelProjection_fs.glsl +++ b/modules/newhorizons/shaders/renderableModelProjection_fs.glsl @@ -31,7 +31,8 @@ in vec4 vs_normal; in vec2 vs_uv; in vec4 ProjTexCoord; -out vec4 color; +layout (location = 0) out vec4 color; +layout (location = 1) out float stencil; uniform sampler2D projectionTexture; @@ -44,22 +45,24 @@ bool inRange(float x, float a, float b) { } void main() { - vec2 uv = vec2(0.5,0.5)*vs_uv+vec2(0.5,0.5); + vec2 uv = vec2(0.5,0.5)*vs_uv+vec2(0.5,0.5); - vec3 n = normalize(vs_normal.xyz); - vec4 projected = ProjTexCoord; + vec3 n = normalize(vs_normal.xyz); + vec4 projected = ProjTexCoord; - //normalize - projected.x /= projected.w; - projected.y /= projected.w; - //invert gl coordinates - projected.x = 1 - projected.x; - - if ((inRange(projected.x, 0, 1) && inRange(projected.y, 0, 1)) && (dot(n, boresight) < 0)) { + // normalize + projected.x /= projected.w; + projected.y /= projected.w; + // invert gl coordinates + projected.x = 1 - projected.x; + + if ((inRange(projected.x, 0, 1) && inRange(projected.y, 0, 1)) && (dot(n, boresight) < 0)) { color = texture(projectionTexture, projected.xy); color.a = 1.0; - } - else { - color = vec4(vec3(0.0), 1.0); - } + stencil = 1.0; + } + else { + color = vec4(vec3(0.0), 1.0); + stencil = 0.0; + } } diff --git a/modules/newhorizons/shaders/renderableModel_fs.glsl b/modules/newhorizons/shaders/renderableModel_fs.glsl index d90d70e350..a5a431042b 100644 --- a/modules/newhorizons/shaders/renderableModel_fs.glsl +++ b/modules/newhorizons/shaders/renderableModel_fs.glsl @@ -70,13 +70,13 @@ Fragment getFragment() { vec4 textureColor = texture(baseTexture, vs_st); vec4 projectionColor = texture(projectionTexture, vs_st); - // if (projectionColor.a != 0.0) { + if (projectionColor.a != 0.0) { textureColor.rgb = mix( textureColor.rgb, projectionColor.rgb, _projectionFading ); - // } + } Fragment frag; frag.color = max(intensity * textureColor, ambient); diff --git a/modules/newhorizons/shaders/renderablePlanetProjection_fs.glsl b/modules/newhorizons/shaders/renderablePlanetProjection_fs.glsl index 3f6e2be3ba..7213c6c22f 100644 --- a/modules/newhorizons/shaders/renderablePlanetProjection_fs.glsl +++ b/modules/newhorizons/shaders/renderablePlanetProjection_fs.glsl @@ -27,7 +27,9 @@ #include "PowerScaling/powerScaling_vs.hglsl" in vec4 vs_position; -out vec4 color; + +layout (location = 0) out vec4 color; +layout (location = 1) out vec4 stencil; uniform sampler2D projectionTexture; @@ -82,8 +84,10 @@ void main() { // The 1-x is in this texture call because of flipped textures // to be fixed soon ---abock color = texture(projectionTexture, vec2(projected.x, 1-projected.y)); + stencil = vec4(1.0); } else { color = vec4(0.0); + stencil = vec4(0.0); } } \ No newline at end of file diff --git a/modules/newhorizons/util/projectioncomponent.cpp b/modules/newhorizons/util/projectioncomponent.cpp index 7b78bb8f05..7d99849c7c 100644 --- a/modules/newhorizons/util/projectioncomponent.cpp +++ b/modules/newhorizons/util/projectioncomponent.cpp @@ -186,9 +186,9 @@ bool ProjectionComponent::initializeProjectionSettings(const Dictionary& diction _potentialTargets.resize(potentialTargets.size()); for (int i = 0; i < potentialTargets.size(); ++i) { - std::string target; - potentialTargets.getValue(std::to_string(i + 1), target); - _potentialTargets[i] = target; +std::string target; +potentialTargets.getValue(std::to_string(i + 1), target); +_potentialTargets[i] = target; } } @@ -285,6 +285,11 @@ void ProjectionComponent::imageProjectBegin() { static_cast(_projectionTexture->width()), static_cast(_projectionTexture->height()) ); + + if (_needsTextureMapDilation) { + GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 }; + glDrawBuffers(2, buffers); + } } void ProjectionComponent::imageProjectEnd() { @@ -293,12 +298,16 @@ void ProjectionComponent::imageProjectEnd() { glDisable(GL_BLEND); - ghoul::opengl::TextureUnit unit; - unit.activate(); + ghoul::opengl::TextureUnit unit[2]; + unit[0].activate(); _projectionTexture->bind(); + unit[1].activate(); + _dilation.stencilTexture->bind(); + _dilation.program->activate(); - _dilation.program->setUniform("tex", unit); + _dilation.program->setUniform("tex", unit[0]); + _dilation.program->setUniform("stencil", unit[1]); glBindVertexArray(_dilation.vao); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -337,6 +346,22 @@ bool ProjectionComponent::auxiliaryRendertarget() { if (_needsTextureMapDilation) { + // We only need the stencil texture if we need to dilate + glFramebufferTexture2D( + GL_FRAMEBUFFER, + GL_COLOR_ATTACHMENT1, + GL_TEXTURE_2D, + *_dilation.stencilTexture, + 0 + ); + + // check FBO status + status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if (status != GL_FRAMEBUFFER_COMPLETE) { + LERROR("Main Framebuffer incomplete"); + completeSuccess &= false; + } + glGenFramebuffers(1, &_dilation.fbo); glBindFramebuffer(GL_FRAMEBUFFER, _dilation.fbo); glFramebufferTexture2D( @@ -348,7 +373,7 @@ bool ProjectionComponent::auxiliaryRendertarget() { ); // check FBO status - GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (status != GL_FRAMEBUFFER_COMPLETE) { LERROR("Dilation Framebuffer incomplete"); completeSuccess &= false; @@ -523,6 +548,16 @@ bool ProjectionComponent::generateProjectionLayerTexture() { _dilation.texture->uploadTexture(); //_dilation.texture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap); } + + _dilation.stencilTexture = std::make_unique( + glm::uvec3(maxSize, maxSize / 2, 1), + ghoul::opengl::Texture::Format::RGBA + ); + + if (_dilation.stencilTexture) { + _dilation.stencilTexture->uploadTexture(); + //_dilation.texture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap); + } } diff --git a/modules/newhorizons/util/projectioncomponent.h b/modules/newhorizons/util/projectioncomponent.h index 563494d692..13731d9c24 100644 --- a/modules/newhorizons/util/projectioncomponent.h +++ b/modules/newhorizons/util/projectioncomponent.h @@ -124,6 +124,7 @@ protected: GLuint vbo; std::unique_ptr program; std::unique_ptr texture; + std::unique_ptr stencilTexture; } _dilation; };