OpenWalnut  1.4.0
WGEPostprocessor.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <string>
00026 #include <vector>
00027 
00028 #include "WGEPostprocessorEdgeEnhance.h"
00029 #include "WGEPostprocessorCelShading.h"
00030 #include "WGEPostprocessorGauss.h"
00031 #include "WGEPostprocessorSSAO.h"
00032 #include "WGEPostprocessorLineAO.h"
00033 
00034 #include "WGEPostprocessor.h"
00035 
00036 WGEPostprocessor::ProcessorList WGEPostprocessor::m_postProcessors;
00037 
00038 WGEPostprocessor::WGEPostprocessor( std::string name, std::string description ):
00039     WPrototyped(),
00040     m_resultTextures(),
00041     m_depthTexture(),
00042     m_properties( boost::shared_ptr< WProperties >( new WProperties( "Settings for " + name, "Post-processing properties" ) ) ),
00043     m_name( name ),
00044     m_description( description )
00045 {
00046     // there is always one property:
00047     m_effectOnly = m_properties->addProperty( "Effect Only", "If active, the plain effect will be shown instead a combination of effect "
00048                                                              "and color. This settings does not affect all postprocessors.", false );
00049     m_effectScale = m_properties->addProperty( "Effect Scaling", "Use this to overemphasize an effect or to weaken it. Technically spoken, this "
00050                                                 "factor determines the pre-multiplication done prior to blending with the input color.", 1.0,
00051                                                 true );
00052     m_effectScale->setMin( 0.0 );
00053     m_effectScale->setMax( 10.0 );
00054 
00055     // for convenience, also create a preprocessor for this property
00056     m_effectOnlyPreprocessor = WGEShaderPreprocessor::SPtr( new WGEShaderPropertyDefineOptions< WPropBool >( m_effectOnly,
00057         "WGE_POSTPROCESSOR_OUTPUT_COMBINE", "WGE_POSTPROCESSOR_OUTPUT_EFFECT_ONLY" ) );
00058 }
00059 
00060 WGEPostprocessor::~WGEPostprocessor()
00061 {
00062     // cleanup
00063 }
00064 
00065 WPropGroup WGEPostprocessor::getProperties() const
00066 {
00067     return m_properties;
00068 }
00069 
00070 osg::ref_ptr< osg::Texture2D > WGEPostprocessor::getOutput( size_t idx ) const
00071 {
00072     return m_resultTextures[ idx ];
00073 }
00074 
00075 const std::vector< osg::ref_ptr< osg::Texture2D > >& WGEPostprocessor::getOutputList() const
00076 {
00077     return m_resultTextures;
00078 }
00079 
00080 osg::ref_ptr< osg::Texture2D > WGEPostprocessor::getDepth() const
00081 {
00082     return m_depthTexture;
00083 }
00084 
00085 WGEPostprocessor::PostprocessorInput::PostprocessorInput()
00086 {
00087     // leave them uni-initialized
00088 }
00089 
00090 WGEPostprocessor::PostprocessorInput::PostprocessorInput( std::vector< osg::ref_ptr< osg::Texture2D > > from )
00091 {
00092     if( from.size() > 0 )
00093     {
00094         m_colorTexture = from[0];
00095     }
00096     if( from.size() > 1 )
00097     {
00098         m_normalTexture = from[1];
00099     }
00100     if( from.size() > 2 )
00101     {
00102         m_parameterTexture = from[2];
00103     }
00104     if( from.size() > 3 )
00105     {
00106         m_tangentTexture = from[3];
00107     }
00108     if( from.size() > 4 )
00109     {
00110         m_depthTexture = from[4];
00111     }
00112 }
00113 
00114 WGEPostprocessor::PostprocessorInput::PostprocessorInput( osg::ref_ptr< osg::Texture2D > color,
00115                                                           osg::ref_ptr< osg::Texture2D > normal,
00116                                                           osg::ref_ptr< osg::Texture2D > parameter,
00117                                                           osg::ref_ptr< osg::Texture2D > tangent,
00118                                                           osg::ref_ptr< osg::Texture2D > depth ):
00119     m_colorTexture( color ),
00120     m_normalTexture( normal ),
00121     m_parameterTexture( parameter ),
00122     m_tangentTexture( tangent ),
00123     m_depthTexture( depth )
00124 {
00125 }
00126 
00127 WGEPostprocessor::PostprocessorInput WGEPostprocessor::PostprocessorInput::attach( osg::ref_ptr< WGEOffscreenRenderPass > from )
00128 {
00129     PostprocessorInput buf;
00130     buf.m_colorTexture = from->attach( WGECamera::COLOR_BUFFER0 );
00131     buf.m_normalTexture = from->attach( WGECamera::COLOR_BUFFER1, GL_RGB );
00132     buf.m_parameterTexture = from->attach( WGECamera::COLOR_BUFFER2, GL_R32F );
00133     buf.m_tangentTexture = from->attach( WGECamera::COLOR_BUFFER3, GL_RGB );
00134     buf.m_depthTexture = from->attach( WGECamera::DEPTH_BUFFER );
00135 
00136     return buf;
00137 }
00138 
00139 size_t WGEPostprocessor::PostprocessorInput::bind( osg::ref_ptr< WGEOffscreenRenderPass > to ) const
00140 {
00141     to->bind( m_colorTexture, 0 );
00142     to->bind( m_normalTexture, 1 );
00143     to->bind( m_parameterTexture, 2 );
00144     to->bind( m_depthTexture, 3 );
00145     to->bind( m_tangentTexture, 4 );
00146 
00147     return 5;
00148 }
00149 
00150 WGEPostprocessor::ProcessorList WGEPostprocessor::getPostprocessors()
00151 {
00152     return m_postProcessors;
00153 }
00154 
00155 void WGEPostprocessor::initPostprocessors()
00156 {
00157     // create prototypes of the postprocessors we know.
00158     m_postProcessors.push_back( WGEPostprocessor::SPtr( new WGEPostprocessorEdgeEnhance() ) );
00159     m_postProcessors.push_back( WGEPostprocessor::SPtr( new WGEPostprocessorCelShading() ) );
00160     m_postProcessors.push_back( WGEPostprocessor::SPtr( new WGEPostprocessorGauss() ) );
00161     m_postProcessors.push_back( WGEPostprocessor::SPtr( new WGEPostprocessorSSAO() ) );
00162     m_postProcessors.push_back( WGEPostprocessor::SPtr( new WGEPostprocessorLineAO() ) );
00163 }
00164 
00165 size_t WGEPostprocessor::addPostprocessor( SPtr processor )
00166 {
00167     m_postProcessors.push_back( processor );
00168     return m_postProcessors.size() - 1;
00169 }
00170 
00171 const std::string WGEPostprocessor::getName() const
00172 {
00173     return m_name;
00174 }
00175 
00176 const std::string WGEPostprocessor::getDescription() const
00177 {
00178     return m_description;
00179 }
00180 
00181 bool WGEPostprocessor::getFixedViewportSize() const
00182 {
00183     return false;
00184 }