gpt4 book ai didi

c++ - ASSIMP 网格与 directx9

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:10 25 4
gpt4 key购买 nike

可悲的是,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错误,模型不显示。

最佳答案

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

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

关于c++ - ASSIMP 网格与 directx9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15602484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com