00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00038
00039
00040
00041
00042
00043 template< typename T >
00044 class WGELinearTranslationCallback: public osg::NodeCallback
00045 {
00046 public:
00047
00048
00049
00050
00051
00052
00053
00054 WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::TexMat > texMatrix );
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::Uniform > uniform );
00065
00066
00067
00068
00069
00070
00071
00072 WGELinearTranslationCallback( osg::Vec3 axe, T property );
00073
00074
00075
00076
00077 virtual ~WGELinearTranslationCallback();
00078
00079
00080
00081
00082
00083
00084
00085 virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
00086
00087 protected:
00088
00089
00090
00091
00092 osg::Vec3 m_axe;
00093
00094
00095
00096
00097 T m_pos;
00098
00099
00100
00101
00102 double m_oldPos;
00103
00104
00105
00106
00107 osg::ref_ptr< osg::TexMat > m_texMat;
00108
00109
00110
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
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
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
00146 }
00147
00148 template< typename T >
00149 WGELinearTranslationCallback< T >::~WGELinearTranslationCallback()
00150 {
00151
00152 }
00153
00154 template< typename T >
00155 void WGELinearTranslationCallback< T >::operator()( osg::Node* node, osg::NodeVisitor* nv )
00156 {
00157
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
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