OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEOffscreenRenderNode.h
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 #ifndef WGEOFFSCREENRENDERNODE_H
26 #define WGEOFFSCREENRENDERNODE_H
27 
28 #include <string>
29 
30 #include <osg/Camera>
31 
32 #include "../WGEGroupNode.h"
33 #include "WGEOffscreenRenderPass.h"
34 #include "WGEOffscreenTexturePass.h"
35 #include "WGEOffscreenFinalPass.h"
36 #include "../WGETextureHud.h"
37 #include "../shaders/WGEShader.h"
38 #include "../callbacks/WGEViewportCallback.h"
39 #include "../WExportWGE.h"
40 
41 /**
42  * This type of node basically is a convenience class for managing and creating offscreen renderings. The children of this node should be of type
43  * \ref WGEOffscreenRenderPass. This class provides factories to create offscreen-render-pass instances with proper sizes with a coupling to a
44  * reference camera. This is useful to provide automatic viewport scaling etc. to each render-pass. You do not explicitly need this class to
45  * create offscreen-renderings at all. You can manually manage multiple WGEOffscreenRenderPass instances.
46  *
47  * It is important to understand, that the graph (your scene) must not be a children of this node. This node can be placed somewhere in your
48  * scene. The OSG collects all the cameras (and offscreen-cameras) and render then independently from their position in the graph (except for
49  * transformations inherited from others).
50  *
51  * \note Please not that you should not modify the whole wiring and offscreen configuration if the this node has been added as it is not
52  * thread-safe.
53  */
54 class WGE_EXPORT WGEOffscreenRenderNode: public WGEGroupNode // NOLINT
55 {
56 public:
57  /**
58  * Convenience typedef for an osg::ref_ptr
59  */
60  typedef osg::ref_ptr< WGEOffscreenRenderNode > RefPtr;
61 
62  /**
63  * Convenience typedef for an osg::ref_ptr; const
64  */
65  typedef osg::ref_ptr< const WGEOffscreenRenderNode > ConstRefPtr;
66 
67  /**
68  * Create a new managing instance. It uses the specified camera as reference to all created offscreen-render-pass instances. Especially
69  * viewport, clear-mask and clear-color get used. The default texture resolution is 2048x2048 which is more than full-HD resolution. So it
70  * should be enough.
71  *
72  * \param reference camera used as reference
73  * \param width the width of the textures used in this rendering. The real used space is determined by the reference camera.
74  * \param height the height of the textures used in this rendering. The real used space is determined by the reference camera.
75  * \param noHud If true, no hud gets displayed showing the created and used textures.
76  */
77  WGEOffscreenRenderNode( osg::ref_ptr< osg::Camera > reference, size_t width = 2048, size_t height = 2048, bool noHud = false );
78 
79  /**
80  * Destructor.
81  */
82  virtual ~WGEOffscreenRenderNode();
83 
84  /**
85  * Returns the instance of the texture HUD.
86  *
87  * \return the HUD
88  */
89  osg::ref_ptr< WGETextureHud > getTextureHUD() const;
90 
91  /**
92  * Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG graph to a texture.
93  *
94  * \param node the node which represents the subgraph.
95  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
96  *
97  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
98  *
99  * \return the geometry render pass.
100  */
101  virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass( osg::ref_ptr< osg::Node > node, std::string name = "Unnamed" );
102 
103  /**
104  * Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG graph to a texture.
105  *
106  * \param node the node which represents the subgraph.
107  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
108  * \param shader the shader to add.
109  *
110  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
111  *
112  * \return the geometry render pass.
113  */
114  virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGEShader > shader,
115  std::string name = "Unnamed" );
116 
117  /**
118  * Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures. All the in- and output textures
119  * have to be specified manually.
120  *
121  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
122  *
123  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
124  *
125  * \return the texture processing pass created.
126  */
127  virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass( std::string name = "Unnamed" );
128 
129  /**
130  * Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures. All the in- and output textures
131  * have to be specified manually.
132  *
133  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
134  *
135  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
136  * \param shader the shader to add.
137  *
138  * \return the texture processing pass created.
139  */
140  virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass( osg::ref_ptr< WGEShader > shader, std::string name = "Unnamed" );
141 
142  /**
143  * Creates a new render pass which can be seen as put-textures-back-on-screen-pass. It renders a full-screen quad to the on-screen
144  * frame-buffer. An optional shader can be used for final processing (most commonly clipping, blending, color-mapping and so on).
145  *
146  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
147  *
148  * \return the on-screen render pass which draws processed textures back on screen.
149  */
150  virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass( std::string name = "Unnamed" );
151 
152  /**
153  * Creates a new render pass which can be seen as put-textures-back-on-screen-pass. It renders a full-screen quad to the on-screen
154  * frame-buffer. An optional shader can be used for final processing (most commonly clipping, blending, color-mapping and so on).
155  *
156  * \param shader the shader to add
157  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
158  *
159  * \return the on-screen render pass which draws processed textures back on screen.
160  */
161  virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass( osg::ref_ptr< WGEShader > shader, std::string name = "Unnamed" );
162 
163  /**
164  * Creates a new offscreen-render-pass coupled with the reference camera. This pass actually does nothing. The method is useful for custom
165  * variants of WGEOffscreenRenderPass.
166  *
167  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
168  *
169  * \return new instance of a plain render pass
170  *
171  * \tparam T the type of pass to create.
172  */
173  template < typename T >
174  osg::ref_ptr< T > addRenderPass( std::string name = "Unnamed" );
175 
176 protected:
177 
178 private:
179 
180  /**
181  * The camera to which is used for setting this camera up.
182  */
183  osg::ref_ptr< osg::Camera > m_referenceCamera;
184 
185  /**
186  * The pointer to the hud used to render all used texture buffers. This can be NULL. It gets distributed to all created render-pass
187  * instances.
188  */
189  osg::ref_ptr< WGETextureHud > m_hud;
190 
191  /**
192  * The width of each texture in this offscreen rendering.
193  */
195 
196  /**
197  * The height of each texture in this offscreen rendering.
198  */
200 
201  /**
202  * The number of the next pass getting added.
203  */
205 };
206 
207 template < typename T >
208 osg::ref_ptr< T > WGEOffscreenRenderNode::addRenderPass( std::string name )
209 {
210  // create a new pass
211  osg::ref_ptr< T > pass = new T( m_textureWidth, m_textureHeight, m_hud, name, m_nextPassNum );
212  m_nextPassNum++;
213 
214  // this node needs to keep all the pass instances. Only this way, the OSG traverses and renders these nodes in the order specified by
215  // m_nextPassNum.
216  insert( pass ); // insert into this group
217 
218  // ensure proper propagation of viewport changes
219  pass->addUpdateCallback( new WGEViewportCallback< T >( m_referenceCamera ) );
220 
221  // set clear mask and color according to reference cam
222  pass->setClearMask( m_referenceCamera->getClearMask() );
223  pass->setClearColor( m_referenceCamera->getClearColor() );
224 
225  return pass;
226 }
227 
228 #endif // WGEOFFSCREENRENDERNODE_H
229