OpenWalnut  1.3.1
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 
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 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  * On Mac OSX, only GL_RGBA works as internal format, so all input to internalFormat is ignored and overwritten by GL_RGBA internally.
100  *
101  * \param buffer the buffer to attach the new texture to
102  * \param internalFormat the format to use. By default, RGBA
103  *
104  * \note if the node is added to the graph, these functions should only be called from within an update callback.
105  *
106  * \return the newly created texture.
107  */
108  osg::ref_ptr< osg::Texture2D > attach( BufferComponent buffer, GLint internalFormat = GL_RGBA );
109 
110  /**
111  * Detaches the texture currently bound to the specified buffer.
112  *
113  * \param buffer the buffer to detach.
114  *
115  * \note if the node is added to the graph, these functions should only be called from within an update callback.
116  */
117  void detach( BufferComponent buffer );
118 
119  /**
120  * This is a shortcut for wge::bindTexture. See \ref wge::bindTexture for details.
121  *
122  * \param unit the unit to use
123  * \param texture the texture to use.
124  * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
125  */
126  template < typename T >
127  void bind( osg::ref_ptr< T > texture, size_t unit = 0 );
128 
129  /**
130  * Creates a new texture suitable for this offscreen rendering instance. The texture will have the same size as the viewport of this camera.
131  *
132  * \param internalFormat the format to use for the texture.
133  *
134  * \return the newly created texture
135  */
136  osg::ref_ptr< osg::Texture2D > createTexture( GLint internalFormat = GL_RGBA );
137 
138  /**
139  * Returns the name of this render pass.
140  *
141  * \return the name
142  */
143  std::string getName() const;
144 
145  /**
146  * Returns the buffer name. This is useful for debugging messages and so on as it maps a buffer constant to its name.
147  *
148  * \param buffer the buffer to get the name for
149  *
150  * \return the name
151  */
152  static std::string getBufferName( BufferComponent buffer );
153 
154  /**
155  * Get the size of the underlying texture.
156  *
157  * \return the width
158  */
159  size_t getTextureWidth() const;
160 
161  /**
162  * Get the size of the underlying texture.
163  *
164  * \return the height
165  */
166  size_t getTextureHeight() const;
167 
168  /**
169  * The uniform to add. This is a shortcut for this->getOrCreateStateSet()->addUniform( uniform ).
170  *
171  * \param uniform the uniform to add
172  */
173  virtual void addUniform( osg::ref_ptr< osg::Uniform > uniform );
174 protected:
175  /**
176  * The width of the textures used for this pass. This should be as large as needed for each "common" viewport."
177  */
178  size_t m_width;
179 
180  /**
181  * The height of the textures used for this pass. This should be as large as needed for each "common" viewport."
182  */
183  size_t m_height;
184 
185  /**
186  * The framebuffer object to use for this camera.
187  */
188  osg::ref_ptr<osg::FrameBufferObject> m_fbo;
189 
190  /**
191  * Gets notified about any added and removed attachment
192  */
193  osg::ref_ptr< WGETextureHud > m_hud;
194 
195  /**
196  * The name if the rendering pass. Especially useful in conjunction with m_hud.
197  */
198  std::string m_name;
199 private:
200 };
201 
202 template < typename T >
203 void WGEOffscreenRenderPass::bind( osg::ref_ptr< T > texture, size_t unit )
204 {
205  wge::bindTexture( this, texture, unit );
206 }
207 
208 #endif // WGEOFFSCREENRENDERPASS_H
209