C++ c++;directx 9网格纹理
好的,所以我可以完全加载网格,但加载其纹理不起作用。我不确定我做错了什么。C++ c++;directx 9网格纹理,c++,visual-c++,winapi,directx-9,C++,Visual C++,Winapi,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.0
这是我的密码
//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;
}
}
//渲染单个帧
空心渲染帧(空心)
{
d3ddev->清除(0,空,D3DCLEAR_目标,D3DCOLOR_XRGB(0,0,0),1.0f,0);
d3ddev->清除(0,空,D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
d3ddev->BeginScene();
d3ddev->SetFVF(自定义FVF);
初始化矩阵();
D3DXMATRIX matTran;//存储每个三角形旋转的矩阵
d3dx矩阵matRotz;
d3dx矩阵矩阵;
d3dx矩阵矩阵矩阵;
d3dx矩阵翻译&matTran,x,y,z;
D3DXMatrixRotationZ(&matRotz,D3DXToRadian(rz));
D3DXMatrixRotationY(&matRoty,D3DXToRadian(ry));
D3DXMatrixRotationX(&matRotx,D3DXToRadian(rx));
d3ddev->SetTransform(D3DTS_WORLD,&(matTran*matRotz*matRoty*matRotx));//设置世界变换
//画宇宙飞船
for(dwordi=0;iSetMaterial(&material[i]);//为子集设置材质
if(texture[i]!=NULL)//如果子集具有纹理(如果纹理不是NULL)
d3ddev->SetTexture(0,纹理[i]);/…然后设置纹理
meshSpaceship->DrawSubset(i);//绘制子集
}
d3ddev->EndScene();
d3ddev->Present(空,空,空,空);
}
//清理Direct3D和COM
清除空隙3d(空隙)
{
太空船->释放();
d3ddev->Release();//关闭并释放3D设备
d3d->Release();//关闭并释放Direct3D
}
VOID InitGraphic()
{
LPD3DX缓冲缓冲材料;
D3DXLoadMeshFromX(L“ramiz.x”,//加载此文件
D3DXMESH\u SYSTEMMEM,//将网格加载到系统内存中
d3ddev,//Direct3D设备
NULL,//我们没有使用邻接
&bufShipMaterial,//将材料放在这里
NULL,//我们没有使用效果实例
&numMaterials,//此模型中的材质数
&网格(太空船);//将网格放在这里
//检索指向包含材质信息的缓冲区的指针
D3DXMATERIAL*tempMaterials=(D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();
//为网格中的每个材质创建新材质缓冲区和纹理
材料=新D3D材料9[材料];
纹理=新的LPDIRECT3DTEXTURE9[numMaterials];
对于(DWORD i=0;i
首先,在for
循环中将所有纹理设置为NULL
,因此当然没有要渲染的纹理
for(DWORD i = 0; i < numMaterials; i++)
{
/* ... */
texture[i]=NULL; // <--- You're setting all your textures to NULL!
}
函数D3DXCreateTextureFromFile()
仅支持从以下文件类型加载纹理:.bmp、.dds、.dib、.hdr、.jpg、.pfm、.png、.ppm和.tga。您正在尝试从.x文件加载纹理,这可能不正确
我想您是想将tempMaterials[I].ptextureRefileName
传递到D3DXCreateTextureFromFile()
以加载纹理文件,不是吗
:
//////////////////////////////////////////////////////////////////////////////
//源代码段http://www.toymaker.info/Games/html/load_x_simply.html //
//////////////////////////////////////////////////////////////////////////////
对于(DWORD i=0;i如何将纹理加载到网格错误C2664:“D3DXCreateTextureFromFileW”:无法将参数2从“LPSTR”转换为“LPCWSTR”1>指向的类型不相关;转换需要重新解释转换、C样式转换或函数样式转换cast@RamizToma:您使用的是D3DXCreateTextureFromFile()的Unicode版本
。您要么将字符串转换为Unicode,要么直接调用D3DXCreateTextureFromFileA()
取而代之。@Ramiz Toma:没关系。我猜函数找不到该文件。请检查PteTextRefileName
的值-它可能没有指向纹理文件的正确文件路径。请使用类似于MessageBoxA(NULL,tempMaterials[i].PteTextRefileName,“,MB_OK)的方法
在for
循环中,这是一种快速而肮脏的检查方法。@Ramiz Toma:我强烈建议您学习DirectX中的游戏编程,甚至是初学者的游戏编程。DirectX编程并不简单。我还建议您学习。
D3DXCreateTextureFromFile(d3ddev, L"ramiz.x", &texture[i]);
//////////////////////////////////////////////////////////////////////////////
// Code snippet from http://www.toymaker.info/Games/html/load_x_simply.html //
//////////////////////////////////////////////////////////////////////////////
for (DWORD i=0; i<m_numMaterials; i++)
{
// Copy the material
meshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
// Create the texture if it exists - it may not
meshTextures[i] = NULL;
if (d3dxMaterials[i].pTextureFilename)
D3DXCreateTextureFromFile(gD3dDevice,
d3dxMaterials[i].pTextureFilename,
&meshTextures[i]);
}