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 WGELINEARTRANSLATIONCALLBACK_H 00026 #define WGELINEARTRANSLATIONCALLBACK_H 00027 00028 #include <osg/Node> 00029 #include <osg/TexMat> 00030 #include <osg/Uniform> 00031 #include <osg/MatrixTransform> 00032 00033 #include "../../common/WProperties.h" 00034 #include "../WExportWGE.h" 00035 00036 /** 00037 * This class is an OSG Callback which allows simple linear translation of a matrix transform node along a specified axis. It is controlled by a 00038 * WPropDouble. This way, one can simply implement movable slices and similar. 00039 * 00040 * \tparam T the type used as control mechanism. Typically, this should be an property whose type is cast-able to double. The type specified must 00041 * support access via T->get(). Specialize the class if you do not specify a pointer. 00042 */ 00043 template< typename T > 00044 class WGELinearTranslationCallback: public osg::NodeCallback 00045 { 00046 public: 00047 /** 00048 * Constructor. Creates the callback. You still need to add it to the desired node. 00049 * 00050 * \param axe the axe to translate along. Should be normalized. If not, it scales the translation. 00051 * \param property the property containing the value 00052 * \param texMatrix optional pointer to a texture matrix which can be modified too to contain the normalized translation. 00053 */ 00054 WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::TexMat > texMatrix ); 00055 00056 /** 00057 * Constructor. Creates the callback. You still need to add it to the desired node. 00058 * 00059 * \param axe the axe to translate along. Should be normalized. If not, it scales the translation. 00060 * \param property the property containing the value 00061 * \param uniform optional pointer to a uniform that will contain the matrix. Useful if no tex-matrix is available anymore. The matrix is the 00062 * matrix that is NOT scaled to be in texture space. 00063 */ 00064 WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::Uniform > uniform ); 00065 00066 /** 00067 * Constructor. Creates the callback. You still need to add it to the desired node. 00068 * 00069 * \param axe the axe to translate along. Should be normalized. If not, it scales the translation. 00070 * \param property the property containing the value 00071 */ 00072 WGELinearTranslationCallback( osg::Vec3 axe, T property ); 00073 00074 /** 00075 * Destructor. 00076 */ 00077 virtual ~WGELinearTranslationCallback(); 00078 00079 /** 00080 * This operator gets called by OSG every update cycle. It moves the underlying MatrixTransform according to the specified axis and value. 00081 * 00082 * \param node the osg node 00083 * \param nv the node visitor 00084 */ 00085 virtual void operator()( osg::Node* node, osg::NodeVisitor* nv ); 00086 00087 protected: 00088 00089 /** 00090 * The axis to transform along. 00091 */ 00092 osg::Vec3 m_axe; 00093 00094 /** 00095 * The position 00096 */ 00097 T m_pos; 00098 00099 /** 00100 * Cache the old position for proper update 00101 */ 00102 double m_oldPos; 00103 00104 /** 00105 * Texture matrix that contains normalized translation. 00106 */ 00107 osg::ref_ptr< osg::TexMat > m_texMat; 00108 00109 /** 00110 * The uniform to set the matrix to. 00111 */ 00112 osg::ref_ptr< osg::Uniform > m_uniform; 00113 private: 00114 }; 00115 00116 template< typename T > 00117 WGELinearTranslationCallback< T >::WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::TexMat > texMatrix ): 00118 osg::NodeCallback(), 00119 m_axe( axe ), 00120 m_pos( property ), 00121 m_oldPos( -1.0 ), 00122 m_texMat( texMatrix ) 00123 { 00124 // initialize members 00125 } 00126 00127 template< typename T > 00128 WGELinearTranslationCallback< T >::WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::Uniform > uniform ): 00129 osg::NodeCallback(), 00130 m_axe( axe ), 00131 m_pos( property ), 00132 m_oldPos( -1.0 ), 00133 m_uniform( uniform ) 00134 { 00135 // initialize members 00136 } 00137 00138 template< typename T > 00139 WGELinearTranslationCallback< T >::WGELinearTranslationCallback( osg::Vec3 axe, T property ): 00140 osg::NodeCallback(), 00141 m_axe( axe ), 00142 m_pos( property ), 00143 m_oldPos( -1.0 ) 00144 { 00145 // initialize members 00146 } 00147 00148 template< typename T > 00149 WGELinearTranslationCallback< T >::~WGELinearTranslationCallback() 00150 { 00151 // cleanup 00152 } 00153 00154 template< typename T > 00155 void WGELinearTranslationCallback< T >::operator()( osg::Node* node, osg::NodeVisitor* nv ) 00156 { 00157 // this node is a MatrixTransform 00158 float newPos = m_pos->get(); 00159 if( newPos != m_oldPos ) 00160 { 00161 m_oldPos = newPos; 00162 osg::MatrixTransform* m = static_cast< osg::MatrixTransform* >( node ); 00163 if( m ) 00164 { 00165 float max = m_pos->getMax()->getMax(); 00166 float min = m_pos->getMin()->getMin(); 00167 float size = max - min; 00168 float axeLen = m_axe.length(); 00169 00170 osg::Vec3 translation = m_axe * static_cast< float >( m_oldPos - min ); 00171 00172 // set both matrices 00173 if( m_texMat ) 00174 { 00175 m_texMat->setMatrix( osg::Matrix::translate( translation / size / axeLen ) ); 00176 } 00177 if( m_uniform ) 00178 { 00179 m_uniform->set( osg::Matrix::translate( translation ) ); 00180 } 00181 00182 m->setMatrix( osg::Matrix::translate( translation ) ); 00183 } 00184 } 00185 00186 traverse( node, nv ); 00187 } 00188 00189 #endif // WGELINEARTRANSLATIONCALLBACK_H 00190