.x 文件是由3dsmax导出的,专门被direct3D使用。
一个.x文件包含:n 个 D3DXMATERIAL 类型的数据,每个D3DXMATERIAL包含一个D3DMATERIAL9 和 一个texture文件的路径。也就是说一个.x文件包含n个material 和 n 个texture文件路径。
加载过程如下:
LPD3DXBUFFER pMeshMaterialsBuffer
=
NULL;
// 先将.x文件的数据存到buffer中,并得到material的个数,即D3DXMATERIAL的个数
if
( FAILED(D3DXLoadMeshFromX(L
"
rect.x
"
,D3DXMESH_SYSTEMMEM,pd3dDevice,
NULL,
&
pMeshMaterialsBuffer,NULL,
&
g_numMaterials,
&
g_pRectMesh)) )
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if ( FAILED(D3DXLoadMeshFromX(L"..\\rect.x",D3DXMESH_SYSTEMMEM,pd3dDevice,
NULL,&pMeshMaterialsBuffer,NULL,&g_numMaterials,&g_pRectMesh) ))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBoxW(NULL,L"Can't find this .x file",L"rect.exe",MB_OK);
return E_FAIL;
}
}
// 得到buffer的指针
D3DXMATERIAL
*
pMaterial
=
(D3DXMATERIAL
*
)pMeshMaterialsBuffer
->
GetBufferPointer();
// 为读取material 和texture数据分配空间
g_pRectMeshMaterials
=
new
D3DMATERIAL9[g_numMaterials];
if
( g_pRectMeshMaterials
==
NULL )
return
E_OUTOFMEMORY;
g_pRectMeshTextures
=
new
LPDIRECT3DTEXTURE9[g_numMaterials];
if
( g_pRectMeshTextures
==
NULL )
return
E_OUTOFMEMORY;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
for
( DWORD i
=
0
;i
<
g_numMaterials ;
++
i )
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 得到第i个material
g_pRectMeshMaterials[i] = pMaterial[i].MatD3D;
g_pRectMeshMaterials[i].Ambient = g_pRectMeshMaterials[i].Diffuse;
g_pRectMeshTextures[i] = NULL;
// 通过texture文件的路径,得到第i个material 的texture
if ( pMaterial[i].pTextureFilename != NULL &&
lstrlen((LPCWSTR)pMaterial[i].pTextureFilename) > 0 )
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LPWSTR path = (LPWSTR)_alloca(len*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP,0,pMaterial[i].pTextureFileName,
-1,path,len*sizeof(WCHAR));
if ( FAILED(D3DXCreateTextureFromFile(pd3dDevice,
path,&g_pRectMeshTextures[i])) )
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBoxW(NULL, L"Could not find texture map", L"rect.exe", MB_OK);
}
}
}
pMeshMaterialsBuffer
->
Release();
这里有一个问题折腾了我半天:由于使用DXUT,程序必须使用unicode ,D3dxCreateTextureFromFile 转换为D3DXCreateTextureFromFileW,第2个参数的类型由LPSTR变为LPWSTR。我需要将pTextureFileName的类型由ansi转为unicode 。一上来我用强转的方法:(LPWSTR)pMaterial[i].pTextureFileName,无法得到正确的路径,接着我想到用atl的A2W,但一直没有正确导入a2w的相关文件,总报错。没办法,看看A2W是咋写的。原来A2W先从栈上分配一段内存作为buffer,然后调用MultiByteToWideChar,用buffer得到unicode版的字符串。好了,照猫画猫,一切ok!