嗯,终于到了使用网格的教程了。
Step 1 - Loading a Mesh Object
首先要定义一个网格缓冲区。
LPD3DXBUFFER pD3DXMtrlBuffer;
然后就是读入网格啦~别看很复杂,看清楚了,有一部分是重复的。// Load the mesh from the specified file
if( FAILED( D3DXLoadMeshFromX( "Tiger.x", D3DXMESH_SYSTEMMEM,
g_pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL,
&g_dwNumMaterials, &g_pMesh ) ) )
{
// If model is not in current folder, try parent folder
if( FAILED( D3DXLoadMeshFromX( "..\\Tiger.x",
D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,
&g_pMesh ) ) )
{
MessageBox(NULL, "Could not find tiger.x",
"Meshes.exe", MB_OK);
return E_FAIL;
}
}
然后需要提取出材质信息。D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
然后创建材质和纹理。g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];
g_pMeshTextures = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];
接下来解析材质和纹理。对每一个材质和纹理执行下面的代码(就是循环啦)://The first step is to copy the material, as shown in the following code fragment.
g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
//The second step is to set the ambient color for the material, as shown in the following code fragment.
g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;
//The final step is to create the texture for the material, as shown in the following code fragment.
// Create the texture.
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,
d3dxMaterials[i].pTextureFilename, &g_pMeshTextures[i] ) ) )
g_pMeshTextures[i] = NULL;
}
还要把刚才创建的材质缓冲区释放掉。pD3DXMtrlBuffer->Release();
Step 2 - Rendering a Mesh Object
绘制过程中需要设置纹理和材质,只不过这一次是每一个子集循环设定而已,一次设定一个子集的材质和纹理。
g_pd3dDevice->SetMaterial( &g_pMeshMaterials[i] );
g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] );
g_pMesh->DrawSubset( i );
Step 3 - Unloading a Mesh Object
使用网格需要及时释放不需要的网格和缓冲区。
先释放材质数组。
if(g_pMeshMaterials)
delete[] g_pMeshMaterials;
然后是纹理。
if(g_pMeshTextures)
{
for(DWORD i = 0; i < g_dwNumMaterials; i++
{
if(g_pMeshTextures[i])
g_pMeshTextures[i]->Release();
}
delete[] g_pMeshTextures;
最后是整个网格。
// Delete the mesh object
if(g_pMesh)
g_pMesh->Release();