From afd943c2bf1dd289565be557fa778248fee54247 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 20 Aug 2012 23:27:28 +0300 Subject: [PATCH] Helper functions for post-processors Most post-processors render fullscreen quads and use a very basic vertex shader, so it makes sense to provide these as common resources. --- source/postprocessor.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ source/postprocessor.h | 26 +++++++++++++++++----- 2 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 source/postprocessor.cpp diff --git a/source/postprocessor.cpp b/source/postprocessor.cpp new file mode 100644 index 00000000..add8b1ed --- /dev/null +++ b/source/postprocessor.cpp @@ -0,0 +1,48 @@ +#include "mesh.h" +#include "meshbuilder.h" +#include "postprocessor.h" +#include "shader.h" + +namespace { + +const char fullscreen_vs_source[] = + "varying vec2 texcoord;\n" + "void main()\n" + "{\n" + " gl_Position = gl_Vertex;\n" + " texcoord = gl_Vertex.xy*0.5+0.5;\n" + "}\n"; + +} + + +namespace Msp { +namespace GL { + +Shader &PostProcessor::get_fullscreen_vertex_shader() +{ + static Shader shader(VERTEX_SHADER, fullscreen_vs_source); + return shader; +} + +const Mesh &PostProcessor::get_fullscreen_quad() +{ + static const Mesh &mesh = create_fullscreen_quad(); + return mesh; +} + +const Mesh &PostProcessor::create_fullscreen_quad() +{ + static Mesh mesh(GL::VERTEX2); + MeshBuilder builder(mesh); + builder.begin(TRIANGLE_STRIP); + builder.vertex(-1, 1); + builder.vertex(-1, -1); + builder.vertex(1, 1); + builder.vertex(1, -1); + builder.end(); + return mesh; +} + +} // namespace GL +} // namespace Msp diff --git a/source/postprocessor.h b/source/postprocessor.h index 9e4220ab..d9489de2 100644 --- a/source/postprocessor.h +++ b/source/postprocessor.h @@ -4,10 +4,14 @@ namespace Msp { namespace GL { +class Mesh; +class Shader; class Texture2D; /** -Base class for post-processing effects. +Base class for post-processing effects. Post-processors receive the contents +of the entire framebuffer as a texture and render it back, altering it in the +process. */ class PostProcessor { @@ -16,10 +20,22 @@ protected: public: virtual ~PostProcessor() { } - /** - Renders the effect. Takes the source texture as a parameter. - */ - virtual void render(const Texture2D &, const Texture2D &) = 0; + /// Renders the effect. + virtual void render(const Texture2D &color, const Texture2D &depth) = 0; + +protected: + /** Returns a vertex shader suitable for rendering a fullscreen quad. Input + vertices are assumed to be in normalized device coordinates; no transform is + done. The shader provides a varying vec2 texcoord for fragment a shader to + access textures. */ + static Shader &get_fullscreen_vertex_shader(); + + /** Returns a mesh consisting of a single quad, covering the entire screen. + The vertices are in normalized device coordinates. */ + static const Mesh &get_fullscreen_quad(); + +private: + static const Mesh &create_fullscreen_quad(); }; } // namespace GL -- 2.45.2