OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEOffscreenRenderPass.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 WGEOFFSCREENRENDERPASS_H
26 #define WGEOFFSCREENRENDERPASS_H
27 
28 #include <string>
29 
30 #include <osg/Camera>
31 #include <osg/FrameBufferObject>
32 
33 #include "../WGEUtils.h"
34 #include "../WGETexture.h"
35 #include "../WExportWGE.h"
36 
37 class WGETextureHud;
38 
39 /**
40  * This class encapsulates an OSG Camera and a corresponding framebuffer object. It is especially useful for offscreen renderings. It is a camera
41  * which, by default, is the same as the camera in the this instance nesting graph. It allows simple attachment of textures to a offscreen
42  * rendering as well as easy texture creation.
43  */
44 class WGE_EXPORT WGEOffscreenRenderPass: public osg::Camera // NOLINT
45 {
46 public:
47  /**
48  * Convenience typedef for an osg::ref_ptr
49  */
50  typedef osg::ref_ptr< WGEOffscreenRenderPass > RefPtr;
51 
52  /**
53  * Convenience typedef for an osg::ref_ptr; const
54  */
55  typedef osg::ref_ptr< const WGEOffscreenRenderPass > ConstRefPtr;
56 
57  /**
58  * Creates a new offscreen rendering instance.
59  *
60  * \param textureWidth the width of all the textures created and used by this render pass. This should be large enough for every reasonable
61  * viewport size.
62  * \param textureHeight the height of all the textures created and used by this render pass. This should be large enough for every reasonable
63  * viewport size.*
64  * \param num the order number. This camera gets rendered at the num'th place in the pre render queue of the subgraph it is attached to.
65  */
66  WGEOffscreenRenderPass( size_t textureWidth, size_t textureHeight, int num = 0 );
67 
68  /**
69  * Creates a new offscreen rendering instance.
70  *
71  * \param textureWidth the width of all the textures created and used by this render pass. This should be large enough for every reasonable
72  * viewport size.
73  * \param textureHeight the height of all the textures created and used by this render pass. This should be large enough for every reasonable
74  * viewport size.*
75  * \param num the order number. This camera gets rendered at the num'th place in the pre render queue of the subgraph it is attached to.
76  * \param hud the hud that gets notified about attached and detached textures. Useful for debugging.
77  * \param name the name of this render pass. This is a nice debugging feature in conjunction with WGETextureHud as it gets displayed there.
78  */
79  WGEOffscreenRenderPass( size_t textureWidth, size_t textureHeight, osg::ref_ptr< WGETextureHud > hud, std::string name, int num = 0 );
80 
81  /**
82  * Destructor.
83  */
84  virtual ~WGEOffscreenRenderPass();
85 
86  /**
87  * Attach a given texture to a buffer.
88  *
89  * \param buffer the buffer to attach the texture to
90  * \param texture the texture to attach
91  *
92  * \note if the node is added to the graph, these functions should only be called from within an update callback.
93  */
94  void attach( BufferComponent buffer, osg::ref_ptr< osg::Texture2D > texture );
95 
96  /**
97  * This method attaches a texture to the given buffer. The texture gets created with the resolution of the FBO.
98  *
99  * \param buffer the buffer to attach the new texture to
100  * \param internalFormat the format to use. By default, RGBA
101  *
102  * \note if the node is added to the graph, these functions should only be called from within an update callback.
103  *
104  * \return the newly created texture.
105  */
106  osg::ref_ptr< osg::Texture2D > attach( BufferComponent buffer, GLint internalFormat = GL_RGBA );
107 
108  /**
109  * Detaches the texture currently bound to the specified buffer.
110  *
111  * \param buffer the buffer to detach.
112  *
113  * \note if the node is added to the graph, these functions should only be called from within an update callback.
114  */
115  void detach( BufferComponent buffer );
116 
117  /**
118  * This is a shortcut for wge::bindTexture. See \ref wge::bindTexture for details.
119  *
120  * \param unit the unit to use
121  * \param texture the texture to use.
122  * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
123  */
124  template < typename T >
125  void bind( osg::ref_ptr< T > texture, size_t unit = 0 );
126 
127  /**
128  * Creates a new texture suitable for this offscreen rendering instance. The texture will have the same size as the viewport of this camera.
129  *
130  * \param internalFormat the format to use for the texture.
131  *
132  * \return the newly created texture
133  */
134  osg::ref_ptr< osg::Texture2D > createTexture( GLint internalFormat = GL_RGBA );
135 
136  /**
137  * Returns the name of this render pass.
138  *
139  * \return the name
140  */
141  std::string getName() const;
142 
143  /**
144  * Returns the buffer name. This is useful for debugging messages and so on as it maps a buffer constant to its name.
145  *
146  * \param buffer the buffer to get the name for
147  *
148  * \return the name
149  */
150  static std::string getBufferName( BufferComponent buffer );
151 
152  /**
153  * Get the size of the underlying texture.
154  *
155  * \return the width
156  */
157  size_t getTextureWidth() const;
158 
159  /**
160  * Get the size of the underlying texture.
161  *
162  * \return the height
163  */
164  size_t getTextureHeight() const;
165 
166  /**
167  * The uniform to add. This is a shortcut for this->getOrCreateStateSet()->addUniform( uniform ).
168  *
169  * \param uniform the uniform to add
170  */
171  virtual void addUniform( osg::ref_ptr< osg::Uniform > uniform );
172 protected:
173 
174  /**
175  * The width of the textures used for this pass. This should be as large as needed for each "common" viewport."
176  */
177  size_t m_width;
178 
179  /**
180  * The height of the textures used for this pass. This should be as large as needed for each "common" viewport."
181  */
182  size_t m_height;
183 
184  /**
185  * The framebuffer object to use for this camera.
186  */
187  osg::ref_ptr<osg::FrameBufferObject> m_fbo;
188 
189  /**
190  * Gets notified about any added and removed attachment
191  */
192  osg::ref_ptr< WGETextureHud > m_hud;
193 
194  /**
195  * The name if the rendering pass. Especially useful in conjunction with m_hud.
196  */
197  std::string m_name;
198 private:
199 };
200 
201 template < typename T >
202 void WGEOffscreenRenderPass::bind( osg::ref_ptr< T > texture, size_t unit )
203 {
204  wge::bindTexture( this, texture, unit );
205 }
206 
207 #endif // WGEOFFSCREENRENDERPASS_H
208