Updating Skinned Meshes
A skinned mesh works like this: Each vertex is attached to an imaginary bone
(which is specified by a frame object). As these frames move, so do the vertices
attached to them. To update the coordinates of the vertices as the bones move,
you need to call a special function that takes the source vertex data,
transforms it according to the bones' transformations, and stores the results in
a second mesh object. This special function is called
ID3DXSkinInfo::UpdateSkinnedMesh.
Whenever you load a mesh using the D3DXLoadSkinMeshFromXof function (which is
what the second LoadMesh function does), you get a pointer to an ID3DXSkinInfo
object. This object contains the information about which vertices are attached
to which bones. This way, the object knows which transformations to apply to the
vertices.
To update the vertices, you must first lock the mesh's vertex buffer (which
contains the source vertex coordinates), as well as the destination mesh's
vertex buffer. The destination mesh will receive the updated vertices as they
are transformed. Once locked, you need to call UpdateSkinnedMesh, also
specifying a series of transformation matrices (stored as D3DXMATRIX objects)
that represent the various bone transformations.
For now, just check out the update_skin_mesh helper function code to see how
it updates the skinned meshes for you.
HRESULT update_skin_mesh(D3DXMESHCONTAINER_EX* mesh_container)
{
if(mesh_container == NULL)
return E_FAIL;
if(mesh_container->MeshData.pMesh == NULL || mesh_container->skin_mesh == NULL || mesh_container->pSkinInfo == NULL)
return E_FAIL;
if(mesh_container->bone_matrices == NULL || mesh_container->frame_matrices == NULL)
return E_FAIL;
// copy the bone matrices over (must have been combined before call draw_mesh)
for(DWORD i = 0; i < mesh_container->pSkinInfo->GetNumBones(); i++)
{
// start with bone offset matrix
mesh_container->bone_matrices[i] = *(mesh_container->pSkinInfo->GetBoneOffsetMatrix(i));
// apply frame transformation
if(mesh_container->frame_matrices[i])
mesh_container->bone_matrices[i] *= (*mesh_container->frame_matrices[i]);
}
void* src_vertices;
void* dest_vertices;
mesh_container->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&src_vertices);
mesh_container->skin_mesh->LockVertexBuffer(0, (void**)&dest_vertices);
// update the skinned mesh using provided transformations
mesh_container->pSkinInfo->UpdateSkinnedMesh(mesh_container->bone_matrices, NULL, src_vertices, dest_vertices);
mesh_container->MeshData.pMesh->UnlockVertexBuffer();
mesh_container->skin_mesh->UnlockVertexBuffer();
return S_OK;
}
Aside from the typical error−checking code, the update_skin_mesh function starts
by looping through each bone contained within the ID3DXSkinInfo object (stored
in the D3DXMESHCONTAINER_EX object you've already loaded). For each bone, the
original transformation matrix from the .X file is grabbed and stored in an
array of matrices used in the call to UpdateSkinnedMesh.
From here the bone's
transformation, as stored in the bone's respective frame object, is applied to
the transformation matrix. This process continues until all transformation
matrices are set up
At this point, you are ready to lock the vertex buffers and call the
UpdateSkinnedMesh function.
The function is finished by unlocking the buffers and returning a success
code.
And once again speaking of rendering, it is finally time to see the helper
functions I created to get those
meshes on screen!