OpenWalnut  1.3.1
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 
32 #include <osg/Array>
33 #include <osg/Vec3>
34 #include <osg/Vec4>
35 #include <osg/Camera>
36 
37 #include "../common/WColor.h"
38 #include "../common/WAssert.h"
39 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
40 
41 namespace wge
42 {
43  /**
44  * Enable transparency for the given node. This enabled blending and sets the node to the transparency bin.
45  *
46  * \param node the node
47  */
48  void enableTransparency( osg::ref_ptr< osg::Node > node );
49 
50  /**
51  * Transforms a direction given via two points into a RGB color.
52  *
53  * \param pos1 First point
54  * \param pos2 Second point
55  *
56  * \return converts a vector to a color
57  */
58  WColor getRGBAColorFromDirection( const WPosition &pos1, const WPosition &pos2 );
59 
60  /**
61  * Converts a whole vector of WPositions into an osg::Vec3Array.
62  *
63  * \param posArray The given positions vector
64  *
65  * \return Refernce to the same vector but as osg::Vec3Array.
66  */
67  osg::ref_ptr< osg::Vec3Array > osgVec3Array( const std::vector< WPosition >& posArray );
68 
69  /**
70  * Converts screen coordinates into Camera coordinates.
71  *
72  * \param screen the screen coordinates
73  * \param camera The matrices of this camera will used for unprojecting.
74  *
75  * \return un-projects a screen coordinate back to world space
76  */
77  osg::Vec3 unprojectFromScreen( const osg::Vec3 screen, osg::ref_ptr< osg::Camera > camera );
78 
79  /**
80  * Converts screen coordinates into Camera coordinates.
81  * \note this method can be useful to work with vectors (w component 0)
82  *
83  * \param screen the screen coordinates
84  * \param camera The matrices of this camera will used for unprojecting.
85  *
86  * \return un-projects a screen coordinate back to world space
87  */
88  osg::Vec4 unprojectFromScreen( const osg::Vec4 screen, osg::ref_ptr< osg::Camera > camera );
89 
90  /**
91  * creates the same color as the atlas colormap shader from the index
92  *
93  * \param index unsigned char that indexes the color
94  * \return the color
95  */
96  WColor createColorFromIndex( int index );
97 
98  /**
99  * creates a rgb WColor from a HSV value
100  * \param h hue
101  * \param s saturation
102  * \param v value
103  * \return the color
104  */
105  WColor createColorFromHSV( int h, float s = 1.0, float v = 1.0 );
106 
107  /**
108  * creates the nth color of a partition of the hsv color circle
109  *
110  * \param n number of the color
111  * \return the color
112  */
113  WColor getNthHSVColor( int n );
114 }
115 
116 inline WColor wge::getRGBAColorFromDirection( const WPosition &pos1, const WPosition &pos2 )
117 {
118  WPosition direction( normalize( pos2 - pos1 ) );
119  return WColor( std::abs( direction[0] ), std::abs( direction[1] ), std::abs( direction[2] ), 1.0f );
120 }
121 
122 #endif // WGEUTILS_H
123