PostProcessorFXAAPlugin.cc 6.55 KB
Newer Older
1 2 3 4
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
Christopher Tenter's avatar
Christopher Tenter committed
37 38 39
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
40 41 42
*                                                                            *
\*===========================================================================*/

43
#include <ACG/GL/acg_glew.hh>
44 45 46 47 48 49 50 51 52 53 54 55 56

#include "PostProcessorFXAAPlugin.hh"

#include <iostream>
#include <ACG/GL/GLState.hh>
#include <ACG/GL/gl.hh>
#include <ACG/GL/ScreenQuad.hh>

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>


PostProcessorFXAAPlugin::PostProcessorFXAAPlugin() :
57
fxaa_(0), luma_(0)
58 59 60 61 62 63
{
}

PostProcessorFXAAPlugin::~PostProcessorFXAAPlugin()
{
  delete fxaa_;
64
  delete luma_;
65 66 67 68 69 70 71
}


QString PostProcessorFXAAPlugin::postProcessorName() {
  return QString("FXAA");
}

72
QString PostProcessorFXAAPlugin::checkOpenGL() {
73
  if (!ACG::openGLVersion(3, 0))
74
    return QString("Insufficient OpenGL Version! OpenGL 3.0 or higher required");
75

76 77 78 79 80
  return QString("");
}


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 87 88 89 90 91 92
    fxaa_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "FXAA/fxaa.glsl");

  if (!luma_)
    luma_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "FXAA/luma.glsl");

  if (!fxaa_ || !luma_)
    return;
93 94 95 96 97

  // ======================================================================================================
  // Bind input texture
  // ======================================================================================================

98
  _input[0]->bindColorTex(0);
99

100
  // ======================================================================================================
101
  // Put luma in alpha channel
102 103
  // ======================================================================================================

104 105 106 107 108 109 110 111 112 113 114 115 116
  if (!lumaRT_.width())
    lumaRT_.attachTexture2D(GL_COLOR_ATTACHMENT0, _input[0]->width, _input[0]->height, GL_RGBA, GL_RGBA, GL_CLAMP, GL_LINEAR, GL_LINEAR);
  else
    lumaRT_.resize(_input[0]->width, _input[0]->height);

  lumaRT_.bind();

  luma_->use();
  luma_->setUniform("textureSampler", 0);

  ACG::ScreenQuad::draw(luma_);

  glBindTexture(GL_TEXTURE_2D, lumaRT_.getAttachment(GL_COLOR_ATTACHMENT0));
117 118

  // ======================================================================================================
119
  // Bind output FBO
120
  // ======================================================================================================
121

122
  _output.bind();
123 124 125 126 127 128 129 130

  // ======================================================================================================
  // Setup shader
  // ======================================================================================================

  fxaa_->use();
  fxaa_->setUniform("textureSampler", 0);

131 132 133 134 135 136 137 138
  ACG::Vec2f texelSize(1.0f / float(_input[0]->width), 1.0f / float(_input[0]->height));
  fxaa_->setUniform("fxaaQualityRcpFrame", texelSize);

  ACG::Vec4f fxaaConsoleRcp(-texelSize[0], -texelSize[1], texelSize[0], texelSize[1]);
  fxaa_->setUniform("fxaaConsoleRcpFrameOpt", fxaaConsoleRcp * 0.5f);

  fxaa_->setUniform("fxaaConsoleRcpFrameOpt2", fxaaConsoleRcp * 2.0f);

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

  // ======================================================================================================
  // Execute
  // ======================================================================================================

  ACG::ScreenQuad::draw(fxaa_);

  
  fxaa_->disable();
}


#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( postprocessorfxaaplugin , PostProcessorFXAAPlugin );
#endif