在 C++ 中使用 Direct X 9 从 X 文件加载网格

发布于 2024-07-24 16:28:26 字数 1626 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-07-31 16:28:26

该错误不是转换为 D3DXMATERIAL*,而是可能 bufMaterial 不是有效的指针。

LPD3DXBUFFERID3DXBuffer * 的类型定义,因此指向 ID3DXBuffer 的指针必须在使用之前初始化。 您可以这样做:

D3DXCreateBuffer(NumBytes, &bufMaterial);

NumBytes 应该是一个整数,它指定缓冲区的大小。

The bug is not the cast to D3DXMATERIAL*, but probably that bufMaterial is not a valid pointer.

LPD3DXBUFFER is a typedef for ID3DXBuffer * and therefore a pointer to a ID3DXBuffer wich has to be initialized before using it. You could do this like so:

D3DXCreateBuffer(NumBytes, &bufMaterial);

NumBytes should be an integer which specifies the size of the buffer.

哎呦我呸! 2024-07-31 16:28:26

如果网格上没有材质,则它不会返回材质数组的 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.

孤君无依 2024-07-31 16:28:26

好的,我把你的代码进行了快速测试,我改变了一些东西,但没有大的改变,我希望它能帮助你!

void LoadModel(Model* model, LPCTSTR File)
{ 
 LPD3DXBUFFER bufMaterial;
D3DXLoadMeshFromXW(File, D3DXMESH_SYSTEMMEM, d3ddev, NULL, &bufMaterial, NULL, 
                  &model->numMaterials, &model->Mesh); 
OutputDebugString(L"LOAD MESH \n"); 

D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer(); 
OutputDebugString(L"GET BUFFER\n"); 

model->Material = new D3DMATERIAL9[model->numMaterials]; 
model->Texture = new LPDIRECT3DTEXTURE9[model->numMaterials]; 

OutputDebugString(L"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;
} 

更新
您可能想看的重要内容是这个。

struct Model
{
    DWORD numMaterials;
    LPD3DXMESH Mesh;
    LPDIRECT3DTEXTURE9* Texture;
    D3DMATERIAL9* Material;
};

这只是我为帮助我进行测试而制作的简单示例。 如果不起作用尝试使用其他动画

ok i took your code to quick test i changed some things but not big changes i hope it will help you!

void LoadModel(Model* model, LPCTSTR File)
{ 
 LPD3DXBUFFER bufMaterial;
D3DXLoadMeshFromXW(File, D3DXMESH_SYSTEMMEM, d3ddev, NULL, &bufMaterial, NULL, 
                  &model->numMaterials, &model->Mesh); 
OutputDebugString(L"LOAD MESH \n"); 

D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer(); 
OutputDebugString(L"GET BUFFER\n"); 

model->Material = new D3DMATERIAL9[model->numMaterials]; 
model->Texture = new LPDIRECT3DTEXTURE9[model->numMaterials]; 

OutputDebugString(L"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;
} 

UPDATE
something important you may wanna look at is this.

struct Model
{
    DWORD numMaterials;
    LPD3DXMESH Mesh;
    LPDIRECT3DTEXTURE9* Texture;
    D3DMATERIAL9* Material;
};

this is just simple example i made to help me with my testing. if didn't work try using another animation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文