ASSIMP与directx9网格化

时间:2013-03-24 18:44:27

标签: c++ directx direct3d9

可悲的是,this问题从未得到解答..

我正在尝试使用ASSIMP加载.X文件[或任何其他]模型。

这是我正在尝试使用的代码:

LPDIRECT3DVERTEXBUFFER9 vbuf;
IDirect3DDevice9* dev;
Assimp::Importer imp;
aiMesh* mm1;

struct CUSTOMVERTEX {
    float x,y,z;
    D3DVECTOR normal;
    float tx,ty;
};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)


void loadtextures() {
    const aiScene* sc=imp.ReadFile("model1.x",aiProcess_Triangulate | aiProcess_ConvertToLeftHanded | aiProcess_ValidateDataStructure | aiProcess_FindInvalidData);
    if (!sc) {
        MessageBox(NULL,"critical error in loading model file. see next dialog for errcode.",0,0);
        crash((char*) imp.GetErrorString());
    }
    if (!sc->HasMeshes()) {
        crash("failed to load model, meshes missing");
    }
    mm1=sc->mMeshes[0];
    if (!mm1) crash("no model found in file.");
    ZeroMemory(&vbuf,sizeof(vbuf));
    dev->CreateVertexBuffer(mm1->mNumVertices*sizeof(CUSTOMVERTEX),0,CUSTOMFVF,D3DPOOL_DEFAULT,&vbuf,NULL);
    CUSTOMVERTEX* vptr[1024];
    vbuf->Lock(0,0,(void**) &vptr,0);
    for(unsigned int i=0;i<mm1->mNumVertices;i++) {
        CUSTOMVERTEX* c=new CUSTOMVERTEX;
        c->x=mm1->mVertices[i].x; // after debugging, x,y,z are all 0.
        c->y=mm1->mVertices[i].y;
        c->z=mm1->mVertices[i].z;
        if (mm1->HasTextureCoords(i)) {
            c->tx=mm1->mTextureCoords[i]->x;
            c->ty=mm1->mTextureCoords[i]->y;
        } else c->tx=c->ty=0;
        if (mm1->HasNormals()) {
            c->normal=D3DXVECTOR3(mm1->mNormals[i].x,mm1->mNormals[i].y,mm1->mNormals[i].z);
        }
        vptr[i]=c;
    }
    vbuf->Unlock();
}

void draw_frame(HWND hwnd) {



    static float cr=0;cr+=0.01f;

    D3DXMATRIX mat_translate,mat_viewport,mat_projection,mat_roty,mat_rotx,mat_rotz;

    D3DXMatrixLookAtLH(&mat_viewport,&D3DXVECTOR3(0,0,-20),&D3DXVECTOR3(0,0,0),&D3DXVECTOR3(0,1,0));
    D3DXMatrixPerspectiveFovLH(&mat_projection,fov,(float) SCRW / (float) SCRH,1,100);
    D3DXMatrixRotationY(&mat_roty,roty);
    D3DXMatrixRotationX(&mat_rotx,rotx);

    mat_translate=mat_rotx*mat_roty;

    dev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1,0);
    dev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    dev->BeginScene();
        dev->SetFVF(CUSTOMFVF);
        dev->SetTransform(D3DTS_WORLD,&mat_translate);
        dev->SetTransform(D3DTS_VIEW,&mat_viewport);
        dev->SetTransform(D3DTS_PROJECTION,&mat_projection);
        dev->SetStreamSource(0,vbuf,0,sizeof(CUSTOMVERTEX));
        HRESULT f=dev->DrawPrimitive(D3DPT_TRIANGLELIST,0,mm1->mNumFaces);
        if (FAILED(f)) {
            crash("failed to draw vertex buffer.",f);
        }
    dev->EndScene();
    dev->Present(NULL,NULL,NULL,NULL);
}

没有DirectX错误,模型也没有显示。

1 个答案:

答案 0 :(得分:0)

我成功地使用Assimp将任何网格格式加载到我的directx9应用程序中。如果您仍然坚持这一点,请告诉我。

现在,我只能在没有任何纹理的情况下在第0个索引处加载网格物体。继续处理剩下的事情......让我知道:)