OpenWalnut 1.2.5

WGEUtils.cpp

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <algorithm>
00026 #include <vector>
00027 
00028 #include <osg/Array>
00029 
00030 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
00031 
00032 #include "WGETexture.h"
00033 
00034 #include "WGEUtils.h"
00035 
00036 osg::ref_ptr< osg::Vec3Array > wge::osgVec3Array( const std::vector< WPosition >& posArray )
00037 {
00038     osg::ref_ptr< osg::Vec3Array > result = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
00039     result->reserve( posArray.size() );
00040     std::vector< WPosition >::const_iterator cit;
00041     for( cit = posArray.begin(); cit != posArray.end(); ++cit )
00042     {
00043         result->push_back( *cit );
00044     }
00045     return result;
00046 }
00047 
00048 osg::Vec3 wge::unprojectFromScreen( const osg::Vec3 screen, osg::ref_ptr< osg::Camera > camera )
00049 {
00050     return screen * osg::Matrix::inverse( camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix() );
00051 }
00052 
00053 WColor wge::createColorFromIndex( int index )
00054 {
00055     float r = 0.0;
00056     float g = 0.0;
00057     float b = 0.0;
00058     float mult = 1.0;
00059 
00060     if( index == 0 )
00061     {
00062         return WColor( 0.0, 0.0, 0.0, 1.0 );
00063     }
00064 
00065     if( ( index & 1 ) == 1 )
00066     {
00067         b = 1.0;
00068     }
00069     if( ( index & 2 ) == 2 )
00070     {
00071         g = 1.0;
00072     }
00073     if( ( index & 4 ) == 4 )
00074     {
00075         r = 1.0;
00076     }
00077     if( ( index & 8 ) == 8 )
00078     {
00079         mult -= 0.15;
00080         if( r < 1.0 && g < 1.0 && b < 1.0 )
00081         {
00082             r = 1.0;
00083             g = 1.0;
00084         }
00085     }
00086     if( ( index & 16 ) == 16 )
00087     {
00088         mult -= 0.15;
00089         if( r < 1.0 && g < 1.0 && b < 1.0 )
00090         {
00091             b = 1.0;
00092             g = 1.0;
00093         }
00094     }
00095     if( ( index & 32 ) == 32 )
00096     {
00097         mult -= 0.15;
00098         if( r < 1.0 && g < 1.0 && b < 1.0 )
00099         {
00100             r = 1.0;
00101             b = 1.0;
00102         }
00103     }
00104     if( ( index & 64 ) == 64 )
00105     {
00106         mult -= 0.15;
00107         if( r < 1.0 && g < 1.0 && b < 1.0 )
00108         {
00109             g = 1.0;
00110         }
00111     }
00112     if( ( index & 128 ) == 128 )
00113     {
00114         mult -= 0.15;
00115         if( r < 1.0 && g < 1.0 && b < 1.0 )
00116         {
00117             r = 1.0;
00118         }
00119     }
00120     r *= mult;
00121     g *= mult;
00122     b *= mult;
00123 
00124     return WColor( r, g, b, 1.0 );
00125 }
00126 
00127 WColor wge::createColorFromHSV( int h, float s, float v )
00128 {
00129     h = h % 360;
00130 
00131     int hi = h / 60;
00132     float f =  ( static_cast<float>( h ) / 60.0 ) - hi;
00133 
00134     float p = v * ( 1.0 - s );
00135     float q = v * ( 1.0 - s * f );
00136     float t = v * ( 1.0 - s * ( 1.0 - f ) );
00137 
00138     switch ( hi )
00139     {
00140         case 0:
00141             return WColor( v, t, p, 1.0 );
00142         case 1:
00143             return WColor( q, v, p, 1.0 );
00144         case 2:
00145             return WColor( p, v, t, 1.0 );
00146         case 3:
00147             return WColor( p, q, v, 1.0 );
00148         case 4:
00149             return WColor( t, p, v, 1.0 );
00150         case 5:
00151             return WColor( v, p, q, 1.0 );
00152         case 6:
00153             return WColor( v, t, p, 1.0 );
00154         default:
00155             return WColor( v, t, p, 1.0 );
00156     }
00157 }
00158 
00159 WColor wge::getNthHSVColor( int n )
00160 {
00161     int h = 0;
00162     float s = 1.0;
00163     float v = 1.0;
00164 
00165     if( ( n & 1 ) == 1 )
00166     {
00167         h += 180;
00168     }
00169     if( ( n & 2 ) == 2 )
00170     {
00171         h += 90;
00172     }
00173     if( ( n & 4 ) == 4 )
00174     {
00175         h += 45;
00176     }
00177     if( ( n & 8 ) == 8 )
00178     {
00179         h += 202;
00180         h = h % 360;
00181     }
00182     if( ( n & 16 ) == 16 )
00183     {
00184         v -= .25;
00185     }
00186     if( ( n & 32 ) == 32 )
00187     {
00188         s -= .25;
00189     }
00190     if( ( n & 64 ) == 64 )
00191     {
00192         v -= .25;
00193     }
00194     if( ( n & 128 ) == 128 )
00195     {
00196         s -= 0.25;
00197     }
00198     if( ( n & 256 ) == 256 )
00199     {
00200         v -= 0.25;
00201     }
00202 
00203     return createColorFromHSV( h, s, v );
00204 }
00205 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends