GPBFile.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "Base.h"
  2. #include "GPBFile.h"
  3. namespace gameplay
  4. {
  5. static GPBFile* __instance = NULL;
  6. GPBFile::GPBFile(void)
  7. : _file(NULL), _animationsAdded(false)
  8. {
  9. __instance = this;
  10. }
  11. GPBFile::~GPBFile(void)
  12. {
  13. }
  14. GPBFile* GPBFile::getInstance()
  15. {
  16. return __instance;
  17. }
  18. void GPBFile::saveBinary(const std::string& filepath)
  19. {
  20. _file = fopen(filepath.c_str(), "w+b");
  21. // identifier
  22. char identifier[] = { '«', 'G', 'P', 'B', '»', '\r', '\n', '\x1A', '\n' };
  23. fwrite(identifier, 1, sizeof(identifier), _file);
  24. // version
  25. fwrite(GPB_VERSION, 1, sizeof(GPB_VERSION), _file);
  26. // write refs
  27. _refTable.writeBinary(_file);
  28. // meshes
  29. write(_geometry.size(), _file);
  30. for (std::list<Mesh*>::const_iterator i = _geometry.begin(); i != _geometry.end(); ++i)
  31. {
  32. (*i)->writeBinary(_file);
  33. }
  34. // Objects
  35. write(_objects.size(), _file);
  36. for (std::list<Object*>::const_iterator i = _objects.begin(); i != _objects.end(); ++i)
  37. {
  38. (*i)->writeBinary(_file);
  39. }
  40. _refTable.updateOffsets(_file);
  41. fclose(_file);
  42. }
  43. void GPBFile::saveText(const std::string& filepath)
  44. {
  45. _file = fopen(filepath.c_str(), "w");
  46. fprintf(_file, "<root>\n");
  47. // write refs
  48. _refTable.writeText(_file);
  49. // meshes
  50. for (std::list<Mesh*>::const_iterator i = _geometry.begin(); i != _geometry.end(); ++i)
  51. {
  52. (*i)->writeText(_file);
  53. }
  54. // Objects
  55. for (std::list<Object*>::const_iterator i = _objects.begin(); i != _objects.end(); ++i)
  56. {
  57. (*i)->writeText(_file);
  58. }
  59. fprintf(_file, "</root>");
  60. fclose(_file);
  61. }
  62. void GPBFile::add(Object* obj)
  63. {
  64. _objects.push_back(obj);
  65. }
  66. void GPBFile::addScene(Scene* scene)
  67. {
  68. addToRefTable(scene);
  69. _objects.push_back(scene);
  70. }
  71. void GPBFile::addCamera(Camera* camera)
  72. {
  73. addToRefTable(camera);
  74. _cameras.push_back(camera);
  75. }
  76. void GPBFile::addLight(Light* light)
  77. {
  78. addToRefTable(light);
  79. _lights.push_back(light);
  80. }
  81. void GPBFile::addMesh(Mesh* mesh)
  82. {
  83. addToRefTable(mesh);
  84. _geometry.push_back(mesh);
  85. }
  86. void GPBFile::addNode(Node* node)
  87. {
  88. addToRefTable(node);
  89. _nodes.push_back(node);
  90. }
  91. void GPBFile::addAnimation(Animation* animation)
  92. {
  93. _animations.add(animation);
  94. if (!_animationsAdded)
  95. {
  96. // The animations container should only be added once and only if the file has at least one animation.
  97. _animationsAdded = true;
  98. addToRefTable(&_animations);
  99. add(&_animations);
  100. }
  101. }
  102. void GPBFile::addToRefTable(Object* obj)
  103. {
  104. if (obj)
  105. {
  106. const std::string& id = obj->getId();
  107. if (id.length() > 0)
  108. {
  109. if (_refTable.get(id) == NULL)
  110. {
  111. _refTable.add(id, obj);
  112. }
  113. }
  114. }
  115. }
  116. Object* GPBFile::getFromRefTable(const std::string& id)
  117. {
  118. return _refTable.get(id);
  119. }
  120. bool GPBFile::idExists(const std::string& id)
  121. {
  122. return _refTable.get(id) != NULL;
  123. }
  124. Camera* GPBFile::getCamera(const char* id)
  125. {
  126. if (!id)
  127. return NULL;
  128. // TODO: O(n) search is not ideal
  129. for (std::list<Camera*>::const_iterator i = _cameras.begin(); i != _cameras.end(); ++i)
  130. {
  131. const std::string& _id = (*i)->getId();
  132. if (_id.length() > 0 && strncmp(id, _id.c_str(), 255) == 0)
  133. {
  134. return *i;
  135. }
  136. }
  137. return NULL;
  138. }
  139. Light* GPBFile::getLight(const char* id)
  140. {
  141. if (!id)
  142. return NULL;
  143. // TODO: O(n) search is not ideal
  144. for (std::list<Light*>::const_iterator i = _lights.begin(); i != _lights.end(); ++i)
  145. {
  146. const std::string& _id = (*i)->getId();
  147. if (_id.length() > 0 && strncmp(id, _id.c_str(), 255) == 0)
  148. {
  149. return *i;
  150. }
  151. }
  152. return NULL;
  153. }
  154. Mesh* GPBFile::getMesh(const char* id)
  155. {
  156. if (!id)
  157. return NULL;
  158. // TODO: O(n) search is not ideal
  159. for (std::list<Mesh*>::const_iterator i = _geometry.begin(); i != _geometry.end(); ++i)
  160. {
  161. const std::string& _id = (*i)->getId();
  162. if (_id.length() > 0 && strncmp(id, _id.c_str(), 255) == 0)
  163. {
  164. return *i;
  165. }
  166. }
  167. return NULL;
  168. }
  169. Node* GPBFile::getNode(const char* id)
  170. {
  171. if (!id)
  172. return NULL;
  173. // TODO: O(n) search is not ideal
  174. for (std::list<Node*>::const_iterator i = _nodes.begin(); i != _nodes.end(); ++i)
  175. {
  176. const std::string& _id = (*i)->getId();
  177. if (_id.length() > 0 && strncmp(id, _id.c_str(), 255) == 0)
  178. {
  179. return *i;
  180. }
  181. }
  182. return NULL;
  183. }
  184. Animations* GPBFile::getAnimations()
  185. {
  186. return &_animations;
  187. }
  188. void GPBFile::adjust()
  189. {
  190. // calculate the ambient color for each scene
  191. for (std::list<Object*>::iterator i = _objects.begin(); i != _objects.end(); ++i)
  192. {
  193. Object* obj = *i;
  194. if (obj->getTypeId() == Object::SCENE_ID)
  195. {
  196. Scene* scene = dynamic_cast<Scene*>(obj);
  197. scene->calcAmbientColor();
  198. }
  199. }
  200. // TODO:
  201. // remove ambient _lights
  202. // for each node
  203. // if node has ambient light
  204. // if node has no camera, mesh or children but 1 ambient light
  205. // delete node and remove from ref table
  206. // delete light and remove from ref table
  207. //
  208. // merge animations if possible
  209. // Search for animations that have the same target and key times and see if they can be merged.
  210. // Blender will output a simple translation animation to 3 separate animations with the same key times but targetting X, Y and Z.
  211. // This can be merged into one animation. Same for scale animations.
  212. }
  213. }