Model.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4. using SlimDX;
  5. using SlimDX.D3DCompiler;
  6. using SlimDX.Direct3D11;
  7. using SlimDX.DXGI;
  8. using SlimDX.Windows;
  9. using Device = SlimDX.Direct3D11.Device;
  10. using Resource = SlimDX.Direct3D11.Resource;
  11. using Buffer = SlimDX.Direct3D11.Buffer;
  12. using System.IO;
  13. using System.Collections.Generic;
  14. using System.Collections;
  15. using Everquest2.Util;
  16. using Everquest2.Visualization;
  17. namespace EQ2ModelViewer
  18. {
  19. public class Model
  20. {
  21. public Vector3 Position = new Vector3(0.0f, 0.0f, 0.0f);
  22. public Vector3 Rotation = new Vector3(0.0f, 0.0f, 0.0f);
  23. public float Scale = 1.0f;
  24. public UInt32 WidgetID = 0;
  25. public UInt32 GridID = 0;
  26. public String modelName = "";
  27. public UInt32 nodeFlags = 0;
  28. List<MeshClass> m_meshes = new List<MeshClass>();
  29. LightShaderClass lightShader = new LightShaderClass();
  30. public bool Initialize(Device device, string modelFileName, string[] textureFileName)
  31. {
  32. if (!LoadModel(modelFileName))
  33. {
  34. Console.WriteLine("Model: Failed to load the model");
  35. return false;
  36. }
  37. int count = 0;
  38. foreach (MeshClass mesh in m_meshes)
  39. {
  40. if (!mesh.InitializeBuffers(device))
  41. {
  42. Console.WriteLine("Model: Failed to initialize buffers.");
  43. return false;
  44. }
  45. if (count >= textureFileName.Length)
  46. mesh.LoadTexture(device, textureFileName[0]);
  47. else
  48. mesh.LoadTexture(device, textureFileName[count]);
  49. count++;
  50. }
  51. lightShader.Initialize(device);
  52. return true;
  53. }
  54. public bool Initialize(Device device, VeMeshGeometryNode item, String baseDir)
  55. {
  56. if (item.collisionMeshName == null || item.collisionMeshName.Length < 1)
  57. {
  58. Console.WriteLine("No collision mesh for MeshGeometryNode");
  59. return false;
  60. }
  61. VeCollisionMesh collision = null;
  62. try
  63. {
  64. Eq2Reader reader2 = new Eq2Reader(new System.IO.FileStream(frmMain.DirName + item.collisionMeshName, System.IO.FileMode.Open, System.IO.FileAccess.Read));
  65. collision = (VeCollisionMesh)reader2.ReadObject();
  66. reader2.Dispose();
  67. }
  68. catch (Exception ex)
  69. {
  70. }
  71. ArrayList textures = new ArrayList();
  72. string[][] meshes = ((VeMeshGeometryNode)item).renderMeshNames;
  73. for (int i = 0; i < meshes.Length; i++)
  74. {
  75. for (int j = 0; j < meshes[i].Length; j++)
  76. {
  77. if (meshes[i][j] != null)
  78. {
  79. string path = meshes[i][j];
  80. path = baseDir + path.Replace("/", "\\");
  81. // if (path.Contains("\\flora\\"))
  82. if (path.Contains("\\accessories\\"))
  83. {
  84. Console.WriteLine("Model: skipping loading of model (Accessories suck!)" + path);
  85. return false;
  86. }
  87. else if (path.Contains("\\flora\\") && (path.Contains("leaves") || path.Contains("top_") || path.Contains("top0")))
  88. {
  89. Console.WriteLine("Model: skipping loading of model (Leaves and Tree Tops suck!)" + path);
  90. return false;
  91. }
  92. string[] texture = frmMain.GetTextureFile(((VeMeshGeometryNode)item).shaderPaletteNames, baseDir);
  93. string pickedTexture = "";
  94. if (i < texture.Length && texture[i] != "goblin_ice.dds")
  95. {
  96. pickedTexture = texture[i];
  97. }
  98. else if (texture[0] != "goblin_ice.dds")
  99. {
  100. pickedTexture = texture[0];
  101. }
  102. if (pickedTexture.Length < 1)
  103. {
  104. Console.WriteLine("Model: missing texture " + path + " at i:" + i + " and j:" + j);
  105. }
  106. else
  107. {
  108. textures.Add(pickedTexture);
  109. if (!LoadModel(path))
  110. {
  111. Console.WriteLine("Model: Failed to load the model " + path);
  112. return false;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. int count = 0;
  119. ArrayList removeList = new ArrayList();
  120. foreach (MeshClass mesh in m_meshes)
  121. {
  122. if (mesh.GetVertices().Count < 1)
  123. {
  124. removeList.Add(mesh);
  125. continue;
  126. }
  127. if (!mesh.InitializeBuffers(device))
  128. {
  129. Console.WriteLine("Model: Failed to initialize buffers.");
  130. return false;
  131. }
  132. if (textures.Count > 0)
  133. {
  134. if (count >= textures.Count)
  135. mesh.LoadTexture(device, (string)textures[0]);
  136. else
  137. mesh.LoadTexture(device, (string)textures[count]);
  138. }
  139. count++;
  140. }
  141. // these are for meshclass files we couldn't load correctly, usually cause no primitivecount, only vertex count
  142. // moving on means trying to access a null texture and crashing in the render
  143. foreach (MeshClass mesh in removeList)
  144. {
  145. m_meshes.Remove(mesh);
  146. }
  147. lightShader.Initialize(device);
  148. return true;
  149. }
  150. public void Render(GraphicClass Graphics, CameraClass camera, bool highlight = false)
  151. {
  152. Matrix temp = Matrix.Multiply(Graphics.GetWorldMatrix(), Matrix.Scaling(Scale, Scale, Scale));
  153. temp = Matrix.Multiply(temp, Matrix.RotationYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z));
  154. temp = Matrix.Multiply(temp, Matrix.Translation(Position.X, Position.Y, Position.Z));
  155. Vector4 ambientColor;
  156. if (highlight)
  157. ambientColor = new Vector4(0.0f, 1.0f, 0.0f, 1.0f);
  158. else
  159. ambientColor = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
  160. float increment = 0.05f;
  161. foreach (MeshClass mesh in m_meshes)
  162. {
  163. if (mesh.GetTexture() == null)
  164. continue;
  165. mesh.RenderBuffers(Graphics.Context);
  166. lightShader.Render(Graphics.Context, mesh.GetIndexCount(), temp, camera.GetViewMatrix(), Graphics.GetProjectionMatrix(), mesh.GetTexture(), new Vector3(0.0f, 0.0f, 0.0f), ambientColor/*new Vector4(1.0f, 1.0f, 1.0f, 1.0f)*/, new Vector4(0.0f, 0.0f, 0.0f, 0.0f), camera.GetPosition(), new Vector4(0.0f, 0.0f, 0.0f, 0.0f), 0.0f);
  167. if (highlight)
  168. ambientColor = new Vector4(increment, 1.0f, 0.0f, 1.0f - increment);
  169. increment += 0.05f;
  170. if (increment > .5f)
  171. increment = 0.05f;
  172. }
  173. }
  174. public float TestIntersection(Ray ray, GraphicClass Graphics)
  175. {
  176. Matrix temp = Matrix.Multiply(Graphics.GetWorldMatrix(), Matrix.Scaling(Scale, Scale, Scale));
  177. temp = Matrix.Multiply(temp, Matrix.RotationYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z));
  178. temp = Matrix.Multiply(temp, Matrix.Translation(Position.X, Position.Y, Position.Z));
  179. float ret = 0.0f;
  180. foreach (MeshClass mesh in m_meshes)
  181. {
  182. int i = 0;
  183. while (i < mesh.m_model.Length)
  184. {
  185. Vector3 vec1 = new Vector3(mesh.m_model[i].x, mesh.m_model[i].y, mesh.m_model[i].z);
  186. Vector3 vec2 = new Vector3(mesh.m_model[i + 1].x, mesh.m_model[i + 1].y, mesh.m_model[i + 1].z);
  187. Vector3 vec3 = new Vector3(mesh.m_model[i + 2].x, mesh.m_model[i + 2].y, mesh.m_model[i + 2].z);
  188. Vector4 transformVec = Vector3.Transform(vec1, temp);
  189. vec1 = new Vector3(transformVec.X, transformVec.Y, transformVec.Z);
  190. transformVec = Vector3.Transform(vec2, temp);
  191. vec2 = new Vector3(transformVec.X, transformVec.Y, transformVec.Z);
  192. transformVec = Vector3.Transform(vec3, temp);
  193. vec3 = new Vector3(transformVec.X, transformVec.Y, transformVec.Z);
  194. if (Ray.Intersects(ray, vec1, vec2, vec3, out ret))
  195. {
  196. break;
  197. }
  198. i += 3;
  199. }
  200. if (ret != 0.0f)
  201. break;
  202. }
  203. return ret;
  204. }
  205. private bool LoadModel(string modelFileName)
  206. {
  207. frmMain.AppendLoadFile("LoadModel: " + modelFileName);
  208. Eq2Reader reader = new Eq2Reader(File.OpenRead(modelFileName));
  209. VeRenderMesh model = (VeRenderMesh)reader.ReadObject();
  210. reader.Dispose();
  211. bool mod = false;
  212. if (modelFileName.Contains("ant_terrain_neroom_geo04_l"))
  213. {
  214. mod = true;
  215. }
  216. if (model.subMeshes.Length > 0)
  217. {
  218. for (int count = 0; count < model.subMeshes.Length; count++)
  219. {
  220. MeshClass mesh = new MeshClass();
  221. mesh.SetFaceCount(model.subMeshes[count].PrimitiveCount * 3);
  222. int start = model.subMeshes[count].StartingIndex;
  223. int end = model.subMeshes[count].PrimitiveCount * 3;
  224. for (int i = 0; i < end; i++)
  225. {
  226. int index = i + start;
  227. int indicesIdx = model.indices[index];
  228. if (indicesIdx < 0 || indicesIdx >= model.vertices.Length)
  229. break;
  230. float x = model.vertices[model.indices[index]].X;
  231. float y = model.vertices[model.indices[index]].Y;
  232. float z = model.vertices[model.indices[index]].Z;
  233. float nx = model.normals[model.indices[index]].X;
  234. float ny = model.normals[model.indices[index]].Y;
  235. float nz = model.normals[model.indices[index]].Z;
  236. float tu = model.texCoords[0][model.indices[index]].U;
  237. float tv = model.texCoords[0][model.indices[index]].V;
  238. mesh.AddData(i, x, y, z, nx, ny, nz, tu, tv);
  239. }
  240. m_meshes.Add(mesh);
  241. }
  242. }
  243. else
  244. {
  245. MeshClass mesh = new MeshClass();
  246. mesh.SetFaceCount(model.indices.Length);
  247. for (int i = 0; i < model.indices.Length /*m_VertexCount*/; i++)
  248. {
  249. if (model.indices[i] < 0 || model.indices[i] > model.vertices.Length)
  250. {
  251. }
  252. else
  253. mesh.AddData(i, model.vertices[model.indices[i]].X, model.vertices[model.indices[i]].Y, model.vertices[model.indices[i]].Z, model.normals[model.indices[i]].X, model.normals[model.indices[i]].Y, model.normals[model.indices[i]].Z, model.texCoords[0][model.indices[i]].U, model.texCoords[0][model.indices[i]].V);
  254. }
  255. m_meshes.Add(mesh);
  256. }
  257. return true;
  258. }
  259. public void ShutDown()
  260. {
  261. foreach (MeshClass mesh in m_meshes)
  262. mesh.ShutDown();
  263. }
  264. public List<Vector3> GetVertices()
  265. {
  266. List<Vector3> ret = new List<Vector3>();
  267. foreach (MeshClass m in m_meshes)
  268. {
  269. List<Vector3> newList = m.GetVertices();
  270. ret.AddRange(newList);
  271. }
  272. return ret;
  273. }
  274. }
  275. }