在 C++ 中使用 Direct X 9 从 X 文件加载网格
我在 C++ 中为 Direct X 制作 Load Mesh 函数时遇到问题。我似乎收到此错误:“_.exe 中 0x00401e64 处未处理的异常:0xC00000005:访问冲突读取位置 0x83e05597”。 我知道它在这条线上崩溃了:
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer();
整个函数看起来像这样(到目前为止我一直在关注 directxtutorial.com 以获得直接 X 帮助)。
void LoadModel(Model* model, LPCTSTR File){
LPD3DXBUFFER bufMaterial;
D3DXLoadMeshFromX(File, D3DXMESH_SYSTEMMEM, d3ddev, NULL, &bufMaterial, NULL,
&model->numMaterials, &model->Mesh);
OutputDebugString("LOAD MESH \n");
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer();
OutputDebugString("GET BUFFER\n");
model->Material = new D3DMATERIAL9[model->numMaterials];
model->Texture = new LPDIRECT3DTEXTURE9[model->numMaterials];
OutputDebugString("LOAD MESH \n");
for(DWORD index = 0; index < model->numMaterials; index++)
{
model->Material[index] = tempMaterials[index].MatD3D;
model->Material[index].Ambient = model->Material[index].Diffuse;
// if there is a texture to load, load it
if(FAILED(D3DXCreateTextureFromFileA(d3ddev,
tempMaterials[index].pTextureFilename,
&model->Texture[index])))
model->Texture[index] = NULL; // if there is no texture, set the texture to NULL
}
return;}
我这样称呼它:
LoadModel(networkBase, TEXT("E:\\C++\\Skirmish\\X\\gridbox.x"));
但是,我发现我的旧 Beginning DirectX 书和另一个网站源都使用这种从临时材质缓冲区转换材质缓冲区的类型,就像崩溃的行正在做的那样。 请帮忙!
I have been having problems on making a Load Mesh function in C++ for Direct X. I seem to get this error: "Unhandled exception at 0x00401e64 in _.exe: 0xC00000005: Access violation reading loaction 0x83e05597". I know it is crashing on this line:
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer();
The whole function looks like this (I have been following along directxtutorial.com so far for direct X help).
void LoadModel(Model* model, LPCTSTR File){
LPD3DXBUFFER bufMaterial;
D3DXLoadMeshFromX(File, D3DXMESH_SYSTEMMEM, d3ddev, NULL, &bufMaterial, NULL,
&model->numMaterials, &model->Mesh);
OutputDebugString("LOAD MESH \n");
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer();
OutputDebugString("GET BUFFER\n");
model->Material = new D3DMATERIAL9[model->numMaterials];
model->Texture = new LPDIRECT3DTEXTURE9[model->numMaterials];
OutputDebugString("LOAD MESH \n");
for(DWORD index = 0; index < model->numMaterials; index++)
{
model->Material[index] = tempMaterials[index].MatD3D;
model->Material[index].Ambient = model->Material[index].Diffuse;
// if there is a texture to load, load it
if(FAILED(D3DXCreateTextureFromFileA(d3ddev,
tempMaterials[index].pTextureFilename,
&model->Texture[index])))
model->Texture[index] = NULL; // if there is no texture, set the texture to NULL
}
return;}
I call it like this:
LoadModel(networkBase, TEXT("E:\\C++\\Skirmish\\X\\gridbox.x"));
But, I have found my old Beginning DirectX book and another web site source that all use this type casting of the material buffer from the temporary material buffer like the line that is crashing is doing. Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该错误不是转换为
D3DXMATERIAL*
,而是可能bufMaterial
不是有效的指针。LPD3DXBUFFER
是ID3DXBuffer *
的类型定义,因此指向ID3DXBuffer
的指针必须在使用之前初始化。 您可以这样做:NumBytes 应该是一个整数,它指定缓冲区的大小。
The bug is not the cast to
D3DXMATERIAL*
, but probably thatbufMaterial
is not a valid pointer.LPD3DXBUFFER
is a typedef forID3DXBuffer *
and therefore a pointer to aID3DXBuffer
wich has to be initialized before using it. You could do this like so:NumBytes should be an integer which specifies the size of the buffer.
如果网格上没有材质,则它不会返回材质数组的
ID3DXBuffer
,因为没有材质。函数返回后您是否检查了
model->numMaterials
?另外,在调用该函数之前,您应该将
ID3DXBuffer
指针初始化为零。此外,请考虑 如果您尚未从 D3DX 获取其他调试信息,请链接到调试 D3DX。
If there are no materials on your mesh, then it won't return an
ID3DXBuffer
for the materials array because there are no materials.Did you check
model->numMaterials
after the function returns?Also, you should initialize your
ID3DXBuffer
pointer to zero before calling the function.Additionally, consider linking against the debug D3DX if you aren't already to get additional debug information from D3DX.
好的,我把你的代码进行了快速测试,我改变了一些东西,但没有大的改变,我希望它能帮助你!
更新
您可能想看的重要内容是这个。
这只是我为帮助我进行测试而制作的简单示例。 如果不起作用尝试使用其他动画
ok i took your code to quick test i changed some things but not big changes i hope it will help you!
UPDATE
something important you may wanna look at is this.
this is just simple example i made to help me with my testing. if didn't work try using another animation