Quad.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. namespace OpenVIII.Battle.Dat
  10. {
  11. /// <summary>
  12. /// Section 2f: Quad
  13. /// </summary>
  14. /// <see cref="http://wiki.ffrtt.ru/index.php?title=FF8/FileFormat_DAT#Useful_structures"/>
  15. [StructLayout(LayoutKind.Explicit, Pack = 1, Size = ByteSize)]
  16. public class Quad
  17. {
  18. #region Fields
  19. public const int ByteSize = 20;
  20. [field: FieldOffset(10)]
  21. public readonly ushort TexUnk;
  22. [field: FieldOffset(14)]
  23. public readonly ushort U;
  24. [field: FieldOffset(8)]
  25. public readonly UV Vta;
  26. [field: FieldOffset(12)]
  27. public readonly UV Vtb;
  28. [field: FieldOffset(16)]
  29. public readonly UV Vtc;
  30. [field: FieldOffset(18)]
  31. public readonly UV Vtd;
  32. [field: FieldOffset(0)]
  33. private readonly ushort _a;
  34. [field: FieldOffset(2)]
  35. private readonly ushort _b;
  36. [field: FieldOffset(4)]
  37. private readonly ushort _c;
  38. [field: FieldOffset(6)]
  39. private readonly ushort _d;
  40. #endregion Fields
  41. #region Constructors
  42. [SuppressMessage("ReSharper", "UnusedMember.Local")]
  43. private Quad(BinaryReader br)
  44. {
  45. _a = br.ReadUInt16();
  46. _b = br.ReadUInt16();
  47. _c = br.ReadUInt16();
  48. _d = br.ReadUInt16();
  49. Vta = UV.CreateInstance(br);
  50. TexUnk = br.ReadUInt16();
  51. Vtb = UV.CreateInstance(br);
  52. U = br.ReadUInt16();
  53. Vtc = UV.CreateInstance(br);
  54. Vtd = UV.CreateInstance(br);
  55. }
  56. [SuppressMessage("ReSharper", "UnusedMember.Local")]
  57. private Quad()
  58. {
  59. }
  60. #endregion Constructors
  61. #region Properties
  62. public static byte Count => 6;
  63. public ushort A => (ushort)(_a & 0xFFF);
  64. public ushort B => (ushort)(_b & 0xFFF);
  65. public ushort C => (ushort)(_c & 0xFFF);
  66. public ushort D => (ushort)(_d & 0xFFF);
  67. public byte TextureIndex => (byte)((TexUnk >> 6) & 0b111);
  68. #endregion Properties
  69. #region Indexers
  70. public byte this[int i]
  71. {
  72. get
  73. {
  74. switch (i)
  75. {
  76. case 0:
  77. case 3:
  78. return 0;
  79. case 1:
  80. return 1;
  81. case 2:
  82. case 5:
  83. return 3;
  84. case 4:
  85. return 2;
  86. }
  87. throw new IndexOutOfRangeException($"{this} :: 0-5 are only valid values");
  88. }
  89. }
  90. #endregion Indexers
  91. #region Methods
  92. public static Quad CreateInstance(BinaryReader br) => Extended.ByteArrayToClass<Quad>(br.ReadBytes(ByteSize));
  93. public static IReadOnlyList<Quad> CreateInstances(BinaryReader br, ushort count) =>
  94. Enumerable.Range(0, count).Select(_ => CreateInstance(br)).ToList().AsReadOnly();
  95. public VertexPositionTexture[] GenerateVPT(List<VectorBoneGRP> vertices, Quaternion rotation,
  96. Vector3 translationPosition, TextureHandler preVarTex)
  97. {
  98. var tempVPT = new VertexPositionTexture[Count];
  99. VertexPositionTexture GetVPT(Quad quad, byte i)
  100. {
  101. Vector3 GetVertex(Quad refQuad, byte j)
  102. {
  103. return DatFile.TransformVertex(vertices[refQuad.GetIndex(j)], translationPosition, rotation);
  104. }
  105. // begin stupid hacks to fix Rinoa and Ward in remaster. might break when going above 3X upscale
  106. // if you choose to upscale more crop off dead bottom 128 pixels from SE's render.
  107. // also hack adjusts aspect ratio to handle the eye closing animation frames.
  108. // in remaster you just need to shift all the uv's to the right when eyes close.
  109. var (x, y) = preVarTex.ScaleFactor;
  110. float height;
  111. if (x > y)
  112. height = (float)(preVarTex.Height / Math.Floor(y));
  113. else
  114. height = (float)(preVarTex.Height / Math.Floor(x));
  115. var (newAR, oldAR) = ((float)preVarTex.Width / preVarTex.Height,
  116. preVarTex.ClassicWidth / height);
  117. var width = Math.Abs(newAR - oldAR) < float.Epsilon ? preVarTex.ClassicWidth : height * newAR;
  118. // end hacks
  119. return new VertexPositionTexture(GetVertex(quad, i),
  120. quad.GetUV(i).ToVector2(width, height));
  121. }
  122. tempVPT[0] = tempVPT[3] = GetVPT(this, this[0]);
  123. tempVPT[1] = GetVPT(this, this[1]);
  124. tempVPT[4] = GetVPT(this, this[4]);
  125. tempVPT[2] = tempVPT[5] = GetVPT(this, this[2]);
  126. return tempVPT;
  127. }
  128. public ushort GetIndex(int i)
  129. {
  130. switch (i)
  131. {
  132. case 0:
  133. return A;
  134. case 1:
  135. return B;
  136. case 2:
  137. return C;
  138. case 3:
  139. return D;
  140. }
  141. throw new IndexOutOfRangeException($"{this} :: 0-3 are only valid values");
  142. }
  143. public UV GetUV(int i)
  144. {
  145. switch (i)
  146. {
  147. case 0:
  148. return Vta;
  149. case 1:
  150. return Vtb;
  151. case 2:
  152. return Vtc;
  153. case 3:
  154. return Vtd;
  155. }
  156. throw new IndexOutOfRangeException($"{this} :: 0-3 are only valid values");
  157. }
  158. #endregion Methods
  159. }
  160. }