Question

Sadly, this question was never answered..

I am trying to load a .X file [ or really any other ] of a model with ASSIMP.

This is the code I'm trying to use:

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);
}

There are no DirectX errors, and the model does not show.

Was it helpful?

Solution

I was successfully able to load any mesh format into my directx9 application using Assimp. Please let me know in case you are still stuck on this.

Right now, I am able to load mesh at 0th index only without any textures. Working on rest of the stuff ... let me know :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top