PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ContentSystem/Rendering/Helpers/BoneData.cs

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