OpenWalnut  1.4.0
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.
121  *
122  * \tparam T This is the data used for the uniform. It may be a PropertyType, an integral or an osg::Uniform.
123  *
124  * \param node Node where the uniform should be bound to.
125  * \param prop The type of the uniform.
126  * \param name The name of the uniform.
127  */
128  template< typename T >
129  void bindAsUniform( osg::Node* node, T prop, std::string name );
130 
131  /**
132  * Generate a proxy cube, which ensures OSG does proper near-far plane calculation and culling. This is especially useful if you create some
133  * 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
134  * 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.
135  *
136  * \param bbox the bounding box to cover
137  *
138  * \return the proxy. Add it to your scene root.
139  */
140  osg::ref_ptr< osg::Node > generateCullProxy( const WBoundingBox& bbox );
141 
142  /**
143  * Generate a proxy cube, which ensures OSG does proper near-far plane calculation and culling. This is especially useful if you create some
144  * 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
145  * 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.
146  *
147  * \note dynamic cull proxys use the getBound method of the specified node before culling. So if you implement objects that change their size
148  * dynamically, add a osg::Node::ComputeBoundingSphereCallback to them
149  *
150  * \note This method uses update callbacks. Remember to add your own callbacks via addUpdateCallback, instead of setUpdateCallback.
151  *
152  * \param node the node
153  *
154  * \return the proxy. Add it to your scene root.
155  */
156  osg::ref_ptr< osg::Node > generateDynamicCullProxy( osg::ref_ptr< osg::Node > node );
157 }
158 
159 inline WColor wge::getRGBAColorFromDirection( const WPosition &pos1, const WPosition &pos2 )
160 {
161  WPosition direction( normalize( pos2 - pos1 ) );
162  return WColor( std::abs( direction[0] ), std::abs( direction[1] ), std::abs( direction[2] ), 1.0f );
163 }
164 
165 namespace wge
166 {
167  template< typename T >
168  inline void bindAsUniform( osg::Node* node, T prop, std::string name )
169  {
170  osg::ref_ptr< osg::Uniform > uniform = new osg::Uniform( name.c_str(), prop );
171  osg::StateSet *states = node->getOrCreateStateSet();
172  states->addUniform( uniform );
173  }
174 
175  /**
176  * Template specialization for double values.
177  *
178  * \param node Node where the uniform should be bound to.
179  * \param prop The type of the uniform.
180  * \param name The name of the uniform.
181  */
182  template<>
183  inline void bindAsUniform< double >( osg::Node* node, double prop, std::string name )
184  {
185  osg::ref_ptr< osg::Uniform > uniform( new osg::Uniform( name.c_str(), static_cast< float >( prop ) ) );
186  osg::StateSet *states = node->getOrCreateStateSet();
187  states->addUniform( uniform );
188  }
189 
190  /**
191  * Template specialization for size_t values.
192  *
193  * \param node Node where the uniform should be bound to.
194  * \param prop The type of the uniform.
195  * \param name The name of the uniform.
196  */
197  template<>
198  inline void bindAsUniform< size_t >( osg::Node* node, size_t prop, std::string name )
199  {
200  osg::ref_ptr< osg::Uniform > uniform( new osg::Uniform( name.c_str(), static_cast< int >( prop ) ) );
201  osg::StateSet *states = node->getOrCreateStateSet();
202  states->addUniform( uniform );
203  }
204 
205  /**
206  * Template specialization for WPropDouble values.
207  *
208  * \param node Node where the uniform should be bound to.
209  * \param prop The type of the uniform.
210  * \param name The name of the uniform.
211  */
212  template<>
213  inline void bindAsUniform< WPropDouble >( osg::Node* node, WPropDouble prop, std::string name )
214  {
215  osg::ref_ptr< osg::Uniform > uniform( new WGEPropertyUniform< WPropDouble >( name, prop ) );
216  osg::StateSet *states = node->getOrCreateStateSet();
217  states->addUniform( uniform );
218  }
219 
220  /**
221  * Template specialization for WPropColor values.
222  *
223  * \param node Node where the uniform should be bound to.
224  * \param prop The type of the uniform.
225  * \param name The name of the uniform.
226  */
227  template<>
228  inline void bindAsUniform< WPropColor >( osg::Node* node, WPropColor prop, std::string name )
229  {
230  osg::ref_ptr< osg::Uniform > uniform( new WGEPropertyUniform< WPropColor >( name, prop ) );
231  osg::StateSet *states = node->getOrCreateStateSet();
232  states->addUniform( uniform );
233  }
234 
235  /**
236  * Template specialization for osg::Uniform values.
237  *
238  * \param node Node where the uniform should be bound to.
239  * \param uniform The type of the uniform.
240  */
241  template<>
242  inline void bindAsUniform< osg::ref_ptr< osg::Uniform > >( osg::Node* node, osg::ref_ptr< osg::Uniform > uniform, std::string /* name */ )
243  {
244  osg::StateSet *states = node->getOrCreateStateSet();
245  states->addUniform( uniform );
246  }
247 }
248 #endif // WGEUTILS_H
249 
WColor getRGBAColorFromDirection(const WPosition &pos1, const WPosition &pos2)
Transforms a direction given via two points into a RGB color.
Definition: WGEUtils.h:159
WColor getNthHSVColor(int n)
creates the nth color of a partition of the hsv color circle
Definition: WGEUtils.cpp:168
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:168
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
Class implementing a uniform which can be controlled by a property instance.
WColor createColorFromHSV(int h, float s=1.0, float v=1.0)
creates a rgb WColor from a HSV value
Definition: WGEUtils.cpp:136
void enableTransparency(osg::ref_ptr< osg::Node > node)
Enable transparency for the given node.
Definition: WGEUtils.cpp:215
void bindAsUniform< size_t >(osg::Node *node, size_t prop, std::string name)
Template specialization for size_t values.
Definition: WGEUtils.h:198
This only is a 3d double vector.
Extend the wge utils namespace with additional methods relating WDataTexture3D.
void bindAsUniform< WPropColor >(osg::Node *node, WPropColor prop, std::string name)
Template specialization for WPropColor values.
Definition: WGEUtils.h:228
WColor createColorFromIndex(int index)
creates the same color as the atlas colormap shader from the index
Definition: WGEUtils.cpp:62
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
void bindAsUniform< WPropDouble >(osg::Node *node, WPropDouble prop, std::string name)
Template specialization for WPropDouble values.
Definition: WGEUtils.h:213
void bindAsUniform< double >(osg::Node *node, double prop, std::string name)
Template specialization for double values.
Definition: WGEUtils.h:183
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
osg::Vec3 unprojectFromScreen(const osg::Vec3 screen, osg::ref_ptr< WGECamera > camera)
Converts screen coordinates into Camera coordinates.
Definition: WGEUtils.cpp:52