Here is an example of a function that divides a mesh into a series of irregularly sized subsets, allowing subsets to consist of non-contiguous series of faces:
if (FAILED(SetSubsets(pMesh,faceCount,subsetNum)) {
// handle error
...
DWORD SetSubsets(ID3DXMesh *pMesh,DWORD *pFaceCount,DWORD *pSubsetNum) {
// initialize face counter
DWORD faceNum=0;
// loop through the subsets
for (int i=0;pFaceCount[i];i++) {
// make sure there are enough faces for this subset
if (faceNum+pFaceCount[i]>=numFaces) {
// not enough faces, unlock the attribute buffer and return error code
pMesh->UnlockAttributeBuffer();
}
for (int j=0;j<pFaceCount[i];i++) {
// set the subset number of each face in this range
attribBuf[faceNum]=pSubsetNum[i];
// increment face counter
faceNum++;
}
}
// unlock the attribute buffer
pMesh->UnlockAttributeBuffer();
// allocate storage and generate adjacency data
DWORD *pAdj=new DWORD[numFaces*3];
if (!pAdj)
return E_OUTOFMEMORY;
if (FAILED(hr=pMesh->GenerateAdjacency(0.0f,pAdj) {
delete pAdj;
return hr;
}
// optimize the mesh with attribute sorting
// D3DXMESHOPT_ATTRSORT
if (FAILED(hr=pMesh->OptimizeInPlace(D3DXMESHOPT_VERTEXCACHE,pAdj,NULL,NULL,NULL)) {
delete pAdj;
}
// de-allocate adjacency data storage
delete pAdj;
} else
// return error code on failure to lock attribute buffer
return hr;
// return success
return S_OK;
Note that if you already have adjacency data for the mesh, you won't have to re-compute it in the function as I have here - I included that for simplicity. If you already have adjacency data you can pass it to the optimization function, and may also need to provide a pointer to an array to contain the new adjacency information, as a parameter to the optimization function, if you have further use for this data.