OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGELinearTranslationCallback.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 WGELINEARTRANSLATIONCALLBACK_H
26 #define WGELINEARTRANSLATIONCALLBACK_H
27 
28 #include <osg/Node>
29 #include <osg/TexMat>
30 #include <osg/Uniform>
31 #include <osg/MatrixTransform>
32 
33 #include "../../common/WProperties.h"
34 #include "../WExportWGE.h"
35 
36 /**
37  * 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
38  * WPropDouble. This way, one can simply implement movable slices and similar.
39  *
40  * \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
41  * support access via T->get(). Specialize the class if you do not specify a pointer.
42  */
43 template< typename T >
44 class WGELinearTranslationCallback: public osg::NodeCallback
45 {
46 public:
47  /**
48  * Constructor. Creates the callback. You still need to add it to the desired node.
49  *
50  * \param axe the axe to translate along. Should be normalized. If not, it scales the translation.
51  * \param property the property containing the value
52  * \param texMatrix optional pointer to a texture matrix which can be modified too to contain the normalized translation.
53  */
54  WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::TexMat > texMatrix );
55 
56  /**
57  * Constructor. Creates the callback. You still need to add it to the desired node.
58  *
59  * \param axe the axe to translate along. Should be normalized. If not, it scales the translation.
60  * \param property the property containing the value
61  * \param uniform optional pointer to a uniform that will contain the matrix. Useful if no tex-matrix is available anymore. The matrix is the
62  * matrix that is NOT scaled to be in texture space.
63  */
64  WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::Uniform > uniform );
65 
66  /**
67  * Constructor. Creates the callback. You still need to add it to the desired node.
68  *
69  * \param axe the axe to translate along. Should be normalized. If not, it scales the translation.
70  * \param property the property containing the value
71  */
72  WGELinearTranslationCallback( osg::Vec3 axe, T property );
73 
74  /**
75  * Destructor.
76  */
78 
79  /**
80  * This operator gets called by OSG every update cycle. It moves the underlying MatrixTransform according to the specified axis and value.
81  *
82  * \param node the osg node
83  * \param nv the node visitor
84  */
85  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
86 
87 protected:
88 
89  /**
90  * The axis to transform along.
91  */
92  osg::Vec3 m_axe;
93 
94  /**
95  * The position
96  */
97  T m_pos;
98 
99  /**
100  * Cache the old position for proper update
101  */
102  double m_oldPos;
103 
104  /**
105  * Texture matrix that contains normalized translation.
106  */
107  osg::ref_ptr< osg::TexMat > m_texMat;
108 
109  /**
110  * The uniform to set the matrix to.
111  */
112  osg::ref_ptr< osg::Uniform > m_uniform;
113 private:
114 };
115 
116 template< typename T >
117 WGELinearTranslationCallback< T >::WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::TexMat > texMatrix ):
118  osg::NodeCallback(),
119  m_axe( axe ),
120  m_pos( property ),
121  m_oldPos( -1.0 ),
122  m_texMat( texMatrix )
123 {
124  // initialize members
125 }
126 
127 template< typename T >
128 WGELinearTranslationCallback< T >::WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::Uniform > uniform ):
129  osg::NodeCallback(),
130  m_axe( axe ),
131  m_pos( property ),
132  m_oldPos( -1.0 ),
133  m_uniform( uniform )
134 {
135  // initialize members
136 }
137 
138 template< typename T >
140  osg::NodeCallback(),
141  m_axe( axe ),
142  m_pos( property ),
143  m_oldPos( -1.0 )
144 {
145  // initialize members
146 }
147 
148 template< typename T >
150 {
151  // cleanup
152 }
153 
154 template< typename T >
155 void WGELinearTranslationCallback< T >::operator()( osg::Node* node, osg::NodeVisitor* nv )
156 {
157  // this node is a MatrixTransform
158  float newPos = m_pos->get();
159  if( newPos != m_oldPos )
160  {
161  m_oldPos = newPos;
162  osg::MatrixTransform* m = static_cast< osg::MatrixTransform* >( node );
163  if( m )
164  {
165  float max = m_pos->getMax()->getMax();
166  float min = m_pos->getMin()->getMin();
167  float size = max - min;
168  float axeLen = m_axe.length();
169 
170  osg::Vec3 translation = m_axe * static_cast< float >( m_oldPos - min );
171 
172  // set both matrices
173  if( m_texMat )
174  {
175  m_texMat->setMatrix( osg::Matrix::translate( translation / size / axeLen ) );
176  }
177  if( m_uniform )
178  {
179  m_uniform->set( osg::Matrix::translate( translation ) );
180  }
181 
182  m->setMatrix( osg::Matrix::translate( translation ) );
183  }
184  }
185 
186  traverse( node, nv );
187 }
188 
189 #endif // WGELINEARTRANSLATIONCALLBACK_H
190