OpenWalnut  1.5.0dev
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 "../WGECamera.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 = WGECamera >
42 class WGEViewportCallback: public osg::NodeCallback
43 {
44 public:
45  /**
46  * Creates new instance of viewport callback and sets the viewport size to the reference camera size
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  * Creates new instance of viewport callback and sets the viewport size to the specified size
54  *
55  * \param width viewport width
56  * \param height viewport height
57  */
58  WGEViewportCallback( size_t width, size_t height );
59 
60  /**
61  * Destructor.
62  */
63  virtual ~WGEViewportCallback();
64 
65  /**
66  * This operator gets called by OSG every update cycle. It applies the viewport.
67  *
68  * \param node the osg node
69  * \param nv the node visitor
70  */
71  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
72 
73 protected:
74 private:
75  /**
76  * The reference camera to use.
77  */
78  osg::ref_ptr< Source > m_reference;
79 
80  /**
81  * Forced viewport width
82  */
83  size_t m_width;
84 
85  /**
86  * Forced viewport height
87  */
88  size_t m_height;
89 };
90 
91 template < typename T, typename Source >
92 WGEViewportCallback< T, Source >::WGEViewportCallback( osg::ref_ptr< Source > reference ):
93  osg::NodeCallback(),
94  m_reference( reference ),
95  m_width( 0 ),
96  m_height( 0 )
97 {
98  // initialize members
99 }
100 
101 template < typename T, typename Source >
103  osg::NodeCallback(),
104  m_reference( NULL ),
105  m_width( width ),
106  m_height( height )
107 {
108  // initialize members
109 }
110 
111 template < typename T, typename Source >
113 {
114  // cleanup
115 }
116 
117 template < typename T, typename Source >
118 void WGEViewportCallback< T, Source >::operator()( osg::Node* node, osg::NodeVisitor* nv )
119 {
120  osg::ref_ptr< T > t = dynamic_cast< T* >( node );
121  if( t )
122  {
123  if( m_reference )
124  {
125  t->setViewport( m_reference->getViewport() );
126  }
127  else
128  {
129  t->setViewport( new osg::Viewport( 0, 0, m_width, m_height ) );
130  }
131  }
132  traverse( node, nv );
133 }
134 
135 #endif // WGEVIEWPORTCALLBACK_H
136 
This callback is useful to update viewport information on several nodes supporting it.
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
This operator gets called by OSG every update cycle.
osg::ref_ptr< Source > m_reference
The reference camera to use.
size_t m_width
Forced viewport width.
virtual ~WGEViewportCallback()
Destructor.
size_t m_height
Forced viewport height.
WGEViewportCallback(osg::ref_ptr< Source > reference)
Creates new instance of viewport callback and sets the viewport size to the reference camera size.