VGearsPFile.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreAxisAlignedBox.h>
  17. #include <OgreColourValue.h>
  18. #include <OgreMesh.h>
  19. #include <OgreResource.h>
  20. #include <Ogre.h>
  21. #include "common/TypeDefine.h"
  22. #include "common/VGearsManualObject.h"
  23. #include "data/VGearsRSDFile.h"
  24. namespace VGears{
  25. /**
  26. * Handles P files.
  27. *
  28. * P files are binary files containing data which form 3D model. The files specify model's
  29. * vertices, polygons, colors, texture coordinates and model sub-groups. The files do not
  30. * specify references to the texture files, animations, model skeleton or anything else.
  31. * P-files are used as parts of field models, battle models, battle locations on PC version of
  32. * FF7.
  33. */
  34. class PFile : public Ogre::Resource{
  35. public:
  36. /**
  37. * Constructor.
  38. *
  39. * @param[in] creator Pointer to the ResourceManager that is creating this resource.
  40. * @param[in] name The unique name of the resource.
  41. * @param[in] handle @todo Understand and document.
  42. * @param[in] group The name of the resource group to which this resource belong.
  43. * @param[in] is_manual True if the resource is manually loaded, false otherwise.
  44. * @param[in] loader Pointer to a ManualResourceLoader implementation which will be
  45. * called when the Resource wishes to load (should be supplied if is_manual is set to
  46. * true). It can be null, but the Resource will never be able to reload if anything
  47. * ever causes it to unload. Therefore provision of a proper ManualResourceLoader
  48. * instance is strongly recommended.
  49. */
  50. PFile(
  51. Ogre::ResourceManager* creator, const String &name, Ogre::ResourceHandle handle,
  52. const String& group, bool is_manual = false,
  53. Ogre::ManualResourceLoader* loader = nullptr
  54. );
  55. /**
  56. * Destructor.
  57. */
  58. virtual ~PFile();
  59. /**
  60. * Adds a resource group.
  61. *
  62. * A resource group includes a bone, a mesh and a graphical resource.
  63. *
  64. * @param[in] mesh The mesh to add to the group.
  65. * @param[in] bone_name The bone in the skeleton to which to add the group. The bone
  66. * must be in a skeleton assigned to MESH.
  67. * @param[in] rsd File with the resource to add to the group.
  68. */
  69. virtual void AddGroups(
  70. Ogre::Mesh *mesh, const String &bone_name, const RSDFilePtr &rsd
  71. ) const;
  72. /**
  73. * An edge.
  74. */
  75. struct Edge{
  76. /**
  77. * Each of the edge vertices.
  78. */
  79. uint16 index[2];
  80. };
  81. /**
  82. * A polygon definition in the P file.
  83. */
  84. struct PolygonDefinition{
  85. /**
  86. * Unknown data.
  87. */
  88. uint16 unknown_00;
  89. /**
  90. * Polygon vertices (3, always a triangle).
  91. */
  92. uint16 vertex[3];
  93. /**
  94. * The polygon normal (three components).
  95. */
  96. uint16 normal[3];
  97. /**
  98. * The edges of the polygon (3, always a triangle).
  99. */
  100. uint16 edge[3];
  101. /**
  102. * Unknown data.
  103. */
  104. uint16 unknown_14;
  105. /**
  106. * Unknown data.
  107. */
  108. uint16 unknown_16;
  109. };
  110. /**
  111. * Material information in a P file.
  112. */
  113. struct MaterialInformation{
  114. /**
  115. * Unknown data.
  116. */
  117. uint8 unknown[100];
  118. };
  119. /**
  120. * Group data in a P file.
  121. */
  122. struct Group{
  123. /**
  124. * The primitive type of the group.
  125. *
  126. * @todo Understand and document.
  127. */
  128. uint32 primitive_type;
  129. /**
  130. * @todo Understand and document.
  131. */
  132. uint32 polygon_start_index;
  133. /**
  134. * The number of polygons in the group.
  135. */
  136. uint32 num_polygons;
  137. /**
  138. * @todo Understand and document.
  139. */
  140. uint32 vertex_start_index;
  141. /**
  142. * The number of vertices in the group.
  143. */
  144. uint32 nuvertices_;
  145. /**
  146. * @todo Understand and document.
  147. */
  148. uint32 edge_start_index;
  149. /**
  150. * The number of edges in the group.
  151. */
  152. uint32 nuedges_;
  153. /**
  154. * Unknown data.
  155. */
  156. uint32 unknown_1C;
  157. /**
  158. * Unknown data.
  159. */
  160. uint32 unknown_20;
  161. /**
  162. * Unknown data.
  163. */
  164. uint32 unknown_24;
  165. /**
  166. * Unknown data.
  167. */
  168. uint32 unknown_28;
  169. /**
  170. * @todo Understand and document.
  171. */
  172. uint32 texture_coordinate_start_index;
  173. /**
  174. * Indicates if the group has an assigned texture.
  175. */
  176. uint32 has_texture;
  177. /**
  178. * Index of the assigned texture (if any).
  179. */
  180. uint32 texture_index;
  181. };
  182. /**
  183. * A bounding box entry in a P file.
  184. */
  185. struct BBoxEntry{
  186. /**
  187. * Unknown data.
  188. */
  189. uint32 unknown;
  190. /**
  191. * The bounding box.
  192. */
  193. Ogre::AxisAlignedBox bbox;
  194. };
  195. typedef Ogre::ColourValue Colour;
  196. typedef std::vector<Ogre::Vector3> VertexList;
  197. typedef std::vector<Ogre::Vector3> NormalList;
  198. typedef std::vector<Ogre::Vector3> Unkown1List;
  199. typedef std::vector<Ogre::Vector2> TextureCoordinateList;
  200. typedef std::vector<Colour> VertexColorList;
  201. typedef std::vector<Colour> PolygonColorList;
  202. typedef std::vector<Edge> EdgeList;
  203. typedef std::vector<PolygonDefinition> PolygonDefinitionList;
  204. typedef std::vector<Group> GroupList;
  205. typedef std::vector<BBoxEntry> BBoxList;
  206. /**
  207. * The type of resource.
  208. */
  209. static const String RESOURCE_TYPE;
  210. /**
  211. * Retrieves the vertices in the file.
  212. *
  213. * @return The vertices in the file.
  214. */
  215. virtual VertexList& GetVertices();
  216. /**
  217. * Retrieves the normals in the file.
  218. *
  219. * @return The normals in the file.
  220. */
  221. virtual NormalList& GetNormals();
  222. /**
  223. * Retrieves unknown data from the file.
  224. *
  225. * @return Unknown data.
  226. */
  227. virtual Unkown1List& GetUnknown1();
  228. /**
  229. * Retrieves the texture coordinates in the file.
  230. *
  231. * @return The texture coordinates in the file.
  232. */
  233. virtual TextureCoordinateList& GetTextureCoordinates();
  234. /**
  235. * Retrieves the vertex colurs in the file.
  236. *
  237. * @return The vertex colurs in the file.
  238. */
  239. virtual VertexColorList& GetVertexColors();
  240. /**
  241. * Retrieves the polygon colours in the file.
  242. *
  243. * @return The polygon colours in the file.
  244. */
  245. virtual PolygonColorList& GetPolygonColors();
  246. /**
  247. * Retrieves the edges in the file.
  248. *
  249. * @return The edgesvertices in the file.
  250. */
  251. virtual EdgeList& GetEdges();
  252. /**
  253. * Retrieves the polygon definitions in the file.
  254. *
  255. * @return The polygon definitions in the file.
  256. */
  257. virtual PolygonDefinitionList& GetPolygonDefinitions();
  258. /**
  259. * Retrieves the groups in the file.
  260. *
  261. * A group includes a bone, a mesh and a graphical resource.
  262. *
  263. * @return The groups in the file.
  264. */
  265. virtual GroupList& GetGroups();
  266. /**
  267. * Retrieves the bounding boxes in the file.
  268. *
  269. * @return The vertices in the file.
  270. */
  271. virtual BBoxList& GetBBoxes();
  272. protected:
  273. /**
  274. * Loads the file.
  275. */
  276. virtual void loadImpl() override;
  277. /**
  278. * Unloads the file.
  279. */
  280. virtual void unloadImpl() override;
  281. /**
  282. * Calculates the size of the palette.
  283. *
  284. * @return The size of the palette.
  285. * @todo Units?
  286. */
  287. virtual size_t calculateSize() const override;
  288. /**
  289. * Add a group to the file.
  290. *
  291. * A resource group includes a bone, a mesh and a graphical resource.
  292. *
  293. * @param[in] group The group to add to the file.
  294. * @param[in,out] mo The object to add to the group.
  295. * @param[in] sub_name Name for mo.
  296. * @param[in] material_base_name Name of the material for mo.
  297. * @param[in] material_group Resource group of the materials.
  298. * @param[in] bone The bone to assign to bo.
  299. */
  300. virtual void AddGroup(
  301. const Group &group, ManualObject &mo, const String &sub_name,
  302. const String &material_base_name, const String &material_group, const Ogre::Bone *bone
  303. ) const;
  304. /**
  305. * Retrieves the position of a bone.
  306. *
  307. * @param[in] bone The bone.
  308. * @return The position of the bone.
  309. */
  310. virtual Ogre::Vector3 GetPosition(const Ogre::Bone *bone) const;
  311. /**
  312. * Static rotation.
  313. */
  314. static const Ogre::Quaternion STATIC_ROTATION;
  315. /**
  316. * Creates a static rotation.
  317. */
  318. static Ogre::Quaternion CreateStaticRotation();
  319. private:
  320. /**
  321. * The list of vertices.
  322. */
  323. VertexList vertices_;
  324. /**
  325. * The list of normals.
  326. */
  327. NormalList normals_;
  328. /**
  329. * List of unknown data.
  330. */
  331. Unkown1List unknown_1_;
  332. /**
  333. * The list of texture coordinates.
  334. */
  335. TextureCoordinateList texture_coordinates_;
  336. /**
  337. * The list of vertex colours.
  338. */
  339. VertexColorList vertex_colours_;
  340. /**
  341. * The list of polygon colours.
  342. */
  343. PolygonColorList polygon_colours_;
  344. /**
  345. * The list of edges.
  346. */
  347. EdgeList edges_;
  348. /**
  349. * The list of polygon definitions.
  350. */
  351. PolygonDefinitionList polygon_definitions_;
  352. /**
  353. * The list of groups.
  354. */
  355. GroupList groups_;
  356. /**
  357. * The list of bounding boxes.
  358. */
  359. BBoxList bounding_boxes_;
  360. };
  361. typedef Ogre::SharedPtr<PFile> PFilePtr;
  362. }