From 2930a86959df546008a64c6a7b5775dcce26f80a Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 4 Mar 2015 00:55:01 +0100 Subject: [PATCH] Made first attempt at blurring flames. Conceptually bad, looks very bad. --- data/shader/flame.fsh | 2 +- data/shader/flame_post.fsh | 26 +++++++++++++++++++++++++- graphics.cc | 27 +++++++++++++++++++++++++-- graphics.hh | 3 +++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/data/shader/flame.fsh b/data/shader/flame.fsh index ee34d2c..0bf4756 100644 --- a/data/shader/flame.fsh +++ b/data/shader/flame.fsh @@ -6,5 +6,5 @@ out vec4 oColor; void main() { - oColor = vec4(fColor, 0.5); + oColor = vec4(fColor, 1.0); } diff --git a/data/shader/flame_post.fsh b/data/shader/flame_post.fsh index 7a01fd9..6518b0d 100644 --- a/data/shader/flame_post.fsh +++ b/data/shader/flame_post.fsh @@ -3,9 +3,33 @@ in vec2 vTexCoord; uniform sampler2D flame_fbo; +uniform sampler2D light_fbo; +uniform int windowSizeX; +uniform int windowSizeY; out vec4 oColor; +const float lookup_offset = 4.0; + void main() { - oColor = texture(flame_fbo, vTexCoord).rgba; + vec4 color = texture(flame_fbo, vTexCoord).rgba; + if (color == vec4(0.0, 0.0, 0.0, 1.0)) { + oColor = texture(light_fbo, vTexCoord).rgba; + } + else { + vec4 sum = vec4(0.0); + for(float i = -lookup_offset; i<=lookup_offset; i+=1.0) { + for(float j = -lookup_offset; j<=lookup_offset; j+=1.0) { + vec4 flame_pixel = texture(flame_fbo, vec2(vTexCoord.x + i * 1.0/(windowSizeX/2.0), vTexCoord.y + j * 1.0/(windowSizeY/2.0)))/pow(lookup_offset*2+1, 2.0); + vec4 light_pixel = texture(light_fbo, vec2(vTexCoord.x + i * 1.0/(windowSizeX/2.0), vTexCoord.y + j * 1.0/(windowSizeY/2.0)))/pow(lookup_offset*2+1, 2.0); + if (flame_pixel == vec4(0.0, 0.0, 0.0, 1.0)) { + sum += light_pixel; + } + else { + sum += mix(flame_pixel, light_pixel, 0.7); + } + } + } + oColor = sum; + } } diff --git a/graphics.cc b/graphics.cc index fbf4e1c..6cfc0bc 100644 --- a/graphics.cc +++ b/graphics.cc @@ -155,11 +155,30 @@ void Graphics::init(Level* level) { framebuffer_flames = SharedFrameBufferObject(new FrameBufferObject()); framebuffer_flames->attachColorTexture("oColor", flame_fbo_color_texture); framebuffer_flames->setDepthTexture(flame_fbo_depth_texture); - framebuffer_flames->setClearColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.0f)); + framebuffer_flames->setClearColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)); framebuffer_flames->validate(); + light_fbo_color_texture = SharedTexture2D(new Texture2D(windowSize, GL_RGBA8)); + light_fbo_color_texture->setMinFilter(GL_NEAREST); + light_fbo_color_texture->setMagFilter(GL_NEAREST); + light_fbo_color_texture->setWrapS(GL_CLAMP_TO_BORDER); + light_fbo_color_texture->setWrapT(GL_CLAMP_TO_BORDER); + light_fbo_depth_texture = SharedTexture2D(new Texture2D(windowSize, GL_DEPTH_COMPONENT24)); + light_fbo_depth_texture->setMinFilter(GL_NEAREST); + light_fbo_depth_texture->setMagFilter(GL_NEAREST); + light_fbo_depth_texture->setWrapS(GL_CLAMP_TO_BORDER); + light_fbo_depth_texture->setWrapT(GL_CLAMP_TO_BORDER); + framebuffer_light = SharedFrameBufferObject(new FrameBufferObject()); + framebuffer_light->attachColorTexture("oColor", light_fbo_color_texture); + framebuffer_light->setDepthTexture(light_fbo_depth_texture); + framebuffer_light->setClearColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)); + framebuffer_light->validate(); + flamePostShader->use(); flamePostShader->setTexture("flame_fbo", flame_fbo_color_texture, 15); + flamePostShader->setTexture("light_fbo", light_fbo_color_texture, 16); + flamePostShader->setUniform("windowSizeX", int(windowSize.x)); + flamePostShader->setUniform("windowSizeY", int(windowSize.y)); updateClosestLights(); } @@ -229,7 +248,7 @@ void Graphics::render(double time) } // lighting render pass - glBindFramebuffer(GL_FRAMEBUFFER, 0); + framebuffer_light->bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); lightingShader->use(); @@ -387,6 +406,10 @@ void Graphics::resize(glm::uvec2 windowSize) { for (unsigned int i = 0; iresize(glm::vec2(windowSize.x, windowSize.y)); } + flame_fbo_color_texture->resize(windowSize); + flame_fbo_depth_texture->resize(windowSize); + flamePostShader->setUniform("windowSizeX", int(windowSize.x)); + flamePostShader->setUniform("windowSizeY", int(windowSize.y)); } glm::mat4 Graphics::buildViewMatrix(Level* level) { diff --git a/graphics.hh b/graphics.hh index 57605d5..4b5140a 100644 --- a/graphics.hh +++ b/graphics.hh @@ -42,6 +42,9 @@ class Graphics { SharedFrameBufferObject framebuffer_flames; SharedTexture2D flame_fbo_color_texture; SharedTexture2D flame_fbo_depth_texture; + SharedFrameBufferObject framebuffer_light; + SharedTexture2D light_fbo_color_texture; + SharedTexture2D light_fbo_depth_texture; SharedVertexArrayObject flame_positions; SharedArrayBuffer flame_positions_ab; SharedVertexArrayObject fullscreen_quad;