🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How can I refactor this code to load an animation without a model?

Started by
1 comment, last by LorenzoGatti 3 days, 14 hours ago

Hello, sorry if this is a dumb thread to make, but I finished the tutorial on learnopengl for adding skeleton animations and in their code to load an animation they pass the animation path and model:

Animation danceAnimation("Assets/Animations/Ymca Dance.dae", myModel);

This seems to be necessary for this readMissingBones method not entirely sure what it's purpose is, but here is the code for it:

void Animation::readMissingBones(const aiAnimation* animation, Model& model) {
	int size = animation->mNumChannels;
	auto& boneInfoMap = model.m_BoneInfoMap;
	int& boneCount = model.m_BoneCounter; 

	//reading channels(bones engaged in an animation and their keyframes)
	for (int i = 0; i < size; i++)
	{
		auto channel = animation->mChannels[i];
		std::string boneName = channel->mNodeName.data;

		if (boneInfoMap.find(boneName) == boneInfoMap.end())
		{
			boneInfoMap[boneName].id = boneCount;
			boneCount++;
		}
		m_Bones.push_back(Bone(channel->mNodeName.data, boneInfoMap[channel->mNodeName.data].id, channel));
	}

	m_BoneInfoMap = boneInfoMap;
}

My problem with the way they've done this is it doesn't make sense that you would need to provide a model to load an animation ideally I'd want to just load my animation and play it

Animation myAnimation = loadAnimation("path/to/animation");
myAnimatedModel->m_animator->playAnimation(myAnimation); // or something like playAnimation(myModel, myAnimation)

To achieve I'm pretty sure I'll need to decouple the model from the animation class so that readMissingBones() doesn't need it, but not sure how so just wanted to get some suggestions from others.

Advertisement

From the referenced tutorial:

Then we call a function called ReadMissingBones. I had to write this function because sometimes when I loaded FBX model separately, it had some bones missing and I found those missing bones in the animation file. This function reads the missing bones information and stores their information in m_BoneInfoMap of Model and saves a reference of m_BoneInfoMap locally in the m_BoneInfoMap.

Either you have the same problem, and you need ReadMissingBones() to load bones all over again and deal with loading issues through brute force, or you don't have the same problem, and boneInfoMap from the initial loading is fine.

Personally, I would avoid this mess of defective files and/or defective loading code by adopting a different file format.

Omae Wa Mou Shindeiru

Advertisement