Directx9绘制模型的方法

Directx9 methods of drawing models

本文关键字:方法 模型 绘制 Directx9      更新时间:2023-10-16

我想知道是否有任何其他方法,除了经典的

DrawIndexedPrimitive
DrawIndexedPrimitiveUP
DrawPrimitive
DrawPrimitiveUP
DrawRectPatch
DrawTriPatch 

在屏幕上绘制模型

如果你的意思是模型是一个网格(顶点和纹理的集合),你可以尝试这样做:

// Loading the mesh
LPD3DXBUFFER materialBuffer = NULL;
DWORD numMaterials = 0;
LPD3DXMESH mesh = NULL;
hr=D3DXLoadMeshFromX("d:\temp\tiger.x", D3DXMESH_SYSTEMMEM, 
    d3dDevice, NULL, 
    &materialBuffer,NULL, &numMaterials, 
    &mesh );
if(FAILED(hr))
    THROW_ERROR_AND_EXIT("hr=D3DXLoadMeshFromX");
// Loading the material buffer
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer();
// Holding material and texture pointers
D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials];
LPDIRECT3DTEXTURE9 *meshTextures  = new LPDIRECT3DTEXTURE9[numMaterials];   
// Filling material and texture arrays
for (DWORD i=0; i<numMaterials; i++)
{
    // Copy the material
    meshMaterials[i] = d3dxMaterials[i].MatD3D;
    // Set the ambient color for the material (D3DX does not do this)
    meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
    // Create the texture if it exists - it may not
    meshTextures[i] = NULL;
    if (d3dxMaterials[i].pTextureFilename)
        D3DXCreateTextureFromFile(d3dDevice, d3dxMaterials[i].pTextureFilename, &meshTextures[i]);
}
materialBuffer->Release();
//render mesh
d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255),1.0f,0);
            // Turn on wireframe mode
            d3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
            d3dDevice->BeginScene();
        for (DWORD i=0; i<numMaterials; i++)
        {
            // Set the material and texture for this subset
            d3dDevice->SetMaterial(&meshMaterials[i]);
            d3dDevice->SetTexture(0,meshTextures[i]);
            // Draw the mesh subset
            mesh->DrawSubset( i );
        }
        d3dDevice->EndScene();
        d3dDevice->Present(NULL, NULL, NULL, NULL);