OpenWalnut 1.3.1
|
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 osg::Vec4 wge::unprojectFromScreen( const osg::Vec4 screen, osg::ref_ptr< osg::Camera > camera ) 00054 { 00055 return screen * osg::Matrix::inverse( camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix() ); 00056 } 00057 00058 WColor wge::createColorFromIndex( int index ) 00059 { 00060 float r = 0.0; 00061 float g = 0.0; 00062 float b = 0.0; 00063 float mult = 1.0; 00064 00065 if( index == 0 ) 00066 { 00067 return WColor( 0.0, 0.0, 0.0, 1.0 ); 00068 } 00069 00070 if( ( index & 1 ) == 1 ) 00071 { 00072 b = 1.0; 00073 } 00074 if( ( index & 2 ) == 2 ) 00075 { 00076 g = 1.0; 00077 } 00078 if( ( index & 4 ) == 4 ) 00079 { 00080 r = 1.0; 00081 } 00082 if( ( index & 8 ) == 8 ) 00083 { 00084 mult -= 0.15; 00085 if( r < 1.0 && g < 1.0 && b < 1.0 ) 00086 { 00087 r = 1.0; 00088 g = 1.0; 00089 } 00090 } 00091 if( ( index & 16 ) == 16 ) 00092 { 00093 mult -= 0.15; 00094 if( r < 1.0 && g < 1.0 && b < 1.0 ) 00095 { 00096 b = 1.0; 00097 g = 1.0; 00098 } 00099 } 00100 if( ( index & 32 ) == 32 ) 00101 { 00102 mult -= 0.15; 00103 if( r < 1.0 && g < 1.0 && b < 1.0 ) 00104 { 00105 r = 1.0; 00106 b = 1.0; 00107 } 00108 } 00109 if( ( index & 64 ) == 64 ) 00110 { 00111 mult -= 0.15; 00112 if( r < 1.0 && g < 1.0 && b < 1.0 ) 00113 { 00114 g = 1.0; 00115 } 00116 } 00117 if( ( index & 128 ) == 128 ) 00118 { 00119 mult -= 0.15; 00120 if( r < 1.0 && g < 1.0 && b < 1.0 ) 00121 { 00122 r = 1.0; 00123 } 00124 } 00125 r *= mult; 00126 g *= mult; 00127 b *= mult; 00128 00129 return WColor( r, g, b, 1.0 ); 00130 } 00131 00132 WColor wge::createColorFromHSV( int h, float s, float v ) 00133 { 00134 h = h % 360; 00135 00136 int hi = h / 60; 00137 float f = ( static_cast<float>( h ) / 60.0 ) - hi; 00138 00139 float p = v * ( 1.0 - s ); 00140 float q = v * ( 1.0 - s * f ); 00141 float t = v * ( 1.0 - s * ( 1.0 - f ) ); 00142 00143 switch( hi ) 00144 { 00145 case 0: 00146 return WColor( v, t, p, 1.0 ); 00147 case 1: 00148 return WColor( q, v, p, 1.0 ); 00149 case 2: 00150 return WColor( p, v, t, 1.0 ); 00151 case 3: 00152 return WColor( p, q, v, 1.0 ); 00153 case 4: 00154 return WColor( t, p, v, 1.0 ); 00155 case 5: 00156 return WColor( v, p, q, 1.0 ); 00157 case 6: 00158 return WColor( v, t, p, 1.0 ); 00159 default: 00160 return WColor( v, t, p, 1.0 ); 00161 } 00162 } 00163 00164 WColor wge::getNthHSVColor( int n ) 00165 { 00166 int h = 0; 00167 float s = 1.0; 00168 float v = 1.0; 00169 00170 if( ( n & 1 ) == 1 ) 00171 { 00172 h += 180; 00173 } 00174 if( ( n & 2 ) == 2 ) 00175 { 00176 h += 90; 00177 } 00178 if( ( n & 4 ) == 4 ) 00179 { 00180 h += 45; 00181 } 00182 if( ( n & 8 ) == 8 ) 00183 { 00184 h += 202; 00185 h = h % 360; 00186 } 00187 if( ( n & 16 ) == 16 ) 00188 { 00189 v -= .25; 00190 } 00191 if( ( n & 32 ) == 32 ) 00192 { 00193 s -= .25; 00194 } 00195 if( ( n & 64 ) == 64 ) 00196 { 00197 v -= .25; 00198 } 00199 if( ( n & 128 ) == 128 ) 00200 { 00201 s -= 0.25; 00202 } 00203 if( ( n & 256 ) == 256 ) 00204 { 00205 v -= 0.25; 00206 } 00207 00208 return createColorFromHSV( h, s, v ); 00209 } 00210 00211 void wge::enableTransparency( osg::ref_ptr< osg::Node > node ) 00212 { 00213 osg::StateSet* state = node->getOrCreateStateSet(); 00214 00215 // NOTE: this does not en/disable blending. This is always on. 00216 state->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); 00217 00218 // Enable depth test so that an opaque polygon will occlude a transparent one behind it. 00219 state->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON ); 00220 00221 // Conversely, disable writing to depth buffer so that a transparent polygon will allow polygons behind it to shine through. 00222 // OSG renders transparent polygons after opaque ones. 00223 // NOTE: USEFUL? 00224 // osg::Depth* depth = new osg::Depth; 00225 // depth->setWriteMask( false ); 00226 // state->setAttributeAndModes( depth, osg::StateAttribute::ON ); 00227 00228 // blending. Without this, setting alpha does not cause anything 00229 state->setMode( GL_BLEND, osg::StateAttribute::ON ); 00230 } 00231