Main.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4. using System.Collections.Generic;
  5. using SlimDX;
  6. using SlimDX.D3DCompiler;
  7. using SlimDX.Direct3D11;
  8. using SlimDX.DXGI;
  9. using SlimDX.Windows;
  10. using Device = SlimDX.Direct3D11.Device;
  11. using Resource = SlimDX.Direct3D11.Resource;
  12. using Buffer = SlimDX.Direct3D11.Buffer;
  13. using Everquest2.Util;
  14. using Everquest2.Visualization;
  15. using System.IO;
  16. namespace EQ2ModelViewer
  17. {
  18. public partial class frmMain : Form
  19. {
  20. private System.Collections.Generic.List<Model> m_Models = new System.Collections.Generic.List<Model>();
  21. private GraphicClass Graphics = new GraphicClass();
  22. public Model SelectedModel = null;
  23. private string ZoneFile;
  24. private bool Render3DAspect = true;
  25. private bool AutoExportOnLoad = false;
  26. private String AutoLoadFileName = "";
  27. public frmMain()
  28. {
  29. InitializeComponent();
  30. }
  31. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  32. {
  33. Close();
  34. }
  35. private void CleanUp()
  36. {
  37. foreach (Model model in m_Models)
  38. model.ShutDown();
  39. if (Graphics != null)
  40. Graphics.ShutDown();
  41. }
  42. private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
  43. {
  44. CleanUp();
  45. }
  46. private CameraClass camera;
  47. private void frmMain_Load(object sender, EventArgs e)
  48. {
  49. this.WindowState = FormWindowState.Maximized;
  50. Graphics.Initialize(pGraphics);
  51. /*LightShaderClass lightShader = new LightShaderClass();
  52. lightShader.Initialize(Graphics.Device);*/
  53. camera = new CameraClass();
  54. // Initialize a base view matrix for 2D rendering
  55. camera.SetPosition(0.0f, 0.0f, -1.0f);
  56. camera.Render();
  57. Matrix baseViewMatrix = camera.GetViewMatrix();
  58. // now set the cameras starting position
  59. camera.SetPosition(0.0f, 1.50f, -3.0f);
  60. PositionClass position = new PositionClass();
  61. position.SetPosition(0.0f, 1.50f, -3.0f);
  62. InputClass input = new InputClass();
  63. input.Initialize(this);
  64. TimerClass timer = new TimerClass();
  65. timer.Initialize();
  66. FPSClass fps = new FPSClass();
  67. fps.Initialize();
  68. TextClass text = new TextClass();
  69. text.Initialize(Graphics.Device, Graphics.Context, pGraphics.ClientSize.Width, pGraphics.ClientSize.Height, baseViewMatrix);
  70. BitmapClass bmp = new BitmapClass();
  71. bmp.Initialize(Graphics.Device, pGraphics.ClientSize.Width, pGraphics.ClientSize.Height, "Background.bmp", 145, 220, baseViewMatrix);
  72. string[] args = Environment.GetCommandLineArgs();
  73. if (args.Length > 1)
  74. {
  75. for (int i = 1; i < args.Length; i++)
  76. {
  77. string cmd = args[i].ToLower();
  78. if (cmd.Equals("norender"))
  79. {
  80. Render3DAspect = false;
  81. }
  82. else if (cmd.Equals("export"))
  83. {
  84. AutoExportOnLoad = true;
  85. }
  86. else
  87. {
  88. AutoLoadFileName = args[i];
  89. break;
  90. }
  91. }
  92. }
  93. if (AutoLoadFileName.Length > 0)
  94. LoadZoneFile(AutoLoadFileName);
  95. if (AutoExportOnLoad)
  96. exportToolStripMenuItem_Click(null, EventArgs.Empty);
  97. if (!Render3DAspect)
  98. {
  99. Application.Exit();
  100. return;
  101. }
  102. // FrustumClass frustum = new FrustumClass();
  103. try
  104. {
  105. MessagePump.Run(this, () =>
  106. {
  107. if (!Graphics.SwapChain.Disposed) {
  108. timer.Frame();
  109. fps.Frame();
  110. // Input code
  111. input.Frame();
  112. position.SetFrameTime(timer.GetTime());
  113. if (this.Focused)
  114. {
  115. position.TurnLeft(input.IsLeftPressed());
  116. position.TurnRight(input.IsRightPressed());
  117. position.MoveForward(input.IsUpPressed());
  118. position.MoveBackward(input.IsDownPressed());
  119. position.MoveUpward(input.IsAPressed());
  120. position.MoveDownward(input.IsZPressed());
  121. position.LookUpward(input.IsPgUpPressed());
  122. position.LookDownward(input.IsPgDownPressed());
  123. if (input.IsLeftMousePressed())
  124. {
  125. TestIntersection(input.GetMouseX(), input.GetMouseY());
  126. }
  127. if (SelectedModel != null)
  128. {
  129. if (input.IsKeyPressed(SlimDX.DirectInput.Key.Delete))
  130. {
  131. m_Models.Remove(SelectedModel);
  132. SelectedModel = null;
  133. }
  134. else if (input.IsKeyPressed(SlimDX.DirectInput.Key.Escape))
  135. {
  136. SelectedModel = null;
  137. }
  138. }
  139. }
  140. camera.SetPosition(position.GetPosition());
  141. camera.SetRotation(position.GetRotation());
  142. // Render Code
  143. Graphics.BeginScene();
  144. // 3D
  145. // View matrix
  146. camera.Render();
  147. //frustum.ConstructFrustum(1000.0f, Graphics.GetProjectionMatrix(), camera.GetViewMatrix());
  148. foreach (Model model in m_Models) {
  149. //if (frustum.CheckSphere(model.Position.X, model.Position.Y, model.Position.Z, 10.0f))
  150. //{
  151. /*Matrix temp = Matrix.Multiply(Graphics.GetWorldMatrix(), Matrix.Scaling(model.Scale, model.Scale, model.Scale));
  152. temp = Matrix.Multiply(temp, Matrix.RotationYawPitchRoll(model.Rotation.X, model.Rotation.Y, model.Rotation.Z));
  153. temp = Matrix.Multiply(temp, Matrix.Translation(model.Position.X, model.Position.Y, model.Position.Z));*/
  154. model.Render(Graphics, camera, (model == SelectedModel)/*Graphics.Context*/);
  155. //lightShader.Render(Graphics.Context, model.GetIndexCount(), temp, camera.GetViewMatrix(), Graphics.GetProjectionMatrix(), model.GetTexture(), new Vector3(0.0f, 0.0f, 0.0f), 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);
  156. //}
  157. }
  158. // 2D
  159. Graphics.TurnZBufferOff();
  160. Graphics.TurnOnAlphaBlending();
  161. bmp.Render(Graphics, 10, 10);
  162. text.SetFPS(fps.GetFPS(), Graphics.Context);
  163. text.SetPosition(position.GetPosition(), Graphics.Context);
  164. text.SetSelectedModel(SelectedModel, Graphics.Context);
  165. text.Render(Graphics.Context, Graphics.GetWorldMatrix(), Graphics.GetOrthoMatrix());
  166. Graphics.TurnOffAlphaBlending();
  167. Graphics.TurnZBufferOn();
  168. Graphics.EndScene();
  169. }
  170. });
  171. }
  172. catch (Exception exception) { }
  173. }
  174. public bool TestIntersection(int mouseX, int mouseY) {
  175. float pointX, pointY;
  176. Matrix projectionMatrix, viewMatrix, inverseViewMatrix;
  177. projectionMatrix = Graphics.GetProjectionMatrix();
  178. pointX = (2.0F * (float)mouseX / (float)pGraphics.ClientSize.Width - 1.0f) / projectionMatrix.M11;
  179. pointY = (-2.0f * (float)mouseY / (float)pGraphics.ClientSize.Height + 1.0f) / projectionMatrix.M22;
  180. Ray ray = new Ray(new Vector3(), new Vector3(pointX, pointY, 1.0f));
  181. viewMatrix = camera.GetViewMatrix();
  182. inverseViewMatrix = Matrix.Invert(viewMatrix);
  183. ray = new Ray(Vector3.TransformCoordinate(ray.Position, inverseViewMatrix), Vector3.TransformNormal(ray.Direction, inverseViewMatrix));
  184. ray.Direction.Normalize();
  185. float selectionDistance = 0.0f;
  186. foreach (Model model in m_Models) {
  187. float distance = model.TestIntersection(ray, Graphics);
  188. if (distance > 0.0f && (selectionDistance == 0.0f || distance < selectionDistance)) {
  189. selectionDistance = distance;
  190. SelectedModel = model;
  191. }
  192. }
  193. return false;
  194. }
  195. public static void AppendLoadFile(String txt)
  196. {
  197. StreamWriter sw = File.AppendText("loaded.txt");
  198. sw.WriteLine(txt);
  199. sw.Close();
  200. }
  201. private void loadZoneToolStripMenuItem_Click(object sender, EventArgs e)
  202. {
  203. LoadZoneFile();
  204. }
  205. private void LoadZoneFile(String filename="")
  206. {
  207. string fullName = "";
  208. String dirName = "";
  209. if (filename.Length < 1)
  210. {
  211. OpenFileDialog fd = new OpenFileDialog();
  212. fd.Filter = "lut files (*.lut)|*.lut";
  213. if (fd.ShowDialog() == DialogResult.OK)
  214. {
  215. AppendLoadFile("===================================================");
  216. AppendLoadFile("Loading " + fd.FileName);
  217. string temp = fd.FileName.Substring(0, fd.FileName.IndexOf("zones"));
  218. ZoneFile = fd.SafeFileName.Substring(0, fd.SafeFileName.IndexOf(".lut"));
  219. fullName = ZoneFile;
  220. dirName = temp;
  221. filename = fd.FileName;
  222. }
  223. }
  224. else
  225. {
  226. string temp = filename.Substring(0, filename.IndexOf("zones"));
  227. ZoneFile = filename.Substring(0, filename.IndexOf(".lut"));
  228. fullName = filename;
  229. dirName = temp;
  230. }
  231. if (fullName.Length < 1)
  232. {
  233. MessageBox.Show("No filename provided for loading a zonefile!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  234. return;
  235. }
  236. System.IO.BinaryReader reader2 = new System.IO.BinaryReader(new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read));
  237. // Image(2020): Was ReadUint32, qey_harbor.lut however has 00 1F 00 7A, so that as an int32 is a very large number!
  238. reader2.ReadUInt32();
  239. do
  240. {
  241. if (reader2.BaseStream.Position + 2 >= reader2.BaseStream.Length)
  242. {
  243. break;
  244. }
  245. UInt16 size = reader2.ReadUInt16();
  246. string file = new string(reader2.ReadChars(size));
  247. // was duplicating drive name
  248. file = file.Replace("/", "\\");
  249. file = dirName + file;
  250. AppendLoadFile("VOC Loading: " + file);
  251. Eq2Reader reader = new Eq2Reader(new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read));
  252. VeNode venode = reader.ReadNodeObject();
  253. CheckNode(dirName, venode);
  254. //MessageBox.Show("Done!");
  255. // 16 bytes between file names, grid id's maybe?
  256. reader2.ReadBytes(16);
  257. } while (true);
  258. }
  259. float x, y, z = 0;
  260. float yaw, pitch, roll = 0;
  261. float scale = 0;
  262. UInt32 widgetID;
  263. UInt32 GridID;
  264. private void CheckNode(string temp, object item)
  265. {
  266. if (item is VeMeshGeometryNode)
  267. {
  268. widgetID = ((VeNode)item).WidgetID;
  269. if ( widgetID == 3365739999)
  270. {
  271. int test = 0;
  272. }
  273. Model model = new Model();
  274. model.Initialize(Graphics.Device, (VeMeshGeometryNode)item, temp);
  275. model.Position.X = x;
  276. model.Position.Y = y;
  277. model.Position.Z = z;
  278. model.Rotation.X = yaw;
  279. model.Rotation.Y = pitch;
  280. model.Rotation.Z = roll;
  281. model.Scale = scale;
  282. model.WidgetID = widgetID;
  283. model.GridID = GridID;
  284. m_Models.Add(model);
  285. }
  286. else
  287. {
  288. float x1 = 0.0f;
  289. float y1 = 0.0f;
  290. float z1 = 0.0f;
  291. if (item is VeRoomItemNode)
  292. {
  293. yaw = ((VeRoomItemNode)item).orientation[0];
  294. pitch = ((VeRoomItemNode)item).orientation[1];
  295. roll = ((VeRoomItemNode)item).orientation[2];
  296. GridID = ((VeRoomItemNode)item).unk0;
  297. }
  298. else if (item is VeXformNode)
  299. {
  300. x1 = ((VeXformNode)item).position[0];
  301. y1 = ((VeXformNode)item).position[1];
  302. z1 = ((VeXformNode)item).position[2];
  303. yaw = ((VeXformNode)item).orientation[0] * (3.141592654f / 180.0f);
  304. pitch = ((VeXformNode)item).orientation[1] * (3.141592654f / 180.0f);
  305. roll = ((VeXformNode)item).orientation[2] * (3.141592654f / 180.0f);
  306. scale = ((VeXformNode)item).scale;
  307. x += x1;
  308. y += y1;
  309. z += z1;
  310. }
  311. if (item != null)
  312. {
  313. System.Collections.IEnumerator enumerator = ((VeNode)item).EnumerateChildren();
  314. while (enumerator.MoveNext())
  315. {
  316. CheckNode(temp, enumerator.Current);
  317. }
  318. x -= x1;
  319. y -= y1;
  320. z -= z1;
  321. }
  322. }
  323. }
  324. public static string[] GetTextureFile(string[] spPath, string basePath)
  325. {
  326. string ret = "goblin_ice.dds";
  327. System.Collections.Generic.List<string> strings = new System.Collections.Generic.List<string>();
  328. int i = 0;
  329. while (i < spPath.Length /*&& ret == "goblin_ice.dds"*/)
  330. {
  331. Eq2Reader reader = new Eq2Reader(new System.IO.FileStream(basePath + spPath[i], System.IO.FileMode.Open, System.IO.FileAccess.Read));
  332. VeBase sp = reader.ReadObject();
  333. reader.Close();
  334. if (sp is VeShaderPalette)
  335. {
  336. bool found = false;
  337. for (int s = 0; s < ((VeShaderPalette)sp).shaderNames.Length; s++)
  338. {
  339. String fileName = basePath + ((VeShaderPalette)sp).shaderNames[s];
  340. fileName = fileName.Replace("/", "\\");
  341. System.IO.StreamReader reader2 = new System.IO.StreamReader(fileName);
  342. while (!reader2.EndOfStream)
  343. {
  344. string lineOrig = reader2.ReadLine();
  345. if (lineOrig.Contains("name = \"@tex") && !lineOrig.Contains("Blend") && !lineOrig.Contains("UVSet"))
  346. {
  347. String line = reader2.ReadLine();
  348. while (line.Length < 1)
  349. line = reader2.ReadLine();
  350. line = line.Substring(line.IndexOf('"') + 1);
  351. line = line.Substring(0, line.Length - 1);
  352. ret = basePath + line;
  353. strings.Add(ret);
  354. found = true;
  355. break;
  356. //break;
  357. }
  358. if (found)
  359. break;
  360. }
  361. reader2.Close();
  362. }
  363. }
  364. i++;
  365. }
  366. if (strings.Count == 0)
  367. strings.Add(ret);
  368. return strings.ToArray();
  369. }
  370. private void exportToolStripMenuItem_Click(object sender, EventArgs e)
  371. {
  372. //List<Vector3> MasterVertexList = new List<Vector3>();
  373. Dictionary<UInt32, List<Vector3>> MasterVertexList = new Dictionary<UInt32, List<Vector3>>();
  374. foreach (Model model in m_Models)
  375. {
  376. List<Vector3> VertexList = model.GetVertices();
  377. UInt32 grid = model.GridID;
  378. if (!MasterVertexList.ContainsKey(grid))
  379. MasterVertexList[grid] = new List<Vector3>();
  380. MasterVertexList[grid].AddRange(VertexList);
  381. }
  382. float minX = float.NaN;
  383. float minZ = float.NaN;
  384. float maxX = float.NaN;
  385. float maxZ = float.NaN;
  386. foreach (KeyValuePair<UInt32, List<Vector3>> entry in MasterVertexList)
  387. {
  388. foreach (Vector3 v in entry.Value)
  389. {
  390. if (float.IsNaN(minX))
  391. {
  392. minX = v.X;
  393. maxX = v.X;
  394. minZ = v.Z;
  395. maxZ = v.Z;
  396. }
  397. else
  398. {
  399. if (v.X < minX)
  400. minX = v.X;
  401. if (v.X > maxX)
  402. maxX = v.X;
  403. if (v.Z < minZ)
  404. minZ = v.Z;
  405. if (v.Z > maxZ)
  406. maxZ = v.Z;
  407. }
  408. }
  409. }
  410. using (StreamWriter file = new StreamWriter(ZoneFile + ".obj"))
  411. {
  412. // file.WriteLine(ZoneFile);
  413. // file.WriteLine("Min");
  414. // file.WriteLine(minX + " " + minZ);
  415. // file.WriteLine("Max");
  416. // file.WriteLine(maxX + " " + maxZ);
  417. // file.WriteLine("Grid count");
  418. // file.WriteLine(MasterVertexList.Count);
  419. // file.WriteLine();
  420. List<string> indices = new List<string>();
  421. int count = 0;
  422. string buildStr = "";
  423. int curcount = 0;
  424. foreach (KeyValuePair<UInt32, List<Vector3>> entry in MasterVertexList)
  425. {
  426. buildStr = "f ";
  427. // file.WriteLine("Grid");
  428. // file.WriteLine(entry.Key);
  429. // file.WriteLine("Face count");
  430. // file.WriteLine(entry.Value.Count);
  431. foreach (Vector3 v in entry.Value)
  432. {
  433. if (curcount > 2)
  434. {
  435. buildStr += count;
  436. indices.Add(buildStr);
  437. buildStr = "f ";
  438. curcount = 0;
  439. }
  440. else
  441. buildStr += count + " ";
  442. file.WriteLine("v " + v.X.ToString() + " " + v.Y.ToString()
  443. + " " + v.Z.ToString());
  444. count++;
  445. curcount++;
  446. }
  447. }
  448. foreach (string str in indices)
  449. {
  450. file.WriteLine(str);
  451. }
  452. file.Close();
  453. }
  454. using (BinaryWriter file = new BinaryWriter(File.Open(ZoneFile + ".EQ2Map", FileMode.Create)))
  455. {
  456. file.Write(ZoneFile);
  457. file.Write(minX);
  458. file.Write(minZ);
  459. file.Write(maxX);
  460. file.Write(maxZ);
  461. file.Write(MasterVertexList.Count);
  462. foreach (KeyValuePair<UInt32, List<Vector3>> entry in MasterVertexList)
  463. {
  464. file.Write(entry.Key);
  465. file.Write(entry.Value.Count);
  466. foreach (Vector3 v in entry.Value)
  467. {
  468. file.Write(v.X);
  469. file.Write(v.Y);
  470. file.Write(v.Z);
  471. }
  472. }
  473. file.Close();
  474. }
  475. if (sender != null)
  476. MessageBox.Show("Export Complete!");
  477. }
  478. }
  479. }