Jump to content
  • Advertisement
Sign in to follow this  
noatom

What is wrong?(assimp)

This topic is 2098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!