OpenWalnut  1.5.0dev
WGEUtils.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 WGEUTILS_H
26 #define WGEUTILS_H
27 
28 #include <string>
29 #include <vector>
30 
31 #include <osg/Array>
32 #include <osg/Vec3>
33 #include <osg/Vec4>
34 #include <osg/Camera>
35 #include <osg/Uniform>
36 
37 #include "../common/WColor.h"
38 #include "../common/WBoundingBox.h"
39 #include "../common/WAssert.h"
40 #include "../common/WPropertyVariable.h"
41 
42 #include "shaders/WGEPropertyUniform.h"
43 #include "WGECamera.h"
44 
45 namespace wge
46 {
47  /**
48  * Enable transparency for the given node. This enabled blending and sets the node to the transparency bin.
49  *
50  * \param node the node
51  */
52  void enableTransparency( osg::ref_ptr< osg::Node > node );
53 
54  /**
55  * Transforms a direction given via two points into a RGB color.
56  *
57  * \param pos1 First point
58  * \param pos2 Second point
59  *
60  * \return converts a vector to a color
61  */
62  WColor getRGBAColorFromDirection( const WPosition &pos1, const WPosition &pos2 );
63 
64  /**
65  * Converts a whole vector of WPositions into an osg::Vec3Array.
66  *
67  * \param posArray The given positions vector
68  *
69  * \return Refernce to the same vector but as osg::Vec3Array.
70  */
71  osg::ref_ptr< osg::Vec3Array > osgVec3Array( const std::vector< WPosition >& posArray );
72 
73  /**
74  * Converts screen coordinates into Camera coordinates.
75  *
76  * \param screen the screen coordinates
77  * \param camera The matrices of this camera will used for unprojecting.
78  *
79  * \return un-projects a screen coordinate back to world space
80  */
81  osg::Vec3 unprojectFromScreen( const osg::Vec3 screen, osg::ref_ptr< WGECamera > camera );
82 
83  /**
84  * Converts screen coordinates into Camera coordinates.
85  * \note this method can be useful to work with vectors (w component 0)
86  *
87  * \param screen the screen coordinates
88  * \param camera The matrices of this camera will used for unprojecting.
89  *
90  * \return un-projects a screen coordinate back to world space
91  */
92  osg::Vec4 unprojectFromScreen( const osg::Vec4 screen, osg::ref_ptr< WGECamera > camera );
93 
94  /**
95  * creates the same color as the atlas colormap shader from the index
96  *
97  * \param index unsigned char that indexes the color
98  * \return the color
99  */
100  WColor createColorFromIndex( int index );
101 
102  /**
103  * creates a rgb WColor from a HSV value
104  * \param h hue
105  * \param s saturation
106  * \param v value
107  * \return the color
108  */
109  WColor createColorFromHSV( int h, float s = 1.0, float v = 1.0 );
110 
111  /**
112  * creates the nth color of a partition of the hsv color circle
113  *
114  * \param n number of the color
115  * \return the color
116  */
117  WColor getNthHSVColor( int n );
118 
119  /**
120  * Creates a osg::Uniform with given type and name and applies it to the given node. This is a shortcut for using WGEPropertyUniform in this way:
121  * @code
122  * node->getOrCreateStateSet()->addUniform( new WGEPropertyUniform< T >( name, prop ) );
123  * @endcode*
124  *
125  * \tparam T This is the data used for the uniform. It may be a PropertyType, an integral or an osg::Uniform.
126  *
127  * \param node Node where the uniform should be bound to.
128  * \param prop The type of the uniform.
129  * \param name The name of the uniform.
130  */
131  template< typename T >
132  void bindAsUniform( osg::Node* node, T prop, std::string name );
133 
134  /**
135  * Generate a proxy cube, which ensures OSG does proper near-far plane calculation and culling. This is especially useful if you create some
136  * geometry and modify it on the GPU by shaders. In these cases, OSG will not properly cull and near-far clip. This cull proxy is basically a
137  * cube, which gets shrinked to zero size on the GPU. This ensures you cannot see it, but it makes OSG see you proper bounding volume.
138  *
139  * \param bbox the bounding box to cover
140  *
141  * \return the proxy. Add it to your scene root.
142  */
143  osg::ref_ptr< osg::Node > generateCullProxy( const WBoundingBox& bbox );
144 
145  /**
146  * Generate a proxy cube, which ensures OSG does proper near-far plane calculation and culling. This is especially useful if you create some
147  * geometry and modify it on the GPU by shaders. In these cases, OSG will not properly cull and near-far clip. This cull proxy is basically a
148  * cube, which gets shrinked to zero size on the GPU. This ensures you cannot see it, but it makes OSG see you proper bounding volume.
149  *
150  * \note dynamic cull proxys use the getBound method of the specified node before culling. So if you implement objects that change their size
151  * dynamically, add a osg::Node::ComputeBoundingSphereCallback to them
152  *
153  * \note This method uses update callbacks. Remember to add your own callbacks via addUpdateCallback, instead of setUpdateCallback.
154  *
155  * \param node the node
156  *
157  * \return the proxy. Add it to your scene root.
158  */
159  osg::ref_ptr< osg::Node > generateDynamicCullProxy( osg::ref_ptr< osg::Node > node );
160 }
161 
162 inline WColor wge::getRGBAColorFromDirection( const WPosition &pos1, const WPosition &pos2 )
163 {
164  WPosition direction( normalize( pos2 - pos1 ) );
165  return WColor( std::abs( direction[0] ), std::abs( direction[1] ), std::abs( direction[2] ), 1.0f );
166 }
167 
168 namespace wge
169 {
170  template< typename T >
171  inline void bindAsUniform( osg::Node* node, T prop, std::string name )
172  {
173  osg::ref_ptr< osg::Uniform > uniform = new osg::Uniform( name.c_str(), prop );
174  osg::StateSet *states = node->getOrCreateStateSet();
175  states->addUniform( uniform );
176  }
177 
178  /**
179  * Template specialization for double values. This is a shortcut for osg::Uniform generation and osg::StateSet-binding.
180  *
181  * \param node Node where the uniform should be bound to.
182  * \param prop The type of the uniform.
183  * \param name The name of the uniform.
184  */
185  template<>
186  inline void bindAsUniform< double >( osg::Node* node, double prop, std::string name )
187  {
188  osg::ref_ptr< osg::Uniform > uniform( new osg::Uniform( name.c_str(), static_cast< float >( prop ) ) );
189  osg::StateSet *states = node->getOrCreateStateSet();
190  states->addUniform( uniform );
191  }
192 
193  /**
194  * Template specialization for size_t values. This is a shortcut for osg::Uniform generation and osg::StateSet-binding.
195  *
196  * \param node Node where the uniform should be bound to.
197  * \param prop The type of the uniform.
198  * \param name The name of the uniform.
199  */
200  template<>
201  inline void bindAsUniform< size_t >( osg::Node* node, size_t prop, std::string name )
202  {
203  osg::ref_ptr< osg::Uniform > uniform( new osg::Uniform( name.c_str(), static_cast< int >( prop ) ) );
204  osg::StateSet *states = node->getOrCreateStateSet();
205  states->addUniform( uniform );
206  }
207 
208  /**
209  * Template specialization for WPropDouble values. This is a shortcut for osg::Uniform generation and osg::StateSet-binding.
210  *
211  * \param node Node where the uniform should be bound to.
212  * \param prop The type of the uniform.
213  * \param name The name of the uniform.
214  */
215  template<>
216  inline void bindAsUniform< WPropDouble >( osg::Node* node, WPropDouble prop, std::string name )
217  {
218  osg::ref_ptr< osg::Uniform > uniform( new WGEPropertyUniform< WPropDouble >( name, prop ) );
219  osg::StateSet *states = node->getOrCreateStateSet();
220  states->addUniform( uniform );
221  }
222 
223  /**
224  * Template specialization for WPropColor values. This is a shortcut for osg::Uniform generation and osg::StateSet-binding.
225  *
226  * \param node Node where the uniform should be bound to.
227  * \param prop The type of the uniform.
228  * \param name The name of the uniform.
229  */
230  template<>
231  inline void bindAsUniform< WPropColor >( osg::Node* node, WPropColor prop, std::string name )
232  {
233  osg::ref_ptr< osg::Uniform > uniform( new WGEPropertyUniform< WPropColor >( name, prop ) );
234  osg::StateSet *states = node->getOrCreateStateSet();
235  states->addUniform( uniform );
236  }
237 
238  /**
239  * Template specialization for WPropBool values. This is a shortcut for osg::Uniform generation and osg::StateSet-binding.
240  *
241  * \param node Node where the uniform should be bound to.
242  * \param prop The type of the uniform.
243  * \param name The name of the uniform.
244  */
245  template<>
246  inline void bindAsUniform< WPropBool >( osg::Node* node, WPropBool prop, std::string name )
247  {
248  osg::ref_ptr< osg::Uniform > uniform( new WGEPropertyUniform< WPropBool >( name, prop ) );
249  osg::StateSet *states = node->getOrCreateStateSet();
250  states->addUniform( uniform );
251  }
252 
253  /**
254  * Template specialization for WPropInt values. This is a shortcut for osg::Uniform generation and osg::StateSet-binding.
255  *
256  * \param node Node where the uniform should be bound to.
257  * \param prop The type of the uniform.
258  * \param name The name of the uniform.
259  */
260  template<>
261  inline void bindAsUniform< WPropInt >( osg::Node* node, WPropInt prop, std::string name )
262  {
263  osg::ref_ptr< osg::Uniform > uniform( new WGEPropertyUniform< WPropInt >( name, prop ) );
264  osg::StateSet *states = node->getOrCreateStateSet();
265  states->addUniform( uniform );
266  }
267 
268  /**
269  * Template specialization for osg::Uniform values.
270  *
271  * \param node Node where the uniform should be bound to.
272  * \param uniform The type of the uniform.
273  */
274  template<>
275  inline void bindAsUniform< osg::ref_ptr< osg::Uniform > >( osg::Node* node, osg::ref_ptr< osg::Uniform > uniform, std::string /* name */ )
276  {
277  osg::StateSet *states = node->getOrCreateStateSet();
278  states->addUniform( uniform );
279  }
280 }
281 #endif // WGEUTILS_H
282 
Class implementing a uniform which can be controlled by a property instance.
This only is a 3d double vector.
Extend the wge utils namespace with additional methods relating WDataTexture3D.
void bindAsUniform(osg::Node *node, T prop, std::string name)
Creates a osg::Uniform with given type and name and applies it to the given node.
Definition: WGEUtils.h:171
WColor getNthHSVColor(int n)
creates the nth color of a partition of the hsv color circle
Definition: WGEUtils.cpp:168
void enableTransparency(osg::ref_ptr< osg::Node > node)
Enable transparency for the given node.
Definition: WGEUtils.cpp:215
WColor createColorFromHSV(int h, float s=1.0, float v=1.0)
creates a rgb WColor from a HSV value
Definition: WGEUtils.cpp:136
osg::ref_ptr< osg::Node > generateCullProxy(const WBoundingBox &bbox)
Generate a proxy cube, which ensures OSG does proper near-far plane calculation and culling.
Definition: WGEUtils.cpp:236
WColor getRGBAColorFromDirection(const WPosition &pos1, const WPosition &pos2)
Transforms a direction given via two points into a RGB color.
Definition: WGEUtils.h:162
void bindAsUniform< WPropDouble >(osg::Node *node, WPropDouble prop, std::string name)
Template specialization for WPropDouble values.
Definition: WGEUtils.h:216
void bindAsUniform< WPropBool >(osg::Node *node, WPropBool prop, std::string name)
Template specialization for WPropBool values.
Definition: WGEUtils.h:246
osg::Vec3 unprojectFromScreen(const osg::Vec3 screen, osg::ref_ptr< WGECamera > camera)
Converts screen coordinates into Camera coordinates.
Definition: WGEUtils.cpp:52
void bindAsUniform< WPropInt >(osg::Node *node, WPropInt prop, std::string name)
Template specialization for WPropInt values.
Definition: WGEUtils.h:261
WColor createColorFromIndex(int index)
creates the same color as the atlas colormap shader from the index
Definition: WGEUtils.cpp:62
void bindAsUniform< double >(osg::Node *node, double prop, std::string name)
Template specialization for double values.
Definition: WGEUtils.h:186
osg::ref_ptr< osg::Node > generateDynamicCullProxy(osg::ref_ptr< osg::Node > node)
Generate a proxy cube, which ensures OSG does proper near-far plane calculation and culling.
Definition: WGEUtils.cpp:295
void bindAsUniform< size_t >(osg::Node *node, size_t prop, std::string name)
Template specialization for size_t values.
Definition: WGEUtils.h:201
void bindAsUniform< WPropColor >(osg::Node *node, WPropColor prop, std::string name)
Template specialization for WPropColor values.
Definition: WGEUtils.h:231
osg::ref_ptr< osg::Vec3Array > osgVec3Array(const std::vector< WPosition > &posArray)
Converts a whole vector of WPositions into an osg::Vec3Array.
Definition: WGEUtils.cpp:40