OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 "../WExportWGE.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 = osg::Camera >
00042 class WGEViewportCallback: public osg::NodeCallback
00043 {
00044 public:
00045     /**
00046      * Creates new instance of viewport callback.
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      * Destructor.
00054      */
00055     virtual ~WGEViewportCallback();
00056 
00057      /**
00058      * This operator gets called by OSG every update cycle. It applies the viewport.
00059      *
00060      * \param node the osg node
00061      * \param nv the node visitor
00062      */
00063     virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
00064 
00065 protected:
00066 private:
00067     /**
00068      * The reference camera to use.
00069      */
00070     osg::ref_ptr< Source > m_reference;
00071 };
00072 
00073 template < typename T, typename Source >
00074 WGEViewportCallback< T, Source >::WGEViewportCallback( osg::ref_ptr< Source > reference ):
00075     osg::NodeCallback(),
00076     m_reference( reference )
00077 {
00078     // initialize members
00079 }
00080 
00081 template < typename T, typename Source >
00082 WGEViewportCallback< T, Source >::~WGEViewportCallback()
00083 {
00084     // cleanup
00085 }
00086 
00087 template < typename T, typename Source >
00088 void WGEViewportCallback< T, Source >::operator()( osg::Node* node, osg::NodeVisitor* nv )
00089 {
00090     osg::ref_ptr< T > t = dynamic_cast< T* >( node );
00091     if( t )
00092     {
00093         t->setViewport( m_reference->getViewport() );
00094     }
00095     traverse( node, nv );
00096 }
00097 
00098 #endif  // WGEVIEWPORTCALLBACK_H
00099 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends