分享
 
 
 

建立自己的3D静态模型文件

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

跟着我一步一步来吧(我的思路和过程)。

我有一个能画出微软例子中tiger.x的工程,其中创建Mesh的代码片断如下(代码中有一些变量在.h文档中定义的):

HRESULT CMyMesh::Create( LPDIRECT3DDEVICE9 pDevice, string MeshFile )

{

if( pDevice == NULL )

return E_FAIL;

LPD3DXBUFFER pD3DXMtrlBuffer;

if( FAILED( D3DXLoadMeshFromX( MeshFile.c_str(), D3DXMESH_SYSTEMMEM, pDevice, NULL, &pD3DXMtrlBuffer, NULL, &NumOfMaterials, &Mesh ) ) )

return E_FAIL;

D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();

MeshMaterials = new D3DMATERIAL9[NumOfMaterials];

MeshTextures = new LPDIRECT3DTEXTURE9[NumOfMaterials];

for( DWORD i = 0; i < NumOfMaterials_; i ++ )

{

MeshMaterials[i] = d3dxMaterials[i].MatD3D;

MeshMaterials[i].Ambient = MeshMaterials[i].Diffuse;

MeshTextures[i] = NULL;

if( d3dxMaterials[i].pTextureFilename != NULL && lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )

{

if( FAILED( D3DXCreateTextureFromFile( pDevice, d3dxMaterials[i].pTextureFilename, &MeshTextures[i] ) ) )

return E_FAIL;

}

}

pD3DXMtrlBuffer->Release();

return S_OK;

}

画这个Mesh的代码如下:

void CMyMesh::Render( LPDIRECT3DDEVICE9 pDevice )

{

for( DWORD i = 0; i < NumOfMaterials; i ++ )

{

pDevice->SetMaterial( &MeshMaterials[i] );

pDevice->SetTexture( 0, MeshTextures[i] );

Mesh->DrawSubset( i );

}

}

我们先想办法替换DrawSubset函数,代码如下:

void CMyMesh::Render( LPDIRECT3DDEVICE9 pDevice )

{

LPDIRECT3DVERTEXBUFFER9 pVertexBuffer;

LPDIRECT3DINDEXBUFFER9 pIndexBuffer;

DWORD dwNumBytesPerVertex;

DWORD dwFVF;

DWORD dwNumVertex;

DWORD dwFaces;

Mesh->GetVertexBuffer(&pVertexBuffer);

Mesh->GetIndexBuffer(&pIndexBuffer);

dwNumBytesPerVertex = Mesh->GetNumBytesPerVertex();

dwFVF = Mesh->GetFVF();

dwNumVertex = Mesh->GetNumVertices();

dwFaces = Mesh->GetNumFaces();

pDevice->SetStreamSource(0, pVertexBuffer, 0, dwNumBytesPerVertex);

pDevice->SetFVF(dwFVF);

pDevice->SetTexture(0, MeshTextures[0]);

pDevice->SetMaterial(&MeshMaterials[0]);

pDevice->SetIndices(pIndexBuffer);

pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, dwNumVertex, 0, dwFaces);

}

虽然MeshTextures和MeshMaterials处有点问题,但是我们知道了画tiger.x的时候都需要什么东西:

VB,IB,NumBytesPerVertex,FVF,NumVertex,Faces,Texture,Material

那么我们将这些东西保存到文件中,不就和.x文件目的相同了么, 并且可以绕过D3DXMesh了。那么这些东西怎么得到呢:我修改了Create部分的代码:

HRESULT CMyMesh::Create( LPDIRECT3DDEVICE9 pDevice, string MeshFile )

{

if( pDevice == NULL )

return E_FAIL;

LPD3DXBUFFER pD3DXMtrlBuffer;

if( FAILED( D3DXLoadMeshFromX( MeshFile.c_str(), D3DXMESH_SYSTEMMEM, pDevice, NULL, &pD3DXMtrlBuffer, NULL, &NumOfMaterials, &Mesh ) ) )

return E_FAIL;

D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();

MeshMaterials = new D3DMATERIAL9[NumOfMaterials];

MeshTextures = new LPDIRECT3DTEXTURE9[NumOfMaterials];

for( DWORD i = 0; i < NumOfMaterials_; i ++ )

{

MeshMaterials_[i] = d3dxMaterials[i].MatD3D;

MeshMaterials_[i].Ambient = MeshMaterials_[i].Diffuse;

MeshTextures_[i] = NULL;

if( d3dxMaterials[i].pTextureFilename != NULL && lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )

{

FILE* fp;

int nLen = 0;

char* pBuf;

if(fp=fopen( d3dxMaterials[i].pTextureFilename, "rb" )) {

fseek(fp,0,SEEK_END);

nLen = ftell(fp);

fseek(fp,0,SEEK_SET);

pBuf = new char[nLen];

fread(pBuf,1,nLen,fp);

fclose(fp);

}

HRESULT hr = D3DXCreateTextureFromFileInMemory(D3DDevice,pBuf,nLen,&MeshTextures[i]);

delete [] pBuf;

//HRESULT hr = D3DXCreateTextureFromFile( D3DDevice_, d3dxMaterials[i].pTextureFilename, &MeshTextures_[i] );

if( FAILED( hr ) )

return E_FAIL;

}

}

//下面是增加的内容

FILE* fp;

if( ( fp = fopen("tiger.o","w") ) != NULL ) {

LPDIRECT3DVERTEXBUFFER9 pVertexBuffer;

LPDIRECT3DINDEXBUFFER9 pIndexBuffer;

D3DVERTEXBUFFER_DESC desc;

D3DINDEXBUFFER_DESC idesc;

Mesh->GetVertexBuffer(&pVertexBuffer);

Mesh->GetIndexBuffer(&pIndexBuffer);

pVertexBuffer->GetDesc( &desc );

pIndexBuffer->GetDesc( &idesc );

VOID* pVBData = NULL;

VOID* pIBData = NULL;

pVertexBuffer->Lock( 0, 0, (VOID**)&pVBData, 0 );

pIndexBuffer->Lock( 0, 0, (VOID**)&pIBData, 0 );

DWORD dwFVF = Mesh->GetFVF();

DWORD dwNumBytesPerVertex = Mesh->GetNumBytesPerVertex();

DWORD dwNumFaces = Mesh->GetNumFaces();

//怎么样?该有的都有了吧,要怎么保存就看你的了

//这段代码显然不完善,你需要自己去完善它

//有人说了你光考虑了Mesh,还有多AnimationSet等等东西呢?

//这里本来就只是建立静态模型,当然没有AnimationSet啦。动画的部分我后面在写吧。

pVertexBuffer->UnLock();

pIndexBuffer->UnLock();

fclose(fp);

}

//增加结束

pD3DXMtrlBuffer->Release();

return S_OK;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有