2012-08-09 09:45:04 +00:00
|
|
|
#region Using Statements
|
2011-11-07 21:19:15 +00:00
|
|
|
using System;
|
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
|
|
|
public sealed class Model
|
|
|
|
{
|
2011-11-12 00:17:25 +00:00
|
|
|
private ModelBoneCollection bones;
|
|
|
|
|
|
|
|
public ModelBoneCollection Bones
|
2011-11-07 21:19:15 +00:00
|
|
|
{
|
2011-11-12 00:17:25 +00:00
|
|
|
get { return bones; }
|
2011-11-07 21:19:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-12 00:17:25 +00:00
|
|
|
private ModelMeshCollection meshes;
|
|
|
|
|
|
|
|
public ModelMeshCollection Meshes
|
2011-11-07 21:19:15 +00:00
|
|
|
{
|
2011-11-12 00:17:25 +00:00
|
|
|
get { return meshes; }
|
2011-11-07 21:19:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-12 00:17:25 +00:00
|
|
|
private ModelBone root;
|
|
|
|
|
|
|
|
public ModelBone Root
|
2011-11-07 21:19:15 +00:00
|
|
|
{
|
2011-11-12 00:17:25 +00:00
|
|
|
get { return root; }
|
2011-11-07 21:19:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-12 00:17:25 +00:00
|
|
|
private Object tag;
|
|
|
|
|
|
|
|
public Object Tag
|
2011-11-07 21:19:15 +00:00
|
|
|
{
|
2011-11-12 00:17:25 +00:00
|
|
|
get { return tag; }
|
|
|
|
set { tag = value; }
|
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)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destinationBoneTransforms.Length < bones.Count)
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException("destinationBoneTransforms");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < bones.Count; i++)
|
|
|
|
{
|
|
|
|
var bone = bones[i];
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sourceBoneTransforms.Length < bones.Count)
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException("sourceBoneTransforms");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < bones.Count; i++)
|
|
|
|
{
|
|
|
|
bones[i].Transform = sourceBoneTransforms[i];
|
|
|
|
}
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destinationBoneTransforms.Length < bones.Count)
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException("destinationBoneTransforms");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < bones.Count; i++)
|
|
|
|
{
|
|
|
|
destinationBoneTransforms[i] = bones[i].Transform;
|
|
|
|
}
|
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;
|
|
|
|
if (absTransforms == null || absTransforms.Length < this.bones.Count)
|
|
|
|
{
|
|
|
|
Array.Resize<Matrix>(ref absTransforms, this.bones.Count);
|
|
|
|
Model.cachedBonesArray = absTransforms;
|
|
|
|
}
|
|
|
|
this.CopyAbsoluteBoneTransformsTo(absTransforms);
|
|
|
|
|
|
|
|
foreach (var mesh in meshes)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|