OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 "../WExportWGE.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 WGE_EXPORT WGEOffscreenRenderPass: public osg::Camera // 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      * This method attaches a texture to the given buffer. The texture gets created with the resolution of the FBO.
00098      *
00099      * \param buffer the buffer to attach the new texture to
00100      * \param internalFormat the format to use. By default, RGBA
00101      *
00102      * \note if the node is added to the graph, these functions should only be called from within an update callback.
00103      *
00104      * \return the newly created texture.
00105      */
00106     osg::ref_ptr< osg::Texture2D > attach( BufferComponent buffer, GLint internalFormat = GL_RGBA );
00107 
00108     /**
00109      * Detaches the texture currently bound to the specified buffer.
00110      *
00111      * \param buffer the buffer to detach.
00112      *
00113      * \note if the node is added to the graph, these functions should only be called from within an update callback.
00114      */
00115     void detach( BufferComponent buffer );
00116 
00117     /**
00118      * This is a shortcut for wge::bindTexture. See \ref wge::bindTexture for details.
00119      *
00120      * \param unit the unit to use
00121      * \param texture the texture to use.
00122      * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
00123      */
00124     template < typename T >
00125     void bind( osg::ref_ptr< T > texture, size_t unit = 0 );
00126 
00127     /**
00128      * Creates a new texture suitable for this offscreen rendering instance. The texture will have the same size as the viewport of this camera.
00129      *
00130      * \param internalFormat the format to use for the texture.
00131      *
00132      * \return the newly created texture
00133      */
00134     osg::ref_ptr< osg::Texture2D > createTexture( GLint internalFormat = GL_RGBA );
00135 
00136     /**
00137      * Returns the name of this render pass.
00138      *
00139      * \return the name
00140      */
00141     std::string getName() const;
00142 
00143     /**
00144      * Returns the buffer name. This is useful for debugging messages and so on as it maps a buffer constant to its name.
00145      *
00146      * \param buffer the buffer to get the name for
00147      *
00148      * \return the name
00149      */
00150     static std::string getBufferName( BufferComponent buffer );
00151 
00152     /**
00153      * Get the size of the underlying texture.
00154      *
00155      * \return the width
00156      */
00157     size_t getTextureWidth() const;
00158 
00159     /**
00160      * Get the size of the underlying texture.
00161      *
00162      * \return the height
00163      */
00164     size_t getTextureHeight() const;
00165 
00166     /**
00167      * The uniform to add. This is a shortcut for this->getOrCreateStateSet()->addUniform( uniform ).
00168      *
00169      * \param uniform the uniform to add
00170      */
00171     virtual void addUniform( osg::ref_ptr< osg::Uniform > uniform );
00172 protected:
00173 
00174     /**
00175      * The width of the textures used for this pass. This should be as large as needed for each "common" viewport."
00176      */
00177     size_t m_width;
00178 
00179     /**
00180      * The height of the textures used for this pass. This should be as large as needed for each "common" viewport."
00181      */
00182     size_t m_height;
00183 
00184     /**
00185      * The framebuffer object to use for this camera.
00186      */
00187     osg::ref_ptr<osg::FrameBufferObject> m_fbo;
00188 
00189     /**
00190      * Gets notified about any added and removed attachment
00191      */
00192     osg::ref_ptr< WGETextureHud > m_hud;
00193 
00194     /**
00195      * The name if the rendering pass. Especially useful in conjunction with m_hud.
00196      */
00197     std::string m_name;
00198 private:
00199 };
00200 
00201 template < typename T >
00202 void WGEOffscreenRenderPass::bind( osg::ref_ptr< T > texture, size_t unit )
00203 {
00204     wge::bindTexture( this, texture, unit );
00205 }
00206 
00207 #endif  // WGEOFFSCREENRENDERPASS_H
00208 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends