OpenWalnut  1.5.0dev
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 "../WGECamera.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 WGEOffscreenRenderPass: public WGECamera // 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  * Attach a given image to a buffer. Use this if you want to read back your rendered textures. This does not replace the attach call for
98  * textures. This only utilizes the multiple render target extension and allows you to read back your buffers.
99  *
100  * \param buffer the buffer to attach the texture to
101  * \param image the image to attach
102  *
103  * \note if the node is added to the graph, these functions should only be called from within an update callback.
104  */
105  void attach( BufferComponent buffer, osg::ref_ptr< osg::Image > image );
106 
107  /**
108  * This method attaches a texture to the given buffer. The texture gets created with the resolution of the FBO.
109  *
110  * On Mac OSX, only GL_RGBA works as internal format, so all input to internalFormat is ignored and overwritten by GL_RGBA internally.
111  *
112  * \param buffer the buffer to attach the new texture to
113  * \param internalFormat the format to use. By default, RGBA
114  *
115  * \note if the node is added to the graph, these functions should only be called from within an update callback.
116  *
117  * \return the newly created texture.
118  */
119  osg::ref_ptr< osg::Texture2D > attach( BufferComponent buffer, GLint internalFormat = GL_RGBA );
120 
121  /**
122  * Detaches the texture currently bound to the specified buffer.
123  *
124  * \param buffer the buffer to detach.
125  *
126  * \note if the node is added to the graph, these functions should only be called from within an update callback.
127  */
128  void detach( BufferComponent buffer );
129 
130  /**
131  * This is a shortcut for wge::bindTexture. See \ref wge::bindTexture for details.
132  *
133  * \param unit the unit to use
134  * \param texture the texture to use.
135  * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
136  */
137  template < typename T >
138  void bind( osg::ref_ptr< T > texture, size_t unit = 0 );
139 
140  /**
141  * Creates a new texture suitable for this offscreen rendering instance. The texture will have the same size as the viewport of this camera.
142  *
143  * \param internalFormat the format to use for the texture.
144  *
145  * \return the newly created texture
146  */
147  osg::ref_ptr< osg::Texture2D > createTexture( GLint internalFormat = GL_RGBA );
148 
149  /**
150  * Returns the name of this render pass.
151  *
152  * \return the name
153  */
154  std::string getName() const;
155 
156  /**
157  * Returns the buffer name. This is useful for debugging messages and so on as it maps a buffer constant to its name.
158  *
159  * \param buffer the buffer to get the name for
160  *
161  * \return the name
162  */
163  static std::string getBufferName( BufferComponent buffer );
164 
165  /**
166  * Get the size of the underlying texture.
167  *
168  * \return the width
169  */
170  size_t getTextureWidth() const;
171 
172  /**
173  * Get the size of the underlying texture.
174  *
175  * \return the height
176  */
177  size_t getTextureHeight() const;
178 
179  /**
180  * The uniform to add. This is a shortcut for this->getOrCreateStateSet()->addUniform( uniform ).
181  *
182  * \param uniform the uniform to add
183  */
184  virtual void addUniform( osg::ref_ptr< osg::Uniform > uniform );
185 protected:
186  /**
187  * The width of the textures used for this pass. This should be as large as needed for each "common" viewport."
188  */
189  size_t m_width;
190 
191  /**
192  * The height of the textures used for this pass. This should be as large as needed for each "common" viewport."
193  */
194  size_t m_height;
195 
196  /**
197  * The framebuffer object to use for this camera.
198  */
199  osg::ref_ptr<osg::FrameBufferObject> m_fbo;
200 
201  /**
202  * Gets notified about any added and removed attachment
203  */
204  osg::ref_ptr< WGETextureHud > m_hud;
205 
206  /**
207  * The name if the rendering pass. Especially useful in conjunction with m_hud.
208  */
209  std::string m_name;
210 private:
211 };
212 
213 template < typename T >
214 void WGEOffscreenRenderPass::bind( osg::ref_ptr< T > texture, size_t unit )
215 {
216  wge::bindTexture( this, texture, unit );
217 }
218 
219 #endif // WGEOFFSCREENRENDERPASS_H
220 
Class for wrapping around the OSG Camera class.
Definition: WGECamera.h:36
This class encapsulates an OSG Camera and a corresponding framebuffer object.
std::string m_name
The name if the rendering pass.
osg::ref_ptr< const WGEOffscreenRenderPass > ConstRefPtr
Convenience typedef for an osg::ref_ptr; const.
osg::ref_ptr< WGETextureHud > m_hud
Gets notified about any added and removed attachment.
osg::ref_ptr< osg::Texture2D > createTexture(GLint internalFormat=GL_RGBA)
Creates a new texture suitable for this offscreen rendering instance.
osg::ref_ptr< osg::FrameBufferObject > m_fbo
The framebuffer object to use for this camera.
size_t m_height
The height of the textures used for this pass.
WGEOffscreenRenderPass(size_t textureWidth, size_t textureHeight, int num=0)
Creates a new offscreen rendering instance.
osg::ref_ptr< WGEOffscreenRenderPass > RefPtr
Convenience typedef for an osg::ref_ptr.
void attach(BufferComponent buffer, osg::ref_ptr< osg::Texture2D > texture)
Attach a given texture to a buffer.
void bind(osg::ref_ptr< T > texture, size_t unit=0)
This is a shortcut for wge::bindTexture.
void detach(BufferComponent buffer)
Detaches the texture currently bound to the specified buffer.
size_t m_width
The width of the textures used for this pass.
size_t getTextureWidth() const
Get the size of the underlying texture.
virtual void addUniform(osg::ref_ptr< osg::Uniform > uniform)
The uniform to add.
size_t getTextureHeight() const
Get the size of the underlying texture.
static std::string getBufferName(BufferComponent buffer)
Returns the buffer name.
virtual ~WGEOffscreenRenderPass()
Destructor.
std::string getName() const
Returns the name of this render pass.
This class implements a HUD showing several textures on screen.
Definition: WGETextureHud.h:47
void bindTexture(osg::ref_ptr< osg::Node > node, osg::ref_ptr< WDataTexture3D > texture, size_t unit=0, std::string prefix="")
Binds the specified texture to the specified unit.