OpenWalnut 1.2.5

WGEOffscreenRenderNode.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 WGEOFFSCREENRENDERNODE_H
00026 #define WGEOFFSCREENRENDERNODE_H
00027 
00028 #include <string>
00029 
00030 #include <osg/Camera>
00031 
00032 #include "../WGEGroupNode.h"
00033 #include "WGEOffscreenRenderPass.h"
00034 #include "WGEOffscreenTexturePass.h"
00035 #include "WGEOffscreenFinalPass.h"
00036 #include "../WGETextureHud.h"
00037 #include "../shaders/WGEShader.h"
00038 #include "../callbacks/WGEViewportCallback.h"
00039 #include "../WExportWGE.h"
00040 
00041 /**
00042  * This type of node basically is a convenience class for managing and creating offscreen renderings. The children of this node should be of type
00043  * \ref WGEOffscreenRenderPass. This class provides factories to create offscreen-render-pass instances with proper sizes with a coupling to a
00044  * reference camera. This is useful to provide automatic viewport scaling etc. to each render-pass. You do not explicitly need this class to
00045  * create offscreen-renderings at all. You can manually manage multiple WGEOffscreenRenderPass instances.
00046  *
00047  * It is important to understand, that the graph (your scene) must not be a children of this node. This node can be placed somewhere in your
00048  * scene. The OSG collects all the cameras (and offscreen-cameras) and render then independently from their position in the graph (except for
00049  * transformations inherited from others).
00050  *
00051  * \note Please not that you should not modify the whole wiring and offscreen configuration if the this node has been added as it is not
00052  * thread-safe.
00053  */
00054 class WGE_EXPORT WGEOffscreenRenderNode: public WGEGroupNode // NOLINT
00055 {
00056 public:
00057     /**
00058      * Convenience typedef for an osg::ref_ptr
00059      */
00060     typedef osg::ref_ptr< WGEOffscreenRenderNode > RefPtr;
00061 
00062     /**
00063      * Convenience typedef for an osg::ref_ptr; const
00064      */
00065     typedef osg::ref_ptr< const WGEOffscreenRenderNode > ConstRefPtr;
00066 
00067     /**
00068      * Create a new managing instance. It uses the specified camera as reference to all created offscreen-render-pass instances. Especially
00069      * viewport, clear-mask and clear-color get used. The default texture resolution is 2048x2048 which is more than full-HD resolution. So it
00070      * should be enough.
00071      *
00072      * \param reference camera used as reference
00073      * \param width the width of the textures used in this rendering. The real used space is determined by the reference camera.
00074      * \param height the height of the textures used in this rendering. The real used space is determined by the reference camera.
00075      * \param noHud If true, no hud gets displayed showing the created and used textures.
00076      */
00077     WGEOffscreenRenderNode( osg::ref_ptr< osg::Camera > reference, size_t width = 2048, size_t height = 2048, bool noHud = false );
00078 
00079     /**
00080      * Destructor.
00081      */
00082     virtual ~WGEOffscreenRenderNode();
00083 
00084     /**
00085      * Returns the instance of the texture HUD.
00086      *
00087      * \return the HUD
00088      */
00089     osg::ref_ptr< WGETextureHud > getTextureHUD() const;
00090 
00091     /**
00092      * Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG graph to a texture.
00093      *
00094      * \param node the node which represents the subgraph.
00095      * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
00096      *
00097      * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
00098      *
00099      * \return the geometry render pass.
00100      */
00101     virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass( osg::ref_ptr< osg::Node > node, std::string name = "Unnamed" );
00102 
00103     /**
00104      * Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG graph to a texture.
00105      *
00106      * \param node the node which represents the subgraph.
00107      * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
00108      * \param shader the shader to add.
00109      *
00110      * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
00111      *
00112      * \return the geometry render pass.
00113      */
00114     virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGEShader > shader,
00115                                                                           std::string name = "Unnamed" );
00116 
00117     /**
00118      * Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures. All the in- and output textures
00119      * have to be specified manually.
00120      *
00121      * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
00122      *
00123      * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
00124      *
00125      * \return the texture processing pass created.
00126      */
00127     virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass( std::string name = "Unnamed" );
00128 
00129     /**
00130      * Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures. All the in- and output textures
00131      * have to be specified manually.
00132      *
00133      * \note never forget to remove the returned node if not used anymore or use WGEGroup::clean.
00134      *
00135      * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
00136      * \param shader the shader to add.
00137      *
00138      * \return the texture processing pass created.
00139      */
00140     virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass( osg::ref_ptr< WGEShader > shader, std::string name = "Unnamed" );
00141 
00142     /**
00143      * Creates a new render pass which can be seen as put-textures-back-on-screen-pass. It renders a full-screen quad to the on-screen
00144      * frame-buffer. An optional shader can be used for final processing (most commonly clipping, blending, color-mapping and so on).
00145      *
00146      * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
00147      *
00148      * \return the on-screen render pass which draws processed textures back on screen.
00149      */
00150     virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass( std::string name = "Unnamed" );
00151 
00152     /**
00153      * Creates a new render pass which can be seen as put-textures-back-on-screen-pass. It renders a full-screen quad to the on-screen
00154      * frame-buffer. An optional shader can be used for final processing (most commonly clipping, blending, color-mapping and so on).
00155      *
00156      * \param shader the shader to add
00157      * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
00158      *
00159      * \return the on-screen render pass which draws processed textures back on screen.
00160      */
00161     virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass( osg::ref_ptr< WGEShader > shader, std::string name = "Unnamed" );
00162 
00163     /**
00164      * Creates a new offscreen-render-pass coupled with the reference camera. This pass actually does nothing. The method is useful for custom
00165      * variants of WGEOffscreenRenderPass.
00166      *
00167      * \param name the name of the render pass. You should specify it to enable the nice debugging feature of WGETextureHud.
00168      *
00169      * \return new instance of a plain render pass
00170      *
00171      * \tparam T the type of pass to create.
00172      */
00173     template < typename T >
00174     osg::ref_ptr< T >  addRenderPass( std::string name = "Unnamed" );
00175 
00176 protected:
00177 
00178 private:
00179 
00180     /**
00181      * The camera to which is used for setting this camera up.
00182      */
00183     osg::ref_ptr< osg::Camera > m_referenceCamera;
00184 
00185     /**
00186      * The pointer to the hud used to render all used texture buffers. This can be NULL. It gets distributed to all created render-pass
00187      * instances.
00188      */
00189     osg::ref_ptr< WGETextureHud > m_hud;
00190 
00191     /**
00192      * The width of each texture in this offscreen rendering.
00193      */
00194     size_t m_textureWidth;
00195 
00196     /**
00197      * The height of each texture in this offscreen rendering.
00198      */
00199     size_t m_textureHeight;
00200 
00201     /**
00202      * The number of the next pass getting added.
00203      */
00204     size_t m_nextPassNum;
00205 };
00206 
00207 template < typename T >
00208 osg::ref_ptr< T > WGEOffscreenRenderNode::addRenderPass( std::string name )
00209 {
00210     // create a new pass
00211     osg::ref_ptr< T > pass = new T( m_textureWidth, m_textureHeight, m_hud, name, m_nextPassNum );
00212     m_nextPassNum++;
00213 
00214     // this node needs to keep all the pass instances. Only this way, the OSG traverses and renders these nodes in the order specified by
00215     // m_nextPassNum.
00216     insert( pass );   // insert into this group
00217 
00218     // ensure proper propagation of viewport changes
00219     pass->addUpdateCallback( new WGEViewportCallback< T >( m_referenceCamera ) );
00220 
00221     // set clear mask and color according to reference cam
00222     pass->setClearMask( m_referenceCamera->getClearMask() );
00223     pass->setClearColor( m_referenceCamera->getClearColor() );
00224 
00225     return pass;
00226 }
00227 
00228 #endif  // WGEOFFSCREENRENDERNODE_H
00229 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends