天行健 君子当自强而不息

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 阅读(883) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论