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