OpenWalnut 1.3.1
WGEOffscreenRenderNode.cpp
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 #include <string>
00026 
00027 #include <osg/Geode>
00028 
00029 #include "../../common/WAssert.h"
00030 
00031 #include "WGEOffscreenRenderNode.h"
00032 
00033 bool isPowerOfTwo( size_t x )
00034 {
00035     return ( (x != 0 ) && ( (x & ( ~x + 1 ) ) == x ) );
00036 }
00037 
00038 bool checkTextureSize( size_t size )
00039 {
00040     return !( ( size > 4096 ) || ( size < 8 ) || !isPowerOfTwo( size ) );
00041 }
00042 
00043 WGEOffscreenRenderNode::WGEOffscreenRenderNode( osg::ref_ptr< osg::Camera > reference, size_t width, size_t height, bool noHud ):
00044     WGEGroupNode(),
00045     m_referenceCamera( reference ),
00046     m_hud(),
00047     m_textureWidth( width ),
00048     m_textureHeight( height ),
00049     m_nextPassNum( 0 )
00050 {
00051     WAssert( checkTextureSize( width ) && checkTextureSize( height ), "Invalid offscreen texture size. Must be power of two and in [8,4096]." );
00052 
00053     // initialize members
00054     m_hud = new WGETextureHud();
00055     if( !noHud )
00056     {
00057         m_hud->addUpdateCallback( new WGEViewportCallback< WGETextureHud >( m_referenceCamera ) );
00058         m_hud->coupleViewportWithTextureViewport();
00059         insert( m_hud );
00060     }
00061 }
00062 
00063 WGEOffscreenRenderNode::~WGEOffscreenRenderNode()
00064 {
00065     // cleanup
00066 }
00067 
00068 osg::ref_ptr< WGEOffscreenRenderPass > WGEOffscreenRenderNode::addGeometryRenderPass( osg::ref_ptr< osg::Node > node, std::string name )
00069 {
00070     // create a plain render pass and add some geometry
00071     osg::ref_ptr< WGEOffscreenRenderPass > pass = addRenderPass< WGEOffscreenRenderPass >( name );
00072     pass->addChild( node );
00073     return pass;
00074 }
00075 
00076 osg::ref_ptr< WGEOffscreenRenderPass > WGEOffscreenRenderNode::addGeometryRenderPass( osg::ref_ptr< osg::Node > node,
00077                                                                                       osg::ref_ptr< WGEShader > shader,
00078                                                                                       std::string name )
00079 {
00080     // create a plain render pass and add some geometry
00081     osg::ref_ptr< WGEOffscreenRenderPass > pass = addRenderPass< WGEOffscreenRenderPass >( name );
00082     pass->addChild( node );
00083     shader->apply( pass );
00084     return pass;
00085 }
00086 
00087 osg::ref_ptr< WGEOffscreenTexturePass > WGEOffscreenRenderNode::addTextureProcessingPass( std::string name )
00088 {
00089     osg::ref_ptr< WGEOffscreenTexturePass > pass = addRenderPass< WGEOffscreenTexturePass >( name );
00090     return pass;
00091 }
00092 
00093 osg::ref_ptr< WGEOffscreenTexturePass > WGEOffscreenRenderNode::addTextureProcessingPass( osg::ref_ptr< WGEShader > shader, std::string name )
00094 {
00095     osg::ref_ptr< WGEOffscreenTexturePass > pass = addRenderPass< WGEOffscreenTexturePass >( name );
00096     shader->apply( pass );
00097     return pass;
00098 }
00099 
00100 osg::ref_ptr< WGEOffscreenFinalPass > WGEOffscreenRenderNode::addFinalOnScreenPass( std::string name )
00101 {
00102     osg::ref_ptr< WGEOffscreenFinalPass > pass = addRenderPass< WGEOffscreenFinalPass >( name );
00103     return pass;
00104 }
00105 
00106 osg::ref_ptr< WGEOffscreenFinalPass > WGEOffscreenRenderNode::addFinalOnScreenPass( osg::ref_ptr< WGEShader > shader, std::string name )
00107 {
00108     osg::ref_ptr< WGEOffscreenFinalPass > pass = addRenderPass< WGEOffscreenFinalPass >( name );
00109     shader->apply( pass );
00110     return pass;
00111 }
00112 
00113 osg::ref_ptr< WGETextureHud > WGEOffscreenRenderNode::getTextureHUD() const
00114 {
00115     return m_hud;
00116 }
00117