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

Started by
1 comment, last by lougv22 12 years, 3 months ago
[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.
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?

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.

This topic is closed to new replies.

Advertisement