VeRenderMesh.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. using System.IO;
  29. using Everquest2.Util;
  30. #endregion
  31. namespace Everquest2.Visualization
  32. {
  33. public class VeRenderMesh : VeBase
  34. {
  35. public enum PrimitiveType : uint
  36. {
  37. Triangles = 2
  38. }
  39. public struct SubMesh
  40. {
  41. public PrimitiveType PrimType;
  42. public int StartingVertex;
  43. public int VertexCount;
  44. public int StartingIndex;
  45. public int PrimitiveCount;
  46. }
  47. public struct TexCoord
  48. {
  49. public float U;
  50. public float V;
  51. }
  52. public struct Vertex
  53. {
  54. public float X;
  55. public float Y;
  56. public float Z;
  57. }
  58. public VeRenderMesh()
  59. {
  60. }
  61. /// <summary>
  62. /// Special constructor used when deserializing the instance of the class.
  63. /// </summary>
  64. /// <param name="reader">Reader used to read the instance data.</param>
  65. protected VeRenderMesh(Util.Eq2Reader reader, Util.StreamingContext context) : base(reader, context)
  66. {
  67. byte classVersion = context.ClassVersions[typeof(VeRenderMesh)];
  68. Debug.Assert(classVersion >= 11 && classVersion <= 15, "VeRenderMesh version " + classVersion + " not supported");
  69. flags = reader.ReadUInt32();
  70. uint vertexCount = reader.ReadUInt32();
  71. vertices = new Vertex[vertexCount];
  72. for (uint i = 0; i < vertexCount; ++i)
  73. {
  74. vertices[i].X = reader.ReadSingle();
  75. vertices[i].Y = reader.ReadSingle();
  76. vertices[i].Z = reader.ReadSingle();
  77. }
  78. uint normalCount = reader.ReadUInt32();
  79. normals = new Vertex[normalCount];
  80. for (uint i = 0; i < normalCount; ++i)
  81. {
  82. normals[i].X = reader.ReadSingle();
  83. normals[i].Y = reader.ReadSingle();
  84. normals[i].Z = reader.ReadSingle();
  85. }
  86. uint texCoordSetCount = (uint)(classVersion >= 15 ? 5 : 8);
  87. texCoords = new TexCoord[texCoordSetCount][];
  88. for (uint i = 0; i < texCoordSetCount; ++i)
  89. {
  90. uint texCoordCount = reader.ReadUInt32();
  91. texCoords[i] = new TexCoord[texCoordCount];
  92. for (uint j = 0; j < texCoordCount; ++j)
  93. {
  94. texCoords[i][j].U = reader.ReadSingle();
  95. texCoords[i][j].V = reader.ReadSingle();
  96. }
  97. }
  98. uint indexCount = reader.ReadUInt32();
  99. indices = new short[indexCount];
  100. for (uint i = 0; i < indexCount; ++i) indices[i] = reader.ReadInt16();
  101. uint subMeshCount = reader.ReadUInt32();
  102. subMeshes = new SubMesh[subMeshCount];
  103. for (uint i = 0; i < subMeshCount; ++i)
  104. {
  105. subMeshes[i].PrimType = (PrimitiveType)reader.ReadUInt32();
  106. subMeshes[i].StartingVertex = reader.ReadInt32();
  107. subMeshes[i].VertexCount = reader.ReadInt32();
  108. subMeshes[i].StartingIndex = reader.ReadInt32();
  109. subMeshes[i].PrimitiveCount = reader.ReadInt32();
  110. }
  111. uint faceNormalCount = reader.ReadUInt32();
  112. faceNormals = new float[faceNormalCount * 3];
  113. for (uint i = 0; i < faceNormalCount * 3; ++i) faceNormals[i] = reader.ReadSingle();
  114. uint edgeCount = reader.ReadUInt32();
  115. edges = new float[edgeCount * 3];
  116. for (uint i = 0; i < edgeCount * 3; ++i) edges[i] = reader.ReadSingle();
  117. uint tangentBasisCount = reader.ReadUInt32();
  118. tangentBases = new float[tangentBasisCount,9];
  119. for (uint i = 0; i < tangentBasisCount; ++i)
  120. {
  121. for (uint j = 0; j < tangentBasisCount; ++j)
  122. {
  123. tangentBases[i,j] = reader.ReadSingle();
  124. }
  125. }
  126. uint boneNameCount = reader.ReadUInt32();
  127. boneNames = new string[boneNameCount];
  128. for (uint i = 0; i < boneNameCount; ++i) boneNames[i] = reader.ReadString(2);
  129. uint boneIndexCount = reader.ReadUInt32();
  130. boneIndices = reader.ReadBytes((int)boneIndexCount);
  131. uint boneWeightCount = reader.ReadUInt32();
  132. boneWeights = new float[boneWeightCount];
  133. for (uint i = 0; i < boneWeightCount; ++i) boneWeights[i] = reader.ReadSingle();
  134. uint autoDropVertexIndexCount = reader.ReadUInt32();
  135. autoDropVertexIndices = new short[autoDropVertexIndexCount];
  136. for (uint i = 0; i < autoDropVertexIndexCount; ++i) autoDropVertexIndices[i] = reader.ReadInt16();
  137. defaultShaderPaletteName = reader.ReadString(2);
  138. uint subMeshNameCount = reader.ReadUInt32();
  139. subMeshNames = new string[subMeshNameCount];
  140. for (uint i = 0; i < subMeshNameCount; ++i) subMeshNames[i] = reader.ReadString(2);
  141. centroid[0] = reader.ReadSingle();
  142. centroid[1] = reader.ReadSingle();
  143. centroid[2] = reader.ReadSingle();
  144. radius = reader.ReadSingle();
  145. aabbMin[0] = reader.ReadSingle();
  146. aabbMin[1] = reader.ReadSingle();
  147. aabbMin[2] = reader.ReadSingle();
  148. aabbMax[0] = reader.ReadSingle();
  149. aabbMax[1] = reader.ReadSingle();
  150. aabbMax[2] = reader.ReadSingle();
  151. if (classVersion >= 12)
  152. {
  153. uint degenerateVertexCount = reader.ReadUInt32();
  154. degenerateVertexIndices = new short[degenerateVertexCount];
  155. for (uint i = 0; i < degenerateVertexCount; ++i) degenerateVertexIndices[i] = reader.ReadInt16();
  156. uint degenerateEdgeCount = reader.ReadUInt32();
  157. degenerateEdgeIndices = new short[degenerateEdgeCount];
  158. for (uint i = 0; i < degenerateEdgeCount; ++i) degenerateEdgeIndices[i] = reader.ReadInt16();
  159. }
  160. if (classVersion >= 13)
  161. {
  162. uint noPolygonShadowTriangleIndexCount = reader.ReadUInt32();
  163. noPolygonShadowTriangleIndices = new short[noPolygonShadowTriangleIndexCount];
  164. for (uint i = 0; i < noPolygonShadowTriangleIndexCount; ++i) noPolygonShadowTriangleIndices[i] = reader.ReadInt16();
  165. }
  166. }
  167. public uint flags;
  168. public Vertex[] vertices;
  169. public Vertex[] normals;
  170. public TexCoord[][] texCoords;
  171. public short[] indices;
  172. public SubMesh[] subMeshes;
  173. public float[] faceNormals;
  174. public float[] edges;
  175. public float[,] tangentBases;
  176. public string[] boneNames;
  177. public byte[] boneIndices;
  178. public float[] boneWeights;
  179. public short[] autoDropVertexIndices;
  180. public string defaultShaderPaletteName;
  181. public string[] subMeshNames;
  182. public float[] centroid = new float[3];
  183. public float radius;
  184. public float[] aabbMin = new float[3];
  185. public float[] aabbMax = new float[3];
  186. public short[] degenerateVertexIndices;
  187. public short[] degenerateEdgeIndices;
  188. public short[] noPolygonShadowTriangleIndices;
  189. }
  190. }