OpenWalnut  1.5.0dev
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 "core/common/WLogger.h"
33 
34 #include "../WGEGroupNode.h"
35 #include "WGEOffscreenRenderPass.h"
36 #include "WGEOffscreenTexturePass.h"
37 #include "WGEOffscreenFinalPass.h"
38 #include "../WGETextureHud.h"
39 #include "../shaders/WGEShader.h"
40 #include "../callbacks/WGEViewportCallback.h"
41 
42 /**
43  * This type of node basically is a convenience class for managing and creating offscreen renderings. The children of this node should be of type
44  * \ref WGEOffscreenRenderPass. This class provides factories to create offscreen-render-pass instances with proper sizes with a coupling to a
45  * reference camera. This is useful to provide automatic viewport scaling etc. to each render-pass. You do not explicitly need this class to
46  * create offscreen-renderings at all. You can manually manage multiple WGEOffscreenRenderPass instances.
47  *
48  * 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
49  * scene. The OSG collects all the cameras (and offscreen-cameras) and render then independently from their position in the graph (except for
50  * transformations inherited from others).
51  *
52  * \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
53  * thread-safe.
54  */
55 class WGEOffscreenRenderNode: public WGEGroupNode // NOLINT
56 {
57 public:
58  /**
59  * Convenience typedef for an osg::ref_ptr
60  */
61  typedef osg::ref_ptr< WGEOffscreenRenderNode > RefPtr;
62 
63  /**
64  * Convenience typedef for an osg::ref_ptr; const
65  */
66  typedef osg::ref_ptr< const WGEOffscreenRenderNode > ConstRefPtr;
67 
68  /**
69  * Create a new managing instance. It uses the specified camera as reference to all created offscreen-render-pass instances. Especially
70  * viewport, clear-mask and clear-color get used. The default texture resolution is 2048x2048 which is more than full-HD resolution. So it
71  * should be enough.
72  *
73  * \note The width and hight define the offscreen texture size. The viewport if each rendering is automatically set to the one of the
74  * reference camera. This means, width and height only define the maximal supported resolution without upscaling of your offscreen renderer.
75  *
76  * \param reference camera used as reference
77  * \param width the width of the textures used in this rendering. Must be in [8,4096] and a power of two.
78  * \param height the height of the textures used in this rendering. Must be in [8,4096] and a power of two.
79  *
80  * \param noHud If true, no hud gets displayed showing the created and used textures.
81  */
82  WGEOffscreenRenderNode( osg::ref_ptr< WGECamera > reference, size_t width = 2048, size_t height = 2048, bool noHud = false );
83 
84  /**
85  * Destructor.
86  */
87  virtual ~WGEOffscreenRenderNode();
88 
89  /**
90  * Returns the instance of the texture HUD.
91  *
92  * \return the HUD
93  */
94  osg::ref_ptr< WGETextureHud > getTextureHUD() const;
95 
96  /**
97  * Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG graph to a texture.
98  *
99  * \param node the node which represents the subgraph.
100  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
101  *
102  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
103  *
104  * \return the geometry render pass.
105  */
106  virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass( osg::ref_ptr< osg::Node > node, std::string name = "Unnamed" );
107 
108  /**
109  * Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG graph to a texture.
110  *
111  * \param node the node which represents the subgraph.
112  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
113  * \param shader the shader to add.
114  *
115  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
116  *
117  * \return the geometry render pass.
118  */
119  virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGEShader > shader,
120  std::string name = "Unnamed" );
121 
122  /**
123  * Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures. All the in- and output textures
124  * have to be specified manually.
125  *
126  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
127  *
128  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
129  *
130  * \return the texture processing pass created.
131  */
132  virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass( std::string name = "Unnamed" );
133 
134  /**
135  * Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures. All the in- and output textures
136  * have to be specified manually.
137  *
138  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
139  *
140  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
141  * \param shader the shader to add.
142  *
143  * \return the texture processing pass created.
144  */
145  virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass( osg::ref_ptr< WGEShader > shader, std::string name = "Unnamed" );
146 
147  /**
148  * 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
149  * frame-buffer. An optional shader can be used for final processing (most commonly clipping, blending, color-mapping and so on).
150  *
151  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
152  *
153  * \return the on-screen render pass which draws processed textures back on screen.
154  */
155  virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass( std::string name = "Unnamed" );
156 
157  /**
158  * 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
159  * frame-buffer. An optional shader can be used for final processing (most commonly clipping, blending, color-mapping and so on).
160  *
161  * \param shader the shader to add
162  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
163  *
164  * \return the on-screen render pass which draws processed textures back on screen.
165  */
166  virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass( osg::ref_ptr< WGEShader > shader, std::string name = "Unnamed" );
167 
168  /**
169  * Creates a new offscreen-render-pass coupled with the reference camera. This pass actually does nothing. The method is useful for custom
170  * variants of WGEOffscreenRenderPass.
171  *
172  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
173  *
174  * \return new instance of a plain render pass
175  *
176  * \tparam T the type of pass to create.
177  */
178  template < typename T >
179  osg::ref_ptr< T > addRenderPass( std::string name = "Unnamed" );
180 
181  /**
182  * Get FBO texture width.
183  *
184  * \return the width
185  */
186  size_t getTextureWidth() const;
187 
188  /**
189  * Get FBO texture height.
190  *
191  * \return the height
192  */
193  size_t getTextureHeight() const;
194 
195  /**
196  * If true, the viewport has the size of the resulting texture. This is very interesting if you want to force large scale renderings and want
197  * to decouple your offscreen pass from the reference camera viewport.
198  *
199  * \param vp if true, viewport is forced to be the whole texture size
200  */
201  void setLinkViewportToTextureSize( bool vp = true );
202 
203  /**
204  * The flag denotes whether the viewport is linked to the texture size or the reference camera. See \ref setLinkViewportToTextureSize for
205  * details.
206  *
207  * \return the flag.
208  */
209  bool getLinkViewportToTextureSize() const;
210 
211 protected:
212 private:
213  /**
214  * The camera to which is used for setting this camera up.
215  */
216  osg::ref_ptr< WGECamera > m_referenceCamera;
217 
218  /**
219  * The pointer to the hud used to render all used texture buffers. This can be NULL. It gets distributed to all created render-pass
220  * instances.
221  */
222  osg::ref_ptr< WGETextureHud > m_hud;
223 
224  /**
225  * The width of each texture in this offscreen rendering.
226  */
228 
229  /**
230  * The height of each texture in this offscreen rendering.
231  */
233 
234  /**
235  * The number of the next pass getting added.
236  */
238 
239  /**
240  * Flag denotes whether the viewport is coupled to the reference camera or the texture size
241  */
243 };
244 
245 template < typename T >
246 osg::ref_ptr< T > WGEOffscreenRenderNode::addRenderPass( std::string name )
247 {
248  // create a new pass
249  osg::ref_ptr< T > pass = new T( m_textureWidth, m_textureHeight, m_hud, name, m_nextPassNum );
250  m_nextPassNum++;
251 
252  // ensure proper propagation of viewport changes
254  {
255  pass->addUpdateCallback( new WGEViewportCallback< T >( m_textureWidth, m_textureHeight ) );
256  }
257  else
258  {
259  pass->addUpdateCallback( new WGEViewportCallback< T >( m_referenceCamera ) );
260  }
261 
262  // set clear mask and color according to reference cam
263  pass->setClearMask( m_referenceCamera->getClearMask() );
264  // We disabled this. This is needed to transport ALPHA values properly in RGBA attachments
265  // pass->setClearColor( m_referenceCamera->getClearColor() );
266 
267  // inherit cull settings
268  pass->setCullSettings( *m_referenceCamera );
269 
270  pass->setComputeNearFarMode( osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR );
271  pass->setNearFarRatio( 0.000001 );
272  pass->setCullingMode( osg::CullSettings::NO_CULLING );
273 
274  // this node needs to keep all the pass instances. Only this way, the OSG traverses and renders these nodes in the order specified by
275  // m_nextPassNum.
276  insert( pass ); // insert into this group
277 
278  return pass;
279 }
280 
281 #endif // WGEOFFSCREENRENDERNODE_H
282 
Class to wrap around the osg Group node and providing a thread safe add/removal mechanism.
Definition: WGEGroupNode.h:48
void insert(osg::ref_ptr< osg::Node > node)
Adds the specified node to the child list of this node in a safe manner.
This type of node basically is a convenience class for managing and creating offscreen renderings.
osg::ref_ptr< WGETextureHud > getTextureHUD() const
Returns the instance of the texture HUD.
size_t getTextureWidth() const
Get FBO texture width.
virtual ~WGEOffscreenRenderNode()
Destructor.
size_t m_textureHeight
The height of each texture in this offscreen rendering.
void setLinkViewportToTextureSize(bool vp=true)
If true, the viewport has the size of the resulting texture.
virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass(std::string name="Unnamed")
Creates a new render pass which can be seen as put-textures-back-on-screen-pass.
osg::ref_ptr< T > addRenderPass(std::string name="Unnamed")
Creates a new offscreen-render-pass coupled with the reference camera.
WGEOffscreenRenderNode(osg::ref_ptr< WGECamera > reference, size_t width=2048, size_t height=2048, bool noHud=false)
Create a new managing instance.
osg::ref_ptr< WGECamera > m_referenceCamera
The camera to which is used for setting this camera up.
size_t m_textureWidth
The width of each texture in this offscreen rendering.
osg::ref_ptr< WGETextureHud > m_hud
The pointer to the hud used to render all used texture buffers.
osg::ref_ptr< WGEOffscreenRenderNode > RefPtr
Convenience typedef for an osg::ref_ptr.
osg::ref_ptr< const WGEOffscreenRenderNode > ConstRefPtr
Convenience typedef for an osg::ref_ptr; const.
size_t m_nextPassNum
The number of the next pass getting added.
bool m_forceViewportTextureSize
Flag denotes whether the viewport is coupled to the reference camera or the texture size.
virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass(osg::ref_ptr< osg::Node > node, std::string name="Unnamed")
Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG g...
bool getLinkViewportToTextureSize() const
The flag denotes whether the viewport is linked to the texture size or the reference camera.
virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass(std::string name="Unnamed")
Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures...
size_t getTextureHeight() const
Get FBO texture height.
This callback is useful to update viewport information on several nodes supporting it.