From 4a4650e085cdf2842d63fd2bea92903dc6078702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20M=C3=B6bius?= Date: Mon, 16 Sep 2013 14:50:09 +0000 Subject: [PATCH] Added Sobel Post Processor git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free-Staging@17536 383ad7c9-94d9-4d36-a494-682f7c89f535 --- .../Plugin-Sobel/CMakeLists.txt | 2 + .../Plugin-Sobel/PostProcessorSobelPlugin.cc | 138 ++++++++++++++++++ .../Plugin-Sobel/PostProcessorSobelPlugin.hh | 86 +++++++++++ .../Shaders/Sobel/screenquad.glsl | 10 ++ .../Plugin-Sobel/Shaders/Sobel/sobel.glsl | 34 +++++ 5 files changed, 270 insertions(+) create mode 100644 PluginCollection-PostProcessors/Plugin-Sobel/CMakeLists.txt create mode 100644 PluginCollection-PostProcessors/Plugin-Sobel/PostProcessorSobelPlugin.cc create mode 100644 PluginCollection-PostProcessors/Plugin-Sobel/PostProcessorSobelPlugin.hh create mode 100644 PluginCollection-PostProcessors/Plugin-Sobel/Shaders/Sobel/screenquad.glsl create mode 100644 PluginCollection-PostProcessors/Plugin-Sobel/Shaders/Sobel/sobel.glsl diff --git a/PluginCollection-PostProcessors/Plugin-Sobel/CMakeLists.txt b/PluginCollection-PostProcessors/Plugin-Sobel/CMakeLists.txt new file mode 100644 index 0000000..faea732 --- /dev/null +++ b/PluginCollection-PostProcessors/Plugin-Sobel/CMakeLists.txt @@ -0,0 +1,2 @@ +include (plugin) +openflipper_plugin ( INSTALLDATA Shaders ) diff --git a/PluginCollection-PostProcessors/Plugin-Sobel/PostProcessorSobelPlugin.cc b/PluginCollection-PostProcessors/Plugin-Sobel/PostProcessorSobelPlugin.cc new file mode 100644 index 0000000..75a08ed --- /dev/null +++ b/PluginCollection-PostProcessors/Plugin-Sobel/PostProcessorSobelPlugin.cc @@ -0,0 +1,138 @@ +/*===========================================================================*\ +* * +* OpenFlipper * +* Copyright (C) 2001-2013 by Computer Graphics Group, RWTH Aachen * +* 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 . * +* * +\*===========================================================================*/ + +/*===========================================================================*\ +* * +* $Revision$ * +* $LastChangedBy$ * +* $Date$ * +* * +\*===========================================================================*/ + +#if QT_VERSION >= 0x050000 + #include +#else + #include +#endif + +#include "PostProcessorSobelPlugin.hh" + +#include +#include +#include +#include + +#include +#include + + +PostProcessorSobelPlugin::PostProcessorSobelPlugin() : +shader_(0) +{ +} + +PostProcessorSobelPlugin::~PostProcessorSobelPlugin() +{ + delete shader_; +} + + +QString PostProcessorSobelPlugin::postProcessorName() { + return QString("Sobel"); +} + + + +void PostProcessorSobelPlugin::postProcess(ACG::GLState* _glstate, const PostProcessorInput& _input, GLuint _targetFBO) { + + // ====================================================================================================== + // Load shader if needed + // ====================================================================================================== + if (!shader_) + shader_ = GLSL::loadProgram("Sobel/screenquad.glsl", "Sobel/sobel.glsl"); + + // ====================================================================================================== + // Bind input texture + // ====================================================================================================== + + glActiveTexture(GL_TEXTURE0); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, _input.colorTex_); + + // ====================================================================================================== + // Bind output FBO + // ====================================================================================================== + + glBindFramebuffer(GL_FRAMEBUFFER, _targetFBO); + + // ====================================================================================================== + // Clear rendering buffer + // ====================================================================================================== + _glstate->clearBuffers(); + + // ====================================================================================================== + // Setup render states + // ====================================================================================================== + + glDepthMask(1); + glColorMask(1,1,1,1); + + glDisable(GL_DEPTH_TEST); + glDisable(GL_BLEND); + + // ====================================================================================================== + // Setup shader + // ====================================================================================================== + + shader_->use(); + shader_->setUniform("textureSampler", 0); + + ACG::Vec2f texcoordOffset(1.0f / float(_input.width), 1.0f / float(_input.height)); + shader_->setUniform("texcoordOffset", texcoordOffset); + + // ====================================================================================================== + // Execute + // ====================================================================================================== + + ACG::ScreenQuad::draw(shader_); + + + shader_->disable(); +} + + +#if QT_VERSION < 0x050000 + Q_EXPORT_PLUGIN2( postprocessorsobelplugin , PostProcessorSobelPlugin ); +#endif + diff --git a/PluginCollection-PostProcessors/Plugin-Sobel/PostProcessorSobelPlugin.hh b/PluginCollection-PostProcessors/Plugin-Sobel/PostProcessorSobelPlugin.hh new file mode 100644 index 0000000..64cce0a --- /dev/null +++ b/PluginCollection-PostProcessors/Plugin-Sobel/PostProcessorSobelPlugin.hh @@ -0,0 +1,86 @@ +/*===========================================================================*\ +* * +* OpenFlipper * +* Copyright (C) 2001-2013 by Computer Graphics Group, RWTH Aachen * +* 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 . * +* * +\*===========================================================================*/ + +/*===========================================================================*\ +* * +* $Revision$ * +* $LastChangedBy$ * +* $Date$ * +* * +\*===========================================================================*/ + + +#include + +#include +#include + +#include +#include + +class PostProcessorSobelPlugin : public QObject, BaseInterface, PostProcessorInterface +{ + Q_OBJECT + Q_INTERFACES(BaseInterface) + Q_INTERFACES(PostProcessorInterface) + +#if QT_VERSION >= 0x050000 + Q_PLUGIN_METADATA(IID "org.OpenFlipper.Plugins.Plugin-PostProcessorSobel") +#endif + + public: + PostProcessorSobelPlugin(); + ~PostProcessorSobelPlugin(); + + public : + QString name() { return (QString("Sobel Postprocessor Plugin")); }; + QString description( ) { return (QString(tr("Runs a sobel filter on the image"))); }; + + + public slots: + QString version() { return QString("1.0"); }; + + private slots: + + void postProcess(ACG::GLState* _glstate, const PostProcessorInput& _input, GLuint _targetFBO); + + QString postProcessorName(); + + private: + + /// shader + GLSL::Program* shader_; + +}; + diff --git a/PluginCollection-PostProcessors/Plugin-Sobel/Shaders/Sobel/screenquad.glsl b/PluginCollection-PostProcessors/Plugin-Sobel/Shaders/Sobel/screenquad.glsl new file mode 100644 index 0000000..c6824bc --- /dev/null +++ b/PluginCollection-PostProcessors/Plugin-Sobel/Shaders/Sobel/screenquad.glsl @@ -0,0 +1,10 @@ +#version 150 + +in vec4 inPosition; +out vec2 vTexCoord; + +void main() +{ + gl_Position = inPosition; + vTexCoord = inPosition.xy * 0.5 + vec2(0.5, 0.5); +} \ No newline at end of file diff --git a/PluginCollection-PostProcessors/Plugin-Sobel/Shaders/Sobel/sobel.glsl b/PluginCollection-PostProcessors/Plugin-Sobel/Shaders/Sobel/sobel.glsl new file mode 100644 index 0000000..76df9f8 --- /dev/null +++ b/PluginCollection-PostProcessors/Plugin-Sobel/Shaders/Sobel/sobel.glsl @@ -0,0 +1,34 @@ +#version 150 + +uniform sampler2D textureSampler; + +in vec2 vTexCoord; +out vec4 oColor; + +// The inverse of the texture dimensions along X and Y +uniform vec2 texcoordOffset; + +void main() +{ + vec2 coord_tl = vec2( vTexCoord.x + texcoordOffset.x , vTexCoord.y - texcoordOffset.y ); + vec2 coord_t = vec2( vTexCoord.x + texcoordOffset.x , vTexCoord.y ); + vec2 coord_tr = vec2( vTexCoord.x + texcoordOffset.x , vTexCoord.y + texcoordOffset.y ); + + vec2 coord_cl = vec2( vTexCoord.x,vTexCoord.y-texcoordOffset.y ); + vec2 coord_cr = vec2( vTexCoord.x,vTexCoord.y+texcoordOffset.y ); + + vec2 coord_bl = vec2( vTexCoord.x-texcoordOffset.x,vTexCoord.y-texcoordOffset.y ); + vec2 coord_b = vec2( vTexCoord.x-texcoordOffset.x,vTexCoord.y ); + vec2 coord_br = vec2( vTexCoord.x-texcoordOffset.x,vTexCoord.y+texcoordOffset.y ); + + + vec4 color_horizontal = abs(texture2D(textureSampler, coord_tl) - texture2D(textureSampler, coord_tr)); + color_horizontal += abs(2.0 * texture2D(textureSampler, coord_cl) - 2.0 * texture2D(textureSampler, coord_cr)); + color_horizontal += abs(texture2D(textureSampler, coord_bl) - texture2D(textureSampler, coord_br)); + + vec4 color_vertical = abs(texture2D(textureSampler, coord_tl) - texture2D(textureSampler, coord_bl)); + color_vertical += abs(2.0 * texture2D(textureSampler, coord_t) - 2.0 * texture2D(textureSampler, coord_b)); + color_vertical += abs(texture2D(textureSampler, coord_tr) - texture2D(textureSampler, coord_br)); + + oColor = color_horizontal + color_vertical; +} -- GitLab