c++ directx 9 网格纹理
好的,所以我可以完美加载网格,但加载其纹理不起作用。我不确定我做错了什么。
这是我的代码..
//render a single frame
void RenderFrame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
d3ddev->SetFVF(CUSTOMFVF);
InitMatrices();
D3DXMATRIX matTran; // a matrix to store the rotation for each triangle
D3DXMATRIX matRotz;
D3DXMATRIX matRoty;
D3DXMATRIX matRotx;
D3DXMatrixTranslation(&matTran, x, y, z);
D3DXMatrixRotationZ(&matRotz, D3DXToRadian(rz));
D3DXMatrixRotationY(&matRoty, D3DXToRadian(ry));
D3DXMatrixRotationX(&matRotx, D3DXToRadian(rx));
d3ddev->SetTransform(D3DTS_WORLD, &(matTran * matRotz * matRoty * matRotx)); // set the world transform
// draw the spaceship
for(DWORD i = 0; i < numMaterials; i++) // loop through each subset
{
d3ddev->SetMaterial(&material[i]); // set the material for the subset
if(texture[i] != NULL) // if the subset has a texture (if texture is not NULL)
d3ddev->SetTexture(0, texture[i]); // ...then set the texture
meshSpaceship->DrawSubset(i); // draw the subset
}
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
// cleans up Direct3D and COM
void CleanD3D(void)
{
meshSpaceship->Release();
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D
}
VOID InitGraphic()
{
LPD3DXBUFFER bufShipMaterial;
D3DXLoadMeshFromX(L"ramiz.x", // load this file
D3DXMESH_SYSTEMMEM, // load the mesh into system memory
d3ddev, // the Direct3D Device
NULL, // we aren't using adjacency
&bufShipMaterial, // put the materials here
NULL, // we aren't using effect instances
&numMaterials, // the number of materials in this model
&meshSpaceship); // put the mesh here
// retrieve the pointer to the buffer containing the material information
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();
// create a new material buffer and texture for each material in the mesh
material = new D3DMATERIAL9[numMaterials];
texture = new LPDIRECT3DTEXTURE9[numMaterials];
for(DWORD i = 0; i < numMaterials; i++) // for each material...
{
material[i] = tempMaterials[i].MatD3D; // get the material info
material[i].Ambient = material[i].Diffuse; // make ambient the same as diffuse
D3DXCreateTextureFromFile(d3ddev,
L"ramiz.x"
,
&texture[i]);
texture[i]=NULL;
}
}
ok so i can load a mesh perfectly but loading its texture is not working. im not sure what im doing wrong.
here is my code..
//render a single frame
void RenderFrame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
d3ddev->SetFVF(CUSTOMFVF);
InitMatrices();
D3DXMATRIX matTran; // a matrix to store the rotation for each triangle
D3DXMATRIX matRotz;
D3DXMATRIX matRoty;
D3DXMATRIX matRotx;
D3DXMatrixTranslation(&matTran, x, y, z);
D3DXMatrixRotationZ(&matRotz, D3DXToRadian(rz));
D3DXMatrixRotationY(&matRoty, D3DXToRadian(ry));
D3DXMatrixRotationX(&matRotx, D3DXToRadian(rx));
d3ddev->SetTransform(D3DTS_WORLD, &(matTran * matRotz * matRoty * matRotx)); // set the world transform
// draw the spaceship
for(DWORD i = 0; i < numMaterials; i++) // loop through each subset
{
d3ddev->SetMaterial(&material[i]); // set the material for the subset
if(texture[i] != NULL) // if the subset has a texture (if texture is not NULL)
d3ddev->SetTexture(0, texture[i]); // ...then set the texture
meshSpaceship->DrawSubset(i); // draw the subset
}
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
// cleans up Direct3D and COM
void CleanD3D(void)
{
meshSpaceship->Release();
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D
}
VOID InitGraphic()
{
LPD3DXBUFFER bufShipMaterial;
D3DXLoadMeshFromX(L"ramiz.x", // load this file
D3DXMESH_SYSTEMMEM, // load the mesh into system memory
d3ddev, // the Direct3D Device
NULL, // we aren't using adjacency
&bufShipMaterial, // put the materials here
NULL, // we aren't using effect instances
&numMaterials, // the number of materials in this model
&meshSpaceship); // put the mesh here
// retrieve the pointer to the buffer containing the material information
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();
// create a new material buffer and texture for each material in the mesh
material = new D3DMATERIAL9[numMaterials];
texture = new LPDIRECT3DTEXTURE9[numMaterials];
for(DWORD i = 0; i < numMaterials; i++) // for each material...
{
material[i] = tempMaterials[i].MatD3D; // get the material info
material[i].Ambient = material[i].Diffuse; // make ambient the same as diffuse
D3DXCreateTextureFromFile(d3ddev,
L"ramiz.x"
,
&texture[i]);
texture[i]=NULL;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您在
for
循环中将所有纹理设置为NULL
,因此当然没有要渲染的纹理!另外:
函数
D3DXCreateTextureFromFile()
仅支持从以下文件类型加载纹理:.bmp、.dds、.dib、.hdr、.jpg、.pfm、.png、.ppm 和 .tga 。您正在尝试从 .x 文件加载纹理,这不可能是正确的。我认为您的意思是将
tempMaterials[i].pTextureFilename
传递给D3DXCreateTextureFromFile()
来加载纹理文件,不是吗?以下是如何正确加载纹理文件以进行(简单).x 文件渲染:
First of all, you're setting all the textures to
NULL
in yourfor
loop, so of course there's no texture to render!Also:
The function
D3DXCreateTextureFromFile()
only supports loading textures from these file types: .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga. You're attempting to load textures from an .x file, which can't possibly be right.I think you meant to pass
tempMaterials[i].pTextureFilename
toD3DXCreateTextureFromFile()
to load the texture files, no?Here's how to properly load texture files for (simple) .x file rendering: