OpenWalnut  1.4.0
WGEPostprocessorMergeOp.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 
00027 #include <osg/Camera>
00028 
00029 #include "../shaders/WGEPropertyUniform.h"
00030 #include "../shaders/WGEShaderPropertyDefineOptions.h"
00031 
00032 #include "WGEPostprocessorMergeOp.h"
00033 
00034 WGEPostprocessorMergeOp::WGEPostprocessorMergeOp():
00035     WGEPostprocessor( "MergeOp",
00036                       "MergeOp - combines all input textures in a user defined way." )
00037 {
00038 }
00039 
00040 WGEPostprocessorMergeOp::WGEPostprocessorMergeOp( osg::ref_ptr< WGEOffscreenRenderNode > offscreen,
00041                                               osg::ref_ptr< osg::Texture2D > tex0,
00042                                               osg::ref_ptr< osg::Texture2D > tex1,
00043                                               osg::ref_ptr< osg::Texture2D > tex2,
00044                                               osg::ref_ptr< osg::Texture2D > tex3,
00045                                               osg::ref_ptr< osg::Texture2D > tex4,
00046                                               osg::ref_ptr< osg::Texture2D > tex5,
00047                                               osg::ref_ptr< osg::Texture2D > tex6,
00048                                               osg::ref_ptr< osg::Texture2D > tex7 ):
00049     WGEPostprocessor( "MergeOp",
00050                       "MergeOp - combines all input textures in a user defined way." ),
00051     m_codeInjector( new WGEShaderCodeInjector( "WGE_POSTPROCESSOR_MERGEOP_CODE" ) )
00052 
00053 {
00054     // Use the standard postprocessor uber-shader
00055     m_mergeOpShader = new WGEShader( "WGEPostprocessor" );
00056     m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP" );
00057     m_mergeOpShader->addPreprocessor( m_codeInjector );
00058 
00059     // also add the m_effectOnly property as shader preprocessor
00060     m_mergeOpShader->addPreprocessor( m_effectOnlyPreprocessor );
00061 
00062     // create the rendering pass
00063     osg::ref_ptr< WGEOffscreenTexturePass > pass = offscreen->addTextureProcessingPass( m_mergeOpShader, "MergeOp" );
00064 
00065     // for each of the textures do:
00066 
00067     // attach color0 output and bind tex0
00068     m_resultTextures.push_back( pass->attach( WGECamera::COLOR_BUFFER0, GL_RGBA ) );
00069     pass->bind( tex0, 0 );
00070     m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT0" );
00071 
00072     // attach color1 output and bind tex1
00073     if( tex1 )
00074     {
00075         pass->bind( tex1, 1 );
00076         m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT1" );
00077     }
00078     // attach color2 output and bind tex2
00079     if( tex2 )
00080     {
00081         pass->bind( tex2, 2 );
00082         m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT2" );
00083     }
00084     // attach color3 output and bind tex3
00085     if( tex3 )
00086     {
00087         pass->bind( tex3, 3 );
00088         m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT3" );
00089     }
00090     // attach color4 output and bind tex4
00091     if( tex4 )
00092     {
00093         pass->bind( tex4, 4 );
00094         m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT4" );
00095     }
00096     // attach color5 output and bind tex5
00097     if( tex5 )
00098     {
00099         pass->bind( tex5, 5 );
00100         m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT5" );
00101     }
00102     // attach color6 output and bind tex6
00103     if( tex6 )
00104     {
00105         pass->bind( tex6, 6 );
00106         m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT6" );
00107     }
00108     // attach color7 output and bind tex7
00109     if( tex7 )
00110     {
00111         pass->bind( tex7, 7 );
00112         m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT7" );
00113     }
00114 }
00115 
00116 WGEPostprocessorMergeOp::~WGEPostprocessorMergeOp()
00117 {
00118     // cleanup
00119 }
00120 
00121 WGEPostprocessor::SPtr WGEPostprocessorMergeOp::create( osg::ref_ptr< WGEOffscreenRenderNode > offscreen,
00122     const WGEPostprocessor::PostprocessorInput& gbuffer ) const
00123 {
00124     return WGEPostprocessor::SPtr( new WGEPostprocessorMergeOp( offscreen, gbuffer.m_colorTexture ) );
00125 }
00126 
00127 void WGEPostprocessorMergeOp::setGLSLMergeCode( std::string code )
00128 {
00129     m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_CUSTOM" );
00130     m_codeInjector->setCode( code );
00131 }