Sign in to follow this  
Sagito

ID3DXMesh Problem

Recommended Posts

Sagito    129
Hi, After parsing and loading an external file with a 3D model, I'm trying to use the data I have read to build an ID3DXMesh*. I'm doing the following:
DWORD vertexFVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
D3DXCreateMeshFVF(nfaces, nfaces * 3, D3DXMESH_DYNAMIC, vertexFVF, this->device, &this->mesh);

EObjVertex* vBuffer = NULL;
WORD* vIBuffer = NULL;
if (FAILED(this->mesh->LockVertexBuffer(NULL, (void**)&vBuffer)))
	return;

int index = 0;
for (int a = 0; a < objects.size(); a++)
{
	vector<EObjFace*> faces = objects[a]->getFaces();

	for (int i = 0; i < faces.size(); i++)
	{
		EObjFace* actFace = faces[i];
		EObjVertex* v1 = &actFace->ov1;
		EObjVertex* v2 = &actFace->ov2;
		EObjVertex* v3 = &actFace->ov3;

		memcpy(&vBuffer[index++], v1, sizeof(EObjVertex));
		memcpy(&vBuffer[index++], v2, sizeof(EObjVertex));
		memcpy(&vBuffer[index++], v3, sizeof(EObjVertex));
	}
}

this->mesh->UnlockVertexBuffer();

//Take care of the index buffer
if (FAILED(this->mesh->LockIndexBuffer(NULL, (void**)&vIBuffer)))
	return;

for (int a = 0; a < objects.size(); a++)
{
	vector<EObjFace*> faces = objects[a]->getFaces();

	for (int i = 0; i < faces.size(); i++)
	{
		WORD in = i * 3;
		vIBuffer[in] = i * 3;
		vIBuffer[in + 1] = in + 1;
		vIBuffer[in + 2] = in + 2;
	}
}

this->mesh->UnlockIndexBuffer();

//Attempt to define subsets
DWORD* id;
if (FAILED(this->mesh->LockAttributeBuffer(D3DLOCK_DISCARD, &id)))
	return;

for (int a = 0; a < objects.size(); a++)
{
	vector<EObjFace*> faces = objects[a]->getFaces();

	for (int i = 0; i < faces.size(); i++)
	{
		EObjFace* actFace = faces[i];
		int materialID = 0;

		for (unsigned int e = 0; e < materials.size(); e++)
		{
			if (materials[e].name == actFace->materialname)
			{
				materialID = e;
				break;
			}
		}

		*id = 0;
		id++;
	}
}
this->mesh->UnlockAttributeBuffer();

The problem is that if I draw it like the following code snippet, the mesh is renderer correctly:
this->mesh->GetVertexBuffer(&vb);
this->mesh->GetIndexBuffer(&ib);
this->device->SetStreamSource(0, vb, 0, sizeof(EObjVertex));
this->device->SetFVF(vertexFVF);

this->device->SetIndices(ib);
this->device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 3 * nfaces, 0, nfaces);

But if I call this->mesh->DrawSubset(0) (I'm putting everything in the same subset for now), the mesh is rendered with random vertex positions, all scrambled up... Any ideia why this is happening? Thanks in advance...

Share this post


Link to post
Share on other sites
MJP    19756
I'm not seeing anything immediately wrong with your code (although using D3DXMESH_DYNAMIC is weird...are you constantly changing data in the vertex and index buffers?). I'd suggest looking at what happens in DrawSubset using PIX. Just expand the call, and it'll show you the D3D9 calls being made behind the scenes.

Share this post


Link to post
Share on other sites
Sagito    129

Hmm, tried using PIX here now and it just crashes... PIX's debug shows random places each time I ran it... Once in the mesh creation, another time in light creation (which is completely out of bounds), keyboard handling (also completely out of bounds), etc...

Could this be a bad pointer somewhere, memory leak, or something like that?

Share this post


Link to post
Share on other sites
legalize    116
Like the other poster, I don't see anything obviously wrong with your code, but I've done this before and I suspect that you have a bug somewhere in your code that fills the index buffer or the vertex buffer. Perhaps reading Chapter 19. D3DX Mesh Objects from my book will help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this