directx中第四卷

渐进网格

声明变量

ID3DXPMesh* PMesh = 0;

产生渐进网格

加载模型

hr = SourceMesh->OptimizeInplace(
    D3DXMESHOPT_ATTRSORT|
    D3DXMESHOPT_COMPACT|
    D3DXMESHOPT_VERTEXCACHE,
    (DWORD*)adjbuffer->GetBufferPointer(),
    (DWORD*)adjbuffer->GetBufferPointer(), //添加上优化后的邻接数组指针
    0,0
    );

if (FAILED(hr))
{
    ::MessageBox(0, "OptimizeInplace-lost", 0, 0);
    return false;
}
//由原网格生成渐进网格(网格指针,优化前的邻接数组指针,默认顶点属性权重,默认顶点权重,尽可能简化,简化面数,把结果存到网格指针)
hr = D3DXGeneratePMesh(
    SourceMesh,
    (DWORD*)adjbuffer->GetBufferPointer(),
    0,0,1,
    D3DXMESHSIMP_FACE,
    &PMesh
    );

if (FAILED(hr))
{
    ::MessageBox(0, "D3DXGeneratePMesh-lost", 0, 0);
    return false;
}

adjbuffer->Release();
//得到最大细节
DWORD maxfaces = PMesh->GetMaxFaces();
//设置细节
PMesh->SetNumFaces(maxfaces);

绘制模型

for (int i = 0; i < Mtrls.size(); i++)
{
    g_pd3dDevice->SetMaterial(&Mtrls[i]);
    g_pd3dDevice->SetTexture(0, Textures[i]);
    PMesh->DrawSubset(i);
    //绘制网格骨架,为了看到效果
    g_pd3dDevice->SetMaterial(&yellow_mtrl);
    g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
    PMesh->DrawSubset(i);
    g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
}

调整效果

int numfaces = PMesh->GetNumFaces();
if (::GetAsyncKeyState('A')&0x8000f)
{
    PMesh->SetNumFaces(numfaces + 10);
}

if (::GetAsyncKeyState('S')&0x8000f)
{
    PMesh->SetNumFaces(numfaces - 10);
}

效果
directx中第四卷_第1张图片

你可能感兴趣的:(Directx,Directx,开发)