Main.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4. using System.Collections.Generic;
  5. using System.IO.Compression;
  6. using System.Xml;
  7. using SlimDX;
  8. using SlimDX.D3DCompiler;
  9. using SlimDX.Direct3D11;
  10. using SlimDX.DXGI;
  11. using SlimDX.Windows;
  12. using Device = SlimDX.Direct3D11.Device;
  13. using Resource = SlimDX.Direct3D11.Resource;
  14. using Buffer = SlimDX.Direct3D11.Buffer;
  15. using Everquest2.Util;
  16. using Everquest2.Visualization;
  17. using System.IO;
  18. using SlimDX.DirectInput;
  19. using SlimDX.Direct2D;
  20. using System.Security.Permissions;
  21. namespace EQ2ModelViewer
  22. {
  23. public partial class frmMain : Form
  24. {
  25. private System.Collections.Generic.List<Model> m_Models = new System.Collections.Generic.List<Model>();
  26. private System.Collections.Generic.List<VeRegion> m_Regions = new System.Collections.Generic.List<VeRegion>();
  27. private GraphicClass Graphics = new GraphicClass();
  28. public Model SelectedModel = null;
  29. private string ZoneFile;
  30. private bool Render3DAspect = true;
  31. private bool AutoExportOnLoad = false;
  32. private bool AutoExportRegionOnLoad = false;
  33. private String AutoLoadFileName = "";
  34. public frmMain()
  35. {
  36. InitializeComponent();
  37. }
  38. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  39. {
  40. Close();
  41. }
  42. private void CleanUp()
  43. {
  44. foreach (Model model in m_Models)
  45. model.ShutDown();
  46. if (Graphics != null)
  47. Graphics.ShutDown();
  48. }
  49. private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
  50. {
  51. CleanUp();
  52. }
  53. private Key hitKey = Key.NoConvert;
  54. double timestamp = 0;
  55. private CameraClass camera;
  56. int region_nodes = 0;
  57. private void frmMain_Load(object sender, EventArgs e)
  58. {
  59. this.WindowState = FormWindowState.Maximized;
  60. Graphics.Initialize(pGraphics);
  61. /*LightShaderClass lightShader = new LightShaderClass();
  62. lightShader.Initialize(Graphics.Device);*/
  63. camera = new CameraClass();
  64. // Initialize a base view matrix for 2D rendering
  65. camera.SetPosition(0.0f, 0.0f, -1.0f);
  66. camera.Render();
  67. Matrix baseViewMatrix = camera.GetViewMatrix();
  68. // now set the cameras starting position
  69. camera.SetPosition(0.0f, 1.50f, -3.0f);
  70. PositionClass position = new PositionClass();
  71. position.SetPosition(0.0f, 1.50f, -3.0f);
  72. InputClass input = new InputClass();
  73. input.Initialize(this);
  74. TimerClass timer = new TimerClass();
  75. timer.Initialize();
  76. FPSClass fps = new FPSClass();
  77. fps.Initialize();
  78. TextClass text = new TextClass();
  79. text.Initialize(Graphics.Device, Graphics.Context, pGraphics.ClientSize.Width, pGraphics.ClientSize.Height, baseViewMatrix);
  80. BitmapClass bmp = new BitmapClass();
  81. bmp.Initialize(Graphics.Device, pGraphics.ClientSize.Width, pGraphics.ClientSize.Height, "Background.bmp", 145, 220, baseViewMatrix);
  82. string[] args = Environment.GetCommandLineArgs();
  83. if (args.Length > 1)
  84. {
  85. for (int i = 1; i < args.Length; i++)
  86. {
  87. string cmd = args[i].ToLower();
  88. if (cmd.Equals("norender"))
  89. {
  90. Render3DAspect = false;
  91. }
  92. else if (cmd.Equals("export"))
  93. {
  94. AutoExportOnLoad = true;
  95. }
  96. else if (cmd.Equals("exportregion"))
  97. {
  98. AutoExportRegionOnLoad = true;
  99. }
  100. else
  101. {
  102. AutoLoadFileName = args[i];
  103. break;
  104. }
  105. }
  106. }
  107. if (AutoLoadFileName.Length > 0)
  108. LoadZoneFile(AutoLoadFileName);
  109. if (AutoExportOnLoad)
  110. exportToolStripMenuItem_Click(null, EventArgs.Empty);
  111. if (AutoExportRegionOnLoad)
  112. toolStripMenuItemExportWater_Click(null, EventArgs.Empty);
  113. if (!Render3DAspect)
  114. {
  115. Application.Exit();
  116. return;
  117. }
  118. // FrustumClass frustum = new FrustumClass();
  119. try
  120. {
  121. MessagePump.Run(this, () =>
  122. {
  123. if (!Graphics.SwapChain.Disposed) {
  124. timer.Frame();
  125. fps.Frame();
  126. // Input code
  127. input.Frame();
  128. position.SetFrameTime(timer.GetTime());
  129. if (this.Focused)
  130. {
  131. if (input.IsKeyPressed(SlimDX.DirectInput.Key.LeftShift) ||
  132. input.IsKeyPressed(SlimDX.DirectInput.Key.RightShift))
  133. position.m_ShiftDown = true;
  134. else
  135. position.m_ShiftDown = false;
  136. position.TurnLeft(input.IsLeftPressed());
  137. position.TurnRight(input.IsRightPressed());
  138. position.MoveForward(input.IsUpPressed());
  139. position.MoveBackward(input.IsDownPressed());
  140. position.MoveUpward(input.IsAPressed());
  141. position.MoveDownward(input.IsZPressed());
  142. position.LookUpward(input.IsPgUpPressed());
  143. position.LookDownward(input.IsPgDownPressed());
  144. if (input.IsLeftMousePressed())
  145. {
  146. TestIntersection(input.GetMouseX(), input.GetMouseY());
  147. }
  148. if (SelectedModel != null)
  149. {
  150. if (input.IsKeyPressed(SlimDX.DirectInput.Key.Delete))
  151. {
  152. m_Models.Remove(SelectedModel);
  153. SelectedModel = null;
  154. }
  155. else if (input.IsKeyPressed(SlimDX.DirectInput.Key.Escape))
  156. {
  157. SelectedModel = null;
  158. }
  159. if (input.IsKeyPressed(SlimDX.DirectInput.Key.LeftControl))
  160. {
  161. if (hitKey == Key.NoConvert)
  162. {
  163. if (input.IsKeyPressed(SlimDX.DirectInput.Key.R))
  164. hitKey = Key.R;
  165. else if (input.IsKeyPressed(SlimDX.DirectInput.Key.T))
  166. hitKey = Key.T;
  167. else if (input.IsKeyPressed(SlimDX.DirectInput.Key.Y))
  168. hitKey = Key.Y;
  169. }
  170. double curTime = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
  171. if ((curTime - timestamp) > 50)
  172. {
  173. if (hitKey == Key.R)
  174. {
  175. if (input.IsKeyReleased(SlimDX.DirectInput.Key.R))
  176. hitKey = Key.NoConvert;
  177. SelectedModel.Rotation += new Vector3(0.01f, 0.0f, 0.0f);
  178. timestamp = curTime;
  179. }
  180. else if (hitKey == Key.T)
  181. {
  182. if (input.IsKeyReleased(SlimDX.DirectInput.Key.T))
  183. hitKey = Key.NoConvert;
  184. SelectedModel.Rotation += new Vector3(0.0f, 0.01f, 0.0f);
  185. timestamp = curTime;
  186. }
  187. else if (hitKey == Key.Y)
  188. {
  189. if (input.IsKeyReleased(SlimDX.DirectInput.Key.Y))
  190. hitKey = Key.NoConvert;
  191. SelectedModel.Rotation += new Vector3(0.0f, 0.0f, 0.01f);
  192. timestamp = curTime;
  193. }
  194. if (SelectedModel.Rotation.X > 360.0f)
  195. SelectedModel.Rotation.X = 0.0f;
  196. if (SelectedModel.Rotation.Y > 360.0f)
  197. SelectedModel.Rotation.Y = 0.0f;
  198. if (SelectedModel.Rotation.Z > 360.0f)
  199. SelectedModel.Rotation.Z = 0.0f;
  200. }
  201. }
  202. }
  203. }
  204. camera.SetPosition(position.GetPosition());
  205. camera.SetRotation(position.GetRotation());
  206. // Render Code
  207. Graphics.BeginScene();
  208. // 3D
  209. // View matrix
  210. camera.Render();
  211. //frustum.ConstructFrustum(1000.0f, Graphics.GetProjectionMatrix(), camera.GetViewMatrix());
  212. foreach (Model model in m_Models) {
  213. //if (frustum.CheckSphere(model.Position.X, model.Position.Y, model.Position.Z, 10.0f))
  214. //{
  215. /*Matrix temp = Matrix.Multiply(Graphics.GetWorldMatrix(), Matrix.Scaling(model.Scale, model.Scale, model.Scale));
  216. temp = Matrix.Multiply(temp, Matrix.RotationYawPitchRoll(model.Rotation.X, model.Rotation.Y, model.Rotation.Z));
  217. temp = Matrix.Multiply(temp, Matrix.Translation(model.Position.X, model.Position.Y, model.Position.Z));*/
  218. model.Render(Graphics, camera, (model == SelectedModel)/*Graphics.Context*/);
  219. //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);
  220. //}
  221. }
  222. // 2D
  223. Graphics.TurnZBufferOff();
  224. Graphics.TurnOnAlphaBlending();
  225. bmp.Render(Graphics, 10, 10);
  226. text.SetFPS(fps.GetFPS(), Graphics.Context);
  227. text.SetPosition(position.GetPosition(), Graphics.Context);
  228. text.SetSelectedModel(SelectedModel, Graphics.Context);
  229. text.Render(Graphics.Context, Graphics.GetWorldMatrix(), Graphics.GetOrthoMatrix());
  230. Graphics.TurnOffAlphaBlending();
  231. Graphics.TurnZBufferOn();
  232. Graphics.EndScene();
  233. }
  234. });
  235. }
  236. catch (Exception exception) { }
  237. }
  238. public bool TestIntersection(int mouseX, int mouseY) {
  239. float pointX, pointY;
  240. Matrix projectionMatrix, viewMatrix, inverseViewMatrix;
  241. projectionMatrix = Graphics.GetProjectionMatrix();
  242. pointX = (2.0F * (float)mouseX / (float)pGraphics.ClientSize.Width - 1.0f) / projectionMatrix.M11;
  243. pointY = (-2.0f * (float)mouseY / (float)pGraphics.ClientSize.Height + 1.0f) / projectionMatrix.M22;
  244. Ray ray = new Ray(new Vector3(), new Vector3(pointX, pointY, 1.0f));
  245. viewMatrix = camera.GetViewMatrix();
  246. inverseViewMatrix = Matrix.Invert(viewMatrix);
  247. ray = new Ray(Vector3.TransformCoordinate(ray.Position, inverseViewMatrix), Vector3.TransformNormal(ray.Direction, inverseViewMatrix));
  248. ray.Direction.Normalize();
  249. float selectionDistance = 0.0f;
  250. foreach (Model model in m_Models) {
  251. float distance = model.TestIntersection(ray, Graphics);
  252. if (distance > 0.0f && (selectionDistance == 0.0f || distance < selectionDistance)) {
  253. selectionDistance = distance;
  254. SelectedModel = model;
  255. }
  256. }
  257. return false;
  258. }
  259. public static void AppendLoadFile(String txt)
  260. {
  261. StreamWriter sw = File.AppendText("loaded.txt");
  262. sw.WriteLine(txt);
  263. sw.Close();
  264. }
  265. private void loadZoneToolStripMenuItem_Click(object sender, EventArgs e)
  266. {
  267. LoadZoneFile();
  268. }
  269. public static String DirName = "";
  270. private void LoadZoneFile(String filename="")
  271. {
  272. bool isDrawFile = false;
  273. string fullName = "";
  274. DirName = "";
  275. if (filename.Length < 1)
  276. {
  277. OpenFileDialog fd = new OpenFileDialog();
  278. fd.Filter = "lut/draw files (*.lut;*.draw)|*.lut;*.draw|lut files (*.lut)|*.lut|draw files (*.draw)|*.draw";
  279. if (fd.ShowDialog() == DialogResult.OK)
  280. {
  281. AppendLoadFile("===================================================");
  282. AppendLoadFile("Loading " + fd.FileName);
  283. if (fd.FileName.EndsWith(".draw"))
  284. {
  285. isDrawFile = true;
  286. string temp = fd.FileName.Substring(0, fd.FileName.LastIndexOf("\\"));
  287. ZoneFile = fd.SafeFileName.Substring(0, fd.SafeFileName.IndexOf(".draw"));
  288. fullName = ZoneFile;
  289. DirName = temp;
  290. filename = fd.FileName;
  291. }
  292. else
  293. {
  294. string temp = fd.FileName.Substring(0, fd.FileName.IndexOf("zones"));
  295. ZoneFile = fd.SafeFileName.Substring(0, fd.SafeFileName.IndexOf(".lut"));
  296. fullName = ZoneFile;
  297. DirName = temp;
  298. filename = fd.FileName;
  299. }
  300. }
  301. }
  302. else
  303. {
  304. if (filename.EndsWith(".draw"))
  305. {
  306. isDrawFile = true;
  307. string temp = filename.Substring(0, filename.LastIndexOf("\\"));
  308. ZoneFile = filename.Substring(0, filename.IndexOf(".draw"));
  309. fullName = filename;
  310. DirName = temp;
  311. }
  312. else
  313. {
  314. string temp = filename.Substring(0, filename.IndexOf("zones"));
  315. ZoneFile = filename.Substring(0, filename.IndexOf(".lut"));
  316. fullName = filename;
  317. DirName = temp;
  318. }
  319. }
  320. if (isDrawFile)
  321. {
  322. Model tmpModel = new Model();
  323. string[] textures = new string[1];
  324. textures[0] = "goblin_ice.dds";
  325. tmpModel.Initialize(Graphics.Device, filename, textures);
  326. tmpModel.Position.X = 0;
  327. tmpModel.Position.Y = 0;
  328. tmpModel.Position.Z = 0;
  329. tmpModel.Rotation.X = 0;
  330. tmpModel.Rotation.Y = 0;
  331. tmpModel.Rotation.Z = 0;
  332. tmpModel.Scale = 1;
  333. tmpModel.WidgetID = 1;
  334. tmpModel.GridID = 1;
  335. m_Models.Add(tmpModel);
  336. return;
  337. }
  338. if (fullName.Length < 1)
  339. {
  340. MessageBox.Show("No filename provided for loading a zonefile!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  341. return;
  342. }
  343. region_nodes = 0;
  344. System.IO.BinaryReader reader2 = new System.IO.BinaryReader(new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read));
  345. // Image(2020): Was ReadUint32, qey_harbor.lut however has 00 1F 00 7A, so that as an int32 is a very large number!
  346. reader2.ReadUInt32();
  347. do
  348. {
  349. if (reader2.BaseStream.Position + 2 >= reader2.BaseStream.Length)
  350. {
  351. break;
  352. }
  353. UInt16 size = reader2.ReadUInt16();
  354. string file = new string(reader2.ReadChars(size));
  355. // was duplicating drive name
  356. file = file.Replace("/", "\\");
  357. file = DirName + file;
  358. AppendLoadFile("VOC Loading: " + file);
  359. if ( file.Contains("qey_harbor_qey_terrain_harbor_geo05_rmob_0"))
  360. {
  361. int test = 0;
  362. }
  363. Eq2Reader reader = new Eq2Reader(new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read));
  364. VeNode venode = reader.ReadNodeObject();
  365. CheckNode(DirName, venode, false, null, null, false);
  366. //MessageBox.Show("Done!");
  367. // 16 bytes between file names, grid id's maybe?
  368. reader2.ReadBytes(16);
  369. } while (true);
  370. }
  371. float x, y, z = 0;
  372. float yaw, pitch, roll = 0;
  373. float scale = 0;
  374. UInt32 widgetID;
  375. UInt32 regionMapVersion = 1;
  376. private void toolStripMenuItemExportWater_Click(object sender, EventArgs e)
  377. {
  378. StreamWriter swfile = new StreamWriter(ZoneFile + ".regionread");
  379. using (BinaryWriter file = new BinaryWriter(File.Open(ZoneFile + ".EQ2Region", FileMode.Create)))
  380. {
  381. file.Write(ZoneFile);
  382. file.Write(regionMapVersion);
  383. file.Write(m_Regions.Count);
  384. Int32 regionNum = 0;
  385. foreach (VeRegion region in m_Regions)
  386. {
  387. file.Write(regionNum);
  388. regionNum += 1;
  389. Int32 node = 0;
  390. file.Write(region.region_type);
  391. file.Write(region.position[0]);
  392. file.Write(region.position[1]);
  393. file.Write(region.position[2]);
  394. file.Write(region.splitdistance);
  395. file.Write(region.vert_count);
  396. swfile.WriteLine();
  397. swfile.WriteLine("REGION: " + region.position[0] + " " + region.position[1] + " " + region.position[2] + " " + region.splitdistance + " - RegionType: " + region.region_type);
  398. if (region.parentNode.regionDefinitionFile != null)
  399. swfile.WriteLine("REGIONFILE: " + region.parentNode.regionDefinitionFile);
  400. if (region.parentNode.environmentDefinitions != null)
  401. {
  402. foreach (string str in region.parentNode.environmentDefinitions)
  403. swfile.WriteLine("EnvDefinition: " + str);
  404. }
  405. swfile.WriteLine("EnvData: " + region.unkcount + " / " + region.parentNode.unk1 + " / " + region.parentNode.unk2);
  406. for (ushort i = 0; i < region.vert_count; ++i)
  407. {
  408. Int32 regiontype = 1;
  409. Int32 special = region.special;
  410. swfile.WriteLine(node + " " + region.m_normals[i, 0] + " " + region.m_normals[i, 1] + " " +
  411. region.m_normals[i, 2] + " " + region.m_distance[i] + " " + regiontype + " " + special + " " +
  412. region.m_childindex[i, 0] + " " + region.m_childindex[i, 1]);
  413. file.Write(node);
  414. node += 1;
  415. file.Write(region.m_normals[i, 0]);
  416. file.Write(region.m_normals[i, 1]);
  417. file.Write(region.m_normals[i, 2]);
  418. file.Write(region.m_distance[i]);
  419. file.Write(regiontype);
  420. file.Write(special);
  421. file.Write((Int32)region.m_childindex[i, 0]);
  422. file.Write((Int32)region.m_childindex[i, 1]);
  423. }
  424. }
  425. file.Close();
  426. }
  427. swfile.Close();
  428. }
  429. UInt32 GridID;
  430. private void CheckNode(string temp, object item, bool parentXform, object next, object parentNode, bool selectNodeParent)
  431. {
  432. if (item is VeMeshGeometryNode)
  433. {
  434. widgetID = ((VeNode)item).WidgetID;
  435. // testing antonica spires which are not oriented correctly
  436. //if ( widgetID == 2990295910 )
  437. // testing tutorial_island02 boat
  438. //if (== 1253219127)
  439. // tutorial_island02 water
  440. if(widgetID == 337652899)
  441. {
  442. int test = 0;
  443. }
  444. if(widgetID == 625647901)
  445. {
  446. int test = 0;
  447. }
  448. Model model = new Model();
  449. model.Initialize(Graphics.Device, (VeMeshGeometryNode)item, temp);
  450. model.Position.X = x;
  451. model.Position.Y = y;
  452. model.Position.Z = z;
  453. model.Rotation.X = yaw;
  454. model.Rotation.Y = pitch;
  455. model.Rotation.Z = roll;
  456. model.Scale = scale;
  457. model.WidgetID = widgetID;
  458. model.GridID = GridID;
  459. m_Models.Add(model);
  460. }
  461. else
  462. {
  463. if (widgetID == 2720558016)
  464. {
  465. int test = 0;
  466. }
  467. float x1 = 0.0f;
  468. float y1 = 0.0f;
  469. float z1 = 0.0f;
  470. if (item is VeEnvironmentNode)
  471. {
  472. VeEnvironmentNode env = (VeEnvironmentNode)item;
  473. if (env.regionDefinitionFile != null && env.regionDefinitionFile.Length > 0)
  474. {
  475. int waterType = 0;
  476. String envFile = "";
  477. if (env.environmentDefinitions != null)
  478. {
  479. foreach (string str in env.environmentDefinitions)
  480. {
  481. envFile = str;
  482. envFile = envFile.Replace("/", "\\");
  483. envFile = DirName + envFile;
  484. waterType = LoadEnvXmlParseLiquid(envFile);
  485. if (waterType != 0)
  486. break;
  487. }
  488. }
  489. bool watervol = env.regionDefinitionFile.Contains("watervol");
  490. bool waterregion = env.regionDefinitionFile.Contains("waterregion");
  491. bool waterregion2 = env.regionDefinitionFile.Contains("water_region");
  492. bool iswater = env.regionDefinitionFile.Contains("water");
  493. bool isocean = env.regionDefinitionFile.Contains("ocean");
  494. bool isvolume = env.regionDefinitionFile.Contains("volume");
  495. AppendLoadFile("Region established: " + waterType + ", " + envFile
  496. + " WaterVol: " + watervol + " WaterRegion: " + waterregion +
  497. " WaterRegion2: " + waterregion2 + " IsWater: " + iswater +
  498. " IsOcean: " + isocean + " IsVolume: " + isvolume);
  499. if (waterType > 0)
  500. {
  501. AppendLoadFile("Region accepted: " + waterType + ", " + envFile
  502. + " WaterVol: " + watervol + " WaterRegion: " + waterregion +
  503. " WaterRegion2: " + waterregion2 + " IsWater: " + iswater +
  504. " IsOcean: " + isocean + " IsVolume: " + isvolume);
  505. Eq2Reader reader2 = new Eq2Reader(new System.IO.FileStream(DirName + env.regionDefinitionFile, System.IO.FileMode.Open, System.IO.FileAccess.Read));
  506. VeRegion region = (VeRegion)reader2.ReadObject();
  507. region.parentNode = env;
  508. region.region_type = 0; // default water volume
  509. if (waterregion) // 'sea'/ocean/waterregion in tutorial_island02 / qeynos_harbor
  510. region.region_type = 1;
  511. else if (waterregion2)
  512. region.region_type = 0;
  513. else if (isvolume && selectNodeParent)
  514. region.region_type = 4;
  515. else if ((isocean && selectNodeParent)) // ocean in antonica/commonlands/tutorial
  516. region.region_type = 3;
  517. else if (isocean && iswater) // caves in frostfang(halas)
  518. region.region_type = 4;
  519. else if (isocean)
  520. region.region_type = 5;
  521. region.special = waterType;
  522. MeshClass tmpMesh = new MeshClass();
  523. region_nodes += region.vert_count;
  524. m_Regions.Add(region);
  525. }
  526. else
  527. {
  528. if (env.regionDefinitionFile != null)
  529. {
  530. AppendLoadFile("Region skipped: " + env.regionDefinitionFile);
  531. }
  532. else
  533. AppendLoadFile("Region skipped: ???");
  534. if (env.environmentDefinitions != null)
  535. {
  536. foreach (string str in env.environmentDefinitions)
  537. AppendLoadFile("EnvDefinition: " + str);
  538. }
  539. }
  540. }
  541. }
  542. else if (item is VeRoomItemNode)
  543. {
  544. yaw = ((VeRoomItemNode)item).orientation[0];
  545. pitch = ((VeRoomItemNode)item).orientation[1];
  546. roll = ((VeRoomItemNode)item).orientation[2];
  547. GridID = ((VeRoomItemNode)item).myId_grid;
  548. }
  549. else if (item is VeXformNode)
  550. {
  551. VeXformNode tmp = (VeXformNode)item;
  552. x1 = ((VeXformNode)item).position[0];
  553. y1 = ((VeXformNode)item).position[1];
  554. z1 = ((VeXformNode)item).position[2];
  555. if ( x1 < -16.39 && x1 > -16.41)
  556. {
  557. int test = 0;
  558. }
  559. if (parentXform)
  560. {
  561. yaw += (((VeXformNode)item).orientation[0]) * (3.141592654f / 180.0f);
  562. pitch += (((VeXformNode)item).orientation[1]) * (3.141592654f / 180.0f);
  563. roll += (((VeXformNode)item).orientation[2]) * (3.141592654f / 180.0f);
  564. }
  565. else
  566. {
  567. yaw = (((VeXformNode)item).orientation[0]) * (3.141592654f / 180.0f);
  568. pitch = (((VeXformNode)item).orientation[1]) * (3.141592654f / 180.0f);
  569. roll = (((VeXformNode)item).orientation[2]) * (3.141592654f / 180.0f);
  570. }
  571. scale = ((VeXformNode)item).scale;
  572. x += x1;
  573. y += y1;
  574. z += z1;
  575. }
  576. if (item != null)
  577. {
  578. float old_x = x, old_y = y, old_z = z;
  579. float old_yaw = yaw, old_pitch = pitch, old_roll = roll;
  580. float old_scale = scale;
  581. System.Collections.IEnumerator enumerator = ((VeNode)item).EnumerateChildren();
  582. bool parentBool = item is VeXformNode;
  583. bool parentSelect = item is VeSelectNode;
  584. if (enumerator.MoveNext())
  585. {
  586. object prevNode = null;
  587. do
  588. {
  589. object curNode = enumerator.Current;
  590. object nextNode = null;
  591. object newParentNode = parentNode;
  592. if (item is VeXformNode)
  593. newParentNode = item;
  594. else if ((item is VeSelectNode))
  595. newParentNode = item;
  596. if (enumerator.MoveNext())
  597. nextNode = enumerator.Current;
  598. if (prevNode != null && prevNode is VeXformNode)
  599. parentBool = false;
  600. CheckNode(temp, curNode, parentBool, nextNode, newParentNode, selectNodeParent ? true : parentSelect);
  601. prevNode = curNode;
  602. if (nextNode == null)
  603. break;
  604. } while (true);
  605. }
  606. if (parentNode is VeSelectNode && parentBool && !parentXform)
  607. {
  608. x = old_x;
  609. y = old_y;
  610. z = old_z;
  611. yaw = old_yaw;
  612. pitch = old_pitch;
  613. roll = old_roll;
  614. x -= x1;
  615. y -= y1;
  616. z -= z1;
  617. }
  618. else if(parentNode is VeSelectNode && next == null)
  619. {
  620. x = 0;
  621. y = 0;
  622. z = 0;
  623. yaw = 0;
  624. pitch = 0;
  625. roll = 0;
  626. }
  627. else if(parentBool && next != null)
  628. {
  629. x = old_x;
  630. y = old_y;
  631. z = old_z;
  632. if (((VeNode)next).WidgetID != ((VeNode)item).WidgetID)
  633. {
  634. yaw = 0;
  635. pitch = 0;
  636. roll = 0;
  637. }
  638. else
  639. {
  640. yaw = old_yaw;
  641. pitch = old_pitch;
  642. roll = old_roll;
  643. }
  644. x -= x1;
  645. y -= y1;
  646. z -= z1;
  647. }
  648. else
  649. {
  650. x = old_x;
  651. y = old_y;
  652. z = old_z;
  653. yaw = old_yaw;
  654. pitch = old_pitch;
  655. roll = old_roll;
  656. x -= x1;
  657. y -= y1;
  658. z -= z1;
  659. }
  660. }
  661. }
  662. }
  663. public static string[] GetTextureFile(string[] spPath, string basePath)
  664. {
  665. string ret = "goblin_ice.dds";
  666. System.Collections.Generic.List<string> strings = new System.Collections.Generic.List<string>();
  667. int i = 0;
  668. while (i < spPath.Length /*&& ret == "goblin_ice.dds"*/)
  669. {
  670. Eq2Reader reader = new Eq2Reader(new System.IO.FileStream(basePath + spPath[i], System.IO.FileMode.Open, System.IO.FileAccess.Read));
  671. VeBase sp = reader.ReadObject();
  672. reader.Close();
  673. if (sp is VeShaderPalette)
  674. {
  675. bool found = false;
  676. for (int s = 0; s < ((VeShaderPalette)sp).shaderNames.Length; s++)
  677. {
  678. String fileName = basePath + ((VeShaderPalette)sp).shaderNames[s];
  679. fileName = fileName.Replace("/", "\\");
  680. System.IO.StreamReader reader2 = new System.IO.StreamReader(fileName);
  681. while (!reader2.EndOfStream)
  682. {
  683. string lineOrig = reader2.ReadLine();
  684. if (lineOrig.Contains("name = \"@tex") && !lineOrig.Contains("Blend") && !lineOrig.Contains("UVSet"))
  685. {
  686. String line = reader2.ReadLine();
  687. while (line.Length < 1)
  688. line = reader2.ReadLine();
  689. line = line.Substring(line.IndexOf('"') + 1);
  690. line = line.Substring(0, line.Length - 1);
  691. ret = basePath + line;
  692. strings.Add(ret);
  693. found = true;
  694. break;
  695. //break;
  696. }
  697. if (found)
  698. break;
  699. }
  700. reader2.Close();
  701. }
  702. }
  703. i++;
  704. }
  705. if (strings.Count == 0)
  706. strings.Add(ret);
  707. return strings.ToArray();
  708. }
  709. private void exportToolStripMenuItem_Click(object sender, EventArgs e)
  710. {
  711. //List<Vector3> MasterVertexList = new List<Vector3>();
  712. Dictionary<UInt32, List<Vector3>> MasterVertexList = new Dictionary<UInt32, List<Vector3>>();
  713. foreach (Model model in m_Models)
  714. {
  715. if (model.WidgetID == 1253219127)
  716. {
  717. int test = 0;
  718. }
  719. List<Vector3> VertexList = model.GetVertices();
  720. UInt32 grid = model.GridID;
  721. if (!MasterVertexList.ContainsKey(grid))
  722. MasterVertexList[grid] = new List<Vector3>();
  723. List<Vector3> convertedVertices = new List<Vector3>();
  724. foreach(Vector3 vect in VertexList)
  725. {
  726. Quaternion rotation = Quaternion.RotationYawPitchRoll(model.Rotation.X, model.Rotation.Y, model.Rotation.Z);
  727. var matrix = Matrix.Identity;
  728. Matrix.RotationQuaternion(ref rotation, out matrix);
  729. Matrix scaled = Matrix.Multiply(matrix, Matrix.Scaling(model.Scale, model.Scale, model.Scale));
  730. Vector3 result = Vector3.Add(Vector3.TransformNormal(vect, scaled), model.Position);
  731. convertedVertices.Add(result);
  732. }
  733. MasterVertexList[grid].AddRange(convertedVertices);
  734. }
  735. float minX = float.NaN;
  736. float minZ = float.NaN;
  737. float maxX = float.NaN;
  738. float maxZ = float.NaN;
  739. foreach (KeyValuePair<UInt32, List<Vector3>> entry in MasterVertexList)
  740. {
  741. foreach (Vector3 v in entry.Value)
  742. {
  743. if (float.IsNaN(minX))
  744. {
  745. minX = v.X;
  746. maxX = v.X;
  747. minZ = v.Z;
  748. maxZ = v.Z;
  749. }
  750. else
  751. {
  752. if (v.X < minX)
  753. minX = v.X;
  754. if (v.X > maxX)
  755. maxX = v.X;
  756. if (v.Z < minZ)
  757. minZ = v.Z;
  758. if (v.Z > maxZ)
  759. maxZ = v.Z;
  760. }
  761. }
  762. }
  763. using (StreamWriter file = new StreamWriter(ZoneFile + ".obj"))
  764. {
  765. // file.WriteLine(ZoneFile);
  766. // file.WriteLine("Min");
  767. // file.WriteLine(minX + " " + minZ);
  768. // file.WriteLine("Max");
  769. // file.WriteLine(maxX + " " + maxZ);
  770. // file.WriteLine("Grid count");
  771. // file.WriteLine(MasterVertexList.Count);
  772. // file.WriteLine();
  773. List<string> indices = new List<string>();
  774. int count = 0;
  775. string buildStr = "";
  776. int curcount = 0;
  777. foreach (KeyValuePair<UInt32, List<Vector3>> entry in MasterVertexList)
  778. {
  779. buildStr = "f ";
  780. // file.WriteLine("Grid");
  781. // file.WriteLine(entry.Key);
  782. // file.WriteLine("Face count");
  783. // file.WriteLine(entry.Value.Count);
  784. foreach (Vector3 v in entry.Value)
  785. {
  786. if (curcount > 2)
  787. {
  788. buildStr += count;
  789. indices.Add(buildStr);
  790. buildStr = "f ";
  791. curcount = 0;
  792. }
  793. else
  794. buildStr += count + " ";
  795. file.WriteLine("v " + v.X.ToString() + " " + v.Y.ToString()
  796. + " " + v.Z.ToString());
  797. count++;
  798. curcount++;
  799. }
  800. }
  801. foreach (string str in indices)
  802. {
  803. file.WriteLine(str);
  804. }
  805. file.Close();
  806. }
  807. using (BinaryWriter file = new BinaryWriter(File.Open(ZoneFile + ".EQ2Map", FileMode.Create)))
  808. {
  809. file.Write(ZoneFile);
  810. file.Write(minX);
  811. file.Write(minZ);
  812. file.Write(maxX);
  813. file.Write(maxZ);
  814. file.Write(MasterVertexList.Count);
  815. foreach (KeyValuePair<UInt32, List<Vector3>> entry in MasterVertexList)
  816. {
  817. file.Write(entry.Key);
  818. file.Write(entry.Value.Count);
  819. foreach (Vector3 v in entry.Value)
  820. {
  821. file.Write(v.X);
  822. file.Write(v.Y);
  823. file.Write(v.Z);
  824. }
  825. }
  826. file.Close();
  827. }
  828. FileInfo fileToCompress = new FileInfo(ZoneFile + ".EQ2Map");
  829. using (FileStream originalFileStream = fileToCompress.OpenRead())
  830. {
  831. if ((File.GetAttributes(fileToCompress.FullName) &
  832. FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
  833. {
  834. using (FileStream compressedFileStream = File.Create(ZoneFile + ".EQ2MapDeflated"))
  835. {
  836. using (GZipStream compressionStream = new GZipStream(compressedFileStream,
  837. CompressionMode.Compress))
  838. {
  839. originalFileStream.CopyTo(compressionStream);
  840. }
  841. }
  842. FileInfo info = new FileInfo(ZoneFile + ".EQ2MapDeflated");
  843. Console.WriteLine($"Compressed {fileToCompress.Name} from {fileToCompress.Length.ToString()} to {info.Length.ToString()} bytes.");
  844. }
  845. }
  846. if (sender != null)
  847. MessageBox.Show("Export Complete!");
  848. }
  849. private int LoadEnvXmlParseLiquid(string filename)
  850. {
  851. try
  852. {
  853. XmlDocument xmlDoc = new XmlDocument();
  854. xmlDoc.Load(filename);
  855. var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
  856. nsmgr.AddNamespace("vdl", "Vdl");
  857. nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
  858. XmlNode atmosphereNode = xmlDoc.SelectSingleNode("/vdl:VdlFile/vdl:Environment/vdl:iAtmosphere", nsmgr);
  859. if (atmosphereNode != null && Convert.ToInt32(atmosphereNode.InnerText) < 0)
  860. return Convert.ToInt32(atmosphereNode.InnerText); // lava
  861. XmlNode liquidNode = xmlDoc.SelectSingleNode("/vdl:VdlFile/vdl:Environment/vdl:nLiquid", nsmgr);
  862. if (liquidNode != null)
  863. return Convert.ToInt32(liquidNode.InnerText);
  864. }
  865. catch (Exception ex)
  866. {
  867. }
  868. return 0;
  869. }
  870. }
  871. }