GPBFile.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "Base.h"
  2. #include "GPBFile.h"
  3. #include "Transform.h"
  4. #define EPSILON 1.2e-7f;
  5. namespace gameplay
  6. {
  7. static GPBFile* __instance = NULL;
  8. /**
  9. * Returns true if the given value is close to one.
  10. */
  11. static bool isAlmostOne(float value);
  12. GPBFile::GPBFile(void)
  13. : _file(NULL), _animationsAdded(false)
  14. {
  15. __instance = this;
  16. }
  17. GPBFile::~GPBFile(void)
  18. {
  19. }
  20. GPBFile* GPBFile::getInstance()
  21. {
  22. return __instance;
  23. }
  24. void GPBFile::saveBinary(const std::string& filepath)
  25. {
  26. _file = fopen(filepath.c_str(), "w+b");
  27. // identifier
  28. char identifier[] = { '«', 'G', 'P', 'B', '»', '\r', '\n', '\x1A', '\n' };
  29. fwrite(identifier, 1, sizeof(identifier), _file);
  30. // version
  31. fwrite(GPB_VERSION, 1, sizeof(GPB_VERSION), _file);
  32. // write refs
  33. _refTable.writeBinary(_file);
  34. // meshes
  35. write(_geometry.size(), _file);
  36. for (std::list<Mesh*>::const_iterator i = _geometry.begin(); i != _geometry.end(); ++i)
  37. {
  38. (*i)->writeBinary(_file);
  39. }
  40. // Objects
  41. write(_objects.size(), _file);
  42. for (std::list<Object*>::const_iterator i = _objects.begin(); i != _objects.end(); ++i)
  43. {
  44. (*i)->writeBinary(_file);
  45. }
  46. _refTable.updateOffsets(_file);
  47. fclose(_file);
  48. }
  49. void GPBFile::saveText(const std::string& filepath)
  50. {
  51. _file = fopen(filepath.c_str(), "w");
  52. fprintf(_file, "<root>\n");
  53. // write refs
  54. _refTable.writeText(_file);
  55. // meshes
  56. for (std::list<Mesh*>::const_iterator i = _geometry.begin(); i != _geometry.end(); ++i)
  57. {
  58. (*i)->writeText(_file);
  59. }
  60. // Objects
  61. for (std::list<Object*>::const_iterator i = _objects.begin(); i != _objects.end(); ++i)
  62. {
  63. (*i)->writeText(_file);
  64. }
  65. fprintf(_file, "</root>");
  66. fclose(_file);
  67. }
  68. void GPBFile::add(Object* obj)
  69. {
  70. _objects.push_back(obj);
  71. }
  72. void GPBFile::addScene(Scene* scene)
  73. {
  74. addToRefTable(scene);
  75. _objects.push_back(scene);
  76. }
  77. void GPBFile::addCamera(Camera* camera)
  78. {
  79. addToRefTable(camera);
  80. _cameras.push_back(camera);
  81. }
  82. void GPBFile::addLight(Light* light)
  83. {
  84. addToRefTable(light);
  85. _lights.push_back(light);
  86. }
  87. void GPBFile::addMesh(Mesh* mesh)
  88. {
  89. addToRefTable(mesh);
  90. _geometry.push_back(mesh);
  91. }
  92. void GPBFile::addNode(Node* node)
  93. {
  94. addToRefTable(node);
  95. _nodes.push_back(node);
  96. }
  97. void GPBFile::addScenelessNode(Node* node)
  98. {
  99. addToRefTable(node);
  100. _nodes.push_back(node);
  101. // Nodes are normally written to file as part of a scene.
  102. // Nodes that don't belong to a scene need to be written on their own (outside a scene).
  103. // That is why node is added to the list of objects here.
  104. _objects.push_back(node);
  105. }
  106. void GPBFile::addAnimation(Animation* animation)
  107. {
  108. _animations.add(animation);
  109. if (!_animationsAdded)
  110. {
  111. // The animations container should only be added once and only if the file has at least one animation.
  112. _animationsAdded = true;
  113. addToRefTable(&_animations);
  114. add(&_animations);
  115. }
  116. }
  117. void GPBFile::addToRefTable(Object* obj)
  118. {
  119. if (obj)
  120. {
  121. const std::string& id = obj->getId();
  122. if (id.length() > 0)
  123. {
  124. if (_refTable.get(id) == NULL)
  125. {
  126. _refTable.add(id, obj);
  127. }
  128. }
  129. }
  130. }
  131. Object* GPBFile::getFromRefTable(const std::string& id)
  132. {
  133. return _refTable.get(id);
  134. }
  135. bool GPBFile::idExists(const std::string& id)
  136. {
  137. return _refTable.get(id) != NULL;
  138. }
  139. Camera* GPBFile::getCamera(const char* id)
  140. {
  141. if (!id)
  142. return NULL;
  143. // TODO: O(n) search is not ideal
  144. for (std::list<Camera*>::const_iterator i = _cameras.begin(); i != _cameras.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. Light* GPBFile::getLight(const char* id)
  155. {
  156. if (!id)
  157. return NULL;
  158. // TODO: O(n) search is not ideal
  159. for (std::list<Light*>::const_iterator i = _lights.begin(); i != _lights.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. Mesh* GPBFile::getMesh(const char* id)
  170. {
  171. if (!id)
  172. return NULL;
  173. // TODO: O(n) search is not ideal
  174. for (std::list<Mesh*>::const_iterator i = _geometry.begin(); i != _geometry.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. Node* GPBFile::getNode(const char* id)
  185. {
  186. if (!id)
  187. return NULL;
  188. // TODO: O(n) search is not ideal
  189. for (std::list<Node*>::const_iterator i = _nodes.begin(); i != _nodes.end(); ++i)
  190. {
  191. const std::string& _id = (*i)->getId();
  192. if (_id.length() > 0 && strncmp(id, _id.c_str(), 255) == 0)
  193. {
  194. return *i;
  195. }
  196. }
  197. return NULL;
  198. }
  199. Animations* GPBFile::getAnimations()
  200. {
  201. return &_animations;
  202. }
  203. void GPBFile::adjust()
  204. {
  205. // calculate the ambient color for each scene
  206. for (std::list<Object*>::iterator i = _objects.begin(); i != _objects.end(); ++i)
  207. {
  208. Object* obj = *i;
  209. if (obj->getTypeId() == Object::SCENE_ID)
  210. {
  211. Scene* scene = dynamic_cast<Scene*>(obj);
  212. scene->calcAmbientColor();
  213. }
  214. }
  215. // try to convert joint transform animations into rotation animations
  216. //optimizeTransformAnimations(); // TODO: Fix bounding sphere before re-enabling this
  217. // TODO:
  218. // remove ambient _lights
  219. // for each node
  220. // if node has ambient light
  221. // if node has no camera, mesh or children but 1 ambient light
  222. // delete node and remove from ref table
  223. // delete light and remove from ref table
  224. //
  225. // merge animations if possible
  226. // Search for animations that have the same target and key times and see if they can be merged.
  227. // Blender will output a simple translation animation to 3 separate animations with the same key times but targetting X, Y and Z.
  228. // This can be merged into one animation. Same for scale animations.
  229. }
  230. void GPBFile::optimizeTransformAnimations()
  231. {
  232. const unsigned int animationCount = _animations.getAnimationCount();
  233. for (unsigned int animationIndex = 0; animationIndex < animationCount; ++animationIndex)
  234. {
  235. Animation* animation = _animations.getAnimation(animationIndex);
  236. assert(animation);
  237. const int channelCount = animation->getAnimationChannelCount();
  238. // loop backwards because we will be adding and removing channels
  239. for (int channelIndex = channelCount -1; channelIndex >= 0 ; --channelIndex)
  240. {
  241. AnimationChannel* channel = animation->getAnimationChannel(channelIndex);
  242. assert(channel);
  243. // get target node
  244. const Object* obj = _refTable.get(channel->getTargetId());
  245. if (obj && obj->getTypeId() == Object::NODE_ID)
  246. {
  247. const Node* node = static_cast<const Node*>(obj);
  248. if (node->isJoint() && channel->getTargetAttribute() == Transform::ANIMATE_SCALE_ROTATE_TRANSLATE)
  249. {
  250. decomposeTransformAnimationChannel(animation, channel);
  251. animation->remove(channel);
  252. SAFE_DELETE(channel);
  253. }
  254. }
  255. }
  256. }
  257. }
  258. void GPBFile::decomposeTransformAnimationChannel(Animation* animation, const AnimationChannel* channel)
  259. {
  260. const std::vector<float>& keyTimes = channel->getKeyTimes();
  261. const std::vector<float>& keyValues = channel->getKeyValues();
  262. const size_t keyTimesSize = keyTimes.size();
  263. const size_t keyValuesSize = keyValues.size();
  264. std::vector<float> scaleKeyValues;
  265. std::vector<float> rotateKeyValues;
  266. std::vector<float> translateKeyValues;
  267. scaleKeyValues.reserve(keyTimesSize * 3);
  268. rotateKeyValues.reserve(keyTimesSize * 4);
  269. translateKeyValues.reserve(keyTimesSize * 3);
  270. for (size_t kv = 0; kv < keyValuesSize; kv += 10)
  271. {
  272. scaleKeyValues.push_back(keyValues[kv]);
  273. scaleKeyValues.push_back(keyValues[kv+1]);
  274. scaleKeyValues.push_back(keyValues[kv+2]);
  275. rotateKeyValues.push_back(keyValues[kv+3]);
  276. rotateKeyValues.push_back(keyValues[kv+4]);
  277. rotateKeyValues.push_back(keyValues[kv+5]);
  278. rotateKeyValues.push_back(keyValues[kv+6]);
  279. translateKeyValues.push_back(keyValues[kv+7]);
  280. translateKeyValues.push_back(keyValues[kv+8]);
  281. translateKeyValues.push_back(keyValues[kv+9]);
  282. }
  283. // replace transform animation channel with translate, rotate and scale animation channels
  284. // Don't add the scale channel if all the key values are close to 1.0
  285. size_t oneCount = (size_t)std::count_if(scaleKeyValues.begin(), scaleKeyValues.end(), isAlmostOne);
  286. if (scaleKeyValues.size() != oneCount)
  287. {
  288. AnimationChannel* scaleChannel = new AnimationChannel();
  289. scaleChannel->setTargetId(channel->getTargetId());
  290. scaleChannel->setKeyTimes(channel->getKeyTimes());
  291. scaleChannel->setTangentsIn(channel->getTangentsIn());
  292. scaleChannel->setTangentsOut(channel->getTangentsOut());
  293. scaleChannel->setInterpolations(channel->getInterpolationTypes());
  294. scaleChannel->setTargetAttribute(Transform::ANIMATE_SCALE);
  295. scaleChannel->setKeyValues(scaleKeyValues);
  296. scaleChannel->removeDuplicates();
  297. animation->add(scaleChannel);
  298. }
  299. AnimationChannel* rotateChannel = new AnimationChannel();
  300. rotateChannel->setTargetId(channel->getTargetId());
  301. rotateChannel->setKeyTimes(channel->getKeyTimes());
  302. rotateChannel->setTangentsIn(channel->getTangentsIn());
  303. rotateChannel->setTangentsOut(channel->getTangentsOut());
  304. rotateChannel->setInterpolations(channel->getInterpolationTypes());
  305. rotateChannel->setTargetAttribute(Transform::ANIMATE_ROTATE);
  306. rotateChannel->setKeyValues(rotateKeyValues);
  307. rotateChannel->removeDuplicates();
  308. animation->add(rotateChannel);
  309. AnimationChannel* translateChannel = new AnimationChannel();
  310. translateChannel->setTargetId(channel->getTargetId());
  311. translateChannel->setKeyTimes(channel->getKeyTimes());
  312. translateChannel->setTangentsIn(channel->getTangentsIn());
  313. translateChannel->setTangentsOut(channel->getTangentsOut());
  314. translateChannel->setInterpolations(channel->getInterpolationTypes());
  315. translateChannel->setTargetAttribute(Transform::ANIMATE_TRANSLATE);
  316. translateChannel->setKeyValues(translateKeyValues);
  317. translateChannel->removeDuplicates();
  318. animation->add(translateChannel);
  319. }
  320. static bool isAlmostOne(float value)
  321. {
  322. return std::abs(value - 1.0f) < EPSILON;
  323. }
  324. }