From a811ed57be4bd19e171a79ebd72dd18b5ff1afb1 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 4 Mar 2015 12:13:58 +0100 Subject: [PATCH] Now also blurring the edges of the flames. --- data/shader/flame.fsh | 9 ++++++++- data/shader/flame.gsh | 10 +++++----- graphics.cc | 42 +++++++++++++++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/data/shader/flame.fsh b/data/shader/flame.fsh index 9ae570f..7a4127d 100644 --- a/data/shader/flame.fsh +++ b/data/shader/flame.fsh @@ -4,7 +4,14 @@ in vec3 fColor; out vec4 oColor; +uniform bool withColor; + void main() { - oColor = vec4(fColor, 0.6); + if (withColor) { + oColor = vec4(fColor, 0.6); + } + else { + oColor = vec4(0.0, 0.0, 0.0, 0.0); + } } diff --git a/data/shader/flame.gsh b/data/shader/flame.gsh index 6fa8e4d..0fda82a 100644 --- a/data/shader/flame.gsh +++ b/data/shader/flame.gsh @@ -1,6 +1,6 @@ #version 150 -uniform mat4 viewProjectionMatrix; +uniform mat4 modelViewProjectionMatrix; uniform float time; uniform bool bottom; uniform bool left; @@ -62,19 +62,19 @@ void main() { float rightAngle = PI * 2.0 / resolution * (j+1); vec4 offset = vec4(cos(rightAngle) * downRadius, i, -sin(rightAngle) * downRadius, 0.0); - gl_Position = gl_in[0].gl_Position + viewProjectionMatrix * offset; + gl_Position = gl_in[0].gl_Position + modelViewProjectionMatrix * offset; EmitVertex(); offset = vec4(cos(rightAngle) * upRadius, i + step, -sin(rightAngle) * upRadius, 0.0); - gl_Position = gl_in[0].gl_Position + viewProjectionMatrix * offset; + gl_Position = gl_in[0].gl_Position + modelViewProjectionMatrix * offset; EmitVertex(); offset = vec4(cos(leftAngle) * downRadius, i, -sin(leftAngle) * downRadius, 0.0); - gl_Position = gl_in[0].gl_Position + viewProjectionMatrix * offset; + gl_Position = gl_in[0].gl_Position + modelViewProjectionMatrix * offset; EmitVertex(); offset = vec4(cos(leftAngle) * upRadius, i + step, -sin(leftAngle) * upRadius, 0.0); - gl_Position = gl_in[0].gl_Position + viewProjectionMatrix * offset; + gl_Position = gl_in[0].gl_Position + modelViewProjectionMatrix * offset; EmitVertex(); EndPrimitive(); diff --git a/graphics.cc b/graphics.cc index 88db43e..f2dcc7e 100644 --- a/graphics.cc +++ b/graphics.cc @@ -278,18 +278,16 @@ void Graphics::render(double time) level->render(lightingShader, true, &lightingViewProjectionMatrix, &depthBiasVPs); // draw flames on top + flameShader->use(); // cull faces to get consistent color while using alpha glEnable(GL_CULL_FACE); glCullFace(GL_BACK); - glEnable(GL_STENCIL_TEST); - glStencilFunc(GL_ALWAYS, 1, 0xFF); //Set any stencil to 1 - glStencilFunc(GL_ALWAYS, 1, 0xFF); - glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); - glStencilMask(0xFF);//write to stencil buffer - glClear(GL_STENCIL_BUFFER_BIT);//clear stencil buffer - flameShader->use(); + + // draw with colors flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix); + flameShader->setUniform("modelViewProjectionMatrix", lightingViewProjectionMatrix); + flameShader->setUniform("withColor", true); flameShader->setUniform("time", (float) time); flameShader->setUniform("bottom", true); flameShader->setUniform("left", true); @@ -302,10 +300,36 @@ void Graphics::render(double time) flameShader->setUniform("left", false); flame_positions->render(); - glDisable(GL_CULL_FACE); + glDepthMask(GL_FALSE); + + // draw slightly larger only for stencil buffer to blur edges + glEnable(GL_STENCIL_TEST); + glStencilFunc(GL_ALWAYS, 1, 0xFF); //Set any stencil to 1 + glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); + glStencilMask(0xFF);//write to stencil buffer + glClear(GL_STENCIL_BUFFER_BIT);//clear stencil buffer + + glm::mat4 modelMatrix = glm::scale(glm::vec3(1.1f)); + flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix); + flameShader->setUniform("modelViewProjectionMatrix", lightingViewProjectionMatrix * modelMatrix); + flameShader->setUniform("withColor", false); + flameShader->setUniform("time", (float) time); + flameShader->setUniform("bottom", true); + flameShader->setUniform("left", true); + flame_positions->render(); + flameShader->setUniform("left", false); + flame_positions->render(); + flameShader->setUniform("bottom", false); + flameShader->setUniform("left", true); + flame_positions->render(); + flameShader->setUniform("left", false); + flame_positions->render(); glStencilFunc(GL_EQUAL, 1, 0xFF); //Pass test if stencil value is 1 - glStencilMask(0x00); //don't write to stencil buffer + glStencilMask(0x00);// don't write to stencil buffer + + glDepthMask(GL_TRUE); + glDisable(GL_CULL_FACE); flamePostShader->use(); fullscreen_quad->render();