OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEViewportCallback.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGEVIEWPORTCALLBACK_H
26 #define WGEVIEWPORTCALLBACK_H
27 
28 #include <osg/Camera>
29 #include <osg/Node>
30 
31 #include "../WExportWGE.h"
32 
33 /**
34  * This callback is useful to update viewport information on several nodes supporting it. The specified type must support an setViewport method.
35  * This is especially useful to keep offscreen render cameras in sync with the scene cam or to update HUD viewport information. Note that the
36  * order of execution of callbacks for a node can cause problems as the new viewport might get set after it is needed.
37  *
38  * \tparam T the type supporting setViewport
39  * \tparam Source the type from who the viewport should be acquired by using osg::Viewport* getViewport()
40  */
41 template < typename T, typename Source = osg::Camera >
42 class WGEViewportCallback: public osg::NodeCallback
43 {
44 public:
45  /**
46  * Creates new instance of viewport callback.
47  *
48  * \param reference set the viewport to the one of the reference camera.
49  */
50  explicit WGEViewportCallback( osg::ref_ptr< Source > reference );
51 
52  /**
53  * Destructor.
54  */
55  virtual ~WGEViewportCallback();
56 
57  /**
58  * This operator gets called by OSG every update cycle. It applies the viewport.
59  *
60  * \param node the osg node
61  * \param nv the node visitor
62  */
63  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
64 
65 protected:
66 private:
67  /**
68  * The reference camera to use.
69  */
70  osg::ref_ptr< Source > m_reference;
71 };
72 
73 template < typename T, typename Source >
74 WGEViewportCallback< T, Source >::WGEViewportCallback( osg::ref_ptr< Source > reference ):
75  osg::NodeCallback(),
76  m_reference( reference )
77 {
78  // initialize members
79 }
80 
81 template < typename T, typename Source >
83 {
84  // cleanup
85 }
86 
87 template < typename T, typename Source >
88 void WGEViewportCallback< T, Source >::operator()( osg::Node* node, osg::NodeVisitor* nv )
89 {
90  osg::ref_ptr< T > t = dynamic_cast< T* >( node );
91  if( t )
92  {
93  t->setViewport( m_reference->getViewport() );
94  }
95  traverse( node, nv );
96 }
97 
98 #endif // WGEVIEWPORTCALLBACK_H
99