Commit cc148866 authored by Dario Seyb's avatar Dario Seyb

improved bloom

parent 6043e95a
#version 330
uniform sampler2D uSamplerColor;
uniform sampler2D uSamplerBlur;
uniform float uBloomFactor;
......@@ -10,8 +9,7 @@ out vec4 oColor;
void main()
{
vec3 originalColor = texture(uSamplerColor, vTexCoord).rgb;
vec3 blurColor = texture(uSamplerBlur, vTexCoord).rgb;
oColor = vec4(originalColor + blurColor * uBloomFactor, 1);
oColor = vec4(blurColor, uBloomFactor);
}
......@@ -40,5 +40,5 @@ vec4 specularSmoothness() {
if(uHasSpecularSmoothnessMap) {
return texture(uSpecularSmoothnessMap, inTexCoord);
}
return vec4(0.4, 0.4, 0.4, 0.6);
return vec4(1, 1, 1, 1);
}
......@@ -10,10 +10,12 @@ private:
ACGL::OpenGL::SharedShaderProgram m_blurProgram;
ACGL::OpenGL::SharedFrameBufferObject m_extractBuffer;
ACGL::OpenGL::SharedFrameBufferObject m_blurBuffer;
ACGL::OpenGL::SharedFrameBufferObject m_blurBufferHorizontal;
ACGL::OpenGL::SharedFrameBufferObject m_blurBufferVertical;
ACGL::OpenGL::SharedTexture2D m_extractTexture;
ACGL::OpenGL::SharedTexture2D m_blurTexture;
ACGL::OpenGL::SharedTexture2D m_blurTextureHorizontal;
ACGL::OpenGL::SharedTexture2D m_blurTextureVertical;
std::array<float, 15> m_gaussianWeights;
std::array<float, 15> m_sampleOffsets;
......
......@@ -25,15 +25,21 @@ void BloomPostFX::startup() {
m_extractBuffer->validate();
m_extractBuffer->setClearColor(glm::vec4(0, 0, 0, 0));
m_blurTexture = m_renderer->createScreenspaceTexture(ScreenSpaceSize::HALF, GL_RGB32F);
m_blurBuffer = SharedFrameBufferObject(new FrameBufferObject());
m_blurBuffer->attachColorTexture("oColor", m_blurTexture);
m_blurBuffer->validate();
m_blurBuffer->setClearColor(glm::vec4(0, 0, 0, 0));
m_blurTextureHorizontal = m_renderer->createScreenspaceTexture(ScreenSpaceSize::HALF, GL_RGB32F);
m_blurBufferHorizontal = SharedFrameBufferObject(new FrameBufferObject());
m_blurBufferHorizontal->attachColorTexture("oColor", m_blurTextureHorizontal);
m_blurBufferHorizontal->validate();
m_blurBufferHorizontal->setClearColor(glm::vec4(0, 0, 0, 0));
m_blurTextureVertical = m_renderer->createScreenspaceTexture(ScreenSpaceSize::HALF, GL_RGB32F);
m_blurBufferVertical = SharedFrameBufferObject(new FrameBufferObject());
m_blurBufferVertical->attachColorTexture("oColor", m_blurTextureVertical);
m_blurBufferVertical->validate();
m_blurBufferVertical->setClearColor(glm::vec4(0, 0, 0, 0));
m_blitProgram = ShaderProgramFileManager::the()->get(ShaderProgramCreator("Bloom/Blit"));
m_extractProgram = ShaderProgramFileManager::the()->get(ShaderProgramCreator("Bloom/Extract"));
m_blurProgram = ShaderProgramFileManager::the()->get(ShaderProgramCreator("Bloom/Blur").fragmentDataLocations(m_blurBuffer->getAttachmentLocations()));
m_blurProgram = ShaderProgramFileManager::the()->get(ShaderProgramCreator("Bloom/Blur").fragmentDataLocations(m_blurBufferHorizontal->getAttachmentLocations()));
int sampleCount = m_gaussianWeights.size();
// The first sample always has a zero offset.
......@@ -73,6 +79,7 @@ void BloomPostFX::apply(ACGL::OpenGL::ConstSharedTextureBase inputBuffer, ACGL::
auto bloomSize = glm::vec2(m_extractTexture->getWidth(), m_extractTexture->getHeight());
glDisable(GL_BLEND);
// Extract bright pixels
m_extractBuffer->bind();
m_extractBuffer->clearBuffers();
......@@ -80,44 +87,63 @@ void BloomPostFX::apply(ACGL::OpenGL::ConstSharedTextureBase inputBuffer, ACGL::
m_extractProgram->use();
m_extractProgram->setTexture("uSamplerColor", inputBuffer, 0);
m_extractProgram->setUniform("uThreshold", 1.0f);
m_extractProgram->setUniform("uThreshold", 0.9f);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
std::array<glm::vec2, 15> offsets;
// Blur horizontally
m_blurBuffer->bind();
m_blurBuffer->clearBuffers();
m_blurProgram->use();
m_blurProgram->setUniform("uSampleWeights", m_gaussianWeights.size(), m_gaussianWeights.data());
m_blurProgram->setTexture("uSamplerColor", m_extractTexture, 0);
for (int i = 0; i < offsets.size(); i++) {
offsets[i] = glm::vec2(m_sampleOffsets[i] * 1.0f/ m_extractTexture->getWidth(), 0);
}
m_blurProgram->setUniform("uSampleOffsets", offsets.size(), offsets.data());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Blur vertically
m_extractBuffer->bind();
m_extractBuffer->clearBuffers();
m_blurProgram->setTexture("uSamplerColor", m_blurTexture, 0);
for (int i = 0; i < offsets.size(); i++) {
offsets[i] = glm::vec2(0, m_sampleOffsets[i] * 1.0f / m_blurTexture->getHeight());
}
m_blurProgram->setUniform("uSampleOffsets", offsets.size(), offsets.data());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
const static float weights[] = { 1, 0.75, 0.25 };
const static float scales[] = { 1, 2, 4.0 };
// Mix with input
outputBuffer->bind();
outputBuffer->clearBuffers();
glViewport(0, 0, inputBuffer->getWidth(), inputBuffer->getHeight());
m_blitProgram->use();
m_blitProgram->setTexture("uSamplerColor", inputBuffer, 0);
m_blitProgram->setTexture("uSamplerBlur", m_extractTexture, 1);
m_blitProgram->setTexture("uSamplerBlur", inputBuffer, 0);
m_blitProgram->setUniform("uBloomFactor", 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
auto blurSrc = m_extractTexture;
for (int pass = 0; pass < 3; pass++) {
glViewport(0, 0, bloomSize.x, bloomSize.y);
// Blur horizontally
m_blurBufferHorizontal->bind();
m_blurBufferHorizontal->clearBuffers();
m_blurProgram->use();
m_blurProgram->setUniform("uSampleWeights", m_gaussianWeights.size(), m_gaussianWeights.data());
m_blurProgram->setTexture("uSamplerColor", blurSrc, 0);
for (int i = 0; i < offsets.size(); i++) {
offsets[i] = glm::vec2(m_sampleOffsets[i] * scales[pass] / blurSrc->getWidth(), 0);
}
m_blurProgram->setUniform("uSampleOffsets", offsets.size(), offsets.data());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Blur vertically
m_blurBufferVertical->bind();
m_blurBufferVertical->clearBuffers();
m_blurProgram->setTexture("uSamplerColor", m_blurTextureHorizontal, 0);
for (int i = 0; i < offsets.size(); i++) {
offsets[i] = glm::vec2(0, m_sampleOffsets[i] * scales[pass] / m_blurTextureHorizontal->getHeight());
}
m_blurProgram->setUniform("uSampleOffsets", offsets.size(), offsets.data());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Add to output
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glViewport(0, 0, inputBuffer->getWidth(), inputBuffer->getHeight());
outputBuffer->bind();
m_blitProgram->use();
m_blitProgram->setTexture("uSamplerBlur", m_blurTextureVertical, 0);
m_blitProgram->setUniform("uBloomFactor", weights[pass]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_BLEND);
blurSrc = m_blurTextureVertical;
}
}
void BloomPostFX::shutdown() {
......
......@@ -259,15 +259,7 @@ void AtmosphereTestScene::switchToMainScene() {
{
auto cockpitPassId = m_renderer->getRenderPassId("Cockpit"_sh);
auto windShield = m_sceneGraph->create();
auto windShieldGeom = Geometry{
VertexArrayObjectCreator("cockpit/cockpit_v2_windshield.obj").create() };
windShieldGeom.vao->setMode(GL_PATCHES);
windShield.assign<Drawable>(
windShieldGeom,
windshieldMaterial,
0, cockpitPassId);
windShield.assign<Transform>();
cockpit = m_sceneGraph->create();
auto cockpitDrawable = cockpit.assign<Drawable>(
......@@ -353,6 +345,16 @@ void AtmosphereTestScene::switchToMainScene() {
0, cockpitPassId);
consoleMiddle.assign<Transform>();
auto windShield = m_sceneGraph->create();
auto windShieldGeom = Geometry{
VertexArrayObjectCreator("cockpit/cockpit_v2_windshield.obj").create() };
windShieldGeom.vao->setMode(GL_PATCHES);
windShield.assign<Drawable>(
windShieldGeom,
windshieldMaterial,
0, cockpitPassId);
windShield.assign<Transform>();
}
loadingText.component<Drawable>()->visible = false;
......@@ -434,7 +436,7 @@ void AtmosphereTestScene::loadMainSceneResources() {
Texture2DFileManager::the()->get(Texture2DCreator(
"cockpit_v2/"
"windshield_DefaultMaterial_AlbedoTransparency.png")),
nullptr, nullptr, cockpitMaterial.prog, false,
nullptr, nullptr, cockpitShader, false,
RenderQueue::OPAQUE, GL_NONE };
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment