Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PostProcessorFXAAPlugin.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
5 * www.openflipper.org *
6 * *
7 *--------------------------------------------------------------------------- *
8 * This file is part of OpenFlipper. *
9 * *
10 * OpenFlipper is free software: you can redistribute it and/or modify *
11 * it under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 3 of *
13 * the License, or (at your option) any later version with the *
14 * following exceptions: *
15 * *
16 * If other files instantiate templates or use macros *
17 * or inline functions from this file, or you compile this file and *
18 * link it with other files to produce an executable, this file does *
19 * not by itself cause the resulting executable to be covered by the *
20 * GNU Lesser General Public License. This exception does not however *
21 * invalidate any other reasons why the executable file might be *
22 * covered by the GNU Lesser General Public License. *
23 * *
24 * OpenFlipper is distributed in the hope that it will be useful, *
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27 * GNU Lesser General Public License for more details. *
28 * *
29 * You should have received a copy of the GNU LesserGeneral Public *
30 * License along with OpenFlipper. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36 * *
37 * $Revision$ *
38 * $LastChangedBy$ *
39 * $Date$ *
40 * *
41 \*===========================================================================*/
42 
43 #include <ACG/GL/acg_glew.hh>
44 
45 #include "PostProcessorFXAAPlugin.hh"
46 
47 #include <iostream>
48 #include <ACG/GL/GLState.hh>
49 #include <ACG/GL/gl.hh>
50 #include <ACG/GL/ScreenQuad.hh>
51 
54 
55 
56 PostProcessorFXAAPlugin::PostProcessorFXAAPlugin() :
57 fxaa_(0), luma_(0)
58 {
59 }
60 
61 PostProcessorFXAAPlugin::~PostProcessorFXAAPlugin()
62 {
63  delete fxaa_;
64  delete luma_;
65 }
66 
67 
68 QString PostProcessorFXAAPlugin::postProcessorName() {
69  return QString("FXAA");
70 }
71 
72 QString PostProcessorFXAAPlugin::checkOpenGL() {
73  if (!ACG::openGLVersion(3, 0))
74  return QString("Insufficient OpenGL Version! OpenGL 3.0 or higher required");
75 
76  return QString("");
77 }
78 
79 
80 void PostProcessorFXAAPlugin::postProcess(ACG::GLState* _glstate, const std::vector<const PostProcessorInput*>& _input, const PostProcessorOutput& _output) {
81 
82  // ======================================================================================================
83  // Load shaders if needed
84  // ======================================================================================================
85  if (!fxaa_)
86  fxaa_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "FXAA/fxaa.glsl");
87 
88  if (!luma_)
89  luma_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "FXAA/luma.glsl");
90 
91  if (!fxaa_ || !luma_)
92  return;
93 
94  // ======================================================================================================
95  // Bind input texture
96  // ======================================================================================================
97 
98  _input[0]->bindColorTex(0);
99 
100  // ======================================================================================================
101  // Put luma in alpha channel
102  // ======================================================================================================
103 
104  if (!lumaRT_.width())
105  lumaRT_.attachTexture2D(GL_COLOR_ATTACHMENT0, _input[0]->width, _input[0]->height, GL_RGBA, GL_RGBA, GL_CLAMP, GL_LINEAR, GL_LINEAR);
106  else
107  lumaRT_.resize(_input[0]->width, _input[0]->height);
108 
109  lumaRT_.bind();
110 
111  luma_->use();
112  luma_->setUniform("textureSampler", 0);
113 
115 
116  glBindTexture(GL_TEXTURE_2D, lumaRT_.getAttachment(GL_COLOR_ATTACHMENT0));
117 
118  // ======================================================================================================
119  // Bind output FBO
120  // ======================================================================================================
121 
122  _output.bind();
123 
124  // ======================================================================================================
125  // Setup shader
126  // ======================================================================================================
127 
128  fxaa_->use();
129  fxaa_->setUniform("textureSampler", 0);
130 
131  ACG::Vec2f texelSize(1.0f / float(_input[0]->width), 1.0f / float(_input[0]->height));
132  fxaa_->setUniform("fxaaQualityRcpFrame", texelSize);
133 
134  ACG::Vec4f fxaaConsoleRcp(-texelSize[0], -texelSize[1], texelSize[0], texelSize[1]);
135  fxaa_->setUniform("fxaaConsoleRcpFrameOpt", fxaaConsoleRcp * 0.5f);
136 
137  fxaa_->setUniform("fxaaConsoleRcpFrameOpt2", fxaaConsoleRcp * 2.0f);
138 
139 
140  // ======================================================================================================
141  // Execute
142  // ======================================================================================================
143 
145 
146 
147  fxaa_->disable();
148 }
149 
150 
151 #if QT_VERSION < 0x050000
152  Q_EXPORT_PLUGIN2( postprocessorfxaaplugin , PostProcessorFXAAPlugin );
153 #endif
154 
static void draw(GLSL::Program *_prog=0)
Draw the screen quad.
Definition: ScreenQuad.cc:147
GLSL::Program * fxaa_
fxaa shader
bool openGLVersion(const int _major, const int _minor)
Definition: gl.cc:95
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
Definition: GLSLShader.cc:391
void attachTexture2D(GLenum _attachment, GLsizei _width, GLsizei _height, GLuint _internalFmt, GLenum _format, GLint _wrapMode=GL_CLAMP, GLint _minFilter=GL_NEAREST, GLint _magFilter=GL_NEAREST)
function to attach a texture to fbo
Definition: FBO.cc:200
bool bind()
bind the fbo and sets it as rendertarget
Definition: FBO.cc:451
ACG::FBO lumaRT_
fbo with rt that contains luminance in alpha
void use()
Enables the program object for using.
Definition: GLSLShader.cc:351
GLsizei width() const
get width of fbo texture
Definition: FBO.hh:161
void resize(GLsizei _width, GLsizei _height, bool _forceResize=false)
resize function (if textures created by this class)
Definition: FBO.cc:543
GLSL::PtrProgram loadProgram(const char *vertexShaderFile, const char *tessControlShaderFile, const char *tessEvaluationShaderFile, const char *geometryShaderFile, const char *fragmentShaderFile, const GLSL::StringList *macros, bool verbose)
Definition: GLSLShader.cc:1082
GLuint getAttachment(GLenum _attachment)
return attached texture id
Definition: FBO.cc:529
GLSL::Program * luma_
luma shader
void disable()
Resets to standard rendering pipeline.
Definition: GLSLShader.cc:361