OpenWalnut  1.4.0
WGEPropertyUniformCallback.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 WGEPROPERTYUNIFORMCALLBACK_H
00026 #define WGEPROPERTYUNIFORMCALLBACK_H
00027 
00028 #include <osg/Uniform>
00029 
00030 #include "../../common/WProperties.h"
00031 
00032 #include "../shaders/WGEUniformTypeTraits.h"
00033 
00034 /**
00035  * This class is an OSG Callback which allows uniforms to be controlled by properties. This is useful to simply forward properties to a shader.
00036  * Please note that you still need to add this callback to the desired uniforms. A convenience class is WGEPropertyUniform which uses this
00037  * 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.
00038  *
00039  * \tparam T the type used as control mechanism. The type specified must support access via T->get(). Specialize the class if
00040  *           you do not specify a pointer.
00041  */
00042 template< typename T >
00043 class WGEPropertyUniformCallback: public osg::Uniform::Callback
00044 {
00045 public:
00046     /**
00047      * Constructor. Creates the callback. You still need to add it to the desired uniform.
00048      *
00049      * \param property the property containing the value
00050      */
00051     explicit WGEPropertyUniformCallback( T property );
00052 
00053     /**
00054      * Destructor.
00055      */
00056     virtual ~WGEPropertyUniformCallback();
00057 
00058     /**
00059      * The callback. Called every render traversal for the uniform. It sets the value of the property to the uniform.
00060      *
00061      * \param uniform the uniform for which this callback is.
00062      * \param nv the visitor.
00063      */
00064     virtual void operator() ( osg::Uniform* uniform, osg::NodeVisitor* nv );
00065 
00066     /**
00067      * The type which is used for this uniform.
00068      */
00069     typedef typename wge::UniformType< typename T::element_type::ValueType >::Type UniformType;
00070 
00071 protected:
00072     /**
00073      * The property
00074      */
00075     T m_property;
00076 
00077 private:
00078 };
00079 
00080 template< typename T >
00081 WGEPropertyUniformCallback< T >::WGEPropertyUniformCallback( T property ):
00082     osg::Uniform::Callback(),
00083     m_property( property )
00084 {
00085     // initialize members
00086 }
00087 
00088 template< typename T >
00089 WGEPropertyUniformCallback< T >::~WGEPropertyUniformCallback()
00090 {
00091     // cleanup
00092 }
00093 
00094 template< typename T >
00095 void WGEPropertyUniformCallback< T >::operator()( osg::Uniform* uniform, osg::NodeVisitor* /*nv*/ )
00096 {
00097     uniform->set( wge::toUniformType( m_property->get() ) );
00098 }
00099 
00100 #endif  // WGEPROPERTYUNIFORMCALLBACK_H
00101 
00102