Jump to content
  • Advertisement
Sign in to follow this  
GameDevEnthusiast

[Assimp] Build the skeleton from the scene

This topic is 2108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How to construct the 'global' skeleton from a scene imported by the Open Asset Import Library (Assimp) ?

 

In Assimp, a scene is essentially a hierarchy of nodes, each node can possibly have meshes, each mesh can have a set of bones so it's easy to build a joint hierarchy for a single mesh, but I need to build the joint tree for the whole mesh (as in Doom 3).

 

Share this post


Link to post
Share on other sites
Advertisement

yes, after a week of painful debugging and studying tens of skeletal anim samples i'm able to build a basic skeleton, interpolate and display it correctly (one of the problems was with my quaternion math - needed to multiply the .w component by -1).

 

I'm going to abandon mSkinOffset and instead calculate inverse bind pose matrices myself (after building the 'whole mesh' skeleton with absolute joint positions and orientations in object space).

 

I will post my code in the hope that it will be useful.

Here's how I build the skeleton: I gather all bone names - I use them to prune the skeleton - the resulting joint tree will be same as originally defined in md5 mesh file.

// set of all animated bones/nodes
typedef std::set< std::string >	BoneNameSetT;

static void GatherBoneNames( const aiScene* scene, const NodeMapT& nodes, BoneNameSetT &bones )
{
	bones.clear();
	// Loop through each bone of each mesh.
    for( int meshIndex = 0; meshIndex < scene->mNumMeshes; meshIndex++ ) 
	{
		const aiMesh* mesh = scene->mMeshes[ meshIndex ];
		for( int boneIndex = 0; boneIndex < mesh->mNumBones; boneIndex++ )
		{
			const aiBone* bone = mesh->mBones[ boneIndex ];
			const aiNode* node = FindNodeByName( nodes, bone->mName.C_Str() );
			while( node )
			{
				bones.insert( node->mName.C_Str() );
				node = node->mParent;
				if( node && node->mNumChildren == 1 ) {
					break;	// don't chase up until the scene root, if possible
				}
			}
		}
	}
}

struct BoneDesc
{
	const aiNode *	node;
	std::string	name;	
	int		parentIndex;
	aiMatrix4x4	globalTransform;// absolute (world => node) transform
};

static void BuildSkeleton(const aiNode* node,
						  const BoneNameSetT& boneNames,
						  const aiMatrix4x4& parentTransform,
						  const int parentBoneIndex,
						  TArray< BoneDesc* > &skeleton)
{
	// accumulated parents => node transform
	const aiMatrix4x4 globalTransform = node->mTransformation * parentTransform;	// P * B

	int newBoneIndex = parentBoneIndex;

	BoneNameSetT::const_iterator boneIt = boneNames.find( node->mName.C_Str() );
	if( boneIt != boneNames.end() )
	{
		newBoneIndex = skeleton.Num();

		BoneDesc* newBone = new BoneDesc();
		skeleton.Add(newBone);

		newBone->node = node;
		newBone->name = node->mName.C_Str();		
		newBone->globalTransform = globalTransform;
		newBone->parentIndex = parentBoneIndex;
	}
	for( int childIndex = 0; childIndex < node->mNumChildren; childIndex++ )
	{
		const aiNode* childNode = node->mChildren[ childIndex ];
		BuildSkeleton(childNode, boneNames, globalTransform, newBoneIndex, skeleton);
	}
}

Here's how I calculate local joint transforms:

		// The bone's transformation in the skeleton space,
		// aka the bind matrix - the bone's parent's local matrices concatenated with the bone's local matrix.
		const aiMatrix4x4 nodeGlobalTransform = boneDesc->globalTransform;
		//Assert(nodeGlobalTransform == CalculateGlobalTransform(node));

		// The transformation relative to the bone's parent (parent => bone space).
		aiMatrix4x4 nodeLocalTransform;
		if( boneDesc->parentIndex != -1 ) {
			const BoneDesc* parentBone = skeleton[ boneDesc->parentIndex ];
			aiMatrix4x4 parentGlobalTransform = parentBone->globalTransform;
			aiMatrix4x4 inverseParentGlobalTransform(parentGlobalTransform);
			inverseParentGlobalTransform.Inverse();
			nodeLocalTransform = nodeGlobalTransform * inverseParentGlobalTransform;	// N = P^-1 * B
		} else {
			nodeLocalTransform = node->mTransformation;
		}

		aiVector3D	scaling;
		aiQuaternion	rotation;
		aiVector3D	translation;
		nodeLocalTransform.Decompose(scaling, rotation, translation);

		Vector3D	jointTranslation = ToMyVec3D(translation);
		Quaternion	jointOrientation = ToMyQuat(rotation);

I studied the samples very closely: scenes are animated by interpolating node movement, but I don't want to have the full node hierarchy at run-time with redundant bones (referring to the same node, but having different mOffsetMatrix matrices, as mentioned.

http://www.gamedev.net/topic/630903-assimp-and-collada-bind-shape-matrix/?view=findpost&p=4977756

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!