VeRenderMesh.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. byte[] lastXBytes = BitConverter.GetBytes(vertices[vertexCount-1].X);
  79. byte[] lastYBytes = BitConverter.GetBytes(vertices[vertexCount - 1].Y);
  80. byte[] lastZBytes = BitConverter.GetBytes(vertices[vertexCount - 1].Z);
  81. long startPos = reader.BaseStream.Position;
  82. if ( startPos == 36897 )
  83. {
  84. int test = 0;
  85. }
  86. do
  87. {
  88. float tmpX = reader.ReadSingle();
  89. float tmpY = reader.ReadSingle();
  90. float tmpZ = reader.ReadSingle();
  91. byte[] xBytes = BitConverter.GetBytes(tmpX);
  92. byte[] yBytes = BitConverter.GetBytes(tmpY);
  93. byte[] zBytes = BitConverter.GetBytes(tmpZ);
  94. if ((xBytes[0] == lastXBytes[0] && xBytes[1] == lastXBytes[1]) ||
  95. (xBytes[2] == lastXBytes[2] && xBytes[3] == lastXBytes[3]) ||
  96. (yBytes[0] == lastYBytes[0] && yBytes[1] == lastYBytes[1]) ||
  97. (yBytes[2] == lastYBytes[2] && yBytes[3] == lastYBytes[3]) ||
  98. (zBytes[0] == lastZBytes[0] && zBytes[1] == lastZBytes[1]) ||
  99. (zBytes[2] == lastZBytes[2] && zBytes[3] == lastZBytes[3]))
  100. {
  101. }
  102. else
  103. {
  104. if (xBytes[0] == lastXBytes[0] || xBytes[0] == lastZBytes[0] || xBytes[0] == lastZBytes[3])
  105. reader.BaseStream.Position -= 11;
  106. else
  107. reader.BaseStream.Position -= 12;
  108. break;
  109. }
  110. }
  111. while (true);
  112. long endPos = reader.BaseStream.Position - startPos;
  113. uint normalCount = reader.ReadUInt32();
  114. normals = new Vertex[normalCount];
  115. for (uint i = 0; i < normalCount; ++i)
  116. {
  117. normals[i].X = reader.ReadSingle();
  118. normals[i].Y = reader.ReadSingle();
  119. normals[i].Z = reader.ReadSingle();
  120. }
  121. do
  122. {
  123. uint tmpCount = reader.ReadUInt32();
  124. if (tmpCount == vertexCount)
  125. break;
  126. reader.BaseStream.Position -= 3;
  127. } while (true);
  128. reader.BaseStream.Position -= 4;
  129. uint texCoordSetCount = (uint)(classVersion >= 15 ? 5 : 8);
  130. if (classVersion == 14)
  131. texCoordSetCount = 2;
  132. texCoords = new TexCoord[texCoordSetCount][];
  133. uint texCoordCount = 0;
  134. for (uint i = 0; i < texCoordSetCount; ++i)
  135. {
  136. uint newtexCoordCount = reader.ReadUInt32();
  137. texCoordCount = newtexCoordCount;
  138. texCoords[i] = new TexCoord[newtexCoordCount];
  139. for (uint j = 0; j < newtexCoordCount; ++j)
  140. {
  141. texCoords[i][j].U = reader.ReadSingle();
  142. texCoords[i][j].V = reader.ReadSingle();
  143. }
  144. startPos = reader.BaseStream.Position;
  145. bool firstRun = true;
  146. do
  147. {
  148. UInt32 tmp = reader.ReadUInt32();
  149. if (tmp == 0 || tmp == texCoordCount)
  150. {
  151. reader.BaseStream.Position -= 4;
  152. break;
  153. }
  154. reader.BaseStream.Position -= 3;
  155. }
  156. while (true);
  157. }
  158. // reader.ReadBytes(12);
  159. // reader.ReadBytes(419);
  160. uint subMeshCount = reader.ReadUInt32();
  161. subMeshes = new SubMesh[subMeshCount];
  162. for (uint i = 0; i < subMeshCount; ++i)
  163. {
  164. subMeshes[i].PrimType = (PrimitiveType)reader.ReadUInt32();
  165. subMeshes[i].StartingVertex = reader.ReadInt32();
  166. subMeshes[i].VertexCount = reader.ReadInt32();
  167. subMeshes[i].StartingIndex = reader.ReadInt32();
  168. subMeshes[i].PrimitiveCount = reader.ReadInt32();
  169. }
  170. uint faceNormalCount = reader.ReadUInt32();
  171. faceNormals = new float[faceNormalCount * 3];
  172. for (uint i = 0; i < faceNormalCount * 3; ++i) faceNormals[i] = reader.ReadSingle();
  173. uint edgeCount = reader.ReadUInt32();
  174. edges = new float[edgeCount * 3];
  175. for (uint i = 0; i < edgeCount * 3; ++i) edges[i] = reader.ReadSingle();
  176. uint tangentBasisCount = reader.ReadUInt32();
  177. tangentBases = new float[tangentBasisCount,9];
  178. for (uint i = 0; i < tangentBasisCount; ++i)
  179. {
  180. for (uint j = 0; j < tangentBasisCount; ++j)
  181. {
  182. tangentBases[i,j] = reader.ReadSingle();
  183. }
  184. }
  185. uint boneNameCount = reader.ReadUInt32();
  186. boneNames = new string[boneNameCount];
  187. for (uint i = 0; i < boneNameCount; ++i) boneNames[i] = reader.ReadString(2);
  188. if (boneNameCount > 0)
  189. {
  190. uint boneIndexCount = reader.ReadUInt32();
  191. boneIndices = reader.ReadBytes((int)boneIndexCount);
  192. uint boneWeightCount = reader.ReadUInt32();
  193. boneWeights = new float[boneWeightCount];
  194. for (uint i = 0; i < boneWeightCount; ++i) boneWeights[i] = reader.ReadSingle();
  195. }
  196. uint autoDropVertexIndexCount = reader.ReadUInt32();
  197. autoDropVertexIndices = new short[autoDropVertexIndexCount];
  198. for (uint i = 0; i < autoDropVertexIndexCount; ++i) autoDropVertexIndices[i] = reader.ReadInt16();
  199. uint indexCount = reader.ReadUInt32();
  200. indices = new short[indexCount];
  201. for (uint i = 0; i < indexCount; ++i) indices[i] = reader.ReadInt16();
  202. startPos = reader.BaseStream.Position;
  203. bool foundZeros = false;
  204. int count = 0;
  205. long lastPos = startPos;
  206. do
  207. {
  208. byte tmpByte = reader.ReadByte();
  209. uint tmp = reader.ReadUInt32();
  210. if (!foundZeros && tmp == 0)
  211. {
  212. foundZeros = true;
  213. count++;
  214. lastPos = reader.BaseStream.Position;
  215. }
  216. else if (foundZeros && tmp == 0 && reader.BaseStream.Position > (lastPos + 4))
  217. {
  218. count++;
  219. lastPos = reader.BaseStream.Position;
  220. }
  221. else if (tmpByte == 3)
  222. {
  223. if (count > 2)
  224. break;
  225. }
  226. reader.BaseStream.Position -= 4;
  227. } while (true);
  228. uint shadercount = reader.ReadUInt32();
  229. string[] shaders = new string[shadercount];
  230. for(uint s=0;s<shadercount;s++)
  231. {
  232. if (s == 0)
  233. {
  234. defaultShaderPaletteName = reader.ReadString(2);
  235. shaders[s] = defaultShaderPaletteName;
  236. }
  237. else
  238. shaders[s] = reader.ReadString(2);
  239. }
  240. if (shadercount == 0)
  241. {
  242. uint subMeshNameCount = reader.ReadUInt32();
  243. subMeshNames = new string[subMeshNameCount];
  244. for (uint i = 0; i < subMeshNameCount; ++i) subMeshNames[i] = reader.ReadString(2);
  245. }
  246. centroid[0] = reader.ReadSingle();
  247. centroid[1] = reader.ReadSingle();
  248. centroid[2] = reader.ReadSingle();
  249. radius = reader.ReadSingle();
  250. aabbMin[0] = reader.ReadSingle();
  251. aabbMin[1] = reader.ReadSingle();
  252. aabbMin[2] = reader.ReadSingle();
  253. aabbMax[0] = reader.ReadSingle();
  254. aabbMax[1] = reader.ReadSingle();
  255. aabbMax[2] = reader.ReadSingle();
  256. if (classVersion >= 12)
  257. {
  258. uint degenerateVertexCount = reader.ReadUInt32();
  259. degenerateVertexIndices = new short[degenerateVertexCount];
  260. for (uint i = 0; i < degenerateVertexCount; ++i) degenerateVertexIndices[i] = reader.ReadInt16();
  261. uint degenerateEdgeCount = reader.ReadUInt32();
  262. degenerateEdgeIndices = new short[degenerateEdgeCount];
  263. for (uint i = 0; i < degenerateEdgeCount; ++i) degenerateEdgeIndices[i] = reader.ReadInt16();
  264. }
  265. if (classVersion >= 13)
  266. {
  267. uint noPolygonShadowTriangleIndexCount = reader.ReadUInt32();
  268. noPolygonShadowTriangleIndices = new short[noPolygonShadowTriangleIndexCount];
  269. for (uint i = 0; i < noPolygonShadowTriangleIndexCount; ++i) noPolygonShadowTriangleIndices[i] = reader.ReadInt16();
  270. }
  271. }
  272. public uint flags;
  273. public Vertex[] vertices;
  274. public Vertex[] normals;
  275. public TexCoord[][] texCoords;
  276. public short[] indices;
  277. public SubMesh[] subMeshes;
  278. public float[] faceNormals;
  279. public float[] edges;
  280. public float[,] tangentBases;
  281. public string[] boneNames;
  282. public byte[] boneIndices;
  283. public float[] boneWeights;
  284. public short[] autoDropVertexIndices;
  285. public string defaultShaderPaletteName;
  286. public string[] subMeshNames;
  287. public float[] centroid = new float[3];
  288. public float radius;
  289. public float[] aabbMin = new float[3];
  290. public float[] aabbMax = new float[3];
  291. public short[] degenerateVertexIndices;
  292. public short[] degenerateEdgeIndices;
  293. public short[] noPolygonShadowTriangleIndices;
  294. }
  295. }