GPBFile.cpp 12 KB

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