The change is very project spedific so I don't know how much sense it makes to take this over to the wrapper.
But I will share anyway.
The rules:
- If there is only one subset, everything behaves as usual.
- If there are more than one subsets, the first one is used for collision, the remaining ones are used for display.
- The collision subset (first subset in mesh) must have at least some sort of skin (best is a skin without texture - it is never seen anyway).
- The subset order can be derived from the mesh group number in MED. It is important to make sure the collision mesh is the first group in MED
- This needs A7.8 due to the vmask instruction.
void newton_treecollisionaddentity(NewtonCollision* treecollision, ENTITY* entity)
{
if(!entity) return;
LPD3DXMESH pmesh = (LPD3DXMESH)ent_getmesh(entity, 0, 0);
int numvertices = pmesh->GetNumVertices();
int numfaces = pmesh->GetNumFaces();
int meshes = ent_status(entity, 9);
if (meshes > 1)
{
entity->vmask |= 0x1; //hide first mesh group, it is used for collision
}
_VERTEX_ *pvertices; pmesh->LockVertexBuffer(0, (void**)&pvertices);
short *pindices; pmesh->LockIndexBuffer(0, (void**)&pindices);
int *pattributes; pmesh->LockAttributeBuffer(0, &pattributes);
// transform vertices to world space
D3DXVECTOR4 *ptransformedvertices = (D3DXVECTOR4*)malloc(sizeof(D3DXVECTOR4) * numvertices);
D3DXMATRIX m; ent_getmatrix(entity, &m);
D3DXVECTOR3 tempvector;
int i;
for(i = 0; i < numvertices; i++)
{
tempvector.x = pvertices[i].x;
tempvector.y = pvertices[i].z; // direct3d -> 3dgs coordinate system
tempvector.z = pvertices[i].y; // direct3d -> 3dgs coordinate system
D3DXVec3Transform(&ptransformedvertices[i], &tempvector, &m);
}
// add triangles to collision tree
for(i = 0; i < numfaces; i++)
{
float v[9];
// use first mesh if only one mesh is available, otherwise use all meshes except first
if ((meshes > 1 && pattributes[i] == 0) || meshes == 1)
{
v[0] = ptransformedvertices[pindices[(i*3)+2]].x * QUANTTOMETER;
v[1] = ptransformedvertices[pindices[(i*3)+2]].y * QUANTTOMETER;
v[2] = ptransformedvertices[pindices[(i*3)+2]].z * QUANTTOMETER;
v[3] = ptransformedvertices[pindices[(i*3)+1]].x * QUANTTOMETER;
v[4] = ptransformedvertices[pindices[(i*3)+1]].y * QUANTTOMETER;
v[5] = ptransformedvertices[pindices[(i*3)+1]].z * QUANTTOMETER;
v[6] = ptransformedvertices[pindices[(i*3)+0]].x * QUANTTOMETER;
v[7] = ptransformedvertices[pindices[(i*3)+0]].y * QUANTTOMETER;
v[8] = ptransformedvertices[pindices[(i*3)+0]].z * QUANTTOMETER;
NewtonTreeCollisionAddFace(treecollision, 3, v, 12, pattributes[i]);
}
}
free(ptransformedvertices);
pmesh->UnlockVertexBuffer();
pmesh->UnlockIndexBuffer();
pmesh->UnlockAttributeBuffer();
}