00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00015 #include "nvsgcommon.h"
00016
00017 #include <map>
00018 #include "nvmath/Plane3f.h"
00019 #include "nvmath/Trafo.h"
00020 #include "nvsg/StateAttribute.h"
00021 #include "nvsg/Texture.h"
00022
00023 namespace nvsg
00024 {
00026 typedef enum
00027 {
00028 TT_INVALID = 0,
00029 TT_TRANSPARENT = 1,
00030 TT_DIFFUSE = 3,
00031 } TextureType;
00032
00034 typedef enum
00035 {
00036 TCA_S = 0
00037 , TCA_T
00038 , TCA_R
00039 , TCA_Q
00040 } TexCoordAxis;
00041
00043 typedef enum
00044 {
00045 TEM_REPLACE = 0
00046 , TEM_MODULATE
00047 , TEM_DECAL
00048 , TEM_BLEND
00049 , TEM_ADD
00050 } TextureEnvMode;
00051
00053 typedef enum
00054 {
00055 TGML_OBJECT = 0
00056 , TGML_EYE
00057 } TexGenLinearMode;
00058
00060 typedef enum
00061 {
00062 TGM_OFF = -1
00063 , TGM_OBJECT_LINEAR = TGML_OBJECT
00064 , TGM_EYE_LINEAR = TGML_EYE
00065 , TGM_SPHERE_MAP
00066
00067
00068 } TexGenMode;
00069
00071 typedef enum
00072 {
00073 TWM_REPEAT = 0
00074 , TWM_CLAMP
00075 , TWM_MIRROR
00076 } TextureWrapMode;
00077
00079 typedef enum
00080 {
00081 TFM_MIN_NEAREST = 0
00082 , TFM_MIN_BILINEAR
00083 , TFM_MIN_TRILINEAR
00084 } TextureMinFilterMode;
00085
00087 typedef enum
00088 {
00089 TFM_MAG_NEAREST = 0
00090 , TFM_MAG_BILINEAR
00091 } TextureMagFilterMode;
00092
00094 class TextureAttributeItemData : public nvutil::RCObject
00095 {
00096 friend class TextureAttributeItem;
00097 public:
00099
00100 NVSG_API TextureAttributeItemData(const TextureAttributeItemData& rhs);
00101
00102 private:
00103
00104 TextureAttributeItemData();
00105 ~TextureAttributeItemData();
00106
00107 TextureAttributeItemData& operator=(const TextureAttributeItemData& rhs) {}
00108
00109 std::string m_fileName;
00110 nvmath::Vec4f m_borderColor;
00111 TextureEnvMode m_envMode;
00112 TextureMagFilterMode m_magFilterMode;
00113 TextureMinFilterMode m_minFilterMode;
00114 TextureType m_type;
00115 nvmath::Trafo m_trafo;
00116 TextureWrapMode m_wrapMode[3];
00117 TexGenMode m_texGenMode[4];
00118 nvmath::Plane3f m_texGenPlane[4];
00119 };
00120
00122
00126 class TextureAttributeItem : public Object
00127 {
00128 friend class TextureAttribute;
00129
00130 public:
00132
00133 NVSG_API static const TextureAttributeItem * create( void );
00134
00136
00137 NVSG_API static const TextureAttributeItem * createFromBase( const Object &rhs
00138 );
00139
00141
00142 NVSG_API virtual const TextureAttributeItem * clone( void ) const;
00143
00145
00146 NVSG_API virtual bool isDataShared(void) const;
00147
00149
00150 NVSG_API virtual DataID getDataID(void) const;
00151
00153
00154 NVSG_API void setFileName( const std::string& fname
00155 );
00156
00158
00159 NVSG_API const std::string& getFileName( void ) const;
00160
00162
00163 NVSG_API const Texture * getTexture( void ) const;
00164
00166
00170 NVSG_API void setTexture( const Texture *p
00171 ) const;
00172
00174 NVSG_API void setBorderColor( const nvmath::Vec4f &color );
00175
00177 NVSG_API const nvmath::Vec4f& getBorderColor( void ) const;
00178
00180
00181 NVSG_API TextureEnvMode getEnvMode( void ) const;
00182
00184
00185 NVSG_API void setEnvMode( TextureEnvMode mode
00186 );
00187
00189
00191 NVSG_API void setMagFilterMode( TextureMagFilterMode mode
00192 );
00193
00195
00196 NVSG_API TextureMagFilterMode getMagFilterMode( void ) const;
00197
00199
00202 NVSG_API void setMinFilterMode( TextureMinFilterMode mode
00203 );
00204
00206
00207 NVSG_API TextureMinFilterMode getMinFilterMode( void ) const;
00208
00210
00213 NVSG_API TextureWrapMode getWrapMode( TexCoordAxis target
00214 ) const;
00215
00217
00220 NVSG_API void setWrapMode( TexCoordAxis target
00221 , TextureWrapMode mode
00222 );
00223
00225
00227 NVSG_API TextureType getType( void ) const;
00228
00230
00232 NVSG_API void setType( TextureType tt
00233 );
00234
00236
00238 NVSG_API nvmath::Mat44f getMatrix( void ) const;
00239
00241
00243 NVSG_API const nvmath::Quatf & getOrientation( void ) const;
00244
00246
00247 NVSG_API void setOrientation( const nvmath::Quatf & orientation
00248 );
00249
00251
00253 NVSG_API const nvmath::Vec3f & getScaling( void ) const;
00254
00256
00257 NVSG_API void setScaling( const nvmath::Vec3f &scale
00258 );
00259
00261
00263 NVSG_API const nvmath::Vec3f & getTranslation( void ) const;
00264
00266
00267 NVSG_API void setTranslation( const nvmath::Vec3f &trans
00268 );
00269
00271
00273 NVSG_API bool isLookupRequired( void ) const;
00274
00276 NVSG_API void setTexGenEnvironmentMapping( bool sAxis = true
00277 , bool tAxis = true
00278 );
00279
00281 NVSG_API void setTexGenLinearMapping( TexCoordAxis axis
00282 , TexGenLinearMode mode
00283 , const nvmath::Plane3f & plane
00284 );
00285
00287 NVSG_API void disableTexGen( TexCoordAxis axis
00288 );
00289
00291 NVSG_API TexGenMode getTexGenMode( TexCoordAxis axis
00292 ) const;
00293
00295 NVSG_API const nvmath::Plane3f &getTexGenPlane( TexCoordAxis axis
00296 ) const;
00297
00298 protected:
00300 NVSG_API TextureAttributeItem( void );
00301
00303 NVSG_API TextureAttributeItem( const Object &rhs );
00304
00306 NVSG_API TextureAttributeItem( const TextureAttributeItem& rhs );
00307
00308
00309 NVSG_API virtual ~TextureAttributeItem();
00310
00311
00312 private:
00313 void addOwner( const TextureAttribute * pAttrib );
00314 void removeOwner( const TextureAttribute * pAttrib );
00315
00316 private:
00317 std::vector<const TextureAttribute*> m_owners;
00318
00319 nvutil::RCPtr<TextureAttributeItemData> m_sharedData;
00320 mutable const Texture * m_texture;
00321 mutable bool m_lookupRequired;
00322 };
00323
00325
00332 class TextureAttribute : public StateAttribute
00333 {
00334 public:
00335
00336 enum {
00337 INVALID_ITEM = 0xFFFFFFFF
00338 };
00339
00341
00342 NVSG_API static const TextureAttribute * create( void );
00343
00345
00346 NVSG_API static const TextureAttribute * createFromBase( const StateAttribute &rhs );
00347
00349
00350 NVSG_API virtual const TextureAttribute * clone( void ) const;
00351
00353
00354 NVSG_API virtual bool isDataShared( void ) const;
00355
00357
00358 NVSG_API virtual DataID getDataID( void ) const;
00359
00361
00364 NVSG_API void bindTextureAttributeItem( const TextureAttributeItem* item
00365 , size_t unit
00366 );
00367
00369
00372 NVSG_API void unbindTextureAttributeItem( size_t unit
00373 );
00374
00376
00382 NVSG_API size_t getFirstItem( void ) const;
00383
00385
00392 NVSG_API size_t getNextItem( void ) const;
00393
00395
00405 NVSG_API size_t getNumberOfItems( void ) const;
00406
00408
00411 NVSG_API const TextureAttributeItem * getTextureAttributeItem( size_t unit
00412 ) const;
00413
00414 protected:
00416 NVSG_API TextureAttribute( void );
00417
00419 NVSG_API TextureAttribute( const StateAttribute &rhs );
00420
00422 NVSG_API TextureAttribute( const TextureAttribute& rhs );
00423
00425 NVSG_API virtual ~TextureAttribute( void );
00426
00427 private:
00428 std::map<size_t, const TextureAttributeItem*> m_bindings;
00429 mutable size_t m_currItem;
00430 };
00431
00432
00433
00434 inline size_t TextureAttribute::getFirstItem( void ) const
00435 {
00436 __TRACE();
00437 m_currItem = !m_bindings.empty() ? m_bindings.begin()->first : INVALID_ITEM;
00438 return m_currItem;
00439 }
00440
00441 inline size_t TextureAttribute::getNextItem( void ) const
00442 {
00443 __TRACE();
00444 if (m_currItem!=INVALID_ITEM)
00445 {
00446 std::map<size_t, const TextureAttributeItem*>::const_iterator ci = m_bindings.upper_bound(m_currItem);
00447 m_currItem = ci!=m_bindings.end() ? ci->first : INVALID_ITEM;
00448 }
00449 return m_currItem;
00450 }
00451
00452 inline size_t TextureAttribute::getNumberOfItems( void ) const
00453 {
00454 __TRACE();
00455 return m_bindings.size();
00456 }
00457
00458 inline const TextureAttributeItem* TextureAttribute::getTextureAttributeItem( size_t unit ) const
00459 {
00460 __TRACE();
00461 std::map<size_t, const TextureAttributeItem*>::const_iterator ci = m_bindings.find(unit);
00462 return ci!=m_bindings.end() ? (*ci).second : NULL;
00463 }
00464
00465
00466 inline const std::string& TextureAttributeItem::getFileName() const
00467 {
00468 __TRACE();
00469 return ( m_sharedData->m_fileName );
00470 }
00471
00472 inline void TextureAttributeItem::setFileName( const std::string& fname )
00473 {
00474 __TRACE();
00475
00476 if ( fname == m_sharedData->m_fileName )
00477 {
00478 return;
00479 }
00480
00481
00482 nvutil::writeAccess( m_sharedData );
00483
00484
00485 setTexture(NULL);
00486
00487 m_sharedData->m_fileName = fname;
00488 m_lookupRequired = true;
00489 }
00490
00491 inline const Texture * TextureAttributeItem::getTexture( void ) const
00492 {
00493 __TRACE();
00494 return( m_texture );
00495 }
00496
00497 inline void TextureAttributeItem::setTexture( const Texture *p ) const
00498 {
00499 __TRACE();
00500 if ( m_texture )
00501 {
00502 m_texture->removeRef();
00503 }
00504 m_texture = p;
00505 if ( m_texture )
00506 {
00507 m_texture->addRef();
00508 }
00509 m_lookupRequired = ! m_texture;
00510 }
00511
00512 inline void TextureAttributeItem::setBorderColor( const nvmath::Vec4f &color )
00513 {
00514 m_sharedData->m_borderColor = color;
00515 }
00516
00517 inline const nvmath::Vec4f& TextureAttributeItem::getBorderColor( void ) const
00518 {
00519 return( m_sharedData->m_borderColor );
00520 }
00521
00522 inline TextureEnvMode TextureAttributeItem::getEnvMode( void ) const
00523 {
00524 __TRACE();
00525 return( m_sharedData->m_envMode );
00526 }
00527
00528 inline void TextureAttributeItem::setEnvMode( TextureEnvMode mode )
00529 {
00530 __TRACE();
00531 nvutil::writeAccess(m_sharedData);
00532 m_sharedData->m_envMode = mode;
00533 }
00534
00535 inline TextureWrapMode TextureAttributeItem::getWrapMode( TexCoordAxis target ) const
00536 {
00537 __TRACE();
00538 return( m_sharedData->m_wrapMode[target] );
00539 }
00540
00541 inline void TextureAttributeItem::setWrapMode( TexCoordAxis target, TextureWrapMode mode )
00542 {
00543 __TRACE();
00544 m_sharedData->m_wrapMode[target] = mode;
00545 }
00546
00547 inline TextureMinFilterMode TextureAttributeItem::getMinFilterMode( void ) const
00548 {
00549 __TRACE();
00550 return( m_sharedData->m_minFilterMode );
00551 }
00552
00553 inline void TextureAttributeItem::setMinFilterMode( TextureMinFilterMode mode )
00554 {
00555 __TRACE();
00556 m_sharedData->m_minFilterMode = mode;
00557 }
00558
00559 inline TextureMagFilterMode TextureAttributeItem::getMagFilterMode( void ) const
00560 {
00561 __TRACE();
00562 return( m_sharedData->m_magFilterMode );
00563 }
00564
00565 inline void TextureAttributeItem::setMagFilterMode( TextureMagFilterMode mode )
00566 {
00567 __TRACE();
00568 m_sharedData->m_magFilterMode = mode;
00569 }
00570
00571 inline TextureType TextureAttributeItem::getType( void ) const
00572 {
00573 __TRACE();
00574 return( m_sharedData->m_type );
00575 }
00576
00577 inline void TextureAttributeItem::setType( TextureType tt )
00578 {
00579 __TRACE();
00580 nvutil::writeAccess(m_sharedData);
00581 if (tt == TT_TRANSPARENT)
00582 {
00583 std::vector<const TextureAttribute*>::const_iterator ci = m_owners.begin();
00584 while (ci != m_owners.end())
00585 {
00586 TextureAttribute * pAttrib = beginEdit(*ci);
00587 pAttrib->invalidateTransparentTextureContainment();
00588 endEdit(pAttrib);
00589
00590 ci++;
00591 }
00592 }
00593 m_sharedData->m_type = tt;
00594 }
00595
00596 inline nvmath::Mat44f TextureAttributeItem::getMatrix( void ) const
00597 {
00598 __TRACE();
00599 return( m_sharedData->m_trafo.getMatrix() );
00600 }
00601
00602 inline const nvmath::Quatf & TextureAttributeItem::getOrientation( void ) const
00603 {
00604 __TRACE();
00605 return( m_sharedData->m_trafo.getOrientation() );
00606 }
00607
00608 inline void TextureAttributeItem::setOrientation( const nvmath::Quatf &orientation )
00609 {
00610 __TRACE();
00611 nvutil::writeAccess(m_sharedData);
00612 m_sharedData->m_trafo.setOrientation(orientation);
00613 }
00614
00615 inline const nvmath::Vec3f & TextureAttributeItem::getScaling( void ) const
00616 {
00617 __TRACE();
00618 return( m_sharedData->m_trafo.getScaling() );
00619 }
00620
00621 inline void TextureAttributeItem::setScaling( const nvmath::Vec3f &scale )
00622 {
00623 __TRACE();
00624 nvutil::writeAccess(m_sharedData);
00625 m_sharedData->m_trafo.setScaling(scale);
00626 }
00627
00628 inline const nvmath::Vec3f & TextureAttributeItem::getTranslation( void ) const
00629 {
00630 __TRACE();
00631 return( m_sharedData->m_trafo.getTranslation() );
00632 }
00633
00634 inline void TextureAttributeItem::setTranslation( const nvmath::Vec3f &trans )
00635 {
00636 __TRACE();
00637 nvutil::writeAccess(m_sharedData);
00638
00639 m_sharedData->m_trafo.setTranslation(trans);
00640 }
00641
00642 inline bool TextureAttributeItem::isLookupRequired( void ) const
00643 {
00644 __TRACE();
00645 __ASSERT((m_lookupRequired==false)==(m_texture!=NULL));
00646 return m_lookupRequired || !m_texture->getHandle();
00647 }
00648
00649 inline void TextureAttributeItem::setTexGenEnvironmentMapping( bool sAxis, bool tAxis )
00650 {
00651 nvutil::writeAccess(m_sharedData);
00652 if ( sAxis )
00653 {
00654 m_sharedData->m_texGenMode[TCA_S] = TGM_SPHERE_MAP;
00655 }
00656 if ( tAxis )
00657 {
00658 m_sharedData->m_texGenMode[TCA_T] = TGM_SPHERE_MAP;
00659 }
00660 }
00661
00662 inline void TextureAttributeItem::setTexGenLinearMapping( TexCoordAxis axis, TexGenLinearMode mode, const nvmath::Plane3f &plane )
00663 {
00664 nvutil::writeAccess(m_sharedData);
00665 m_sharedData->m_texGenMode[axis] = (TexGenMode) mode;
00666 m_sharedData->m_texGenPlane[axis] = plane;
00667 }
00668
00669 inline void TextureAttributeItem::disableTexGen( TexCoordAxis axis )
00670 {
00671 nvutil::writeAccess(m_sharedData);
00672 m_sharedData->m_texGenMode[axis] = TGM_OFF;
00673 }
00674
00675 inline TexGenMode TextureAttributeItem::getTexGenMode( TexCoordAxis axis ) const
00676 {
00677 return( m_sharedData->m_texGenMode[axis] );
00678 }
00679
00680 inline const nvmath::Plane3f &TextureAttributeItem::getTexGenPlane( TexCoordAxis axis ) const
00681 {
00682 return( m_sharedData->m_texGenPlane[axis] );
00683 }
00684 }