首先介绍ID3DXBuffer 接口
此类型有两个方法
LPVOID ID3DXBuffer::GetBufferPointer();//返回指向缓存中数据起始位置的指针
DWORD ID3DXBuffer::GetBufferSize()//返回缓存的大小,单位为字节
下面函数用于创建一个空的ID3DXBuffer对象
HRESULT D3DXCreateBuffer(DWORD NumBytes, LPD3DBUFFER *ppBuffer);
再来介绍一个D3DXMATRIAL结构
typedef struct D3DXMATERIAL
{
D3DMATERIAL9 Mat3D; //存储材质
LPSTR pTextureFilename;//存储纹理路径名
}D3DXMATERIAL;
再来看看一个重要的函数
HRESULT D3DXLoadMeshFromX(
LPCSTR pFilename,//文件名
DWORD Options,//创建网格时所使用的标记
LPDIRECT3DDEVICE9 *pDevice,
LPD3DXBUFFER *ppAdjacency,//邻接表信息
LPD3DXBUFFER *ppMaterials,//材质和纹理信息. D3DXMATRIAL结构
LPD3DXBUFFER *ppEffectInstances,//
PDWORD pNumMaterials,//材质数目
LPD3DXMESH *ppMesh//返回填充好的Mesh对象
};
下面是一个实用的例子
class MyMesh
{
...........
private:
ID3DXMesh* Mesh = 0;
std::vector<D3DMATERIAL9> Mtrls(0);
std::vector<IDirect3DTexture9*> Textures(0);
......
};
bool MyMesh::LoadMyMesh( LPCSTR pName,IDirect3DDevice9* Device)
{
ID3DXBuffer* adjBuffer = 0;
ID3DXBuffer* mtrlBuffer = 0;
DWORD numMtrls = 0;
HRESULT hr = D3DXLoadMeshFromX(pName,D3DXMESH_MANAGED,Device,&adjBuffer,&mtrlBuffer,0,&numMtrls,&Mesh);
if(FAILED(hr))
{
::MessageBox(NULL,"D3DXLoadFromX() - FAILED",0,0);
return false;
}
if(mtrlBuffer!=0&&numMtrls!=0) //如果有材质
{
D3DXMATERIAL* mtrls =
(D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();//GetBufferPointer() 为了适合各种类型,因为返回VOID*类型 需要强制转换
for(int i = 0;i<numMtrls;i++)
{
//得到的材质没有环境光,我们得自己加上,让它等于漫射光
mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
Mtrls.push_back(mtrls[i].MatD3D);
}
if(mtrls[i].pTextureFilename!=0)//纹理不为空
IDirect3DTexture9* tex = 0;
D3DXCreateTextureFromFile(Device,mtrls.pTextureFilename,&tex);
Textures.push_back(tex);
}
else
{
Textures.push_back(0);
}
}
adjBuffer->Release();
mtrlBuffer->Release();
return true;
}
//加载好后,设置好矩阵,就可以进行绘制了.由于Mesh是分为许多子集的,所以要一个一个渲染
void MyMesh::DrawMyMesh(IDreict3DDevice9* Deivice)
{
for(int i =0;i<Mtrls.size();i++)
{
Device->SetMaterial(&Mtrls[i]);
Device->SetTexture(0,Textures[i]);
Mesh->DrawSubset(i);
}
}