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:
- Create a 3D model in 3DS Max 2009
- Export it as a .dae file with OpenCollada
- Run it through Collada Refinery to format the .dae file
- 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