Fix skybox blending with light scatter

This commit is contained in:
Benau 2018-01-31 01:31:12 +08:00
parent af6b075450
commit 154ad8d0db
4 changed files with 48 additions and 22 deletions

View File

@ -6,6 +6,8 @@ uniform sampler2D diffuse_color;
uniform sampler2D depth_stencil; uniform sampler2D depth_stencil;
uniform sampler2D light_scatter; uniform sampler2D light_scatter;
uniform vec4 bg_color;
out vec4 o_final_color; out vec4 o_final_color;
#stk_include "utils/getPosFromUVDepth.frag" #stk_include "utils/getPosFromUVDepth.frag"
@ -32,8 +34,8 @@ void main()
vec4 color_1 = vec4(tmp * ao + (emitMapValue * emitCol), diffuseMatColor.a); vec4 color_1 = vec4(tmp * ao + (emitMapValue * emitCol), diffuseMatColor.a);
// Fog // Fog
float z = texture(depth_stencil, tc).x; float depth = texture(depth_stencil, tc).x;
vec4 xpos = getPosFromUVDepth(vec3(tc, z), u_inverse_projection_matrix); vec4 xpos = getPosFromUVDepth(vec3(tc, depth), u_inverse_projection_matrix);
float dist = length(xpos.xyz); float dist = length(xpos.xyz);
// fog density // fog density
float factor = (1.0 - exp(u_fog_data.w * dist)); float factor = (1.0 - exp(u_fog_data.w * dist));
@ -42,6 +44,12 @@ void main()
// Additively blend the color by fog // Additively blend the color by fog
color_1 = color_1 + vec4(fog, factor); color_1 = color_1 + vec4(fog, factor);
// For skybox blending later
if (depth == 1.0)
{
color_1 = bg_color;
}
// Light scatter (alpha blend function: (GL_ONE, GL_ONE_MINUS_SRC_ALPHA)) // Light scatter (alpha blend function: (GL_ONE, GL_ONE_MINUS_SRC_ALPHA))
vec4 ls = texture(light_scatter, tc); vec4 ls = texture(light_scatter, tc);
vec4 color_2; vec4 color_2;

View File

@ -28,6 +28,7 @@
#include <matrix4.h> #include <matrix4.h>
#include <SColor.h> #include <SColor.h>
#include <vector3d.h> #include <vector3d.h>
#include <array>
#include <string> #include <string>
#include <vector> #include <vector>
@ -219,6 +220,15 @@ private:
setUniformsImpl<N + 1>(arg...); setUniformsImpl<N + 1>(arg...);
} // setUniformsImpl } // setUniformsImpl
// ------------------------------------------------------------------------
/** Implementation for setUniforms for a 4 floats uniform. */
template<unsigned N = 0, typename... Args1>
void setUniformsImpl(const std::array<float, 4> &ff, Args1... arg) const
{
glUniform4f(m_uniforms[N], ff[0], ff[1], ff[2], ff[3]);
setUniformsImpl<N + 1>(arg...);
} // setUniformsImpl
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Implementation for setUniforms for a vector3df uniform. */ /** Implementation for setUniforms for a vector3df uniform. */
template<unsigned N = 0, typename... Args1> template<unsigned N = 0, typename... Args1>

View File

@ -195,14 +195,15 @@ void ShaderBasedRenderer::renderShadows()
} }
// ============================================================================ // ============================================================================
class CombineDiffuseColor : public TextureShader<CombineDiffuseColor, 7> class CombineDiffuseColor : public TextureShader<CombineDiffuseColor, 7,
std::array<float, 4> >
{ {
public: public:
CombineDiffuseColor() CombineDiffuseColor()
{ {
loadProgram(OBJECT, GL_VERTEX_SHADER, "screenquad.vert", loadProgram(OBJECT, GL_VERTEX_SHADER, "screenquad.vert",
GL_FRAGMENT_SHADER, "combine_diffuse_color.frag"); GL_FRAGMENT_SHADER, "combine_diffuse_color.frag");
assignUniforms(); assignUniforms("bg_color");
assignSamplerNames(0, "diffuse_map", ST_NEAREST_FILTERED, assignSamplerNames(0, "diffuse_map", ST_NEAREST_FILTERED,
1, "specular_map", ST_NEAREST_FILTERED, 1, "specular_map", ST_NEAREST_FILTERED,
2, "ssao_tex", ST_NEAREST_FILTERED, 2, "ssao_tex", ST_NEAREST_FILTERED,
@ -213,10 +214,10 @@ public:
} // CombineDiffuseColor } // CombineDiffuseColor
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void render(GLuint dm, GLuint sm, GLuint st, GLuint gm, GLuint dc, void render(GLuint dm, GLuint sm, GLuint st, GLuint gm, GLuint dc,
GLuint ds, GLuint lt) GLuint ds, GLuint lt, const std::array<float, 4> & bg_color)
{ {
setTextureUnits(dm, sm, st, gm, dc, ds, lt); setTextureUnits(dm, sm, st, gm, dc, ds, lt);
drawFullScreenEffect(); drawFullScreenEffect(bg_color);
} // render } // render
}; // CombineDiffuseColor }; // CombineDiffuseColor
@ -226,6 +227,7 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca
bool hasShadow, bool hasShadow,
bool forceRTT) bool forceRTT)
{ {
if (CVS->isARBUniformBufferObjectUsable()) if (CVS->isARBUniformBufferObjectUsable())
{ {
glBindBufferBase(GL_UNIFORM_BUFFER, 0, glBindBufferBase(GL_UNIFORM_BUFFER, 0,
@ -256,22 +258,9 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca
{ {
m_rtts->getFBO(FBO_SP).bind(); m_rtts->getFBO(FBO_SP).bind();
std::array<float, 4> clear_color_world = {{ 1.0f, 1.0f, 1.0f, 0.0f }};
if (World::getWorld() != NULL)
{
clear_color_world[0] =
irr_driver->getClearColor().getRed() / 255.0f;
clear_color_world[1] =
irr_driver->getClearColor().getGreen() / 255.0f;
clear_color_world[2] =
irr_driver->getClearColor().getBlue() / 255.0f;
clear_color_world[3] =
irr_driver->getClearColor().getAlpha() / 255.0f;
}
float clear_color_empty[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; float clear_color_empty[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
float clear_color_gloss[4] = { 0.1f, 0.1f, 0.0f, 0.0f }; float clear_color_gloss[4] = { 0.1f, 0.1f, 0.0f, 0.0f };
glClearBufferfv(GL_COLOR, 0, clear_color_world.data()); glClearBufferfv(GL_COLOR, 0, clear_color_empty);
glClearBufferfv(GL_COLOR, 1, clear_color_empty); glClearBufferfv(GL_COLOR, 1, clear_color_empty);
glClearBufferfv(GL_COLOR, 2, clear_color_gloss); glClearBufferfv(GL_COLOR, 2, clear_color_gloss);
glClearBufferfi(GL_DEPTH_STENCIL, 0, 1.0f, 0); glClearBufferfi(GL_DEPTH_STENCIL, 0, 1.0f, 0);
@ -346,6 +335,19 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE); glDepthMask(GL_FALSE);
glDisable(GL_BLEND); glDisable(GL_BLEND);
std::array<float, 4> bg_color = {{ 1.0f, 1.0f, 1.0f, 0.0f }};
if (World::getWorld() != NULL)
{
bg_color[0] =
irr_driver->getClearColor().getRed() / 255.0f;
bg_color[1] =
irr_driver->getClearColor().getGreen() / 255.0f;
bg_color[2] =
irr_driver->getClearColor().getBlue() / 255.0f;
bg_color[3] =
irr_driver->getClearColor().getAlpha() / 255.0f;
}
CombineDiffuseColor::getInstance()->render( CombineDiffuseColor::getInstance()->render(
m_rtts->getRenderTarget(RTT_DIFFUSE), m_rtts->getRenderTarget(RTT_DIFFUSE),
m_rtts->getRenderTarget(RTT_SPECULAR), m_rtts->getRenderTarget(RTT_SPECULAR),
@ -353,7 +355,8 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca
m_rtts->getRenderTarget(RTT_SP_GLOSS), m_rtts->getRenderTarget(RTT_SP_GLOSS),
m_rtts->getRenderTarget(RTT_SP_DIFF_COLOR), m_rtts->getRenderTarget(RTT_SP_DIFF_COLOR),
m_rtts->getDepthStencilTexture(), m_rtts->getDepthStencilTexture(),
m_rtts->getRenderTarget(RTT_HALF1)); m_rtts->getRenderTarget(RTT_HALF1), !m_skybox ?
bg_color : std::array<float, 4>{{0.0f, 0.0f, 0.0f, 0.0f}});
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER();
} }

View File

@ -347,7 +347,11 @@ void Skybox::render(const scene::ICameraSceneNode *camera) const
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
assert(m_skybox_textures.size() == 6); assert(m_skybox_textures.size() == 6);
glDisable(GL_BLEND); if (CVS->isDeferredEnabled())
{
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
}
SkyboxShader::getInstance()->use(); SkyboxShader::getInstance()->use();
SkyboxShader::getInstance()->bindVertexArray(); SkyboxShader::getInstance()->bindVertexArray();
@ -357,6 +361,7 @@ void Skybox::render(const scene::ICameraSceneNode *camera) const
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0); glBindVertexArray(0);
glDisable(GL_BLEND);
} // renderSkybox } // renderSkybox
#endif // !SERVER_ONLY #endif // !SERVER_ONLY