OpenWalnut  1.3.1
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 
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 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  * \note The width and hight define the offscreen texture size. The viewport if each rendering is automatically set to the one of the
73  * reference camera. This means, width and height only define the maximal supported resolution without upscaling of your offscreen renderer.
74  *
75  * \param reference camera used as reference
76  * \param width the width of the textures used in this rendering. Must be in [8,4096] and a power of two.
77  * \param height the height of the textures used in this rendering. Must be in [8,4096] and a power of two.
78  *
79  * \param noHud If true, no hud gets displayed showing the created and used textures.
80  */
81  WGEOffscreenRenderNode( osg::ref_ptr< osg::Camera > reference, size_t width = 2048, size_t height = 2048, bool noHud = false );
82 
83  /**
84  * Destructor.
85  */
86  virtual ~WGEOffscreenRenderNode();
87 
88  /**
89  * Returns the instance of the texture HUD.
90  *
91  * \return the HUD
92  */
93  osg::ref_ptr< WGETextureHud > getTextureHUD() const;
94 
95  /**
96  * Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG graph to a texture.
97  *
98  * \param node the node which represents the subgraph.
99  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
100  *
101  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
102  *
103  * \return the geometry render pass.
104  */
105  virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass( osg::ref_ptr< osg::Node > node, std::string name = "Unnamed" );
106 
107  /**
108  * Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG graph to a texture.
109  *
110  * \param node the node which represents the subgraph.
111  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
112  * \param shader the shader to add.
113  *
114  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
115  *
116  * \return the geometry render pass.
117  */
118  virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGEShader > shader,
119  std::string name = "Unnamed" );
120 
121  /**
122  * Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures. All the in- and output textures
123  * have to be specified manually.
124  *
125  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
126  *
127  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
128  *
129  * \return the texture processing pass created.
130  */
131  virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass( std::string name = "Unnamed" );
132 
133  /**
134  * Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures. All the in- and output textures
135  * have to be specified manually.
136  *
137  * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
138  *
139  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
140  * \param shader the shader to add.
141  *
142  * \return the texture processing pass created.
143  */
144  virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass( osg::ref_ptr< WGEShader > shader, std::string name = "Unnamed" );
145 
146  /**
147  * 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
148  * frame-buffer. An optional shader can be used for final processing (most commonly clipping, blending, color-mapping and so on).
149  *
150  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
151  *
152  * \return the on-screen render pass which draws processed textures back on screen.
153  */
154  virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass( std::string name = "Unnamed" );
155 
156  /**
157  * 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
158  * frame-buffer. An optional shader can be used for final processing (most commonly clipping, blending, color-mapping and so on).
159  *
160  * \param shader the shader to add
161  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
162  *
163  * \return the on-screen render pass which draws processed textures back on screen.
164  */
165  virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass( osg::ref_ptr< WGEShader > shader, std::string name = "Unnamed" );
166 
167  /**
168  * Creates a new offscreen-render-pass coupled with the reference camera. This pass actually does nothing. The method is useful for custom
169  * variants of WGEOffscreenRenderPass.
170  *
171  * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
172  *
173  * \return new instance of a plain render pass
174  *
175  * \tparam T the type of pass to create.
176  */
177  template < typename T >
178  osg::ref_ptr< T > addRenderPass( std::string name = "Unnamed" );
179 
180 protected:
181 private:
182  /**
183  * The camera to which is used for setting this camera up.
184  */
185  osg::ref_ptr< osg::Camera > m_referenceCamera;
186 
187  /**
188  * The pointer to the hud used to render all used texture buffers. This can be NULL. It gets distributed to all created render-pass
189  * instances.
190  */
191  osg::ref_ptr< WGETextureHud > m_hud;
192 
193  /**
194  * The width of each texture in this offscreen rendering.
195  */
197 
198  /**
199  * The height of each texture in this offscreen rendering.
200  */
202 
203  /**
204  * The number of the next pass getting added.
205  */
207 };
208 
209 template < typename T >
210 osg::ref_ptr< T > WGEOffscreenRenderNode::addRenderPass( std::string name )
211 {
212  // create a new pass
213  osg::ref_ptr< T > pass = new T( m_textureWidth, m_textureHeight, m_hud, name, m_nextPassNum );
214  m_nextPassNum++;
215 
216  // this node needs to keep all the pass instances. Only this way, the OSG traverses and renders these nodes in the order specified by
217  // m_nextPassNum.
218  insert( pass ); // insert into this group
219 
220  // ensure proper propagation of viewport changes
221  pass->addUpdateCallback( new WGEViewportCallback< T >( m_referenceCamera ) );
222 
223  // set clear mask and color according to reference cam
224  pass->setClearMask( m_referenceCamera->getClearMask() );
225  pass->setClearColor( m_referenceCamera->getClearColor() );
226 
227  return pass;
228 }
229 
230 #endif // WGEOFFSCREENRENDERNODE_H
231