00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef WGETEXTURE_H
00026 #define WGETEXTURE_H
00027
00028 #include <string>
00029
00030 #include <boost/lexical_cast.hpp>
00031 #include <boost/shared_ptr.hpp>
00032
00033 #include <osg/Node>
00034 #include <osg/StateSet>
00035 #include <osg/Texture>
00036 #include <osg/Texture1D>
00037 #include <osg/Texture2D>
00038 #include <osg/Texture3D>
00039
00040 #include "callbacks/WGEFunctorCallback.h"
00041 #include "../common/WLimits.h"
00042 #include "../common/WBoundingBox.h"
00043 #include "../common/WProperties.h"
00044 #include "../common/WPropertyHelper.h"
00045 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
00046
00047 #include "WGETextureUtils.h"
00048
00049
00050
00051
00052
00053 template < typename TextureType = osg::Texture >
00054 class WGETexture: public TextureType
00055 {
00056 public:
00057
00058 static std::size_t const MAX_NUMBER_OF_TEXTURES = 8;
00059
00060
00061 static std::size_t const MAX_TEXTURE_DIMENSION = 2048;
00062
00063
00064
00065
00066
00067
00068
00069 WGETexture( double scale = 1.0, double min = 0.0 );
00070
00071
00072
00073
00074
00075
00076
00077
00078 WGETexture( osg::Image* image, double scale = 1.0, double min = 0.0 );
00079
00080
00081
00082
00083
00084
00085
00086 WGETexture( const WGETexture< TextureType >& texture, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY );
00087
00088
00089
00090
00091 virtual ~WGETexture();
00092
00093
00094
00095
00096
00097
00098 WPropString name() const;
00099
00100
00101
00102
00103
00104
00105
00106 WPropDouble minimum() const;
00107
00108
00109
00110
00111
00112
00113
00114 WPropDouble scale() const;
00115
00116
00117
00118
00119
00120
00121 WPropDouble alpha() const;
00122
00123
00124
00125
00126
00127
00128 WPropDouble threshold() const;
00129
00130
00131
00132
00133
00134
00135 WPropBool thresholdEnabled() const;
00136
00137
00138
00139
00140
00141
00142 WPropBool interpolation() const;
00143
00144
00145
00146
00147
00148
00149 WPropSelection colormap() const;
00150
00151
00152
00153
00154
00155
00156 WPropBool active() const;
00157
00158
00159
00160
00161
00162
00163
00164 WPropMatrix4X4 transformation() const;
00165
00166
00167
00168
00169
00170
00171
00172
00173 void bind( osg::ref_ptr< osg::Node > node, size_t unit = 0 );
00174
00175
00176
00177
00178
00179
00180
00181 boost::shared_ptr< WProperties > getProperties() const;
00182
00183
00184
00185
00186
00187
00188 boost::shared_ptr< WProperties > getInformationProperties() const;
00189
00190
00191
00192
00193
00194
00195
00196 virtual void applyUniforms( std::string prefix, osg::StateSet* states ) const;
00197
00198
00199
00200
00201
00202
00203 void setFilterMinMag( osg::Texture::FilterMode mode );
00204
00205
00206
00207
00208
00209
00210 void setWrapSTR( osg::Texture::WrapMode mode );
00211
00212
00213
00214
00215
00216
00217
00218 virtual WBoundingBox getBoundingBox() const;
00219
00220 protected:
00221
00222
00223
00224
00225 virtual void handleUpdate();
00226
00227
00228
00229
00230 virtual void create();
00231
00232
00233
00234
00235
00236
00237 virtual void updateCallback( osg::StateAttribute* state );
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 static void initTextureSize( osg::Texture1D* texture, int width, int height, int depth );
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 static void initTextureSize( osg::Texture2D* texture, int width, int height, int depth );
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 static void initTextureSize( osg::Texture3D* texture, int width, int height, int depth );
00271
00272 private:
00273
00274
00275
00276
00277
00278
00279 void setupProperties( double scale, double min );
00280
00281
00282
00283
00284 boost::shared_ptr< WCondition > m_propCondition;
00285
00286
00287
00288
00289 boost::shared_ptr< WProperties > m_properties;
00290
00291
00292
00293
00294
00295
00296 boost::shared_ptr< WProperties > m_infoProperties;
00297
00298
00299
00300
00301 bool m_needCreate;
00302
00303
00304
00305
00306 WPropString m_name;
00307
00308
00309
00310
00311 WPropDouble m_min;
00312
00313
00314
00315
00316 WPropDouble m_scale;
00317
00318
00319
00320
00321 boost::shared_ptr< WItemSelection > m_colorMapSelectionsList;
00322
00323
00324
00325
00326 WPropSelection m_colorMap;
00327
00328
00329
00330
00331 WPropDouble m_alpha;
00332
00333
00334
00335
00336 WPropDouble m_threshold;
00337
00338
00339
00340
00341 WPropBool m_thresholdEnabled;
00342
00343
00344
00345
00346 WPropBool m_interpolation;
00347
00348
00349
00350
00351 WPropBool m_active;
00352
00353
00354
00355
00356 WPropMatrix4X4 m_texMatrix;
00357 };
00358
00359
00360
00361
00362
00363
00364 typedef WGETexture< osg::Texture1D > WGETexture1D;
00365
00366
00367
00368
00369 typedef WGETexture< osg::Texture2D > WGETexture2D;
00370
00371
00372
00373
00374 typedef WGETexture< osg::Texture3D > WGETexture3D;
00375
00376
00377 template < typename TextureType >
00378 WGETexture< TextureType >::WGETexture( double scale, double min ):
00379 TextureType(),
00380 m_propCondition( boost::shared_ptr< WCondition >( new WCondition() ) ),
00381 m_properties( boost::shared_ptr< WProperties >( new WProperties( "Texture Properties", "Properties of a texture." ) ) ),
00382 m_infoProperties( boost::shared_ptr< WProperties >( new WProperties( "Texture Info Properties", "Texture's information properties." ) ) ),
00383 m_needCreate( true )
00384 {
00385 setupProperties( scale, min );
00386 }
00387
00388 template < typename TextureType >
00389 WGETexture< TextureType >::WGETexture( osg::Image* image, double scale, double min ):
00390 TextureType( image ),
00391 m_propCondition( boost::shared_ptr< WCondition >( new WCondition() ) ),
00392 m_properties( boost::shared_ptr< WProperties >( new WProperties( "Texture Properties", "Properties of a texture." ) ) ),
00393 m_infoProperties( boost::shared_ptr< WProperties >( new WProperties( "Texture Info Properties", "Texture's information properties." ) ) ),
00394 m_needCreate( true )
00395 {
00396 setupProperties( scale, min );
00397 WGETexture< TextureType >::initTextureSize( this, image->s(), image->t(), image->r() );
00398 }
00399
00400 template < typename TextureType >
00401 WGETexture< TextureType >::WGETexture( const WGETexture< TextureType >& texture, const osg::CopyOp& copyop ):
00402 TextureType( texture, copyop ),
00403 m_min( texture.m_min ),
00404 m_scale( texture.m_scale )
00405 {
00406
00407 }
00408
00409 template < typename TextureType >
00410 void WGETexture< TextureType >::setupProperties( double scale, double min )
00411 {
00412 m_propCondition->subscribeSignal( boost::bind( &WGETexture< TextureType >::handleUpdate, this ) );
00413
00414 m_name = m_properties->addProperty( "Name", "The name of the texture.", std::string( "Unnamed" ) );
00415
00416
00417 m_min = m_properties->addProperty( "Minimum", "The minimum value in the original space.", min, true );
00418 m_min->removeConstraint( m_min->getMin() );
00419 m_min->removeConstraint( m_min->getMax() );
00420
00421 m_scale = m_properties->addProperty( "Scale", "The scaling factor to un-scale the texture values to the original space.", scale, true );
00422 m_scale->removeConstraint( m_scale->getMin() );
00423 m_scale->removeConstraint( m_scale->getMax() );
00424
00425 m_alpha = m_properties->addProperty( "Alpha", "The alpha blending value.", 1.0 );
00426 m_alpha->setMin( 0.0 );
00427 m_alpha->setMax( 1.0 );
00428
00429 m_thresholdEnabled = m_properties->addProperty( "Enable Threshold",
00430 "If enabled, threshold based clipping is used. If not, threshold is ignored.", false );
00431
00432 m_threshold = m_properties->addProperty( "Threshold", "The threshold used to clip areas.", 0.0 );
00433 m_threshold->setMin( min );
00434 m_threshold->setMax( min + scale );
00435
00436 m_interpolation = m_properties->addProperty( "Interpolate", "Interpolation of the volume data.", true, m_propCondition );
00437
00438 m_colorMapSelectionsList = boost::shared_ptr< WItemSelection >( new WItemSelection() );
00439 m_colorMapSelectionsList->addItem( "Grayscale", "" );
00440 m_colorMapSelectionsList->addItem( "Rainbow", "" );
00441 m_colorMapSelectionsList->addItem( "Hot iron", "" );
00442 m_colorMapSelectionsList->addItem( "Negative to positive", "" );
00443 m_colorMapSelectionsList->addItem( "Atlas", "" );
00444 m_colorMapSelectionsList->addItem( "Blue-Green-Purple", "" );
00445 m_colorMapSelectionsList->addItem( "Vector", "" );
00446
00447 m_colorMap = m_properties->addProperty( "Colormap", "The colormap of this texture.", m_colorMapSelectionsList->getSelectorFirst() );
00448 WPropertyHelper::PC_SELECTONLYONE::addTo( m_colorMap );
00449
00450 m_active = m_properties->addProperty( "Active", "Can dis-enable a texture.", true );
00451
00452 WMatrix4d m = WMatrix4d::identity();
00453 m_texMatrix = m_properties->addProperty( "Texture Transformation", "Usable to transform the texture.", m );
00454 m_texMatrix->setPurpose( PV_PURPOSE_INFORMATION );
00455
00456 TextureType::setResizeNonPowerOfTwoHint( false );
00457 TextureType::setUpdateCallback( new WGEFunctorCallback< osg::StateAttribute >(
00458 boost::bind( &WGETexture< TextureType >::updateCallback, this, _1 ) )
00459 );
00460
00461
00462 TextureType::setFilter( osg::Texture::MIN_FILTER, m_interpolation->get( true ) ? osg::Texture::LINEAR : osg::Texture::NEAREST );
00463 TextureType::setFilter( osg::Texture::MAG_FILTER, m_interpolation->get( true ) ? osg::Texture::LINEAR : osg::Texture::NEAREST );
00464 }
00465
00466 template < typename TextureType >
00467 WGETexture< TextureType >::~WGETexture()
00468 {
00469
00470 }
00471
00472 template < typename TextureType >
00473 boost::shared_ptr< WProperties > WGETexture< TextureType >::getProperties() const
00474 {
00475 return m_properties;
00476 }
00477
00478 template < typename TextureType >
00479 boost::shared_ptr< WProperties > WGETexture< TextureType >::getInformationProperties() const
00480 {
00481 return m_infoProperties;
00482 }
00483
00484 template < typename TextureType >
00485 inline WPropString WGETexture< TextureType >::name() const
00486 {
00487 return m_name;
00488 }
00489
00490 template < typename TextureType >
00491 inline WPropDouble WGETexture< TextureType >::minimum() const
00492 {
00493 return m_min;
00494 }
00495
00496 template < typename TextureType >
00497 inline WPropDouble WGETexture< TextureType >::scale() const
00498 {
00499 return m_scale;
00500 }
00501
00502 template < typename TextureType >
00503 inline WPropDouble WGETexture< TextureType >::alpha() const
00504 {
00505 return m_alpha;
00506 }
00507
00508 template < typename TextureType >
00509 inline WPropDouble WGETexture< TextureType >::threshold() const
00510 {
00511 return m_threshold;
00512 }
00513
00514 template < typename TextureType >
00515 inline WPropBool WGETexture< TextureType >::thresholdEnabled() const
00516 {
00517 return m_thresholdEnabled;
00518 }
00519
00520 template < typename TextureType >
00521 inline WPropBool WGETexture< TextureType >::interpolation() const
00522 {
00523 return m_interpolation;
00524 }
00525
00526 template < typename TextureType >
00527 inline WPropSelection WGETexture< TextureType >::colormap() const
00528 {
00529 return m_colorMap;
00530 }
00531
00532 template < typename TextureType >
00533 inline WPropBool WGETexture< TextureType >::active() const
00534 {
00535 return m_active;
00536 }
00537
00538 template < typename TextureType >
00539 inline WPropMatrix4X4 WGETexture< TextureType >::transformation() const
00540 {
00541 return m_texMatrix;
00542 }
00543
00544 template < typename TextureType >
00545 void WGETexture< TextureType >::handleUpdate()
00546 {
00547 if( m_interpolation->changed() )
00548 {
00549 TextureType::setFilter( osg::Texture::MIN_FILTER, m_interpolation->get( true ) ? osg::Texture::LINEAR : osg::Texture::NEAREST );
00550 TextureType::setFilter( osg::Texture::MAG_FILTER, m_interpolation->get( true ) ? osg::Texture::LINEAR : osg::Texture::NEAREST );
00551 }
00552 }
00553
00554 template < typename TextureType >
00555 void WGETexture< TextureType >::applyUniforms( std::string prefix, osg::StateSet* states ) const
00556 {
00557 states->addUniform( new WGEPropertyUniform< WPropDouble >( prefix + "Min", minimum() ) );
00558 states->addUniform( new WGEPropertyUniform< WPropDouble >( prefix + "Scale", scale() ) );
00559 states->addUniform( new WGEPropertyUniform< WPropDouble >( prefix + "Alpha", alpha() ) );
00560 states->addUniform( new WGEPropertyUniform< WPropBool >( prefix + "ThresholdEnabled", thresholdEnabled() ) );
00561 states->addUniform( new WGEPropertyUniform< WPropDouble >( prefix + "Threshold", threshold() ) );
00562 states->addUniform( new WGEPropertyUniform< WPropSelection >( prefix + "Colormap", colormap() ) );
00563 states->addUniform( new WGEPropertyUniform< WPropBool >( prefix + "Active", active() ) );
00564 }
00565
00566 template < typename TextureType >
00567 void WGETexture< TextureType >::bind( osg::ref_ptr< osg::Node > node, size_t unit )
00568 {
00569
00570 wge::bindTexture( node, osg::ref_ptr< WGETexture< TextureType > >( this ), unit );
00571 }
00572
00573 template < typename TextureType >
00574 void WGETexture< TextureType >::create()
00575 {
00576
00577 }
00578
00579 template < typename TextureType >
00580 void WGETexture< TextureType >::updateCallback( osg::StateAttribute* )
00581 {
00582
00583 if( m_needCreate )
00584 {
00585 m_needCreate = false;
00586 create();
00587 TextureType::dirtyTextureObject();
00588 }
00589 }
00590
00591 template < typename TextureType >
00592 void WGETexture< TextureType >::setFilterMinMag( osg::Texture::FilterMode mode )
00593 {
00594 this->setFilter( osg::Texture2D::MIN_FILTER, mode );
00595 this->setFilter( osg::Texture2D::MAG_FILTER, mode );
00596 }
00597
00598 template < typename TextureType >
00599 void WGETexture< TextureType >::setWrapSTR( osg::Texture::WrapMode mode )
00600 {
00601 this->setWrap( osg::Texture2D::WRAP_S, mode );
00602 this->setWrap( osg::Texture2D::WRAP_T, mode );
00603 this->setWrap( osg::Texture2D::WRAP_R, mode );
00604 }
00605
00606 template < typename TextureType >
00607 void WGETexture< TextureType >::initTextureSize( osg::Texture1D* texture, int width, int , int )
00608 {
00609 texture->setTextureWidth( width );
00610 }
00611
00612 template < typename TextureType >
00613 void WGETexture< TextureType >::initTextureSize( osg::Texture2D* texture, int width, int height, int )
00614 {
00615 texture->setTextureSize( width, height );
00616 }
00617
00618 template < typename TextureType >
00619 void WGETexture< TextureType >::initTextureSize( osg::Texture3D* texture, int width, int height, int depth )
00620 {
00621 texture->setTextureSize( width, height, depth );
00622 }
00623
00624 template < typename TextureType >
00625 WBoundingBox WGETexture< TextureType >::getBoundingBox() const
00626 {
00627 return WBoundingBox( 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 );
00628 }
00629
00630 #endif // WGETEXTURE_H
00631