• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當自強而不息

            DXUT源碼分析 ---- 類CDXUTMesh(3)

            接下來的Create()函數從接口ID3DXFileData創建網格模型:

            HRESULT CDXUTMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice, LPD3DXFILEDATA pFileData )
            {
                LPD3DXBUFFER pMtrlBuffer 
            = NULL;
                LPD3DXBUFFER pAdjacencyBuffer 
            = NULL;
                HRESULT      hr;

                
            // Cleanup previous mesh if any
                Destroy();

                
            // Load the mesh from the DXFILEDATA object
                if( FAILED( hr = D3DXLoadMeshFromXof( pFileData, D3DXMESH_MANAGED, pd3dDevice, &pAdjacencyBuffer, &pMtrlBuffer, 
                                                      NULL, 
            &m_dwNumMaterials, &m_pMesh ) ) )
                {
                    
            return hr;
                }

                
            // Optimize the mesh for performance
                if( FAILED( hr = m_pMesh->OptimizeInplace(
                                    D3DXMESHOPT_COMPACT 
            | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
                                    (DWORD
            *)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) )
                {
                    SAFE_RELEASE( pAdjacencyBuffer );
                    SAFE_RELEASE( pMtrlBuffer );
                    
            return hr;
                }

                D3DXMATERIAL
            * d3dxMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
                hr 
            = CreateMaterials( L"", pd3dDevice, d3dxMtrls, m_dwNumMaterials );

                SAFE_RELEASE( pAdjacencyBuffer );
                SAFE_RELEASE( pMtrlBuffer );

                
            // Extract data from m_pMesh for easy access
                D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];

                m_dwNumVertices    
            = m_pMesh->GetNumVertices();
                m_dwNumFaces       
            = m_pMesh->GetNumFaces();
                m_dwBytesPerVertex 
            = m_pMesh->GetNumBytesPerVertex();

                m_pMesh
            ->GetIndexBuffer( &m_pIB );
                m_pMesh
            ->GetVertexBuffer( &m_pVB );
                m_pMesh
            ->GetDeclaration( decl );

                pd3dDevice
            ->CreateVertexDeclaration( decl, &m_pDecl );

                
            return hr;
            }

             

            該函數與上一個Create()函數并無太大區別,只是在加載網格模型時調用D3DXLoadMeshFromXof()而不是D3DXLoadMeshFromXW()。

             

            最后一個Create()函數從輸入的網格模型中創建新的網格模型:

            HRESULT CDXUTMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice, ID3DXMesh* pInMesh, 
                                       D3DXMATERIAL
            * pd3dxMaterials, DWORD dwMaterials )
            {
                
            // Cleanup previous mesh if any
                Destroy();

                
            // Optimize the mesh for performance

                DWORD 
            *rgdwAdjacency = NULL;

                rgdwAdjacency 
            = new DWORD[pInMesh->GetNumFaces() * 3];
                
            if( rgdwAdjacency == NULL )
                    
            return E_OUTOFMEMORY;

                pInMesh
            ->GenerateAdjacency(1e-6f, rgdwAdjacency);

                D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
                pInMesh
            ->GetDeclaration( decl );

                DWORD dwOptions 
            = pInMesh->GetOptions();

                dwOptions 
            &= ~(D3DXMESH_32BIT | D3DXMESH_SYSTEMMEM | D3DXMESH_WRITEONLY);
                dwOptions 
            |= D3DXMESH_MANAGED;
                dwOptions 
            |= D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE;

                ID3DXMesh
            * pTempMesh = NULL;

                
            if( FAILED( pInMesh->Optimize( dwOptions, rgdwAdjacency, NULL, NULL, NULL, &pTempMesh ) ) )
                {
                    SAFE_DELETE_ARRAY( rgdwAdjacency );
                    
            return E_FAIL;
                }

                SAFE_DELETE_ARRAY( rgdwAdjacency );
                SAFE_RELEASE( m_pMesh );

                m_pMesh 
            = pTempMesh;

                HRESULT hr 
            = CreateMaterials( L"", pd3dDevice, pd3dxMaterials, dwMaterials );;    

                
            // Extract data from m_pMesh for easy access

                m_dwNumVertices    
            = m_pMesh->GetNumVertices();
                m_dwNumFaces       
            = m_pMesh->GetNumFaces();
                m_dwBytesPerVertex 
            = m_pMesh->GetNumBytesPerVertex();

                m_pMesh
            ->GetIndexBuffer( &m_pIB );
                m_pMesh
            ->GetVertexBuffer( &m_pVB );
                m_pMesh
            ->GetDeclaration( decl );

                pd3dDevice
            ->CreateVertexDeclaration( decl, &m_pDecl );

                
            return hr;
            }

             

            為了優化網格的渲染,函數首先生成網格面鄰接信息緩存:
                DWORD *rgdwAdjacency = NULL;
                rgdwAdjacency = new DWORD[pInMesh->GetNumFaces() * 3];
                if( rgdwAdjacency == NULL )
                    return E_OUTOFMEMORY;
                pInMesh->GenerateAdjacency(1e-6f, rgdwAdjacency);

            GenerateAdjacency()的聲明如下:

            Generate a list of mesh edges, as well as a list of faces that share each edge.

            HRESULT GenerateAdjacency(
            FLOAT Epsilon,
            DWORD * pAdjacency
            );

            Parameters

            Epsilon
            [in] Specifies that vertices that differ in position by less than epsilon should be treated as coincident.
            pAdjacency
            [in] Pointer to an array of three DWORDs per face to be filled with the indices of adjacent faces. The number of bytes in this array must be at least 3 * ID3DXBaseMesh::GetNumFaces * sizeof(DWORD).

            Return Values

            If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be one of the following: D3DERR_INVALIDCALL, E_OUTOFMEMORY.

            Remarks

            After an application generates adjacency information for a mesh, the mesh data can be optimized for better drawing performance.

            The order of the entries in the adjacency buffer is determined by the order of the vertex indices in the index buffer. The adjacent triangle 0 always corresponds to the edge between the indices of the corners 0 and 1. The adjacent triangle 1 always corresponds to the edge between the indices of the corners 1 and 2 while the adjacent triangle 2 corresponds to the edge between the indices of the corners 2 and 0.

            為了優化網格的渲染,函數去除了32位索引、系統內存的使用、只寫訪問,增加了托管內存的使用,以及去除了無用的頂點和索引項、根據屬性給三角形排序并調整屬性表,增加了頂點緩存的命中率,并調用Optimize()對網格模型進行優化,優化后的網格模型存儲在pTempMesh中:

                DWORD dwOptions = pInMesh->GetOptions();
                dwOptions &= ~(D3DXMESH_32BIT | D3DXMESH_SYSTEMMEM | D3DXMESH_WRITEONLY);
            dwOptions |= D3DXMESH_MANAGED;
            dwOptions |= D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE;
                ID3DXMesh* pTempMesh = NULL;
                if( FAILED( pInMesh->Optimize( dwOptions, rgdwAdjacency, NULL, NULL, NULL, &pTempMesh ) ) )
            {
            SAFE_DELETE_ARRAY( rgdwAdjacency );
            return E_FAIL;
            }

            接下來刪除了鄰接數組、網格指針,并將優化后的網格模型指針賦給m_pMesh,并調用CreateMaterials()創建材質和紋理資源:

                SAFE_DELETE_ARRAY( rgdwAdjacency );
            SAFE_RELEASE( m_pMesh );
                m_pMesh = pTempMesh;
                HRESULT hr = CreateMaterials( L"", pd3dDevice, pd3dxMaterials, dwMaterials );

            最后從優化后的網格模型中提取數據以方便日后訪問,并調用CreateVertexDeclaration()來創建頂點聲明:

               // Extract data from m_pMesh for easy access
                m_dwNumVertices    = m_pMesh->GetNumVertices();
            m_dwNumFaces = m_pMesh->GetNumFaces();
            m_dwBytesPerVertex = m_pMesh->GetNumBytesPerVertex();
                m_pMesh->GetIndexBuffer( &m_pIB );
            m_pMesh->GetVertexBuffer( &m_pVB );
                D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
            m_pMesh->GetDeclaration( decl );
            pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl );
                return hr;

            CreateVertexDeclaration()的聲明如下:

            Create a vertex shader declaration from the device and the vertex elements.

            HRESULT CreateVertexDeclaration(
            CONST D3DVERTEXELEMENT9* pVertexElements,
            IDirect3DVertexDeclaration9** ppDecl
            );

            Parameters

            pVertexElements
            [in] An array of D3DVERTEXELEMENT9 vertex elements.
            ppDecl
            [out, retval] Pointer to an IDirect3DVertexDeclaration9 pointer that returns the created vertex shader declaration.

            Return Values

            If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

            Remarks

            See the Vertex Declaration (Direct3D 9) page for a detailed description of how to map vertex declarations between different versions of DirectX.

             

            函數Destroy()用來在程序退出時銷毀指定的資源:

            HRESULT CDXUTMesh::Destroy()
            {
            InvalidateDeviceObjects();
                for( UINT i=0; i<m_dwNumMaterials; i++ )
            SAFE_RELEASE( m_pTextures[i] );
                SAFE_DELETE_ARRAY( m_pTextures );
            SAFE_DELETE_ARRAY( m_pMaterials );
            SAFE_DELETE_ARRAY( m_strMaterials );
                SAFE_RELEASE( m_pMesh );
                m_dwNumMaterials = 0L;
                return S_OK;
            }

            posted on 2008-05-31 10:21 lovedday 閱讀(905) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            国产精品青草久久久久婷婷| 91精品国产综合久久精品| 久久国产精品无码网站| 久久免费国产精品| 国产精品久久久久久久app | 亚洲av伊人久久综合密臀性色 | 青草国产精品久久久久久| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 久久久久亚洲av综合波多野结衣 | 91精品国产综合久久香蕉| 久久这里只有精品视频99| 亚洲va中文字幕无码久久 | 伊人久久大香线蕉AV一区二区| 久久久久se色偷偷亚洲精品av| 国产一级做a爰片久久毛片| 久久综合久久鬼色| 久久91亚洲人成电影网站| 人妻无码αv中文字幕久久琪琪布| 精品久久久久久国产潘金莲| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | A级毛片无码久久精品免费| 色成年激情久久综合| 中文无码久久精品| 亚洲国产精品嫩草影院久久 | 久久精品国产亚洲沈樵| 亚洲伊人久久大香线蕉综合图片| 99久久免费国产精品| 久久精品无码一区二区三区| 亚洲乱码精品久久久久..| 欧美激情一区二区久久久| 久久夜色撩人精品国产小说| 国产精品久久成人影院| 色欲综合久久躁天天躁蜜桃| 一本一道久久a久久精品综合| 久久91精品综合国产首页| 伊人热人久久中文字幕| 久久99免费视频| 青青草原综合久久| 一级做a爱片久久毛片| 一本久久a久久精品综合夜夜| 青青热久久综合网伊人|