GPBFile.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef GPBFILE_H_
  2. #define GPBFILE_H_
  3. #include "FileIO.h"
  4. #include "Object.h"
  5. #include "Scene.h"
  6. #include "Node.h"
  7. #include "Camera.h"
  8. #include "Light.h"
  9. #include "Mesh.h"
  10. #include "Reference.h"
  11. #include "ReferenceTable.h"
  12. #include "Animations.h"
  13. #include "Animation.h"
  14. #include "AnimationChannel.h"
  15. namespace gameplay
  16. {
  17. /**
  18. * Increment the version number when making a change that break binary compatibility.
  19. * [0] is major, [1] is minor.
  20. */
  21. const unsigned char GPB_VERSION[2] = {1, 3};
  22. /**
  23. * The GamePlay Binary file class handles writing the GamePlay Binary file.
  24. */
  25. class GPBFile
  26. {
  27. public:
  28. /**
  29. * Constructor.
  30. */
  31. GPBFile(void);
  32. /**
  33. * Destructor.
  34. */
  35. ~GPBFile(void);
  36. /**
  37. * Returns the GPBFile instance.
  38. */
  39. static GPBFile* getInstance();
  40. /**
  41. * Saves the GPBFile as a binary file at filepath.
  42. *
  43. * @param filepath The file name and path to save to.
  44. *
  45. * @return True if successful, false if error.
  46. */
  47. bool saveBinary(const std::string& filepath);
  48. /**
  49. * Saves the GPBFile as a text file at filepath. Useful for debugging.
  50. *
  51. * @param filepath The file name and path to save to.
  52. *
  53. * @return True if successful, false if error.
  54. */
  55. bool saveText(const std::string& filepath);
  56. void add(Object* obj);
  57. void addScene(Scene* scene);
  58. void addCamera(Camera* camera);
  59. void addLight(Light* light);
  60. void addMesh(Mesh* mesh);
  61. void addNode(Node* node);
  62. /**
  63. * Adds a node that does not belong to a scene.
  64. */
  65. void addScenelessNode(Node* node);
  66. void addAnimation(Animation* animation);
  67. /**
  68. * Adds the given object to the ref table.
  69. */
  70. void addToRefTable(Object* obj);
  71. /**
  72. * Returns the object with the given id. Returns NULL if not found.
  73. */
  74. Object* getFromRefTable(const std::string& id);
  75. /**
  76. * Returns true if the id was found in the ref table.
  77. */
  78. bool idExists(const std::string& id);
  79. Camera* getCamera(const char* id);
  80. Light* getLight(const char* id);
  81. Mesh* getMesh(const char* id);
  82. Node* getNode(const char* id);
  83. Animations* getAnimations();
  84. /**
  85. * Returns the number of lights.
  86. */
  87. unsigned int getLightCount() const;
  88. /**
  89. * Adjusts the game play binary file before it is written.
  90. */
  91. void adjust();
  92. /**
  93. * Groups the animations of all mesh skins to be under one animation per mesh skin.
  94. */
  95. void groupMeshSkinAnimations();
  96. /**
  97. * Renames the animations in the list of animation ids to the new animation id.
  98. *
  99. * @param animationIds The list of animations to rename.
  100. * @param newId The new animation id.
  101. */
  102. void renameAnimations(std::vector<std::string>& animationIds, const char* newId);
  103. private:
  104. /**
  105. * Computes the bounds of all meshes in the node hierarchy.
  106. */
  107. void computeBounds(Node* node);
  108. /**
  109. * Optimizes animation data by removing unneccessary channels and keyframes.
  110. */
  111. void optimizeAnimations();
  112. /**
  113. * Decomposes an ANIMATE_SCALE_ROTATE_TRANSLATE channel into 3 new channels. (Scale, Rotate and Translate)
  114. *
  115. * @param animation The animation that the channel belongs to.
  116. * @param channel The animation channel to decompose.
  117. * @param channelIndex Index of the channel.
  118. */
  119. void decomposeTransformAnimationChannel(Animation* animation, AnimationChannel* channel, int channelIndex);
  120. /**
  121. * Moves the animation channels that target the given node and its children to be under the given animation.
  122. *
  123. * @param node The node to recursively search from.
  124. * @param animation The animation to move the channels to.
  125. */
  126. void moveAnimationChannels(Node* node, Animation* animation);
  127. private:
  128. FILE* _file;
  129. std::list<Object*> _objects;
  130. std::list<Camera*> _cameras;
  131. std::list<Light*> _lights;
  132. std::list<Mesh*> _geometry;
  133. /**
  134. * The flat list of all nodes.
  135. */
  136. std::list<Node*> _nodes;
  137. Animations _animations;
  138. bool _animationsAdded;
  139. ReferenceTable _refTable;
  140. };
  141. }
  142. #endif