OpenWalnut 1.2.5
|
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 #include "../WExportWGE.h" 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 /** 00074 * The property 00075 */ 00076 T m_property; 00077 00078 private: 00079 }; 00080 00081 template< typename T > 00082 WGEPropertyUniformCallback< T >::WGEPropertyUniformCallback( T property ): 00083 osg::Uniform::Callback(), 00084 m_property( property ) 00085 { 00086 // initialize members 00087 } 00088 00089 template< typename T > 00090 WGEPropertyUniformCallback< T >::~WGEPropertyUniformCallback() 00091 { 00092 // cleanup 00093 } 00094 00095 template< typename T > 00096 void WGEPropertyUniformCallback< T >::operator()( osg::Uniform* uniform, osg::NodeVisitor* /*nv*/ ) 00097 { 00098 uniform->set( static_cast< typename WGEPropertyUniformCallback< T >::UniformType >( m_property->get() ) ); 00099 } 00100 00101 #endif // WGEPROPERTYUNIFORMCALLBACK_H 00102 00103