Multi-Texturing models with Collada and DirectX Problem

I have been working on a Collada Model Viewer/Loader with DirectX and came across a problem with texturing meshes. The problem I’m coming across is when rendering the model with DirectX it is only using the last texture in a vector of the model’s textures and it is creating new verts per subset.

The model is a cube and it should only have 0-7 verts but after exporting it is showing that it contains 0-23 verts within the indices. This is creating confusion with Directx

Here is a screen shot of a cube that is supposed to have 6 different textures 1-6:

The process that I’m using is:

  1. Create a 3D model in 3DS Max 2009
  2. Export it as a .dae file with OpenCollada
  3. Run it through Collada Refinery to format the .dae file
  4. Read and parse the data from the .dae file in my DirectX application

The code that I wrote out to render the mesh is below:

void Mesh::Render(IDirect3DDevice9 *device, CCARCamera *camera)
{

	device->SetVertexDeclaration(Vertex::Decl);
	//Set the vertex and index buffers
	device->SetStreamSource(0,m_VertexBuffer,0,sizeof(Vertex));
	device->SetIndices(m_IndexBuffer);
	
	//Set the camera's view and projection matrices to the scene
	m_fx->SetTechnique(mhTech);
	m_fx->SetMatrix("View", &camera->GetViewMtx());
	m_fx->SetMatrix("Projection", &camera->GetProjectionMtx());
	//Set the world matrix to the effects
	m_fx->SetMatrix("World",&m_mWorld);

	//Check to see if this mesh has textures
	if(m_iTexCount > 0)
	{
		//Begin drawing the mesh
		UINT numPasses = 0;
		m_fx->Begin(&numPasses, 0);
		for(int i = 0; i < m_iTexCount; i++)
		{
			m_fx->SetTexture(mhTex,m_vTextureList[i]);
			for(UINT it = 0; it < numPasses; ++it)
			{
				m_fx->BeginPass(it);
				m_fx->CommitChanges();
				//Draw the mesh with the index and vertex data
				DrawSubset(i);		
			}
		}
		//End drawing the mesh
		m_fx->EndPass();
		m_fx->End();
	}
	//else render the model without textures
	else
	{
		//Begin drawing the mesh
		UINT numPasses = 0;
		m_fx->Begin(&numPasses, 0);
		for(UINT i = 0; i < numPasses; ++i)
		{
			m_fx->BeginPass(i);
			//Draw the mesh with the index and vertex data
			device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_vVertices.size(), 0, m_vIndices.size() / 3);
			//End drawing the mesh
			m_fx->EndPass();
		}
		m_fx->End();
	}
}

void Mesh::DrawSubset(int i)
{
	int subsetSize = 0;
	//iterate through the subsets vector and find the size of the current subset	
	for(int it = 0; it < m_vSubsets.size();it++)
	{
		if(m_vSubsets[it].first == i)
			subsetSize++;
		else
			break;
	}
	//Draw the mesh with the index and vertex data
	gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, i*subsetSize, 0, m_vVertices.size(), 0, m_vIndices.size()/3);
}

So to reiterate, the main problem I’m having is texturing any model with multiple textures within specific groups.
I’d greatly appreciate any help that anyone can provide.

A.J. Garcia

Why do you do step (3)? How is this changing to document for you?

Can you upload the document exported by OpenCOLLADA (before any other steps are taken)?

I utilize Collada Refinery in order to format the .dae file in a manner where my DirectX code can read the file.

The difference between the original .dae file and the .dae file after being run with Collada Refinery is:

  1. I optimize the mesh with the optimization macro,
  2. Format the <polgons> tags to <triangles>
  3. Utilize the deindexer macro to reduce the number of indices that are being unnecessarily rewritten.

Here is a link the original .dae file that was exported from 3ds Max 2009:
http://www.mediafire.com/?tkumebeq67tj5lp

Here is a link to the .dae file that was ran through Collada Refinery:
http://www.mediafire.com/?nw9vtivr7fv9pzc

It’s the Refinery that has baked your box into a set of 24 vertex attributes because there are 24 unique sets of normal, texcoord, textangent, and texbinormal for the 8 vertex positions.

There are 6 materials with different diffuse textures, one per face (two triangles).

Either way the two documents appear valid and contain the same information (and triangles), so you should be able to import them both.

It looks like you have not setup your indexed primitive correctly in D3D from the content in the document.

Hello!
I think the main problem here is that m_fx->EndPass() is not in proper place. I put some comments in red in your code.
Greetings