Jump to content
  • Advertisement
Sign in to follow this  
lougv22

why is bone -1 for each keyframe in a 3d model?

This topic is 2543 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

[color="#4A4A4A"]I am trying to load a 3D model using the following code:

[color="#4a4a4a"]public void Load(string modelFileName)
{
if (!_isInitialized)
Initialize();

_model = Game.Content.Load<Model>(GameAssetsPath.MODELS_PATH + modelFileName);

// Get the animated model data
Dictionary<string, object> modelTag = (Dictionary<string, object>)_model.Tag;
if (modelTag == null)
throw new InvalidOperationException("This is not a valid animated model.");

// Read tag data
if (modelTag.ContainsKey("AnimatedModelData"))
_animatedModelData = (AnimatedModelData)modelTag["AnimatedModelData"];
else
throw new InvalidOperationException("This is not a valid animated model.");

// Animation
_animationSpeed = 1.0f;
_activeAnimationKeyframe = 0;
_activeAnimationTime = TimeSpan.Zero;

if (_animatedModelData.Animations.Length > 0)
_activeAnimation = _animatedModelData.Animations[0];

_bones = new Matrix[_animatedModelData.BonesBindPose.Length];
_bonesAbsolute = new Matrix[_animatedModelData.BonesBindPose.Length];
_bonesAnimation = new Matrix[_animatedModelData.BonesBindPose.Length];
_bonesTransform = new Matrix[_animatedModelData.BonesBindPose.Length];

for (int i = 0; i < _bones.Length; i++)
{
_bones = _animatedModelData.BonesBindPose;
_bonesTransform = Matrix.Identity;
}

// Get the animated model effect - shared by all meshes
_animatedModelEffect = new AnimatedModelEffect(_model.Meshes[0].Effects[0]);

// Create a default material
_lightMaterial = new LightMaterial();
}



[color="#4A4A4A"]the problem is that the Bone property for each keyframe is -1 (as shown in the screenshot below):
loading_mesh_debug.png

[color="#4A4A4A"]and that causes a System.IndexOutOfRangeException was unhandled error in the following code:

[color="#4A4A4A"]loading_mesh_debug_1.png


[color="#4A4A4A"]What am I doing wrong here? Why is this happening?

[color="#4A4A4A"]Btw, I am using XNA 4.0.

Share this post


Link to post
Share on other sites
Advertisement
[color="#4A4A4A"]Upon further inspection I've narrowed the problem down to the following function in my custom Data Reader class:

public class KeyframeReader : ContentTypeReader<Keyframe>
{
protected override Keyframe Read(ContentReader input, Keyframe existingInstance)
{
TimeSpan time = input.ReadObject<TimeSpan>();
int boneIndex = input.ReadInt32();
Matrix transform = input.ReadMatrix();

return new Keyframe(time, boneIndex, transform);
}
}





public class AnimationDataReader : ContentTypeReader<AnimationData>
{
protected override AnimationData Read(ContentReader input, AnimationData existingInstance)
{
String name = input.ReadString();
TimeSpan duration = input.ReadObject<TimeSpan>();
Keyframe[] keyframes = input.ReadObject<Keyframe[]>();

return new AnimationData(name, duration, keyframes);
} }


[color="#4A4A4A"]In the first function above, boneIndex is always -1. This method is being called by the second one.

[color="#4A4A4A"]Any ideas?

Share this post


Link to post
Share on other sites
I figured it out. The problem was that I wasn't correctly writing the bone index in my custom writer. At first I thought the writer wasn't even getting called at all because I had put break points in it and none of them were getting hit, but then I realized the writer only executes during compile time. Duh.

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!