OpenWalnut  1.4.0
WGEPostprocessingNode.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 "../../common/WPropertyHelper.h"
26 #include "../../common/WItemSelection.h"
27 
28 #include "../shaders/WGEShaderPropertyDefineOptions.h"
29 #include "../callbacks/WGENodeMaskCallback.h"
30 #include "../WGEUtils.h"
31 
32 #include "WGEPostprocessor.h"
33 
34 #include "WGEPostprocessingNode.h"
35 
36 WGEPostprocessingNode::WGEPostprocessingNode( osg::ref_ptr< WGECamera > reference, size_t width, size_t height, bool noHud ):
37  osg::Switch(),
38  m_childs( new WGEGroupNode() ),
39  m_properties( boost::shared_ptr< WProperties >( new WProperties( "Post-processing", "Post-processing properties" ) ) )
40 {
41  // the geometry is always the first in the switch node
42  addChild( m_childs );
43 
44  // this node has some properties:
45  boost::shared_ptr< WItemSelection > m_possibleSelections( new WItemSelection() );
46  m_possibleSelections->addItem( "None", "No postprocessing." );
47 
48  m_showHud = m_properties->addProperty( "Texture Debug", "If set, all intermediate texture are shown on screen for debugging.", false );
49  m_active = m_properties->addProperty( "Enable", "If set, post-processing is enabled.", false, true );
50  m_activePostprocessor = m_properties->addProperty( "Postprocessor", "Selection one of the postprocessors.",
51  m_possibleSelections->getSelectorFirst(),
52  boost::bind( &WGEPostprocessingNode::postprocessorSelected, this ) );
55 
56  // control texture HUD
57  osg::ref_ptr< WGENodeMaskCallback > textureHudCallback = new WGENodeMaskCallback( m_showHud );
58 
59  // get available postprocessors and setup the node
61  for( WGEPostprocessor::ProcessorList::const_iterator iter = processors.begin(); iter != processors.end(); ++iter )
62  {
63  // offscreen node
64  osg::ref_ptr< WGEOffscreenRenderNode > offscreen( new WGEOffscreenRenderNode( reference, width, height, noHud ) );
65  offscreen->getTextureHUD()->addUpdateCallback( textureHudCallback );
66 
67  // if the specific postprocessor requires us to fix the viewport size to the result-texture size:
68  offscreen->setLinkViewportToTextureSize( ( *iter )->getFixedViewportSize() );
69 
70  // the geometry render step
71  osg::ref_ptr< WGEOffscreenRenderPass > render = offscreen->addGeometryRenderPass(
72  m_childs,
73  "Rendered"
74  );
75 
76  // create G-Buffer
78 
79  // let the specific post processor build its pipeline
80  WGEPostprocessor::SPtr processor = ( *iter )->create( offscreen, buf );
81  m_postprocs.push_back( processor );
82 
83  // add the postprocessor's properties
84  m_properties->addProperty( processor->getProperties() );
85  processor->getProperties()->setHidden( true );
86 
87  // add it to the selection prop
88  m_possibleSelections->addItem( processor->getName(), processor->getDescription() );
89 
90  // the final step
91  osg::ref_ptr< WGEOffscreenFinalPass > output = offscreen->addFinalOnScreenPass( new WGEShader( "WGEPostprocessorCombiner" ),
92  "Output" );
93  output->bind( processor->getOutput(), 0 );
94 
95  // does this processor provide a depth?
96  osg::ref_ptr< osg::Texture2D > depthTex = processor->getDepth();
97  if( !depthTex )
98  {
99  depthTex = buf.m_depthTexture;
100  }
101  output->bind( depthTex, 1 );
102 
103  // add the offscreen renderer and the original node to the switch
104  addChild( offscreen );
105  }
106 
107  // let the props control some stuff
109 }
110 
112 {
113  // cleanup
114 }
115 
117 {
118  return m_properties;
119 }
120 
121 void WGEPostprocessingNode::insert( osg::ref_ptr< osg::Node > node, WGEShader::RefPtr shader )
122 {
123  // the shader needs an own preprocessor.
125  m_active, "WGE_POSTPROCESSING_DISABLED", "WGE_POSTPROCESSING_ENABLED" )
126  );
127 
128  // we need to inject some code to the shader at this point.
129  shader->addPreprocessor( preproc );
130 
131  // do it thread-safe as we promise to be thread-safe
133  // to keep track of which node is associated with which shader and preprocessor:
134  w->get()[ node ] = std::make_pair( shader, preproc );
135 
136  // insert node to group node of all children
137  m_childs->insert( node );
138 }
139 
140 void WGEPostprocessingNode::remove( osg::ref_ptr< osg::Node > node )
141 {
142  // do it thread-safe as we promise to be thread-safe
144 
145  // remove the item from our map
146  NodeShaderAssociation::Iterator item = w->get().find( node );
147 
148  if( item != w->get().end() )
149  {
150  // we need to remove the preprocessor from the shader.
151  ( *item ).second.first->removePreprocessor( ( *item ).second.second );
152  w->get().erase( item );
153  }
154 
155  // although we may not find the node in our association list, try to remove it
156  m_childs->remove( node );
157 }
158 
160 {
161  // do it thread-safe as we promise to be thread-safe
163 
164  // remove from node-shader association list
165  for( NodeShaderAssociation::Iterator iter = w->get().begin(); iter != w->get().end(); ++iter )
166  {
167  ( *iter ).second.first->removePreprocessor( ( *iter ).second.second );
168  }
169  w->get().clear();
170 
171  // remove the node from the render group
172  m_childs->clear();
173 }
174 
176 {
177  return m_postprocs[ m_activePostprocessor->get() - 1 ];
178 }
179 
181 {
182  if( m_postprocs.size() == 0 )
183  {
184  m_active->set( false );
185  return;
186  }
187 
188  size_t active = m_activePostprocessor->get();
189 
190  // this triggers several shader preprocessors of all child nodes
191  m_active->set( active != 0 );
192 
193  // hide all, but not the active one
194  for( size_t i = 0; i < m_postprocs.size(); ++i )
195  {
196  m_postprocs[ i ]->getProperties()->setHidden( i != ( active - 1 ) );
197  }
198 }
osg::ref_ptr< osg::Texture2D > m_depthTexture
Depth.
boost::shared_ptr< WSharedObjectTicketWrite< std::map< osg::ref_ptr< osg::Node >, std::pair< WGEShader::RefPtr, WGEShaderPreprocessor::SPtr > > > > WriteTicket
Type for write tickets.
Definition: WSharedObject.h:69
WPropGroup m_properties
All the properties of the post-processor.
WPropBool m_active
If true, post-processing is enabled.
This class encapsulates a G-Buffer.
WGEPostprocessor::ProcessorList m_postprocs
The postprocessors.
std::vector< WGEPostprocessor::SPtr > ProcessorList
Type used for returning lists of postprocessor prototypes.
boost::shared_ptr< WGEPostprocessor > SPtr
Convenience typedef for an osg::ref_ptr< WGEPostprocessor >.
WPropBool m_showHud
Activate to show the texture HUDs.
This callback is able to switch a osg::Switch node using a property.
This is a WGEShaderDefineOptions class which additionally uses a property to automatically control th...
void clear()
Removes all associated nodes.
virtual ~WGEPostprocessingNode()
Destructor.
WGEPostprocessingNode(osg::ref_ptr< WGECamera > reference, size_t width=2048, size_t height=2048, bool noHud=false)
Create a new post-processing node.
std::map< osg::ref_ptr< osg::Node >, std::pair< WGEShader::RefPtr, WGEShaderPreprocessor::SPtr > >::iterator Iterator
A typedef for the correct iterator to traverse this sequence container.
osg::ref_ptr< WGEShader > RefPtr
Convenience typedef for an osg::ref_ptr.
Definition: WGEShader.h:57
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader...
Definition: WGEShader.h:51
Class to manage properties of an object and to provide convenience methods for easy access and manipu...
A class containing a list of named items.
Class to wrap around the osg Group node and providing a thread safe add/removal mechanism.
Definition: WGEGroupNode.h:48
void addTo(WPropSelection prop)
Add the PC_NOTEMPTY constraint to the property.
This type of node basically is a convenience class for managing and creating offscreen renderings...
osg::ref_ptr< WGEGroupNode > m_childs
The group of child nodes to post-process.
NodeShaderAssociation m_nodeShaderAssociation
List of nodes and their corresponding shader and preprocessor.
void insert(osg::ref_ptr< osg::Node > node, WGEShader::RefPtr shader=NULL)
Inserts a node to the post-processor and injects the needed code to the specified shader...
boost::shared_ptr< WGEShaderPreprocessor > SPtr
Shared pointer for this class.
WPropSelection m_activePostprocessor
The property containing the currently active method or a combination.
static PostprocessorInput attach(osg::ref_ptr< WGEOffscreenRenderPass > from)
Attaches the needed textures to the specified render pass and returns the G-Buffer.
void postprocessorSelected()
Callback for changes in m_activePostprocessor.
This callback is useful to en-/disable nodes using the node mask based on properties.
WPropGroup getProperties() const
Returns the set of properties controlling the post-processing node.
static ProcessorList getPostprocessors()
Returns a list of all known postprocessor prototypes.
void addTo(WPropSelection prop)
Add the PC_SELECTONLYONE constraint to the property.
void remove(osg::ref_ptr< osg::Node > node)
Removes the node from the post-processing.
WGEPostprocessor::SPtr getCurrentPostprocessor() const
The post processor currently in use.