-->

Load a .obj model with ASSIMP in DirectX9

2020-07-18 03:09发布

问题:

This is my first time posting. I have this issue with this 3d model loading library called ASSIMP. I am trying to integrate it in a sample Direct3d9 app. and it is not going so well. I am an experienced C++ programmer so it shouldn't take too much hassle to help me :). So i have in the past made several d3d9 apps and rendered manual primitives. but now i am trying to render an obj model loaded with ASSIMP. when i try to render it, NOTHING is rendered at all. this is very weird, not even one poly is rendered. this is VERY frustrating as i have spent 1 week trying to fix this one problem and searching on google returns no useful results. you guys are honestly my last hope lol. ok so here is my code. pretty please take a look and help me understand what i am doing wrong. also if you know of a link where a directx9 ASSIMP example may be that would also be appreciated as google only shows OpenGL :(. Any help will be much appreciated thanks :)

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:

I might be getting old but i can't find anything wrong in this code. Are you sure your pointers are all pointing where they should?



标签: c++ directx