OpenWalnut  1.4.0
WGEViewportCallback.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 WGEVIEWPORTCALLBACK_H
00026 #define WGEVIEWPORTCALLBACK_H
00027 
00028 #include <osg/Camera>
00029 #include <osg/Node>
00030 
00031 #include "../WGECamera.h"
00032 
00033 /**
00034  * This callback is useful to update viewport information on several nodes supporting it. The specified type must support an setViewport method.
00035  * This is especially useful to keep offscreen render cameras in sync with the scene cam or to update HUD viewport information. Note that the
00036  * order of execution of callbacks for a node can cause problems as the new viewport might get set after it is needed.
00037  *
00038  * \tparam T the type supporting setViewport
00039  * \tparam Source the type from who the viewport should be acquired by using osg::Viewport* getViewport()
00040  */
00041 template < typename T, typename Source = WGECamera >
00042 class WGEViewportCallback: public osg::NodeCallback
00043 {
00044 public:
00045     /**
00046      * Creates new instance of viewport callback and sets the viewport size to the reference camera size
00047      *
00048      * \param reference set the viewport to the one of the reference camera.
00049      */
00050     explicit WGEViewportCallback( osg::ref_ptr< Source > reference );
00051 
00052     /**
00053      * Creates new instance of viewport callback and sets the viewport size to the specified size
00054      *
00055      * \param width viewport width
00056      * \param height viewport height
00057      */
00058     WGEViewportCallback( size_t width, size_t height );
00059 
00060     /**
00061      * Destructor.
00062      */
00063     virtual ~WGEViewportCallback();
00064 
00065      /**
00066      * This operator gets called by OSG every update cycle. It applies the viewport.
00067      *
00068      * \param node the osg node
00069      * \param nv the node visitor
00070      */
00071     virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
00072 
00073 protected:
00074 private:
00075     /**
00076      * The reference camera to use.
00077      */
00078     osg::ref_ptr< Source > m_reference;
00079 
00080     /**
00081      * Forced viewport width
00082      */
00083     size_t m_width;
00084 
00085     /**
00086      * Forced viewport height
00087      */
00088     size_t m_height;
00089 };
00090 
00091 template < typename T, typename Source >
00092 WGEViewportCallback< T, Source >::WGEViewportCallback( osg::ref_ptr< Source > reference ):
00093     osg::NodeCallback(),
00094     m_reference( reference ),
00095     m_width( 0 ),
00096     m_height( 0 )
00097 {
00098     // initialize members
00099 }
00100 
00101 template < typename T, typename Source >
00102 WGEViewportCallback< T, Source >::WGEViewportCallback( size_t width, size_t height ):
00103     osg::NodeCallback(),
00104     m_reference( NULL ),
00105     m_width( width ),
00106     m_height( height )
00107 {
00108     // initialize members
00109 }
00110 
00111 template < typename T, typename Source >
00112 WGEViewportCallback< T, Source >::~WGEViewportCallback()
00113 {
00114     // cleanup
00115 }
00116 
00117 template < typename T, typename Source >
00118 void WGEViewportCallback< T, Source >::operator()( osg::Node* node, osg::NodeVisitor* nv )
00119 {
00120     osg::ref_ptr< T > t = dynamic_cast< T* >( node );
00121     if( t )
00122     {
00123         if( m_reference )
00124         {
00125             t->setViewport( m_reference->getViewport() );
00126         }
00127         else
00128         {
00129             t->setViewport( new osg::Viewport( 0, 0, m_width, m_height ) );
00130         }
00131     }
00132     traverse( node, nv );
00133 }
00134 
00135 #endif  // WGEVIEWPORTCALLBACK_H
00136