OpenWalnut  1.3.1
WGEUtils.cpp
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 #include <algorithm>
26 #include <vector>
27 
28 #include <osg/Array>
29 
30 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
31 
32 #include "WGETexture.h"
33 
34 #include "WGEUtils.h"
35 
36 osg::ref_ptr< osg::Vec3Array > wge::osgVec3Array( const std::vector< WPosition >& posArray )
37 {
38  osg::ref_ptr< osg::Vec3Array > result = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
39  result->reserve( posArray.size() );
40  std::vector< WPosition >::const_iterator cit;
41  for( cit = posArray.begin(); cit != posArray.end(); ++cit )
42  {
43  result->push_back( *cit );
44  }
45  return result;
46 }
47 
48 osg::Vec3 wge::unprojectFromScreen( const osg::Vec3 screen, osg::ref_ptr< osg::Camera > camera )
49 {
50  return screen * osg::Matrix::inverse( camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix() );
51 }
52 
53 osg::Vec4 wge::unprojectFromScreen( const osg::Vec4 screen, osg::ref_ptr< osg::Camera > camera )
54 {
55  return screen * osg::Matrix::inverse( camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix() );
56 }
57 
58 WColor wge::createColorFromIndex( int index )
59 {
60  float r = 0.0;
61  float g = 0.0;
62  float b = 0.0;
63  float mult = 1.0;
64 
65  if( index == 0 )
66  {
67  return WColor( 0.0, 0.0, 0.0, 1.0 );
68  }
69 
70  if( ( index & 1 ) == 1 )
71  {
72  b = 1.0;
73  }
74  if( ( index & 2 ) == 2 )
75  {
76  g = 1.0;
77  }
78  if( ( index & 4 ) == 4 )
79  {
80  r = 1.0;
81  }
82  if( ( index & 8 ) == 8 )
83  {
84  mult -= 0.15;
85  if( r < 1.0 && g < 1.0 && b < 1.0 )
86  {
87  r = 1.0;
88  g = 1.0;
89  }
90  }
91  if( ( index & 16 ) == 16 )
92  {
93  mult -= 0.15;
94  if( r < 1.0 && g < 1.0 && b < 1.0 )
95  {
96  b = 1.0;
97  g = 1.0;
98  }
99  }
100  if( ( index & 32 ) == 32 )
101  {
102  mult -= 0.15;
103  if( r < 1.0 && g < 1.0 && b < 1.0 )
104  {
105  r = 1.0;
106  b = 1.0;
107  }
108  }
109  if( ( index & 64 ) == 64 )
110  {
111  mult -= 0.15;
112  if( r < 1.0 && g < 1.0 && b < 1.0 )
113  {
114  g = 1.0;
115  }
116  }
117  if( ( index & 128 ) == 128 )
118  {
119  mult -= 0.15;
120  if( r < 1.0 && g < 1.0 && b < 1.0 )
121  {
122  r = 1.0;
123  }
124  }
125  r *= mult;
126  g *= mult;
127  b *= mult;
128 
129  return WColor( r, g, b, 1.0 );
130 }
131 
132 WColor wge::createColorFromHSV( int h, float s, float v )
133 {
134  h = h % 360;
135 
136  int hi = h / 60;
137  float f = ( static_cast<float>( h ) / 60.0 ) - hi;
138 
139  float p = v * ( 1.0 - s );
140  float q = v * ( 1.0 - s * f );
141  float t = v * ( 1.0 - s * ( 1.0 - f ) );
142 
143  switch( hi )
144  {
145  case 0:
146  return WColor( v, t, p, 1.0 );
147  case 1:
148  return WColor( q, v, p, 1.0 );
149  case 2:
150  return WColor( p, v, t, 1.0 );
151  case 3:
152  return WColor( p, q, v, 1.0 );
153  case 4:
154  return WColor( t, p, v, 1.0 );
155  case 5:
156  return WColor( v, p, q, 1.0 );
157  case 6:
158  return WColor( v, t, p, 1.0 );
159  default:
160  return WColor( v, t, p, 1.0 );
161  }
162 }
163 
164 WColor wge::getNthHSVColor( int n )
165 {
166  int h = 0;
167  float s = 1.0;
168  float v = 1.0;
169 
170  if( ( n & 1 ) == 1 )
171  {
172  h += 180;
173  }
174  if( ( n & 2 ) == 2 )
175  {
176  h += 90;
177  }
178  if( ( n & 4 ) == 4 )
179  {
180  h += 45;
181  }
182  if( ( n & 8 ) == 8 )
183  {
184  h += 202;
185  h = h % 360;
186  }
187  if( ( n & 16 ) == 16 )
188  {
189  v -= .25;
190  }
191  if( ( n & 32 ) == 32 )
192  {
193  s -= .25;
194  }
195  if( ( n & 64 ) == 64 )
196  {
197  v -= .25;
198  }
199  if( ( n & 128 ) == 128 )
200  {
201  s -= 0.25;
202  }
203  if( ( n & 256 ) == 256 )
204  {
205  v -= 0.25;
206  }
207 
208  return createColorFromHSV( h, s, v );
209 }
210 
211 void wge::enableTransparency( osg::ref_ptr< osg::Node > node )
212 {
213  osg::StateSet* state = node->getOrCreateStateSet();
214 
215  // NOTE: this does not en/disable blending. This is always on.
216  state->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
217 
218  // Enable depth test so that an opaque polygon will occlude a transparent one behind it.
219  state->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON );
220 
221  // Conversely, disable writing to depth buffer so that a transparent polygon will allow polygons behind it to shine through.
222  // OSG renders transparent polygons after opaque ones.
223  // NOTE: USEFUL?
224  // osg::Depth* depth = new osg::Depth;
225  // depth->setWriteMask( false );
226  // state->setAttributeAndModes( depth, osg::StateAttribute::ON );
227 
228  // blending. Without this, setting alpha does not cause anything
229  state->setMode( GL_BLEND, osg::StateAttribute::ON );
230 }
231