OpenWalnut  1.5.0dev
WGEPostprocessorMergeOp.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 
27 #include <osg/Camera>
28 
29 #include "../shaders/WGEPropertyUniform.h"
30 #include "../shaders/WGEShaderPropertyDefineOptions.h"
31 
32 #include "WGEPostprocessorMergeOp.h"
33 
35  WGEPostprocessor( "MergeOp",
36  "MergeOp - combines all input textures in a user defined way." )
37 {
38 }
39 
40 WGEPostprocessorMergeOp::WGEPostprocessorMergeOp( osg::ref_ptr< WGEOffscreenRenderNode > offscreen,
41  osg::ref_ptr< osg::Texture2D > tex0,
42  osg::ref_ptr< osg::Texture2D > tex1,
43  osg::ref_ptr< osg::Texture2D > tex2,
44  osg::ref_ptr< osg::Texture2D > tex3,
45  osg::ref_ptr< osg::Texture2D > tex4,
46  osg::ref_ptr< osg::Texture2D > tex5,
47  osg::ref_ptr< osg::Texture2D > tex6,
48  osg::ref_ptr< osg::Texture2D > tex7 ):
49  WGEPostprocessor( "MergeOp",
50  "MergeOp - combines all input textures in a user defined way." ),
51  m_codeInjector( new WGEShaderCodeInjector( "WGE_POSTPROCESSOR_MERGEOP_CODE" ) )
52 
53 {
54  // Use the standard postprocessor uber-shader
55  m_mergeOpShader = new WGEShader( "WGEPostprocessor" );
56  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP" );
57  m_mergeOpShader->addPreprocessor( m_codeInjector );
58 
59  // also add the m_effectOnly property as shader preprocessor
60  m_mergeOpShader->addPreprocessor( m_effectOnlyPreprocessor );
61 
62  // create the rendering pass
63  osg::ref_ptr< WGEOffscreenTexturePass > pass = offscreen->addTextureProcessingPass( m_mergeOpShader, "MergeOp" );
64 
65  // for each of the textures do:
66 
67  // attach color0 output and bind tex0
68  m_resultTextures.push_back( pass->attach( WGECamera::COLOR_BUFFER0, GL_RGBA ) );
69  pass->bind( tex0, 0 );
70  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT0" );
71 
72  // attach color1 output and bind tex1
73  if( tex1 )
74  {
75  pass->bind( tex1, 1 );
76  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT1" );
77  }
78  // attach color2 output and bind tex2
79  if( tex2 )
80  {
81  pass->bind( tex2, 2 );
82  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT2" );
83  }
84  // attach color3 output and bind tex3
85  if( tex3 )
86  {
87  pass->bind( tex3, 3 );
88  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT3" );
89  }
90  // attach color4 output and bind tex4
91  if( tex4 )
92  {
93  pass->bind( tex4, 4 );
94  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT4" );
95  }
96  // attach color5 output and bind tex5
97  if( tex5 )
98  {
99  pass->bind( tex5, 5 );
100  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT5" );
101  }
102  // attach color6 output and bind tex6
103  if( tex6 )
104  {
105  pass->bind( tex6, 6 );
106  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT6" );
107  }
108  // attach color7 output and bind tex7
109  if( tex7 )
110  {
111  pass->bind( tex7, 7 );
112  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_UNIT7" );
113  }
114 }
115 
117 {
118  // cleanup
119 }
120 
121 WGEPostprocessor::SPtr WGEPostprocessorMergeOp::create( osg::ref_ptr< WGEOffscreenRenderNode > offscreen,
122  const WGEPostprocessor::PostprocessorInput& gbuffer ) const
123 {
124  return WGEPostprocessor::SPtr( new WGEPostprocessorMergeOp( offscreen, gbuffer.m_colorTexture ) );
125 }
126 
128 {
129  m_mergeOpShader->setDefine( "WGE_POSTPROCESSOR_MERGEOP_CUSTOM" );
130  m_codeInjector->setCode( code );
131 }
WGEShaderCodeInjector::SPtr m_codeInjector
This preprocessor handles insertion of the custom merge code.
WGEShader::RefPtr m_mergeOpShader
The shader used for merging.
void setGLSLMergeCode(std::string code)
Set the GLSL code inserted into the shader as merging operation.
virtual ~WGEPostprocessorMergeOp()
Destructor.
WGEPostprocessorMergeOp()
Default constructor.
virtual WGEPostprocessor::SPtr create(osg::ref_ptr< WGEOffscreenRenderNode > offscreen, const PostprocessorInput &gbuffer) const
Create instance.
This class encapsulates a G-Buffer.
osg::ref_ptr< osg::Texture2D > m_colorTexture
Color in RGBA.
The base class for all custom post-processors.
WGEShaderPreprocessor::SPtr m_effectOnlyPreprocessor
For convenience, this is a shader preprocessor controlled by m_effectOnly property.
std::shared_ptr< WGEPostprocessor > SPtr
Convenience typedef for an osg::ref_ptr< WGEPostprocessor >.
std::vector< osg::ref_ptr< osg::Texture2D > > m_resultTextures
The textures contain the result.
This preprocessor is able to inject code into a shader.
Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader.
Definition: WGEShader.h:48