material.h

Go to the documentation of this file.
00001 
00002 //**************************************************************************/
00003 // Copyright (c) 2008 - 2010 Autodesk, Inc.
00004 // All rights reserved.
00005 //
00006 // Use of this software is subject to the terms of the Autodesk license
00007 // agreement provided at the time of installation or download, or which
00008 // otherwise accompanies this software in either electronic or hard copy form.
00009 //
00010 //**************************************************************************/
00011 // DESCRIPTION: Mudbox Plugin API header -- Material and Texture classes.
00012 // CREATED: October 2008
00013 //**************************************************************************/
00014 
00015 namespace mudbox {
00016 //------------------------------------------------------------------------------    
00073 class MBDLL_DECL Material : virtual public TreeNode
00074 {
00075     DECLARE_CLASS;
00076 
00081     enum VertexDataUsage
00082     {
00086         eVDUPosition,    
00087         eVDUNormal,      
00088         eVDUTextureCoordinate, 
00089         eVDUVertexColor, 
00090         eVDUBoneIndices, 
00091         eVDUBoneWeights, 
00092 
00093         eVDUTangent,     
00094         eVDUBinormal,    
00095 
00096 
00100         eVDUDirectTextureCoordinate0, 
00101         eVDUDirectTextureCoordinate1, 
00102         eVDUDirectTextureCoordinate2, 
00103         eVDUDirectTextureCoordinate3, 
00104         eVDUDirectTextureCoordinate4, 
00105         eVDUDirectTextureCoordinate5, 
00106         eVDUDirectTextureCoordinate6, 
00107         eVDUDirectTextureCoordinate7, 
00108 
00109 
00110         eVDUNotUsed 
00111     };
00112 
00113 protected:
00119     Material( void );  
00120 
00121 public:
00122     ~Material();
00123 
00130     virtual bool Activate(
00131         const Mesh *pMesh,                      
00132         const AxisAlignedBoundingBox &cUVArea,  
00133 
00134         const Color &cColor = Color::white      
00135         );
00136     
00138     virtual void Deactivate( void );
00139 
00149     virtual float Transparency( void ) const;
00150     
00153     virtual void SetTransparency(
00154         float fTransparency         
00155         );
00156 
00168     virtual bool TexturesVisible( void ) const;
00169     
00171     virtual void SetTexturesVisible(
00172         bool bVisible   
00173         );
00174 
00181     virtual bool IsTCNeeded( void ) const;
00182     
00189     virtual bool IsTNBNeeded( void ) const;
00190 
00192     virtual unsigned int TextureCount( void ) const;
00193     
00195     virtual class TexturePool *Texture(
00196         unsigned int iTextureIndex      
00197         ) const;
00198 
00209     virtual void SetTextureTileLive(
00210         const AxisAlignedBoundingBox &cUVArea,  
00211         bool bLive,                 
00212         int iLODDelta = 0       
00213         );
00214     
00216     virtual bool IsTextureTileLive(
00217         const AxisAlignedBoundingBox &cUVArea   
00218         );
00219     
00221     virtual unsigned int TextureTileCount() const;
00222     
00224     virtual AxisAlignedBoundingBox TextureTileArea(
00225         unsigned int iTileIndex     
00226         ) const;
00227 
00234     virtual void ActivateUVArea( const AxisAlignedBoundingBox &cArea );
00235 
00240     virtual VertexDataUsage MapVertexData( 
00241         VertexDataUsage eUsage 
00242 
00243         ) const;
00244 };
00245 
00246 
00247 //-----------------------------------------------------------------------------
00249 class MBDLL_DECL TextureMixer : public Node
00250 {
00251     DECLARE_CLASS;
00252 
00253 protected:
00259     TextureMixer( void );
00260 
00261 public:
00263     virtual unsigned int BlendModeCount( void ) const;
00264     
00266     virtual QString BlendModeDisplayName(
00267         int iBlendMode      
00268         ) const;
00269 
00271     virtual QString BlendModeName(
00272         int iBlendMode      
00273         ) const;
00274 
00275     virtual void SetBlendMode( int iBlendMode ) ;
00276 
00278     virtual void SetLayerCount(
00279         unsigned int iLayerCount    
00280         );
00281     
00283     virtual void SetLayerData(
00284         unsigned int iLayerIndex,           
00285         int iTextureID,                     
00286         float fTransparency,                
00287         int iMaskID = 0,                    
00288         float fMaskTransparency = 1.0f      
00289         );
00290     
00292     virtual void Mix(
00293         class Texture *d,    
00294         unsigned int iWidth,        
00295         unsigned int iHeight,       
00296         enum Image::Format eFormat  
00297         );
00298 
00299 };
00300 
00301 //-----------------------------------------------------------------------------
00306 class MBDLL_DECL TextureTileArray {
00307     private:
00308         // this describes a texture tile. It has a pointer to the texture and 
00309         // a small amount of frequently accessed metadata. This object should 
00310         // be kept as small as possible, as there will be a 2D, non-sparse 
00311         // array of these kept my the texture pool. 
00312         // presently 16 bytes on a 64 bit machine, 12 bytes on a 32 bit machine. 
00313         class TileDescriptor {  
00314             public:
00315                 TileDescriptor() : m_TileStateIndex(-1) {}
00316                 int           m_TileStateIndex;    
00317         };
00318 
00319 
00320         float  m_UOrigin, m_VOrigin; // origin (may be negative) in UV space of this array. 
00321         int    m_USize, m_VSize;     // Size in U and V covered by this tile array
00322 
00323         TileDescriptor *m_Tiles;     // pointer to the tile array - declared as 1D, but
00324                                      // accessed as a 2D array by tileFromUV
00325 
00326         // given UV coordinates, return the TileDescriptor
00327         TileDescriptor *tileFromUV(float U, float V) const
00328         {
00329             U -= m_UOrigin; V -= m_VOrigin;     // bring to 0 origin
00330             int u = (int)U, v = (int)V;         // same as floor now that they are positive
00331             return &m_Tiles[(v * m_USize) + u]; // classic 2D array access math.
00332         }
00333 
00334         // resize the TileArray to include these UV coordinates.
00335         void     resizeArrayToInclude(float U, float V);
00336 
00337         // initialize the class.
00338         void init()
00339         {
00340             m_UOrigin, m_VOrigin = 0;  
00341             m_USize, m_VSize = 0;      
00342             m_Tiles = NULL;
00343         }
00344 
00345     public : 
00346 
00347         TextureTileArray()  { init();    }
00348         ~TextureTileArray() { dropAll(); } 
00349 
00352         float getMinU() const { return m_UOrigin; }
00355         float getMinV() const { return m_VOrigin; }
00358         float getMaxU() const { return m_UOrigin + (float)m_USize; }
00361         float getMaxV() const { return m_VOrigin + (float)m_VSize; }
00362 
00364         void getTotalUVBounds(float &minU, float &minV, float &maxU, float &maxV) const
00365         {
00366             minU = m_UOrigin; 
00367             minV = m_VOrigin; 
00368             maxU = minU + (float)m_USize;
00369             maxV = minV + (float)m_VSize;
00370         }
00371 
00374         bool contains(float U, float V) const
00375         {
00376             return m_Tiles && U >= getMinU() && U < getMaxU() && V >= getMinV() && V < getMaxV();
00377         }
00378 
00379 
00380         int getTileStateIndex(float U, float V) const 
00381         {
00382             if (!contains(U, V)) return -1; else
00383             return tileFromUV(U, V)->m_TileStateIndex;
00384         }
00385 
00386         void setTileStateIndex(float U, float V, int indx) 
00387         {
00388             if (!contains(U, V)) resizeArrayToInclude(U, V);
00389             tileFromUV(U, V)->m_TileStateIndex = indx;
00390         }
00391 
00392 
00397         void dropAll();
00398 
00399         void clearTileStateIndices(); 
00400 };
00401 
00402 
00403 //-----------------------------------------------------------------------------
00419 class MBDLL_DECL TexturePool : virtual public Node
00420 {
00421     DECLARE_CLASS;
00422 
00423 protected:
00429     TexturePool( void );
00430 
00431     TextureTileArray     m_TextureTileArray;
00432 
00433 public:
00434 
00435     enum RenderMode
00436     {
00437         renderModeConstantColor,
00438         renderModeTexture,
00439         renderModeVertexColor
00440     };
00441 
00442     enum Location
00443     {
00444         locationUnknown,
00445         locationGPU,
00446         locationCPU,
00447         locationDisk
00448     };
00449 
00450     enum Usage
00451     {
00452         usagePaintLayer,
00453         usageLayerMask,
00454         usageAdjustmentLayer,
00455         usageUnknown
00456     };
00457 
00467     virtual enum Usage Usage() const;
00468 
00470     virtual void SetUsage( enum Usage eUsage );
00471 
00473     virtual QString Name( void ) const;
00474     
00476     virtual void SetName(
00477         const QString &sName    
00478 
00479         );
00480     
00482     virtual unsigned int RenderMode( void ) const;
00483     
00485     virtual void SetRenderMode( unsigned int iMode );
00486     
00488     virtual void SetLocation( unsigned int iLocation );
00489     
00491     virtual unsigned int Location( void ) const;
00492     
00493     virtual TexturePool &operator =( TexturePool &cT );
00494 
00504     virtual int Width( void ) const;
00505     
00507     virtual void SetWidth( int iWidth );
00508     
00517     virtual void SetFileName(
00518         const QString &sFileName        
00519         );
00520     
00523     virtual const QString& FileName( void ) const;
00524     
00528     virtual void Save(
00529         const QString &sFileName = "",  
00530         const QString &sFormat = "",    
00531 
00532 
00533         Material *m = NULL,             
00534         bool bForced = false            
00535         );          
00536 
00539     virtual void Export(
00540         const QString &sFileName,       
00541         const QString &sFormat = "",        
00542 
00543 
00544         Material *m = NULL             
00545         );
00546 
00550     virtual void SetDirty(
00551         bool bDirty     
00552         );
00553     
00555     virtual bool IsDirty( void ) const;
00556     
00558     virtual void AddToDirtyArea( 
00559         const Vector &cPoint    
00560     );
00561 
00563     virtual void Reload( void );
00564     
00566     virtual void SetContentChanged( void );
00567     
00569     virtual bool IsContentChanged( void );
00570 
00572     virtual unsigned int TileCount( void ) const;
00573     
00575     virtual class Texture *Tile(
00576         unsigned int iTileIndex     
00577         );
00578     
00581     virtual class Texture *Tile(
00582         const AxisAlignedBoundingBox &cTCArea,  
00583         bool bAlloc = true                      
00584 
00585         );
00586 
00590     virtual class Texture *Tile(float U, float V, bool bAlloc = true);
00591     
00593     virtual AxisAlignedBoundingBox TileArea(
00594         unsigned int iTileIndex     
00595         ) const;
00596 
00597     TextureTileArray  *getTextureTileArray() { return &m_TextureTileArray; }
00598     
00600     virtual TextureMixer *Mixer( void ) const;
00601     
00603     virtual int BlendMode( void ) const;
00604     
00606     virtual void SetBlendMode(
00607         int iMode       
00608 
00609         );
00610 
00611     virtual void SetMaterial(
00612         Material* pMaterial 
00613         );
00614 
00615     virtual Material *GetMaterial() const; 
00616 
00617     // Internal use only.
00618     virtual TexturePool *Buffer( void ) const;
00619 
00621     virtual qint64 GPUMemoryUsageInBytes( void ) const;
00622 };
00623 
00624 //-----------------------------------------------------------------------------
00632 class MBDLL_DECL Texture : virtual public Node
00633 {
00634     Q_DECLARE_TR_FUNCTIONS(Texture);
00635     DECLARE_CLASS;
00636 
00637 protected:
00643     Texture( void );
00644 
00645 public:
00647     virtual void Create(
00648         unsigned int iWidth,            
00649         unsigned int iHeight,           
00650         unsigned int iChannelCount,     
00651         enum Image::Format eFormat,     
00652         const Color &cColor             
00653         );
00654     
00656     virtual void Create(
00657         unsigned int iWidth,            
00658         unsigned int iHeight,           
00659         unsigned int iChannelCount,     
00660         enum Image::Format eFormat      
00661         );
00662     
00664     virtual bool CreateFromFile(
00665         const QString &sFileName        
00666         );
00667     
00669     virtual const Image *AsImage( void );
00670 
00673     virtual void CopyTo(
00674         Texture &cTexture       
00675         ) const;
00676 
00678     virtual unsigned int Width( void ) const;
00679     
00681     virtual unsigned int Height( void ) const;
00682     
00684     virtual unsigned int BitDepth( void ) const;
00685     
00687     virtual enum Image::Format Format( void ) const;
00688 
00693     virtual bool Activate( void ) const;
00694     
00697     virtual bool Deactivate( void ) const;
00698 
00700     virtual unsigned int OpenGLName( void ) const;
00701 
00703     virtual bool SetAsRenderTarget( void );
00704     
00706     virtual bool RestoreRenderTarget( void );
00707 
00708     virtual abool *getFilteringPref();
00709 
00711     virtual void CopyFrom(
00712         Image *pImage       
00713         );
00714 
00716     virtual void ReplaceWith(
00717         Image *pImage       
00718         );
00719     
00722     virtual void CopyTo(
00723         Image *pImage,                  
00724         bool tiled = true,              
00725         const ImgTile *region = 0,      
00726         ImageDescriptor::MemoryChannelOrder eOrder = ImageDescriptor::orderRGBA     
00727 
00728         );
00729 
00731     virtual void SetLocation( 
00732         unsigned int iLocation      
00733 
00734         );
00735     
00740     virtual unsigned int Location( void ) const;
00741 
00743     virtual int64 TotalMemoryUsage( void ) const;
00744 
00746     virtual qint64 GPUMemoryUsageInBytes() const;
00747 
00749     virtual void SetLive(
00750         bool bLive
00751         );
00752     
00754     virtual bool IsLive( void ) const;
00755 
00758 
00759 
00767         virtual bool NewVersion();                                      
00768 
00775         virtual bool PrevVersion();
00776 
00783         virtual bool NextVersion();
00784 
00787         virtual int  NumVersions();
00788 
00793         virtual bool MergeOldestVersions();
00794 
00802         virtual bool PurgeNewerVersions();
00803 
00806         virtual bool PurgeAllButCurrentVersion();
00807 
00813         virtual bool BeginUndoableOperation();
00814 
00817         virtual bool EndUndoableOperation();
00818 
00820 
00822         virtual void SetFiltered( 
00823             bool bFiltered 
00824             );
00825 
00828         virtual void SetConvert32To16bitFloat( 
00829             bool bConvert 
00830             );
00831 
00833         virtual void setProxyLevel(unsigned char level);
00834 
00836         virtual unsigned char getProxyLevel() const; 
00837 
00838 protected:
00839     ImgTile   m_DirtyTile;
00840 
00841 public:
00842 
00844     virtual bool IsDirty( void ) const;
00845 
00847     virtual void SetDirty( bool bDirty );
00848 
00853     
00855 
00857         void ResetDirtyTile() 
00858         {
00859             m_DirtyTile = ImgTile(0, 0, 0, 0);       
00860         }
00861 
00866         virtual void AddToDirtyTile(
00867             int x,  
00868             int y   
00869             )
00870         {
00871             m_DirtyTile.ExpandToInclude(x, y);
00872         }
00873 
00879         virtual void AddToDirtyTile(
00880             const ImgTile &t 
00881             )
00882         {
00883             m_DirtyTile.ExpandToInclude(t.x, t.y);
00884             m_DirtyTile.ExpandToInclude(t.x+t.nx, t.y+t.ny);
00885         }
00886 
00888         const ImgTile &DirtyTile() const { return m_DirtyTile; }
00889 
00891 };
00892 
00893 
00894 //-----------------------------------------------------------------------------
00901 class MBDLL_DECL RenderTarget : public Node
00902 {
00903     DECLARE_CLASS;
00904 
00905     enum Slot
00906     {
00907         eSlotColor0 = 0,
00908         eSlotColor1,
00909         eSlotColor2,
00910         eSlotColor3,
00911         eSlotDepth,
00912         eSlotCount      // placeholder for last position - not a type of slot
00913     };
00914 
00915 public:
00918     virtual void DrawTexture( 
00919         const class Texture *pTexture
00920         );
00921     
00924     virtual bool Attach(
00925         mudbox::Texture *pTexture,  
00926         Slot eSlot = eSlotColor0    
00927         );
00928     
00930     virtual void Detach(
00931         Slot eSlot = eSlotColor0    
00932         );
00933     
00935     virtual mudbox::Texture *Texture(
00936         Slot eSlot = eSlotColor0    
00937         ) const;
00938     
00940     virtual unsigned int Width( void ) const;
00941     
00943     virtual unsigned int Height( void ) const;
00944     
00946     virtual void SetSize(
00947         unsigned int iWidth,
00948         unsigned int iHeight
00949         );
00950 
00952     virtual void Activate( void );
00953     
00955     virtual void Restore( void );
00956 
00958     virtual void SetMultisampling( bool bMultisampling );
00959 };
00960 
00961 
00962 
00963 
00964 
00965 }; // end of namespace mudbox
00966