Make use of a secondary stencil buffer for masking the dilation areas

This commit is contained in:
Alexander Bock
2016-08-18 11:43:51 +02:00
parent 1900abc70f
commit e284543434
7 changed files with 75 additions and 28 deletions
@@ -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;
+5 -2
View File
@@ -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
@@ -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;
}
}
@@ -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);
@@ -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);
}
}
@@ -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<GLsizei>(_projectionTexture->width()),
static_cast<GLsizei>(_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<ghoul::opengl::Texture>(
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);
}
}
@@ -124,6 +124,7 @@ protected:
GLuint vbo;
std::unique_ptr<ghoul::opengl::ProgramObject> program;
std::unique_ptr<ghoul::opengl::Texture> texture;
std::unique_ptr<ghoul::opengl::Texture> stencilTexture;
} _dilation;
};