PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 55ms app.codeStats 0ms

/ContentSystem/Rendering/Helpers/BoneData.cs

#
C# | 155 lines | 77 code | 15 blank | 63 comment | 4 complexity | cc8819d836c68146b784ce3262ca4dff MD5 | raw file
  1using System.IO;
  2using Delta.Utilities;
  3using Delta.Utilities.Datatypes;
  4
  5namespace Delta.ContentSystem.Rendering.Helpers
  6{
  7	/// <summary>
  8	/// Helper class to store one bone entry for animated 3D models,
  9	/// this is not directly a content class, but used for MeshData and is
 10	/// also important for AnimationData, which keeps all the animated matrices.
 11	/// </summary>
 12	public class BoneData : ISaveLoadBinary
 13	{
 14		#region Constants
 15		/// <summary>
 16		/// Version number for this MeshData. If this goes above 1, we need
 17		/// to support loading older versions as well. Saving is always going
 18		/// to be the latest version (this one).
 19		/// </summary>
 20		private const int CurrentVersion = 1;
 21		#endregion
 22
 23		#region Id (Public)
 24		/// <summary>
 25		/// Id
 26		/// </summary>
 27		public int Id;
 28		#endregion
 29
 30		#region Name (Public)
 31		/// <summary>
 32		/// Name
 33		/// </summary>
 34		public string Name;
 35		#endregion
 36
 37		#region InverseTransformMatrix (Public)
 38		/// <summary>
 39		/// The inverse bone skin matrix
 40		/// </summary>
 41		public Matrix InverseTransformMatrix;
 42		#endregion
 43
 44		#region ParentId (Public)
 45		/// <summary>
 46		/// Parent id
 47		/// </summary>
 48		public int ParentId;
 49		#endregion
 50
 51		#region ChildrenIds (Public)
 52		/// <summary>
 53		/// Children ids
 54		/// </summary>
 55		public int[] ChildrenIds = new int[0];
 56		#endregion
 57
 58		#region Constructors
 59		/// <summary>
 60		/// Create bone data
 61		/// </summary>
 62		public BoneData()
 63		{
 64		}
 65
 66		/// <summary>
 67		/// Create bone data
 68		/// </summary>
 69		/// <param name="dataReader">Data reader</param>
 70		public BoneData(BinaryReader dataReader)
 71		{
 72			Load(dataReader);
 73		}
 74		#endregion
 75
 76		#region ISaveLoadBinary Members
 77		/// <summary>
 78		/// Load bone data (mostly all the matrices and their relationship).
 79		/// </summary>
 80		/// <param name="reader">Reader</param>
 81		public void Load(BinaryReader reader)
 82		{
 83			// We currently only support our version, if more versions are added,
 84			// we need to do different loading code depending on the version here.
 85			int version = reader.ReadInt32();
 86			if (version != CurrentVersion)
 87			{
 88				Log.InvalidVersionWarning(GetType().Name + ": " + Name,
 89					version, CurrentVersion);
 90				return;
 91			}
 92
 93			// Load the bone id
 94			Id = reader.ReadInt32();
 95			// the name of the bone
 96			Name = reader.ReadString();
 97			//// the initial pose
 98			//InitialPoseMatrix.Load(reader);
 99			// the inverse bone matrix
100			InverseTransformMatrix.Load(reader);
101
102			// before we still save the hierarchy
103			// with the parent
104			ParentId = reader.ReadInt32();
105			// and the children
106			ChildrenIds = new int[reader.ReadInt32()];
107			for (int index = 0; index < ChildrenIds.Length; index++)
108			{
109				ChildrenIds[index] = reader.ReadInt32();
110			}
111		}
112
113		/// <summary>
114		/// Save bone date (mostly all the matrices and their relationship).
115		/// </summary>
116		/// <param name="dataWriter">Data writer</param>
117		public void Save(BinaryWriter dataWriter)
118		{
119			dataWriter.Write(CurrentVersion);
120
121			// Save out the bone id
122			dataWriter.Write(Id);
123			// the name of the bone
124			dataWriter.Write(Name);
125			//// the initial pose
126			//InitialPoseMatrix.Save(dataWriter);
127			// the inverse bone matrix
128			InverseTransformMatrix.Save(dataWriter);
129
130			// before we still save the hierarchy
131			// with the parent
132			dataWriter.Write(ParentId);
133			// and the children
134			dataWriter.Write(ChildrenIds.Length);
135			for (int index = 0; index < ChildrenIds.Length; index++)
136			{
137				dataWriter.Write(ChildrenIds[index]);
138			}
139		}
140		#endregion
141
142		#region ToString (Public)
143		/// <summary>
144		/// To string
145		/// </summary>
146		public override string ToString()
147		{
148			return GetType().Name + ": Name='" + Name +
149			       //"', FinalPosition='" + FinalTransform.Translation +
150			       //"', AnimationFrames='" + AnimationMatrices.Length +
151			       "'";
152		}
153		#endregion
154	}
155}