Model.cs 12 KB

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