Example #1
0
HRESULT CMyD3DApplication::OptimizeMeshData
    (
    LPD3DXMESH pMeshSysMem,
    LPD3DXBUFFER pAdjacencyBuffer,
    DWORD dwOptFlags, 
    SMeshData *pMeshData
    )
{
    HRESULT      hr = S_OK;
    LPD3DXBUFFER pbufTemp = NULL;
    DWORD iMaterial;

    // attribute sort - the un-optimized mesh option
    //          remember the adjacency for the vertex cache optimization
    hr = pMeshSysMem->Optimize( dwOptFlags|D3DXMESH_SYSTEMMEM,
                                 (DWORD*)pAdjacencyBuffer->GetBufferPointer(),
                                 NULL, NULL, NULL, &pMeshData->m_pMeshSysMem);
    if( FAILED(hr) )
        goto End;

    pMeshData->m_cStripDatas = m_dwNumMaterials;
    pMeshData->m_rgStripData = new SStripData[pMeshData->m_cStripDatas];
    if (pMeshData->m_rgStripData == NULL)
    {
        hr = E_OUTOFMEMORY;
        goto End;
    }

    for (iMaterial = 0; iMaterial < m_dwNumMaterials; iMaterial++)
    {
        hr = D3DXConvertMeshSubsetToSingleStrip(pMeshData->m_pMeshSysMem, iMaterial, 
                                D3DXMESH_IB_MANAGED, &pMeshData->m_rgStripData[iMaterial].m_pStrips, 
                                &pMeshData->m_rgStripData[iMaterial].m_cStripIndices);
        if (FAILED(hr))
            goto End;

        hr = D3DXConvertMeshSubsetToStrips(pMeshData->m_pMeshSysMem, iMaterial, 
                                D3DXMESH_IB_MANAGED, &pMeshData->m_rgStripData[iMaterial].m_pStripsMany, 
                                NULL, &pbufTemp, &pMeshData->m_rgStripData[iMaterial].m_cStrips);
        if (FAILED(hr))
            goto End;

        pMeshData->m_rgStripData[iMaterial].m_rgcStripLengths = new DWORD[pMeshData->m_rgStripData[iMaterial].m_cStrips];
        if (pMeshData->m_rgStripData[iMaterial].m_rgcStripLengths == NULL)
        {
            hr = E_OUTOFMEMORY;
            goto End;
        }
        memcpy(pMeshData->m_rgStripData[iMaterial].m_rgcStripLengths, pbufTemp->GetBufferPointer(), sizeof(DWORD)*pMeshData->m_rgStripData[iMaterial].m_cStrips);

    }

End:
    SAFE_RELEASE(pbufTemp);

    return hr;
}