VeSkeleton.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #region License information
  2. // ----------------------------------------------------------------------------
  3. //
  4. // libeq2 - A library for analyzing the Everquest II File Format
  5. // Blaz (blaz@blazlabs.com)
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. //
  21. // ( The full text of the license can be found in the License.txt file )
  22. //
  23. // ----------------------------------------------------------------------------
  24. #endregion
  25. #region Using directives
  26. using System;
  27. using System.Diagnostics;
  28. #endregion
  29. namespace Everquest2.Visualization
  30. {
  31. public class VeSkeleton : VeBase
  32. {
  33. public struct BoneTransform
  34. {
  35. public float[] TranslationVector;
  36. public float[] RotationQuaternion;
  37. public float[] ScalingMatrix;
  38. }
  39. public VeSkeleton()
  40. {
  41. }
  42. /// <summary>
  43. /// Special constructor used when deserializing the instance of the class.
  44. /// </summary>
  45. /// <param name="reader">Reader used to read the instance data.</param>
  46. protected VeSkeleton(Util.Eq2Reader reader, Util.StreamingContext context) : base(reader, context)
  47. {
  48. byte classVersion = context.ClassVersions[typeof(VeSkeleton)];
  49. Debug.Assert(classVersion >= 4 && classVersion <= 6, "VeSkeleton class version " + classVersion + " not supported");
  50. uint boneNameCount = reader.ReadUInt32();
  51. boneNames = new string[boneNameCount];
  52. for (uint i = 0; i < boneNameCount; ++i) boneNames[i] = reader.ReadString(2);
  53. uint boneParentIndexCount = reader.ReadUInt32();
  54. boneParentIndices = reader.ReadBytes((int)boneParentIndexCount);
  55. uint boneRelativeTransformCount = reader.ReadUInt32();
  56. boneRelativeTransforms = new BoneTransform[boneRelativeTransformCount];
  57. for (uint i = 0; i < boneRelativeTransformCount; ++i)
  58. {
  59. boneRelativeTransforms[i].TranslationVector = new float[4];
  60. boneRelativeTransforms[i].RotationQuaternion = new float[4];
  61. boneRelativeTransforms[i].ScalingMatrix = new float[9];
  62. switch (classVersion)
  63. {
  64. case 4:
  65. // 4-component translation vector, rotation quaternion, 3x3 scaling matrix
  66. ReadVector(reader, boneRelativeTransforms[i].TranslationVector, 4);
  67. ReadVector(reader, boneRelativeTransforms[i].RotationQuaternion, 4);
  68. ReadVector(reader, boneRelativeTransforms[i].ScalingMatrix, 9);
  69. break;
  70. case 5:
  71. // 3-component translation vector, rotation quaternion, 3x3 scaling matrix
  72. ReadVector(reader, boneRelativeTransforms[i].TranslationVector, 3);
  73. boneRelativeTransforms[i].TranslationVector[3] = 0;
  74. ReadVector(reader, boneRelativeTransforms[i].RotationQuaternion, 4);
  75. ReadVector(reader, boneRelativeTransforms[i].ScalingMatrix, 9);
  76. break;
  77. default:
  78. // 3-component translation vector, rotation quaternion, uniform scaling factor
  79. ReadVector(reader, boneRelativeTransforms[i].TranslationVector, 3);
  80. boneRelativeTransforms[i].TranslationVector[3] = 0;
  81. ReadVector(reader, boneRelativeTransforms[i].RotationQuaternion, 4);
  82. float scale = reader.ReadSingle();
  83. boneRelativeTransforms[i].ScalingMatrix = new float[9]
  84. {
  85. scale, 0, 0,
  86. 0, scale, 0,
  87. 0, 0, scale
  88. };
  89. break;
  90. }
  91. }
  92. uint inverseSkeletonRelativeTransformCount = reader.ReadUInt32();
  93. inverseSkeletonRelativeTransforms = new float[inverseSkeletonRelativeTransformCount, 16];
  94. for (uint i = 0; i < inverseSkeletonRelativeTransformCount; ++i)
  95. {
  96. for (uint j = 0; j < 16; ++j) inverseSkeletonRelativeTransforms[i, j] = reader.ReadSingle();
  97. }
  98. }
  99. private void ReadVector(Util.Eq2Reader reader, float[] vector, int count)
  100. {
  101. for (int i = 0; i < count; ++i) vector[i] = reader.ReadSingle();
  102. }
  103. public string[] boneNames;
  104. public byte[] boneParentIndices;
  105. public BoneTransform[] boneRelativeTransforms;
  106. public float[,] inverseSkeletonRelativeTransforms;
  107. }
  108. }