What is wrong?(assimp)

Started by
1 comment, last by Krohm 10 years, 2 months ago

So,I'm trying to load a model with assimp,but the resulting model is messed up really bad.

Keep in mind that right now all I use is a vertex position and a vertex normal(no texture coords)

This is the code that creates the vertex/index buffers and fills them.


void testt::build(){
	const aiScene* scene;
	scene = aiImportFile("C:\\Program Files (x86)\\Microsoft DirectX SDK (June 2010)\\Samples\\Media\\Dwarf\\Dwarf.x", aiProcess_MakeLeftHanded |
		aiProcess_FlipWindingOrder);

	total_vertices = 0;
	total_indices = 0;

	
	for (long long i = 0; i < scene->mNumMeshes; i++){
		total_vertices += scene->mMeshes[i]->mNumVertices;
		total_indices += scene->mMeshes[i]->mNumFaces * 3;
	}
	


	Devil_Room::Brain::pDevice->CreateVertexBuffer(total_vertices * sizeof(Devil_Room::Vertex), D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_NORMAL, D3DPOOL_DEFAULT, &vb, NULL);
	Devil_Room::Brain::pDevice->CreateIndexBuffer(sizeof(DWORD32)* total_indices, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT,
		&ib, NULL);

	int nr_meshes;
	int nr_textures;



	long long nr_ofAddedvertices = 0;
	long long nr_ofAddedindices = 0;

	nr_meshes = scene->mNumMeshes;
	for (long long i = 0; i < nr_meshes; i++){
		Vertex* vertices = new Vertex[scene->mMeshes[i]->mNumVertices];
		for (long long x = 0; x < scene->mMeshes[i]->mNumVertices; x++){
			const aiVector3D* pPos = &(scene->mMeshes[i]->mVertices[x]);
			const aiVector3D* pNormal = &(scene->mMeshes[i]->mNormals[x]);
			vertices[x].x = pPos->x;
			vertices[x].y = pPos->y;
			vertices[x].z = pPos->z;
			vertices[x].Normal.x = pNormal->x;
			vertices[x].Normal.y = pNormal->y;
			vertices[x].Normal.z = pNormal->z;
		}

		Vertex* vt;
		vb->Lock(nr_ofAddedvertices * sizeof(Vertex),
			(total_vertices - nr_ofAddedvertices) * sizeof(Vertex),
			reinterpret_cast<void**>(&vt), 0);

		for (long long x = 0; x < scene->mMeshes[i]->mNumVertices; x++){
			vt[x] = vertices[x];
		}
		nr_ofAddedvertices += scene->mMeshes[i]->mNumVertices;


		vb->Unlock();


		DWORD32 * indices = new DWORD32[scene->mMeshes[i]->mNumFaces * 3];
		long long pos_ind = 0;

		for (long long z = 0;z < scene->mMeshes[i]->mNumFaces; z++) {
			const aiFace& Face = scene->mMeshes[i]->mFaces[z];
			indices[pos_ind++] = Face.mIndices[0];
			indices[pos_ind++] = Face.mIndices[1];
			indices[pos_ind++] = Face.mIndices[2];

		}

		unsigned long* vtt;
		ib->Lock(nr_ofAddedindices * sizeof(DWORD32),
			total_indices - nr_ofAddedindices * sizeof(DWORD32),
			reinterpret_cast<void**>(&vtt), 0);

		for (int x = 0; x < scene->mMeshes[i]->mNumFaces * 3; x++){
			vtt[x] = indices[x];
		}

		nr_ofAddedindices += scene->mMeshes[i]->mNumFaces * 3;
		ib->Unlock();

		delete[] vertices;
		delete[] indices;
	}

}
Advertisement

Is the position and normal components of the vertex using floats for there data type ?

That's all i can think of right now, If you would like I could post the ASSIMP V2 related code that I use.

Side notes:

You could just accumalate all the vertices into one buffer and then fill the VB from that with a memcpy.

And you could memcpy all the index data into the VB's buffer in one shot.

Hope that helps.

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

It's a long time I dwelved in my import code to work with AssImp but...


DWORD32 * indices = new DWORD32[scene->mMeshes[i]->mNumFaces * 3];
long long pos_ind = 0;

for (long long z = 0;z < scene->mMeshes[i]->mNumFaces; z++) {
	const aiFace& Face = scene->mMeshes[i]->mFaces[z];
	indices[pos_ind++] = Face.mIndices[0];
	indices[pos_ind++] = Face.mIndices[1];
	indices[pos_ind++] = Face.mIndices[2];
}

unsigned long* vtt;
ib->Lock(nr_ofAddedindices * sizeof(DWORD32),
total_indices - nr_ofAddedindices * sizeof(DWORD32),
reinterpret_cast<void**>(&vtt), 0);
for (int x = 0; x < scene->mMeshes[i]->mNumFaces * 3; x++){
	vtt[x] = indices[x];
}

nr_ofAddedindices += scene->mMeshes[i]->mNumFaces * 3;
ib->Unlock();

This sure does not produce correct indices. Unless you correct the missing information with draw call / stream port offsets.

You cannot copy index values like that. If memory serves, AssImp models each mesh as it's on its own VB. When you concatenate the vertices in a single VB, only the first loaded mesh gets the correct layout. nr_ofAddedvertices has to be added to all values packed to vtt.

BTW, just Lock before the loop. If you look closely at what you're doing, you'll notice all other locks are perfectly equivalent to an offset operation. It doesn't really make any sense to do that!

Previously "Krohm"

This topic is closed to new replies.

Advertisement