From 7d1df1934208ca25593e3980fd9f61ff806f9fed Mon Sep 17 00:00:00 2001 From: vincentlj Date: Mon, 13 Jan 2014 14:50:16 +0000 Subject: [PATCH] Use a passthrough shader for some fullscreen effect. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@15043 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- data/shaders/texturedquad.frag | 4 +- data/shaders/texturedquad.vert | 2 +- src/graphics/post_processing.cpp | 94 +++++++++++++++++++++----------- 3 files changed, 66 insertions(+), 34 deletions(-) diff --git a/data/shaders/texturedquad.frag b/data/shaders/texturedquad.frag index b2c3f9612..cf1aade40 100644 --- a/data/shaders/texturedquad.frag +++ b/data/shaders/texturedquad.frag @@ -1,9 +1,9 @@ #version 130 uniform sampler2D texture; -in vec2 tc; +in vec2 uv; void main() { - gl_FragColor = texture2D(texture, tc); + gl_FragColor = texture2D(texture, uv); } \ No newline at end of file diff --git a/data/shaders/texturedquad.vert b/data/shaders/texturedquad.vert index 6e14ea24e..5364b2209 100644 --- a/data/shaders/texturedquad.vert +++ b/data/shaders/texturedquad.vert @@ -6,7 +6,7 @@ uniform vec2 texsize; in vec2 position; in vec2 texcoord; -out vec2 tc; +out vec2 uv; void main() { diff --git a/src/graphics/post_processing.cpp b/src/graphics/post_processing.cpp index 3632634b2..00071e558 100644 --- a/src/graphics/post_processing.cpp +++ b/src/graphics/post_processing.cpp @@ -484,6 +484,34 @@ namespace Gaussian6VBlurShader } } +namespace PassThroughShader +{ + GLuint Program = 0; + GLuint attrib_position, attrib_texcoord; + GLuint uniform_texture; + + GLuint vao = 0; + + void init() + { + initGL(); + Program = LoadProgram(file_manager->getAsset("shaders/screenquad.vert").c_str(), file_manager->getAsset("shaders/texturedquad.frag").c_str()); + attrib_position = glGetAttribLocation(Program, "Position"); + attrib_texcoord = glGetAttribLocation(Program, "Texcoord"); + uniform_texture = glGetUniformLocation(Program, "texture"); + 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) @@ -703,6 +731,28 @@ void PostProcessing::renderGaussian6Blur(video::ITexture *in, video::ITexture *t glEnable(GL_DEPTH_TEST); } +static +void renderPassThrough(ITexture *tex) +{ + if (!PassThroughShader::Program) + PassThroughShader::init(); + glDisable(GL_DEPTH_TEST); + + glUseProgram(PassThroughShader::Program); + glBindVertexArray(PassThroughShader::vao); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, static_cast(tex)->getOpenGLTextureName()); + glUniform1i(PassThroughShader::uniform_texture, 0); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glEnable(GL_DEPTH_TEST); + glDisable(GL_BLEND); +} + // ---------------------------------------------------------------------------- /** Render the post-processed scene */ void PostProcessing::render() @@ -853,25 +903,17 @@ void PostProcessing::render() } // end forced bloom // To half - m_material.MaterialType = EMT_SOLID; - m_material.setTexture(0, irr_driver->getRTT(RTT_TMP3)); drv->setRenderTarget(irr_driver->getRTT(RTT_HALF1), true, false); + renderPassThrough(irr_driver->getRTT(RTT_TMP3)); - drawQuad(cam, m_material); // To quarter - m_material.MaterialType = EMT_SOLID; - m_material.setTexture(0, irr_driver->getRTT(RTT_HALF1)); - drv->setRenderTarget(irr_driver->getRTT(RTT_QUARTER1), true, false); - - drawQuad(cam, m_material); - + drv->setRenderTarget(irr_driver->getRTT(RTT_QUARTER1), true, false); + renderPassThrough(irr_driver->getRTT(RTT_HALF1)); + // To eighth - m_material.MaterialType = EMT_SOLID; - m_material.setTexture(0, irr_driver->getRTT(RTT_QUARTER1)); - drv->setRenderTarget(irr_driver->getRTT(RTT_EIGHTH1), true, false); - - drawQuad(cam, m_material); + drv->setRenderTarget(irr_driver->getRTT(RTT_EIGHTH1), true, false); + renderPassThrough(irr_driver->getRTT(RTT_QUARTER1)); // Blur it for distribution. renderGaussian6Blur(irr_driver->getRTT(RTT_EIGHTH1), irr_driver->getRTT(RTT_EIGHTH2), 8.f / UserConfigParams::m_width, 8.f / UserConfigParams::m_height); @@ -1075,26 +1117,16 @@ void PostProcessing::render() } // Final blit + // TODO : Use glBlitFramebuffer drv->setRenderTarget(ERT_FRAME_BUFFER, false, false); if (irr_driver->getNormals()) - { - m_material.MaterialType = irr_driver->getShader(ES_FLIP); - m_material.setTexture(0, irr_driver->getRTT(RTT_NORMAL_AND_DEPTH)); - drawQuad(cam, m_material); - } else if (irr_driver->getSSAOViz()) - { - m_material.MaterialType = irr_driver->getShader(ES_FLIP); - m_material.setTexture(0, irr_driver->getRTT(RTT_SSAO)); - drawQuad(cam, m_material); - } else if (irr_driver->getShadowViz()) - { - m_material.MaterialType = irr_driver->getShader(ES_FLIP); - m_material.setTexture(0, irr_driver->getRTT(RTT_DISPLACE)); - drawQuad(cam, m_material); - } else - { + renderPassThrough(irr_driver->getRTT(RTT_NORMAL_AND_DEPTH)); + else if (irr_driver->getSSAOViz()) + renderPassThrough(irr_driver->getRTT(RTT_SSAO)); + else if (irr_driver->getShadowViz()) + renderPassThrough(irr_driver->getRTT(RTT_SHADOW)); + else renderColorLevel(in); - } } } // render