在DirectX9中使用ASSIMP加载.obj模型

时间:2012-12-28 18:51:09

标签: c++ directx

这是我第一次发帖。我在这个名为ASSIMP的3d模型加载库中遇到了这个问题。我试图将它集成到一个示例Direct3d9应用程序中。而且进展不顺利。我是一位经验丰富的C ++程序员,所以不应该太麻烦帮助我:)。所以我在过去制作了几个d3d9应用程序并渲染了手动原语。但现在我想渲染一个装有ASSIMP的obj模型。当我尝试渲染它时,NOTHING完全呈现。这很奇怪,甚至没有渲染一个多边形。这是非常令人沮丧的,因为我花了一个星期试图解决这个问题,并在谷歌搜索返回没有有用的结果。你们老实说我最后的希望哈哈。好的,这是我的代码。请看看,帮助我理解我做错了什么。如果你知道一个链接,其中一个directx9 ASSIMP的例子也可能会被赞赏,因为谷歌只显示OpenGL :(。任何帮助将非常感谢谢谢:)

bool Mesh::LoadMesh(const std::string& Filename)
{

Assimp::Importer Importer;
const aiScene *pScene = NULL;
const aiMesh *pMesh = NULL;

pScene = Importer.ReadFile(Filename.c_str(), aiProcess_Triangulate | aiProcess_ConvertToLeftHanded | aiProcess_ValidateDataStructure | aiProcess_FindInvalidData);

if (!pScene)
{
    printf("Error parsing '%s': '%s'\n", Filename.c_str(), Importer.GetErrorString());
    return false;
}

pMesh = pScene->mMeshes[0];
if (!pMesh)
{
    printf("Error Finding Model In file.  Did you export an empty scene?");
    return false;
}

for (unsigned int i = 0; i < pMesh->mNumFaces; i++) 
{
    if (pMesh->mFaces[i].mNumIndices == 3)
    {
        m_NumIndices = m_NumIndices + 3;
    }

    else
    {
        printf("Error parsing Faces. Try to Re-Export model from 3d package!");
        return false;
    }
}

m_NumFaces = pMesh->mNumFaces;
m_NumVertecies = pMesh->mNumVertices;

ZeroMemory(&m_pVB, sizeof(m_pVB));


m_pRenderDevice->CreateVertexBuffer(sizeof(Vertex) * m_NumVertecies, 0, VertexFVF, D3DPOOL_DEFAULT, &m_pVB, NULL);


m_pVB->Lock(0, 0, (void**)&m_pVertecies, 0);

for (int i = 0; i < pMesh->mNumVertices; i++)
{
    Vertex *pvertex = new Vertex(D3DXVECTOR3(pMesh->mVertices[i].x, pMesh->mVertices[i].y, pMesh->mVertices[i].z), D3DXVECTOR2(pMesh->mTextureCoords[0][i].x, pMesh->mTextureCoords[0][i].y), D3DXVECTOR3(pMesh->mNormals[i].x, pMesh->mNormals[i].y, pMesh->mNormals[i].z));
    m_pVertecies[i] = pvertex;
}

m_pVB->Unlock();


return true;
}





void Mesh::Render()
{
m_pRenderDevice->SetStreamSource(0, m_pVB, 0, sizeof(Vertex));
m_pRenderDevice->SetFVF(VertexFVF);
m_pRenderDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, m_NumFaces);
}





void Render()
{
D3DCOLOR Color = D3DCOLOR_ARGB(255, 0, 0, 255);

//Clear the Z and Back buffers
g_pRenderDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, Color, 1.0f, 0);

g_pRenderDevice->BeginScene();


InitializeViewMatrix();


D3DXMATRIX Scale;
D3DXMatrixScaling(&Scale, CameraScaleX, CameraScaleY, CameraScaleZ);


D3DXMATRIX Rotation;

CameraRotX += 0.025;
D3DXMatrixRotationYawPitchRoll(&Rotation, CameraRotX, CameraRotY, CameraRotZ);

g_pRenderDevice->SetTransform(D3DTS_WORLD, &D3DXMATRIX(Scale * Rotation));


if (pMesh)
{
    pMesh->Render();
}


g_pRenderDevice->EndScene();

g_pRenderDevice->Present(NULL, NULL, NULL, NULL);
}

1 个答案:

答案 0 :(得分:0)

我可能会变老但我在这段代码中找不到任何错误。你确定你的指针都指向他们应该的位置吗?