Main.cs 46 KB

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