OpenWalnut  1.4.0
WGEOffscreenRenderPass.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGEOFFSCREENRENDERPASS_H
00026 #define WGEOFFSCREENRENDERPASS_H
00027 
00028 #include <string>
00029 
00030 #include <osg/Camera>
00031 #include <osg/FrameBufferObject>
00032 
00033 #include "../WGEUtils.h"
00034 #include "../WGETexture.h"
00035 #include "../WGECamera.h"
00036 
00037 class WGETextureHud;
00038 
00039 /**
00040  * This class encapsulates an OSG Camera and a corresponding framebuffer object. It is especially useful for offscreen renderings. It is a camera
00041  * which, by default, is the same as the camera in the this instance nesting graph. It allows simple attachment of textures to a offscreen
00042  * rendering as well as easy texture creation.
00043  */
00044 class WGEOffscreenRenderPass: public WGECamera // NOLINT
00045 {
00046 public:
00047     /**
00048      * Convenience typedef for an osg::ref_ptr
00049      */
00050     typedef osg::ref_ptr< WGEOffscreenRenderPass > RefPtr;
00051 
00052     /**
00053      * Convenience typedef for an osg::ref_ptr; const
00054      */
00055     typedef osg::ref_ptr< const WGEOffscreenRenderPass > ConstRefPtr;
00056 
00057     /**
00058      * Creates a new offscreen rendering instance.
00059      *
00060      * \param textureWidth the width of all the textures created and used by this render pass. This should be large enough for every reasonable
00061      *                     viewport size.
00062      * \param textureHeight the height of all the textures created and used by this render pass. This should be large enough for every reasonable
00063      *                     viewport size.*
00064      * \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.
00065      */
00066     WGEOffscreenRenderPass( size_t textureWidth, size_t textureHeight, int num = 0 );
00067 
00068     /**
00069      * Creates a new offscreen rendering instance.
00070      *
00071      * \param textureWidth the width of all the textures created and used by this render pass. This should be large enough for every reasonable
00072      *                     viewport size.
00073      * \param textureHeight the height of all the textures created and used by this render pass. This should be large enough for every reasonable
00074      *                     viewport size.*
00075      * \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.
00076      * \param hud the hud that gets notified about attached and detached textures. Useful for debugging.
00077      * \param name the name of this render pass. This is a nice debugging feature in conjunction with WGETextureHud as it gets displayed there.
00078      */
00079     WGEOffscreenRenderPass( size_t textureWidth, size_t textureHeight, osg::ref_ptr< WGETextureHud > hud, std::string name, int num = 0 );
00080 
00081     /**
00082      * Destructor.
00083      */
00084     virtual ~WGEOffscreenRenderPass();
00085 
00086     /**
00087      * Attach a given texture to a buffer.
00088      *
00089      * \param buffer the buffer to attach the texture to
00090      * \param texture the texture to attach
00091      *
00092      * \note if the node is added to the graph, these functions should only be called from within an update callback.
00093      */
00094     void attach( BufferComponent buffer, osg::ref_ptr< osg::Texture2D > texture );
00095 
00096     /**
00097      * 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
00098      * textures. This only utilizes the multiple render target extension and allows you to read back your buffers.
00099      *
00100      * \param buffer the buffer to attach the texture to
00101      * \param image the image to attach
00102      *
00103      * \note if the node is added to the graph, these functions should only be called from within an update callback.
00104      */
00105     void attach( BufferComponent buffer, osg::ref_ptr< osg::Image > image );
00106 
00107     /**
00108      * This method attaches a texture to the given buffer. The texture gets created with the resolution of the FBO.
00109      *
00110      * On Mac OSX, only GL_RGBA works as internal format, so all input to internalFormat is ignored and overwritten by GL_RGBA internally.
00111      *
00112      * \param buffer the buffer to attach the new texture to
00113      * \param internalFormat the format to use. By default, RGBA
00114      *
00115      * \note if the node is added to the graph, these functions should only be called from within an update callback.
00116      *
00117      * \return the newly created texture.
00118      */
00119     osg::ref_ptr< osg::Texture2D > attach( BufferComponent buffer, GLint internalFormat = GL_RGBA );
00120 
00121     /**
00122      * Detaches the texture currently bound to the specified buffer.
00123      *
00124      * \param buffer the buffer to detach.
00125      *
00126      * \note if the node is added to the graph, these functions should only be called from within an update callback.
00127      */
00128     void detach( BufferComponent buffer );
00129 
00130     /**
00131      * This is a shortcut for wge::bindTexture. See \ref wge::bindTexture for details.
00132      *
00133      * \param unit the unit to use
00134      * \param texture the texture to use.
00135      * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
00136      */
00137     template < typename T >
00138     void bind( osg::ref_ptr< T > texture, size_t unit = 0 );
00139 
00140     /**
00141      * Creates a new texture suitable for this offscreen rendering instance. The texture will have the same size as the viewport of this camera.
00142      *
00143      * \param internalFormat the format to use for the texture.
00144      *
00145      * \return the newly created texture
00146      */
00147     osg::ref_ptr< osg::Texture2D > createTexture( GLint internalFormat = GL_RGBA );
00148 
00149     /**
00150      * Returns the name of this render pass.
00151      *
00152      * \return the name
00153      */
00154     std::string getName() const;
00155 
00156     /**
00157      * Returns the buffer name. This is useful for debugging messages and so on as it maps a buffer constant to its name.
00158      *
00159      * \param buffer the buffer to get the name for
00160      *
00161      * \return the name
00162      */
00163     static std::string getBufferName( BufferComponent buffer );
00164 
00165     /**
00166      * Get the size of the underlying texture.
00167      *
00168      * \return the width
00169      */
00170     size_t getTextureWidth() const;
00171 
00172     /**
00173      * Get the size of the underlying texture.
00174      *
00175      * \return the height
00176      */
00177     size_t getTextureHeight() const;
00178 
00179     /**
00180      * The uniform to add. This is a shortcut for this->getOrCreateStateSet()->addUniform( uniform ).
00181      *
00182      * \param uniform the uniform to add
00183      */
00184     virtual void addUniform( osg::ref_ptr< osg::Uniform > uniform );
00185 protected:
00186     /**
00187      * The width of the textures used for this pass. This should be as large as needed for each "common" viewport."
00188      */
00189     size_t m_width;
00190 
00191     /**
00192      * The height of the textures used for this pass. This should be as large as needed for each "common" viewport."
00193      */
00194     size_t m_height;
00195 
00196     /**
00197      * The framebuffer object to use for this camera.
00198      */
00199     osg::ref_ptr<osg::FrameBufferObject> m_fbo;
00200 
00201     /**
00202      * Gets notified about any added and removed attachment
00203      */
00204     osg::ref_ptr< WGETextureHud > m_hud;
00205 
00206     /**
00207      * The name if the rendering pass. Especially useful in conjunction with m_hud.
00208      */
00209     std::string m_name;
00210 private:
00211 };
00212 
00213 template < typename T >
00214 void WGEOffscreenRenderPass::bind( osg::ref_ptr< T > texture, size_t unit )
00215 {
00216     wge::bindTexture( this, texture, unit );
00217 }
00218 
00219 #endif  // WGEOFFSCREENRENDERPASS_H
00220