Use custom opengl call for bloom fullscreen shader.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@15022 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
vincentlj 2014-01-12 21:07:14 +00:00
parent 2dea6ded0d
commit 9968ff1ccf
3 changed files with 735 additions and 663 deletions

View File

@ -2,10 +2,12 @@
uniform sampler2D tex; uniform sampler2D tex;
uniform float low; uniform float low;
in vec2 uv;
void main() void main()
{ {
vec3 weights = vec3(0.2126, 0.7152, 0.0722); // ITU-R BT. 709 vec3 weights = vec3(0.2126, 0.7152, 0.0722); // ITU-R BT. 709
vec3 col = texture2D(tex, gl_TexCoord[0].xy).xyz; vec3 col = texture2D(tex, uv).xyz;
float luma = dot(weights, col); float luma = dot(weights, col);
col *= smoothstep(low, 0.9, luma); col *= smoothstep(low, 0.9, luma);

View File

@ -0,0 +1,10 @@
#version 130
in vec2 Position;
in vec2 Texcoord;
out vec2 uv;
void main() {
uv = Texcoord;
gl_Position = vec4(Position, 0., 1.);
}

View File

@ -31,6 +31,7 @@
#include "race/race_manager.hpp" #include "race/race_manager.hpp"
#include "tracks/track.hpp" #include "tracks/track.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"
#include "graphics/glwrap.hpp"
#include <SViewFrustum.h> #include <SViewFrustum.h>
@ -222,6 +223,72 @@ void PostProcessing::renderSolid(const u32 cam)
} }
} }
GLuint quad_vbo = 0;
static void initQuadVBO()
{
initGL();
const float quad_vertex[] = {
-1., -1., 0., 0., // UpperLeft
-1., 1., 0., 1., // LowerLeft
1., -1., 1., 0., // UpperRight
1., 1., 1., 1., // LowerRight
};
glGenBuffers(1, &quad_vbo);
glBindBuffer(GL_ARRAY_BUFFER, quad_vbo);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), quad_vertex, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
namespace BloomShader
{
GLuint Program = 0;
GLuint attrib_position, attrib_texcoord;
GLuint uniform_texture, uniform_low;
GLuint vao = 0;
void init()
{
initGL();
Program = LoadProgram(file_manager->getAsset("shaders/screenquad.vert").c_str(), file_manager->getAsset("shaders/bloom.frag").c_str());
attrib_position = glGetAttribLocation(Program, "Position");
attrib_texcoord = glGetAttribLocation(Program, "Texcoord");
uniform_texture = glGetUniformLocation(Program, "tex");
uniform_low = glGetUniformLocation(Program, "low");
if (!quad_vbo)
initQuadVBO();
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, quad_vbo);
glEnableVertexAttribArray(attrib_position);
glEnableVertexAttribArray(attrib_texcoord);
glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid*)(2 * sizeof(float)));
glBindVertexArray(0);
}
}
static
void renderBloom(ITexture *in)
{
if (!BloomShader::Program)
BloomShader::init();
const float threshold = World::getWorld()->getTrack()->getBloomThreshold();
glUseProgram(BloomShader::Program);
glBindVertexArray(BloomShader::vao);
glUniform1f(BloomShader::uniform_low, threshold);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, static_cast<irr::video::COpenGLTexture*>(in)->getOpenGLTextureName());
glUniform1i(BloomShader::uniform_texture, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** Render the post-processed scene */ /** Render the post-processed scene */
void PostProcessing::render() void PostProcessing::render()
@ -268,15 +335,8 @@ void PostProcessing::render()
if (globalbloom) if (globalbloom)
{ {
const float threshold = World::getWorld()->getTrack()->getBloomThreshold();
((BloomProvider *) irr_driver->getCallback(ES_BLOOM))->setThreshold(threshold);
// Catch bright areas, and progressively minify
m_material.MaterialType = irr_driver->getShader(ES_BLOOM);
m_material.setTexture(0, in);
drv->setRenderTarget(irr_driver->getRTT(RTT_TMP3), true, false); drv->setRenderTarget(irr_driver->getRTT(RTT_TMP3), true, false);
renderBloom(in);
drawQuad(cam, m_material);
} }
// Do we have any forced bloom nodes? If so, draw them now // Do we have any forced bloom nodes? If so, draw them now