OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEPropertyUniformCallback.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 WGEPROPERTYUNIFORMCALLBACK_H
26 #define WGEPROPERTYUNIFORMCALLBACK_H
27 
28 #include <osg/Uniform>
29 
30 #include "../../common/WProperties.h"
31 #include "../WExportWGE.h"
32 #include "../shaders/WGEUniformTypeTraits.h"
33 
34 /**
35  * This class is an OSG Callback which allows uniforms to be controlled by properties. This is useful to simply forward properties to a shader.
36  * Please note that you still need to add this callback to the desired uniforms. A convenience class is WGEPropertyUniform which uses this
37  * callback. On each traversal of the OSG, the callback sets the value of the property to the uniform but does NOT reset the change flag.
38  *
39  * \tparam T the type used as control mechanism. The type specified must support access via T->get(). Specialize the class if
40  * you do not specify a pointer.
41  */
42 template< typename T >
43 class WGEPropertyUniformCallback: public osg::Uniform::Callback
44 {
45 public:
46  /**
47  * Constructor. Creates the callback. You still need to add it to the desired uniform.
48  *
49  * \param property the property containing the value
50  */
51  explicit WGEPropertyUniformCallback( T property );
52 
53  /**
54  * Destructor.
55  */
57 
58  /**
59  * The callback. Called every render traversal for the uniform. It sets the value of the property to the uniform.
60  *
61  * \param uniform the uniform for which this callback is.
62  * \param nv the visitor.
63  */
64  virtual void operator() ( osg::Uniform* uniform, osg::NodeVisitor* nv );
65 
66  /**
67  * The type which is used for this uniform.
68  */
70 
71 protected:
72 
73  /**
74  * The property
75  */
77 
78 private:
79 };
80 
81 template< typename T >
83  osg::Uniform::Callback(),
84  m_property( property )
85 {
86  // initialize members
87 }
88 
89 template< typename T >
91 {
92  // cleanup
93 }
94 
95 template< typename T >
96 void WGEPropertyUniformCallback< T >::operator()( osg::Uniform* uniform, osg::NodeVisitor* /*nv*/ )
97 {
98  uniform->set( static_cast< typename WGEPropertyUniformCallback< T >::UniformType >( m_property->get() ) );
99 }
100 
101 #endif // WGEPROPERTYUNIFORMCALLBACK_H
102 
103