Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

RmMesh.h

00001 //=============================================================================
00002 // filename: RmMesh.h                                                          
00003 //                                                                             
00004 //           ATI Research, Inc.                                                
00005 //           3D Application Research Group                                     
00006 //                                                                             
00007 // Description: RenderMonkey's Mesh Definition                                 
00008 //                                                                             
00009 //=============================================================================
00010 //   (C) 2004 ATI Research, Inc.  All rights reserved.                         
00011 //=============================================================================
00012 
00013 #ifndef _RM_CORE_MESH_H_
00014 #define _RM_CORE_MESH_H_
00015 
00016 #include <Core/RmTypes.h>
00017 #include <Core/RmDefines.h>
00018 #include <Core/RmEffect.h>
00019 #include <Core/RmMath.h>
00020 #include <Core/RmMatrix.h>
00021 
00022 //=============================================================================
00023 // 
00024 // Explanation :
00025 //
00026 //   Mesh class is there to help a programmer to keep information about geometry    
00027 //   in a nicely packed manner. 
00028 //
00029 //   To add new vertex element to Mesh, simply do ( assuming pMesh is RmMeshModel type ):
00030 //
00031 //      pMesh->GetVertexArray()->CreateVertexElementArray(RM_DECLUSAGE_NORMAL,
00032 //                                                        1,
00033 //                                                        RMVSDT_FLOAT3);
00034 //
00035 //      This will add NORMAL1 channel to VertexArray ( with FLOAT3 data type )
00036 //      You can also change datatype of given element, in same way.
00037 //   
00038 //   Then to use Element,
00039 //
00040 //      RmVertexElementArray *pEltArray = 
00041 //         pMesh->GetVertexArray()->GetVertexElementArray(RM_DECLUSAGE_NORMAL,1,RMVSDT_FLOAT3);
00042 //      
00043 //      float *pNormals = (float*)pEltArray->GetBuffer(); 
00044 //
00045 //      // now set Normal of nIndex 15 to 0,1,0
00046 //      pNormals[3*15+0] = 0.0f; // we know that it's a float3 type
00047 //      pNormals[3*15+1] = 1.0f;
00048 //      pNormals[3*15+2] = 0.0f;
00049 //
00050 //       or 
00051 //
00052 //      float *pNormals = (float*)pEltArray->GetBuffer(15);
00053 //      pNormals[0] = 0.0f;
00054 //      pNormals[1] = 1.0f;
00055 //      pNormals[2] = 0.0f;
00056 //
00057 //
00058 //   If you want to change size of vertices,
00059 //
00060 //      pMesh->GetVertexArray()->SetNumVertices(150,true);
00061 //   
00062 //   This will resize ALL element arrays to given size and if bPreserve is true,
00063 //   reszied data has same values from old data.
00064 //
00065 //   You can also destroy certain element array
00066 //
00067 //      pMesh->GetVertexArray()->DestroyVertexElementArray(RM_DECLUSAGE_NORMAL,1,RMVSDT_FLOAT3);
00068 //   
00069 //   ****** Note *******
00070 //
00071 //     It is Possible to have element array with same usage and same usae nIndex, but diffrent
00072 //     data type.   
00073 //
00074 //     Eg.   Mesh may contain two ( or more ) POSITION0.  One with FLOAT3, and other with UBYTE4 etc.
00075 //     
00076 //=============================================================================
00077 
00078 
00079 //=============================================================================
00080 // Helper Function
00081 //=============================================================================
00082 RM_API int RmGetElementSize( RmStreamChannelDataType dataType );
00083 
00084 //=============================================================================
00089 //=============================================================================
00090 class RM_API RmVertexElementArray
00091 {
00092 friend class RmVertexArray;
00093 
00094 public :
00095    //--------------------------------------------------------------------------
00104    //--------------------------------------------------------------------------
00105    RmVertexElementArray( RmStreamChannelUsageType usage, int nUsageIndex, 
00106                          RmStreamChannelDataType  dataType );
00107    //--------------------------------------------------------------------------
00111    //--------------------------------------------------------------------------
00112    virtual ~RmVertexElementArray();
00113 
00114    //--------------------------------------------------------------------------
00120    //--------------------------------------------------------------------------
00121    int  GetNumVertices() const { return m_nNumVertices; };     
00122 
00123    //--------------------------------------------------------------------------
00124    // Infomation Accessors
00125    //--------------------------------------------------------------------------
00126    
00127    //--------------------------------------------------------------------------
00133    //--------------------------------------------------------------------------
00134    RmStreamChannelUsageType GetUsage() const { return m_usage; };
00135    
00136    //--------------------------------------------------------------------------
00142    //--------------------------------------------------------------------------
00143    int                      GetUsageIndex() const { return m_nUsageIndex; };
00144 
00145    //--------------------------------------------------------------------------
00151    //--------------------------------------------------------------------------
00152    int                      GetStridePerVertex() const { return m_stride; };
00153    
00154    //--------------------------------------------------------------------------
00160    //--------------------------------------------------------------------------
00161    RmStreamChannelDataType  GetDataType() const { return m_dataType; };
00162 
00163    //--------------------------------------------------------------------------
00169    //--------------------------------------------------------------------------
00170    RM_BYTE* GetBuffer() { return m_pBuffer; };
00171 
00172    //--------------------------------------------------------------------------
00178    //--------------------------------------------------------------------------
00179    RM_BYTE* GetBuffer() const { return m_pBuffer; };
00180 
00181    //--------------------------------------------------------------------------
00188    //--------------------------------------------------------------------------
00189    RM_BYTE* GetBuffer( int nVertexIndex );
00190    const RM_BYTE* GetBuffer( int nVertexIndex ) const;
00191 
00192 private :
00193    //--------------------------------------------------------------------------
00200    //--------------------------------------------------------------------------
00201    void SetNumVertices( int nNumVertices, bool bPreserve = true );
00202 
00203 private :
00204    RmStreamChannelUsageType m_usage;   
00205    int               m_nUsageIndex;    
00206    RM_BYTE          *m_pBuffer;        
00207    int               m_nNumVertices;   
00208    int               m_stride;         
00209    RmStreamChannelDataType  m_dataType; 
00210 
00211 }; // End of RmVertexElementArray
00212 
00213 typedef RmLinkedList<RmVertexElementArray*>          RmVertexElementArrayList;
00214 typedef RmVertexElementArrayList::iterator           RmVertexElementArrayListIterator;
00215 typedef RmVertexElementArrayList::const_iterator     RmVertexElementArrayListConstIterator;
00216 
00217 
00218 //=============================================================================
00224 //=============================================================================
00225 class RM_API RmVertexArray
00226 {
00227 public :
00228    //--------------------------------------------------------------------------
00232    //--------------------------------------------------------------------------
00233    RmVertexArray();
00234 
00235    //--------------------------------------------------------------------------
00239    //--------------------------------------------------------------------------
00240    virtual ~RmVertexArray();
00241 
00242    //--------------------------------------------------------------------------
00243    // Element Array Functions
00244    //--------------------------------------------------------------------------
00245    
00246    //--------------------------------------------------------------------------
00255    //--------------------------------------------------------------------------
00256    bool CreateVertexElementArray( RmStreamChannelUsageType usage, int nUsageIndex,
00257                                   RmStreamChannelDataType dataType );
00258                                   
00259    //--------------------------------------------------------------------------
00268    //--------------------------------------------------------------------------
00269    void DestroyVertexElementArray( RmStreamChannelUsageType usage, int nUsageIndex, RmStreamChannelDataType dataType );
00270    
00271    //--------------------------------------------------------------------------
00278    //--------------------------------------------------------------------------
00279    void DestroyAllVertexElementArrays();
00280 
00281    //--------------------------------------------------------------------------
00290    //--------------------------------------------------------------------------
00291    RmVertexElementArray* GetVertexElementArray( RmStreamChannelUsageType usage, int nUsageIndex, RmStreamChannelDataType dataType );
00292 
00293    //--------------------------------------------------------------------------
00302    //--------------------------------------------------------------------------
00303    const RmVertexElementArray* GetVertexElementArray( RmStreamChannelUsageType usage, int nUsageIndex, RmStreamChannelDataType dataType ) const;
00304 
00305    //--------------------------------------------------------------------------
00312    //--------------------------------------------------------------------------
00313    void SetNumVertices( int nNumVertices, bool bPreserve );
00314    
00315    //--------------------------------------------------------------------------
00321    //--------------------------------------------------------------------------
00322    int  GetNumVertices() const { return m_nNumVertices; };
00323 
00324    //--------------------------------------------------------------------------
00330    //--------------------------------------------------------------------------
00331    RmVertexElementArrayListIterator      BeginElementArray() { return m_elements.begin(); };
00332 
00333    //--------------------------------------------------------------------------
00339    //--------------------------------------------------------------------------
00340    RmVertexElementArrayListConstIterator BeginElementArray() const { return m_elements.begin(); };
00341 
00342    //--------------------------------------------------------------------------
00348    //--------------------------------------------------------------------------
00349    RmVertexElementArrayListIterator      EndElementArray() { return m_elements.end(); };
00350 
00351    //--------------------------------------------------------------------------
00357    //--------------------------------------------------------------------------
00358    RmVertexElementArrayListConstIterator EndElementArray() const { return m_elements.end(); };
00359 
00360    //-----------------------------------------------------------------
00366    //-----------------------------------------------------------------
00367    void Copy( RmVertexArray *pDestArray ); 
00368 
00369 private :
00370    int                       m_nNumVertices; 
00371    RmVertexElementArrayList  m_elements;     
00372 }; // End of RmVertexArray
00373 
00374 
00375 //=============================================================================
00381 //=============================================================================
00382 class RmMeshModel;
00383 
00384 typedef RmLinkedList<RmMeshModel*>      RmMeshModelList;
00385 typedef RmMeshModelList::iterator       RmMeshModelListIterator;
00386 typedef RmMeshModelList::const_iterator RmMeshModelListConstIterator;
00387 
00388 class RM_API RmMeshModel
00389 {
00390 public :
00391    //--------------------------------------------------------------------------
00396    //--------------------------------------------------------------------------
00397    RmMeshModel();
00398 
00399    //--------------------------------------------------------------------------
00404    //--------------------------------------------------------------------------
00405    virtual ~RmMeshModel();
00406 
00407    //--------------------------------------------------------------------------
00418    //--------------------------------------------------------------------------
00419    void GetGeometryStatistics( int &nNumVertices, int &nNumPrimitives );
00420 
00421    //--------------------------------------------------------------------------
00427    //--------------------------------------------------------------------------
00428    RmVertexArray* GetVertexArray() { return &m_vertexArray; };
00429 
00430    //--------------------------------------------------------------------------
00436    //--------------------------------------------------------------------------
00437    const RmVertexArray* GetVertexArray() const { return &m_vertexArray; };
00438 
00439    //--------------------------------------------------------------------------
00446    //--------------------------------------------------------------------------
00447    void            SetPrimitiveType( RmPrimitiveType type ) { m_primitiveType = type; };
00448 
00449    //--------------------------------------------------------------------------
00455    //--------------------------------------------------------------------------
00456    RmPrimitiveType GetPrimitiveType() const { return m_primitiveType; };
00457 
00458    //--------------------------------------------------------------------------
00464    //--------------------------------------------------------------------------
00465    int             GetNumPrimitives() const;
00466 
00467    //--------------------------------------------------------------------------
00475    //--------------------------------------------------------------------------
00476    void SetNumIndices( int nNumIndices, bool bPreserve = true );
00477 
00478    //--------------------------------------------------------------------------
00484    //--------------------------------------------------------------------------
00485    int  GetNumIndices() const { return m_nNumIndices; };
00486 
00487    //--------------------------------------------------------------------------
00493    //--------------------------------------------------------------------------
00494    RM_DWORD*       GetIndices()       { return m_pIndices; };
00495 
00496    //--------------------------------------------------------------------------
00502    //--------------------------------------------------------------------------
00503    const RM_DWORD* GetIndices() const { return m_pIndices; };
00504 
00505    //--------------------------------------------------------------------------
00515    //--------------------------------------------------------------------------
00516    void  GetIndicesForPrimitive( int nPrimitiveIndex, RM_DWORD *pIndices );
00517 
00518    //--------------------------------------------------------------------------
00524    //--------------------------------------------------------------------------
00525    void               SetParent( RmMeshModel *pParent );
00526 
00527    //--------------------------------------------------------------------------
00533    //--------------------------------------------------------------------------
00534    RmMeshModel*       GetParent() { return m_pParent; };
00535 
00536    //--------------------------------------------------------------------------
00542    //--------------------------------------------------------------------------
00543    const RmMeshModel* GetParent() const { return m_pParent; };
00544 
00545    //--------------------------------------------------------------------------
00546    // Hierarchy Iterators
00547    //--------------------------------------------------------------------------
00548    
00549    //--------------------------------------------------------------------------
00555    //--------------------------------------------------------------------------
00556    RmMeshModelListIterator      BeginChildren() { return m_children.begin(); };
00557 
00558    //--------------------------------------------------------------------------
00564    //--------------------------------------------------------------------------
00565    RmMeshModelListConstIterator BeginChildren() const { return m_children.begin(); };
00566 
00567    //--------------------------------------------------------------------------
00573    //--------------------------------------------------------------------------
00574    RmMeshModelListIterator      EndChildren() { return m_children.end(); };
00575 
00576    //--------------------------------------------------------------------------
00582    //--------------------------------------------------------------------------
00583    RmMeshModelListConstIterator EndChildren() const { return m_children.end(); };
00584 
00585    //--------------------------------------------------------------------------
00591    //--------------------------------------------------------------------------
00592    int GetNumChildren() const { return m_children.size(); };
00593 
00594    //--------------------------------------------------------------------------
00601    //--------------------------------------------------------------------------
00602    void                    AddChild( RmMeshModel *pChild );
00603 
00604    //--------------------------------------------------------------------------
00612    //--------------------------------------------------------------------------
00613    void                    RemoveChild( RmMeshModel *pChild, bool bDelete );
00614 
00615    //--------------------------------------------------------------------------
00623    //--------------------------------------------------------------------------
00624    RmMeshModelListIterator RemoveChild( RmMeshModelListIterator itr, bool bDelete );
00625 
00626    //--------------------------------------------------------------------------
00632    //--------------------------------------------------------------------------
00633    void                    RemoveAllChildren();
00634 
00635    //--------------------------------------------------------------------------
00641    //--------------------------------------------------------------------------
00642    RmMatrix4x4&       GetLocalTM() { return m_localTM; };
00643    const RmMatrix4x4& GetLocalTM() const { return m_localTM; };
00644 
00645    //--------------------------------------------------------------------------
00651    //--------------------------------------------------------------------------
00652    const RmMatrix4x4& GetWorldTM();
00653 
00654    //--------------------------------------------------------------------------
00660    //--------------------------------------------------------------------------
00661    void NotifyTMChange();
00662 
00663    //--------------------------------------------------------------------------
00683    //--------------------------------------------------------------------------
00684    bool ComputeNormals( int usageIndex,
00685                             int *pSortedIndices = NULL,
00686                         bool bBlendDegenVertices = false );
00687 
00688    //--------------------------------------------------------------------------
00699    //--------------------------------------------------------------------------
00700    bool ComputeTangents( int usageIndex,
00701                              int *pSortedIndices = NULL );
00702 
00703    //--------------------------------------------------------------------------
00713    //--------------------------------------------------------------------------
00714    int* CreateSortedVertexIndices( RmVertexElementArray *pPosElementtArray );
00715 
00716    //--------------------------------------------------------------------------
00722    //--------------------------------------------------------------------------
00723    void DestroySortedVertexIndices( int *pSortedIndices );
00724 
00725    //--------------------------------------------------------------------------
00733    //--------------------------------------------------------------------------
00734    void Copy( RmMeshModel *pDestMesh, RmMeshModelContainer *pContainer ); 
00735 
00736    //--------------------------------------------------------------------------
00743    //--------------------------------------------------------------------------
00744    void SetBoundBoxDirty( bool bDirty );
00745 
00746    //--------------------------------------------------------------------------
00752    //--------------------------------------------------------------------------
00753    void UpdateBoundBox();
00754 
00755    //--------------------------------------------------------------------------
00761    //--------------------------------------------------------------------------
00762    RmBoundBox&       GetBoundBox();
00763 
00764    //--------------------------------------------------------------------------
00770    //--------------------------------------------------------------------------
00771    bool GetBoundSphere( RmVector3D &center, float &radius );
00772 
00773    //--------------------------------------------------------------------------
00780    //--------------------------------------------------------------------------
00781    void SetCentroidDirty( bool bDirty );
00782 
00783    //--------------------------------------------------------------------------
00789    //--------------------------------------------------------------------------
00790    void UpdateCentroid();
00791 
00792    //--------------------------------------------------------------------------
00799    //--------------------------------------------------------------------------
00800    RmVector3D& GetCentroid();
00801 
00802    //--------------------------------------------------------------------------
00809    //--------------------------------------------------------------------------
00810    RmVector3D GetSumOfAllVertices( int &totalNumVertices );
00811 
00812    //--------------------------------------------------------------------------
00823    //--------------------------------------------------------------------------
00824    void TransformVertices( const RmMatrix4x4 &mat );
00825 
00826 private :   
00827    char            m_name[RM_NAME_SIZE]; 
00828 
00829    RmMeshModel    *m_pParent;            
00830 
00831    bool            m_bBoundBoxDirty;     
00832    RmBoundBox      m_boundBox;           
00833 
00834    bool            m_bCentroidDirty;     
00835    RmVector3D      m_centroid;           
00836 
00837    RmVertexArray   m_vertexArray;        
00838 
00839    RmPrimitiveType m_primitiveType;      
00840 
00841    int             m_nNumIndices;        
00842    RM_DWORD       *m_pIndices;           
00843 
00844    RmMeshModelList m_children;           
00845 
00846    bool            m_bWorldTMUpdated;    
00847 
00848    RmMatrix4x4     m_localTM;            
00849    RmMatrix4x4     m_worldTM;            
00850 
00851 private :
00852    //--------------------------------------------------------------------------
00855    //  Called to update the world transformation matrix
00858    //--------------------------------------------------------------------------
00859    void UpdateWorldTM();
00860 
00861 
00862    //--------------------------------------------------------------------------
00871    //--------------------------------------------------------------------------
00872    void FixDegeneratedVerticesForNormal( RmVertexElementArray *pPosEltArray, 
00873                                              RmVertexElementArray *pNormalEltArray,
00874                                                                              int *pSortedIndices );
00875 
00876    //--------------------------------------------------------------------------
00895    //--------------------------------------------------------------------------
00896    void AccumulateTangentSpacePerTriangle( float* v0, float* v1, float* v2, 
00897                                            float* t0, float* t1, float* t2, 
00898                                            float* tan0, 
00899                                            float* tan1, 
00900                                            float* tan2, 
00901                                            float* binormal0, 
00902                                            float* binormal1, 
00903                                            float* binormal2);
00904    //--------------------------------------------------------------------------
00913    //--------------------------------------------------------------------------
00914    void FixDegeneratedVerticesForTangentSpace( RmVertexElementArray *pPosEltArray, 
00915                                                RmVertexElementArray *pTangentEltArray,
00916                                                RmVertexElementArray *pBinormalEltArray,
00917                                                                            int *pSortedIndices );
00918 
00919    //--------------------------------------------------------------------------
00929    //--------------------------------------------------------------------------
00930    void CreateSharedCountTable( int *pSharedCountTable );
00931 }; // End of RmMeshModel
00932 
00933 
00934 //=============================================================================
00940 //=============================================================================
00941 class RM_API RmMeshModelContainer
00942 {
00943 public :
00944    //--------------------------------------------------------------------------
00949    //--------------------------------------------------------------------------
00950    RmMeshModelContainer();
00951 
00952    //--------------------------------------------------------------------------
00957    //--------------------------------------------------------------------------
00958    virtual ~RmMeshModelContainer();
00959 
00960    //--------------------------------------------------------------------------
00968    //--------------------------------------------------------------------------
00969    virtual RmMeshModel* CreateNewMeshModel();
00970 
00971    //--------------------------------------------------------------------------
00979    //--------------------------------------------------------------------------
00980    void    AddMeshModel( RmMeshModel* pModel );
00981 
00982    //--------------------------------------------------------------------------
00985    // Destroy mesh model
00989    //--------------------------------------------------------------------------
00990    void    DestroyMeshModel( RmMeshModel* pModel );
00991 
00992    //--------------------------------------------------------------------------
00998    //--------------------------------------------------------------------------
00999    void    DestroyAllMeshModels();
01000 
01001    //--------------------------------------------------------------------------
01012    //--------------------------------------------------------------------------
01013    void GetGeometryStatistics( int &nNumVertices, int &nNumPrimitives );
01014 
01015    //--------------------------------------------------------------------------
01016    // Accessor of Models
01017    //--------------------------------------------------------------------------
01018    
01019    //--------------------------------------------------------------------------
01022    // Returns an iterator that points to the beginning of the model list
01025    //--------------------------------------------------------------------------
01026    RmMeshModelListIterator      BeginModel() { return m_modelList.begin(); };
01027 
01028    //--------------------------------------------------------------------------
01034    //--------------------------------------------------------------------------
01035    RmMeshModelListConstIterator BeginModel() const { return m_modelList.begin(); };
01036 
01037    //--------------------------------------------------------------------------
01043    //--------------------------------------------------------------------------
01044    RmMeshModelListIterator      EndModel() { return m_modelList.end(); };
01045 
01046    //--------------------------------------------------------------------------
01052    //--------------------------------------------------------------------------
01053    RmMeshModelListConstIterator EndModel() const { return m_modelList.end(); };
01054 
01055    //--------------------------------------------------------------------------
01061    //--------------------------------------------------------------------------
01062    int GetNumModels() { return m_modelList.size(); };
01063 
01064    //--------------------------------------------------------------------------
01070    //--------------------------------------------------------------------------
01071    void Copy( RmMeshModelContainer *pDestContainer ); 
01072 
01073    //--------------------------------------------------------------------------
01080    //--------------------------------------------------------------------------
01081    void SetBoundBoxDirty( bool bDirty );
01082 
01083    //--------------------------------------------------------------------------
01089    //--------------------------------------------------------------------------
01090    void UpdateBoundBox();
01091 
01092    //--------------------------------------------------------------------------
01098    //--------------------------------------------------------------------------
01099    RmBoundBox&       GetBoundBox();
01100 
01101    //--------------------------------------------------------------------------
01107    //--------------------------------------------------------------------------
01108    bool GetBoundSphere( RmVector3D &center, float &radius );
01109 
01110    //--------------------------------------------------------------------------
01117    //--------------------------------------------------------------------------
01118    void SetCentroidDirty( bool bDirty );
01119 
01120    //--------------------------------------------------------------------------
01126    //--------------------------------------------------------------------------
01127    void UpdateCentroid();
01128 
01129    //--------------------------------------------------------------------------
01136    //--------------------------------------------------------------------------
01137    RmVector3D& GetCentroid();
01138 
01139 private :
01140    RmMeshModelList   m_modelList;      
01141  
01142    bool              m_bBoundBoxDirty; 
01143    RmBoundBox        m_boundBox;       
01144 
01145    bool              m_bCentroidDirty; 
01146    RmVector3D        m_centroid;       
01147 }; // End of RmMeshModelContainer
01148 
01149 
01150 #endif

Generated on Fri Feb 25 16:08:37 2005 for RenderMonkey SDK by doxygen 1.3.6