2012-08-09 09:45:04 +00:00
|
|
|
#region Using Statements
|
2011-11-07 21:19:15 +00:00
|
|
|
using System;
|
2012-10-13 11:25:07 +00:00
|
|
|
using ANX.Framework.NonXNA.Development;
|
2011-11-07 21:19:15 +00:00
|
|
|
|
|
|
|
#endregion // Using Statements
|
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-11-07 21:19:15 +00:00
|
|
|
|
|
|
|
namespace ANX.Framework.Graphics
|
|
|
|
{
|
2012-10-13 11:25:07 +00:00
|
|
|
[PercentageComplete(100)]
|
2012-10-16 16:43:18 +00:00
|
|
|
[Developer("Glatzemann")]
|
2012-10-13 11:25:07 +00:00
|
|
|
[TestState(TestStateAttribute.TestState.Untested)]
|
2011-11-07 21:19:15 +00:00
|
|
|
public sealed class Model
|
|
|
|
{
|
2012-10-13 11:25:07 +00:00
|
|
|
public ModelBoneCollection Bones { get; private set; }
|
|
|
|
public ModelMeshCollection Meshes { get; private set; }
|
|
|
|
public ModelBone Root { get; private set; }
|
|
|
|
public object Tag { get; set; }
|
2011-11-07 21:19:15 +00:00
|
|
|
|
2012-03-09 23:23:51 +00:00
|
|
|
private static Matrix[] cachedBonesArray;
|
|
|
|
|
2011-11-12 00:17:25 +00:00
|
|
|
public Model(ModelBoneCollection bones, ModelMeshCollection meshes, ModelBone rootBone, Object tag)
|
|
|
|
{
|
2012-10-13 11:25:07 +00:00
|
|
|
this.Bones = bones;
|
|
|
|
this.Meshes = meshes;
|
|
|
|
this.Root = rootBone;
|
|
|
|
this.Tag = tag;
|
2011-11-07 21:19:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-12 00:17:25 +00:00
|
|
|
public void CopyAbsoluteBoneTransformsTo(Matrix[] destinationBoneTransforms)
|
|
|
|
{
|
2012-03-09 23:23:51 +00:00
|
|
|
if (destinationBoneTransforms == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("destinationBoneTransforms");
|
|
|
|
}
|
|
|
|
|
2012-10-13 11:25:07 +00:00
|
|
|
if (destinationBoneTransforms.Length < Bones.Count)
|
2012-03-09 23:23:51 +00:00
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException("destinationBoneTransforms");
|
|
|
|
}
|
|
|
|
|
2012-10-13 11:25:07 +00:00
|
|
|
for (int i = 0; i < Bones.Count; i++)
|
2012-03-09 23:23:51 +00:00
|
|
|
{
|
2012-10-13 11:25:07 +00:00
|
|
|
var bone = Bones[i];
|
2012-03-09 23:23:51 +00:00
|
|
|
if (bone.Parent == null)
|
|
|
|
{
|
|
|
|
destinationBoneTransforms[i] = bone.Transform;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int parentIndex = bone.Parent.Index;
|
|
|
|
destinationBoneTransforms[i] = bone.Transform * destinationBoneTransforms[parentIndex];
|
|
|
|
}
|
|
|
|
}
|
2011-11-07 21:19:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-12 00:17:25 +00:00
|
|
|
public void CopyBoneTransformsFrom(Matrix[] sourceBoneTransforms)
|
|
|
|
{
|
2012-03-09 23:23:51 +00:00
|
|
|
if (sourceBoneTransforms == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("sourceBoneTransforms");
|
|
|
|
}
|
|
|
|
|
2012-10-13 11:25:07 +00:00
|
|
|
if (sourceBoneTransforms.Length < Bones.Count)
|
2012-03-09 23:23:51 +00:00
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException("sourceBoneTransforms");
|
|
|
|
}
|
|
|
|
|
2012-10-13 11:25:07 +00:00
|
|
|
for (int i = 0; i < Bones.Count; i++)
|
2012-03-09 23:23:51 +00:00
|
|
|
{
|
2012-10-13 11:25:07 +00:00
|
|
|
Bones[i].Transform = sourceBoneTransforms[i];
|
2012-03-09 23:23:51 +00:00
|
|
|
}
|
2011-11-07 21:19:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-12 00:17:25 +00:00
|
|
|
public void CopyBoneTransformsTo(Matrix[] destinationBoneTransforms)
|
|
|
|
{
|
2012-03-09 23:23:51 +00:00
|
|
|
if (destinationBoneTransforms == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("destinationBoneTransforms");
|
|
|
|
}
|
|
|
|
|
2012-10-13 11:25:07 +00:00
|
|
|
if (destinationBoneTransforms.Length < Bones.Count)
|
2012-03-09 23:23:51 +00:00
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException("destinationBoneTransforms");
|
|
|
|
}
|
|
|
|
|
2012-10-13 11:25:07 +00:00
|
|
|
for (int i = 0; i < Bones.Count; i++)
|
2012-03-09 23:23:51 +00:00
|
|
|
{
|
2012-10-13 11:25:07 +00:00
|
|
|
destinationBoneTransforms[i] = Bones[i].Transform;
|
2012-03-09 23:23:51 +00:00
|
|
|
}
|
2011-11-07 21:19:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-12 00:17:25 +00:00
|
|
|
public void Draw (Matrix world, Matrix view, Matrix projection)
|
|
|
|
{
|
2012-03-09 23:23:51 +00:00
|
|
|
Matrix[] absTransforms = Model.cachedBonesArray;
|
2012-10-13 11:25:07 +00:00
|
|
|
if (absTransforms == null || absTransforms.Length < this.Bones.Count)
|
2012-03-09 23:23:51 +00:00
|
|
|
{
|
2012-10-13 11:25:07 +00:00
|
|
|
Array.Resize<Matrix>(ref absTransforms, this.Bones.Count);
|
2012-03-09 23:23:51 +00:00
|
|
|
Model.cachedBonesArray = absTransforms;
|
|
|
|
}
|
|
|
|
this.CopyAbsoluteBoneTransformsTo(absTransforms);
|
|
|
|
|
2012-10-13 11:25:07 +00:00
|
|
|
foreach (var mesh in Meshes)
|
2012-03-09 23:23:51 +00:00
|
|
|
{
|
|
|
|
foreach (var effect in mesh.Effects)
|
|
|
|
{
|
|
|
|
IEffectMatrices matEffect = effect as IEffectMatrices;
|
|
|
|
if (matEffect != null)
|
|
|
|
{
|
|
|
|
matEffect.World = absTransforms[mesh.ParentBone.Index] * world;
|
|
|
|
matEffect.View = view;
|
|
|
|
matEffect.Projection = projection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh.Draw();
|
|
|
|
}
|
2011-11-12 00:17:25 +00:00
|
|
|
}
|
2011-11-07 21:19:15 +00:00
|
|
|
}
|
|
|
|
}
|