QGearsPFile.h 13 KB

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