Bundle.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. #include "Base.h"
  2. #include "Bundle.h"
  3. #include "FileSystem.h"
  4. #include "MeshPart.h"
  5. #include "Scene.h"
  6. #include "Joint.h"
  7. #define BUNDLE_VERSION_MAJOR 1
  8. #define BUNDLE_VERSION_MINOR 2
  9. #define BUNDLE_TYPE_SCENE 1
  10. #define BUNDLE_TYPE_NODE 2
  11. #define BUNDLE_TYPE_ANIMATIONS 3
  12. #define BUNDLE_TYPE_ANIMATION 4
  13. #define BUNDLE_TYPE_ANIMATION_CHANNEL 5
  14. #define BUNDLE_TYPE_MODEL 10
  15. #define BUNDLE_TYPE_MATERIAL 16
  16. #define BUNDLE_TYPE_EFFECT 18
  17. #define BUNDLE_TYPE_CAMERA 32
  18. #define BUNDLE_TYPE_LIGHT 33
  19. #define BUNDLE_TYPE_MESH 34
  20. #define BUNDLE_TYPE_MESHPART 35
  21. #define BUNDLE_TYPE_MESHSKIN 36
  22. #define BUNDLE_TYPE_FONT 128
  23. // For sanity checking string reads
  24. #define BUNDLE_MAX_STRING_LENGTH 5000
  25. namespace gameplay
  26. {
  27. static std::vector<Bundle*> __bundleCache;
  28. Bundle::Bundle(const char* path) :
  29. _path(path), _referenceCount(0), _references(NULL), _file(NULL), _trackedNodes(NULL)
  30. {
  31. }
  32. Bundle::~Bundle()
  33. {
  34. clearLoadSession();
  35. // Remove this Bundle from the cache
  36. std::vector<Bundle*>::iterator itr = std::find(__bundleCache.begin(), __bundleCache.end(), this);
  37. if (itr != __bundleCache.end())
  38. {
  39. __bundleCache.erase(itr);
  40. }
  41. SAFE_DELETE_ARRAY(_references);
  42. if (_file)
  43. {
  44. fclose(_file);
  45. _file = NULL;
  46. }
  47. }
  48. template <class T>
  49. bool Bundle::readArray(unsigned int* length, T** ptr)
  50. {
  51. if (!read(length))
  52. {
  53. return false;
  54. }
  55. if (*length > 0)
  56. {
  57. *ptr = new T[*length];
  58. if (fread(*ptr, sizeof(T), *length, _file) != *length)
  59. {
  60. SAFE_DELETE_ARRAY(*ptr);
  61. return false;
  62. }
  63. }
  64. return true;
  65. }
  66. template <class T>
  67. bool Bundle::readArray(unsigned int* length, std::vector<T>* values)
  68. {
  69. if (!read(length))
  70. {
  71. return false;
  72. }
  73. if (*length > 0 && values)
  74. {
  75. values->resize(*length);
  76. if (fread(&(*values)[0], sizeof(T), *length, _file) != *length)
  77. {
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. template <class T>
  84. bool Bundle::readArray(unsigned int* length, std::vector<T>* values, unsigned int readSize)
  85. {
  86. assert(sizeof(T) >= readSize);
  87. if (!read(length))
  88. {
  89. return false;
  90. }
  91. if (*length > 0 && values)
  92. {
  93. values->resize(*length);
  94. if (fread(&(*values)[0], readSize, *length, _file) != *length)
  95. {
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. std::string readString(FILE* fp)
  102. {
  103. unsigned int length;
  104. if (fread(&length, 4, 1, fp) != 1)
  105. {
  106. return std::string();
  107. }
  108. // Sanity check to detect if string length is far too big
  109. assert(length < BUNDLE_MAX_STRING_LENGTH);
  110. std::string str;
  111. if (length > 0)
  112. {
  113. str.resize(length);
  114. if (fread(&str[0], 1, length, fp) != length)
  115. {
  116. return std::string();
  117. }
  118. }
  119. return str;
  120. }
  121. Bundle* Bundle::create(const char* path)
  122. {
  123. // Search the cache for this bundle
  124. for (unsigned int i = 0, count = __bundleCache.size(); i < count; ++i)
  125. {
  126. Bundle* p = __bundleCache[i];
  127. if (p->_path == path)
  128. {
  129. // Found a match
  130. p->addRef();
  131. return p;
  132. }
  133. }
  134. // Open the bundle
  135. FILE* fp = FileSystem::openFile(path, "rb");
  136. if (!fp)
  137. {
  138. WARN_VARG("Failed to open file: '%s'.", path);
  139. return NULL;
  140. }
  141. // Read the GPG header info
  142. char sig[9];
  143. if (fread(sig, 1, 9, fp) != 9 || memcmp(sig, "«GPB»\r\n\x1A\n", 9) != 0)
  144. {
  145. LOG_ERROR_VARG("Invalid bundle header: %s", path);
  146. fclose(fp);
  147. return NULL;
  148. }
  149. // Read version
  150. unsigned char ver[2];
  151. if (fread(ver, 1, 2, fp) != 2 || ver[0] != BUNDLE_VERSION_MAJOR || ver[1] != BUNDLE_VERSION_MINOR)
  152. {
  153. LOG_ERROR_VARG("Unsupported version (%d.%d) for bundle: %s (expected %d.%d)", (int)ver[0], (int)ver[1], path, BUNDLE_VERSION_MAJOR, BUNDLE_VERSION_MINOR);
  154. fclose(fp);
  155. return NULL;
  156. }
  157. // Read ref table
  158. unsigned int refCount;
  159. if (fread(&refCount, 4, 1, fp) != 1)
  160. {
  161. fclose(fp);
  162. return NULL;
  163. }
  164. // Read all refs
  165. Reference* refs = new Reference[refCount];
  166. for (unsigned int i = 0; i < refCount; ++i)
  167. {
  168. if ((refs[i].id = readString(fp)).empty() ||
  169. fread(&refs[i].type, 4, 1, fp) != 1 ||
  170. fread(&refs[i].offset, 4, 1, fp) != 1)
  171. {
  172. fclose(fp);
  173. SAFE_DELETE_ARRAY(refs);
  174. return NULL;
  175. }
  176. }
  177. // Keep file open for faster reading later
  178. Bundle* bundle = new Bundle(path);
  179. bundle->_referenceCount = refCount;
  180. bundle->_references = refs;
  181. bundle->_file = fp;
  182. return bundle;
  183. }
  184. Bundle::Reference* Bundle::find(const char* id) const
  185. {
  186. // Search the ref table for the given id (case-sensitive)
  187. for (unsigned int i = 0; i < _referenceCount; ++i)
  188. {
  189. if (_references[i].id == id)
  190. {
  191. // Found a match
  192. return &_references[i];
  193. }
  194. }
  195. return NULL;
  196. }
  197. void Bundle::clearLoadSession()
  198. {
  199. for (unsigned int i = 0, count = _meshSkins.size(); i < count; ++i)
  200. {
  201. SAFE_DELETE(_meshSkins[i]);
  202. }
  203. _meshSkins.clear();
  204. }
  205. const char* Bundle::getIdFromOffset() const
  206. {
  207. return getIdFromOffset((unsigned int) ftell(_file));
  208. }
  209. const char* Bundle::getIdFromOffset(unsigned int offset) const
  210. {
  211. // Search the ref table for the given offset
  212. if (offset > 0)
  213. {
  214. for (unsigned int i = 0; i < _referenceCount; ++i)
  215. {
  216. if (_references[i].offset == offset && _references[i].id.length() > 0)
  217. {
  218. return _references[i].id.c_str();
  219. }
  220. }
  221. }
  222. return NULL;
  223. }
  224. Bundle::Reference* Bundle::seekTo(const char* id, unsigned int type)
  225. {
  226. Reference* ref = find(id);
  227. if (ref == NULL)
  228. {
  229. LOG_ERROR_VARG("No object with name '%s' in bundle '%s'.", id, _path.c_str());
  230. return NULL;
  231. }
  232. if (ref->type != type)
  233. {
  234. LOG_ERROR_VARG("Object '%s' in bundle '%s' has type %d (expected type %d).", id, _path.c_str(), (int)ref->type, (int)type);
  235. return NULL;
  236. }
  237. // Seek to the offset of this object
  238. if (fseek(_file, ref->offset, SEEK_SET) != 0)
  239. {
  240. LOG_ERROR_VARG("Failed to seek to object '%s' in bundle '%s'.", id, _path.c_str());
  241. return NULL;
  242. }
  243. return ref;
  244. }
  245. Bundle::Reference* Bundle::seekToFirstType(unsigned int type)
  246. {
  247. // for each Reference
  248. for (unsigned int i = 0; i < _referenceCount; ++i)
  249. {
  250. Reference* ref = &_references[i];
  251. if (ref->type == type)
  252. {
  253. // Found a match
  254. if (fseek(_file, ref->offset, SEEK_SET) != 0)
  255. {
  256. LOG_ERROR_VARG("Failed to seek to object '%s' in bundle '%s'.", ref->id.c_str(), _path.c_str());
  257. return NULL;
  258. }
  259. return ref;
  260. }
  261. }
  262. return NULL;
  263. }
  264. bool Bundle::read(unsigned int* ptr)
  265. {
  266. return fread(ptr, sizeof(unsigned int), 1, _file) == 1;
  267. }
  268. bool Bundle::read(unsigned char* ptr)
  269. {
  270. return fread(ptr, sizeof(unsigned char), 1, _file) == 1;
  271. }
  272. bool Bundle::read(float* ptr)
  273. {
  274. return fread(ptr, sizeof(float), 1, _file) == 1;
  275. }
  276. bool Bundle::readMatrix(float* m)
  277. {
  278. return (fread(m, sizeof(float), 16, _file) == 16);
  279. }
  280. Scene* Bundle::loadScene(const char* id)
  281. {
  282. clearLoadSession();
  283. Reference* ref = NULL;
  284. if (id)
  285. {
  286. ref = seekTo(id, BUNDLE_TYPE_SCENE);
  287. }
  288. else
  289. {
  290. ref = seekToFirstType(BUNDLE_TYPE_SCENE);
  291. }
  292. if (!ref)
  293. {
  294. return NULL;
  295. }
  296. Scene* scene = Scene::createScene();
  297. scene->setId(getIdFromOffset());
  298. // Read the number of children
  299. unsigned int childrenCount;
  300. if (!read(&childrenCount))
  301. {
  302. SAFE_RELEASE(scene);
  303. return NULL;
  304. }
  305. if (childrenCount > 0)
  306. {
  307. // Read each child directly into the scene
  308. for (unsigned int i = 0; i < childrenCount; i++)
  309. {
  310. Node* node = readNode(scene, NULL);
  311. if (node)
  312. {
  313. scene->addNode(node);
  314. node->release(); // scene now owns node
  315. }
  316. }
  317. }
  318. // Read active camera
  319. std::string xref = readString(_file);
  320. if (xref.length() > 1 && xref[0] == '#') // TODO: Handle full xrefs
  321. {
  322. Node* node = scene->findNode(xref.c_str() + 1, true);
  323. Camera* camera = node->getCamera();
  324. assert(camera);
  325. scene->setActiveCamera(camera);
  326. }
  327. // Read ambient color
  328. float red, blue, green;
  329. if (!read(&red))
  330. {
  331. SAFE_RELEASE(scene);
  332. LOG_ERROR_VARG("Failed to read scene ambient %s color in pakcage %s", "red", _path.c_str());
  333. return NULL;
  334. }
  335. if (!read(&green))
  336. {
  337. SAFE_RELEASE(scene);
  338. LOG_ERROR_VARG("Failed to read scene ambient %s color in pakcage %s", "green", _path.c_str());
  339. return NULL;
  340. }
  341. if (!read(&blue))
  342. {
  343. SAFE_RELEASE(scene);
  344. LOG_ERROR_VARG("Failed to read scene ambient %s color in pakcage %s", "blue", _path.c_str());
  345. return NULL;
  346. }
  347. scene->setAmbientColor(red, green, blue);
  348. // parse animations
  349. for (unsigned int i = 0; i < _referenceCount; ++i)
  350. {
  351. Reference* ref = &_references[i];
  352. if (ref->type == BUNDLE_TYPE_ANIMATIONS)
  353. {
  354. // Found a match
  355. if (fseek(_file, ref->offset, SEEK_SET) != 0)
  356. {
  357. LOG_ERROR_VARG("Failed to seek to object '%s' in bundle '%s'.", ref->id.c_str(), _path.c_str());
  358. return NULL;
  359. }
  360. readAnimations(scene);
  361. }
  362. }
  363. resolveJointReferences(scene, NULL);
  364. return scene;
  365. }
  366. Node* Bundle::loadNode(const char* id)
  367. {
  368. return loadNode(id, NULL);
  369. }
  370. Node* Bundle::loadNode(const char* id, Scene* sceneContext)
  371. {
  372. assert(id);
  373. clearLoadSession();
  374. // Load the node and any referenced joints with node tracking enabled.
  375. _trackedNodes = new std::map<std::string, Node*>();
  376. Node* node = loadNode(id, sceneContext, NULL);
  377. if (node)
  378. resolveJointReferences(sceneContext, node);
  379. // Load all animations targeting any nodes or mesh skins under this node's hierarchy.
  380. for (unsigned int i = 0; i < _referenceCount; i++)
  381. {
  382. Reference* ref = &_references[i];
  383. if (ref->type == BUNDLE_TYPE_ANIMATIONS)
  384. {
  385. if (fseek(_file, ref->offset, SEEK_SET) != 0)
  386. {
  387. LOG_ERROR_VARG("Failed to seek to object '%s' in bundle '%s'.", ref->id.c_str(), _path.c_str());
  388. SAFE_DELETE(_trackedNodes);
  389. return NULL;
  390. }
  391. // Read the number of animations in this object.
  392. unsigned int animationCount;
  393. if (!read(&animationCount))
  394. {
  395. LOG_ERROR_VARG("Failed to read %s for %s: %s", "animationCount", "Animations");
  396. SAFE_DELETE(_trackedNodes);
  397. return NULL;
  398. }
  399. for (unsigned int j = 0; j < animationCount; j++)
  400. {
  401. const std::string id = readString(_file);
  402. // Read the number of animation channels in this animation.
  403. unsigned int animationChannelCount;
  404. if (!read(&animationChannelCount))
  405. {
  406. LOG_ERROR_VARG("Failed to read %s for %s: %s", "animationChannelCount", "animation", id.c_str());
  407. SAFE_DELETE(_trackedNodes);
  408. return NULL;
  409. }
  410. Animation* animation = NULL;
  411. for (unsigned int k = 0; k < animationChannelCount; k++)
  412. {
  413. // read targetId
  414. std::string targetId = readString(_file);
  415. if (targetId.empty())
  416. {
  417. LOG_ERROR_VARG("Failed to read %s for %s: %s", "targetId", "animation", id.c_str());
  418. SAFE_DELETE(_trackedNodes);
  419. return NULL;
  420. }
  421. // If the target is one of the loaded nodes/joints, then load the animation.
  422. std::map<std::string, Node*>::iterator iter = _trackedNodes->find(targetId);
  423. if (iter != _trackedNodes->end())
  424. {
  425. // Read target attribute
  426. unsigned int targetAttribute;
  427. if (!read(&targetAttribute))
  428. {
  429. LOG_ERROR_VARG("Failed to read %s for %s: %s", "targetAttribute", "animation", id.c_str());
  430. SAFE_DELETE(_trackedNodes);
  431. return NULL;
  432. }
  433. AnimationTarget* target = iter->second;
  434. if (!target)
  435. {
  436. LOG_ERROR_VARG("Failed to read %s for %s: %s", "animation target", targetId.c_str(), id.c_str());
  437. SAFE_DELETE(_trackedNodes);
  438. return NULL;
  439. }
  440. animation = readAnimationChannelData(animation, id.c_str(), target, targetAttribute);
  441. }
  442. else
  443. {
  444. // Skip the animation channel (passing a target attribute of
  445. // 0 causes the animation to not be created).
  446. unsigned int data;
  447. read(&data);
  448. readAnimationChannelData(NULL, id.c_str(), NULL, 0);
  449. }
  450. }
  451. }
  452. }
  453. }
  454. SAFE_DELETE(_trackedNodes);
  455. return node;
  456. }
  457. Node* Bundle::loadNode(const char* id, Scene* sceneContext, Node* nodeContext)
  458. {
  459. assert(id);
  460. Node* node = NULL;
  461. // Search the passed in loading contexts (scene/node) first to see
  462. // if we've already loaded this node during this load session
  463. if (sceneContext)
  464. {
  465. node = sceneContext->findNode(id, true);
  466. }
  467. else if (nodeContext)
  468. {
  469. node = nodeContext->findNode(id, true);
  470. }
  471. if (node == NULL)
  472. {
  473. // If not yet found, search the ref table and read
  474. Reference* ref = seekTo(id, BUNDLE_TYPE_NODE);
  475. if (ref == NULL)
  476. {
  477. return NULL;
  478. }
  479. node = readNode(sceneContext, nodeContext);
  480. }
  481. return node;
  482. }
  483. bool Bundle::skipNode()
  484. {
  485. const char* id = getIdFromOffset();
  486. // Skip the node's type.
  487. unsigned int nodeType;
  488. if (!read(&nodeType))
  489. {
  490. return false;
  491. }
  492. // Skip over the node's transform and parent ID.
  493. fseek(_file, sizeof(float) * 16, SEEK_CUR);
  494. readString(_file);
  495. // Skip over the node's children.
  496. unsigned int childrenCount;
  497. if (!read(&childrenCount))
  498. {
  499. return false;
  500. }
  501. else if (childrenCount > 0)
  502. {
  503. for (unsigned int i = 0; i < childrenCount; i++)
  504. {
  505. if (!skipNode())
  506. return false;
  507. }
  508. }
  509. // Skip over the node's camera, light, and model attachments.
  510. Camera* camera = readCamera(); SAFE_RELEASE(camera);
  511. Light* light = readLight(); SAFE_RELEASE(light);
  512. Model* model = readModel(id); SAFE_RELEASE(model);
  513. return true;
  514. }
  515. Node* Bundle::readNode(Scene* sceneContext, Node* nodeContext)
  516. {
  517. const char* id = getIdFromOffset();
  518. // Read node type
  519. unsigned int nodeType;
  520. if (!read(&nodeType))
  521. {
  522. return NULL;
  523. }
  524. Node* node = NULL;
  525. switch (nodeType)
  526. {
  527. case Node::NODE:
  528. node = Node::create(id);
  529. break;
  530. case Node::JOINT:
  531. node = Joint::create(id);
  532. break;
  533. default:
  534. return NULL;
  535. }
  536. // If we are tracking nodes and it's not in the set yet, add it.
  537. if (_trackedNodes)
  538. {
  539. std::map<std::string, Node*>::iterator iter = _trackedNodes->find(id);
  540. if (iter != _trackedNodes->end())
  541. {
  542. SAFE_RELEASE(node);
  543. // Skip over the node's transform and parent ID.
  544. fseek(_file, sizeof(float) * 16, SEEK_CUR);
  545. readString(_file);
  546. // Skip over the node's children.
  547. unsigned int childrenCount;
  548. if (!read(&childrenCount))
  549. {
  550. return NULL;
  551. }
  552. else if (childrenCount > 0)
  553. {
  554. for (unsigned int i = 0; i < childrenCount; i++)
  555. {
  556. if (!skipNode())
  557. return NULL;
  558. }
  559. }
  560. return iter->second;
  561. }
  562. else
  563. _trackedNodes->insert(std::make_pair(id, node));
  564. }
  565. // If no loading context is set, set this node to the loading context
  566. if (sceneContext == NULL && nodeContext == NULL)
  567. {
  568. nodeContext = node;
  569. }
  570. // Read transform
  571. float transform[16];
  572. if (fread(transform, sizeof(float), 16, _file) != 16)
  573. {
  574. SAFE_RELEASE(node);
  575. return NULL;
  576. }
  577. setTransform(transform, node);
  578. // Skip over the parent ID.
  579. readString(_file);
  580. // Read children
  581. unsigned int childrenCount;
  582. if (!read(&childrenCount))
  583. {
  584. SAFE_RELEASE(node);
  585. return NULL;
  586. }
  587. if (childrenCount > 0)
  588. {
  589. // Read each child
  590. for (unsigned int i = 0; i < childrenCount; i++)
  591. {
  592. // Search the passed in loading contexts (scene/node) first to see
  593. // if we've already loaded this child node during this load session.
  594. Node* child = NULL;
  595. id = getIdFromOffset();
  596. if (sceneContext)
  597. {
  598. child = sceneContext->findNode(id, true);
  599. }
  600. else if (nodeContext)
  601. {
  602. child = nodeContext->findNode(id, true);
  603. }
  604. // If the child node wasn't already loaded, load it.
  605. if (!child)
  606. child = readNode(sceneContext, nodeContext);
  607. else
  608. {
  609. // Otherwise, skip over its data in the file.
  610. readNode(NULL, NULL);
  611. }
  612. if (child)
  613. {
  614. node->addChild(child);
  615. child->release(); // 'node' now owns this child
  616. }
  617. }
  618. }
  619. // Read camera
  620. Camera* camera = readCamera();
  621. if (camera)
  622. {
  623. node->setCamera(camera);
  624. SAFE_RELEASE(camera);
  625. }
  626. // Read light
  627. Light* light = readLight();
  628. if (light)
  629. {
  630. node->setLight(light);
  631. SAFE_RELEASE(light);
  632. }
  633. // Read model
  634. Model* model = readModel(node->getId());
  635. if (model)
  636. {
  637. node->setModel(model);
  638. SAFE_RELEASE(model);
  639. }
  640. return node;
  641. }
  642. Camera* Bundle::readCamera()
  643. {
  644. unsigned char cameraType;
  645. if (!read(&cameraType))
  646. {
  647. LOG_ERROR_VARG("Failed to load camera type in bundle '%s'.", _path.c_str());
  648. }
  649. if (cameraType == 0)
  650. {
  651. return NULL;
  652. }
  653. // aspect ratio
  654. float aspectRatio;
  655. if (!read(&aspectRatio))
  656. {
  657. LOG_ERROR_VARG("Failed to load camera aspectRatio in bundle '%s'.", _path.c_str());
  658. }
  659. // near plane
  660. float nearPlane;
  661. if (!read(&nearPlane))
  662. {
  663. LOG_ERROR_VARG("Failed to load camera near plane in bundle '%s'.", _path.c_str());
  664. }
  665. // far plane
  666. float farPlane;
  667. if (!read(&farPlane))
  668. {
  669. LOG_ERROR_VARG("Failed to load camera far plane in bundle '%s'.", _path.c_str());
  670. }
  671. Camera* camera = NULL;
  672. if (cameraType == Camera::PERSPECTIVE)
  673. {
  674. // field of view
  675. float fieldOfView;
  676. if (!read(&fieldOfView))
  677. {
  678. LOG_ERROR_VARG("Failed to load camera field of view in bundle '%s'.", _path.c_str());
  679. }
  680. camera = Camera::createPerspective(fieldOfView, aspectRatio, nearPlane, farPlane);
  681. }
  682. else if (cameraType == Camera::ORTHOGRAPHIC)
  683. {
  684. // magnification
  685. float zoomX;
  686. if (!read(&zoomX))
  687. {
  688. LOG_ERROR_VARG("Failed to load camera zoomX in bundle '%s'.", _path.c_str());
  689. }
  690. float zoomY;
  691. if (!read(&zoomY))
  692. {
  693. LOG_ERROR_VARG("Failed to load camera zoomY in bundle '%s'.", _path.c_str());
  694. }
  695. camera = Camera::createOrthographic(zoomX, zoomY, aspectRatio, nearPlane, farPlane);
  696. }
  697. else
  698. {
  699. LOG_ERROR_VARG("Failed to load camera type in bundle '%s'. Invalid camera type.", _path.c_str());
  700. }
  701. return camera;
  702. }
  703. Light* Bundle::readLight()
  704. {
  705. unsigned char type;
  706. if (!read(&type))
  707. {
  708. LOG_ERROR_VARG("Failed to load light %s in bundle '%s'.", "type", _path.c_str());
  709. }
  710. if (type == 0)
  711. {
  712. return NULL;
  713. }
  714. // read color
  715. float red, blue, green;
  716. if (!read(&red) || !read(&blue) || !read(&green))
  717. {
  718. LOG_ERROR_VARG("Failed to load light %s in bundle '%s'.", "color", _path.c_str());
  719. }
  720. Vector3 color(red, blue, green);
  721. Light* light = NULL;
  722. if (type == Light::DIRECTIONAL)
  723. {
  724. light = Light::createDirectional(color);
  725. }
  726. else if (type == Light::POINT)
  727. {
  728. float range;
  729. if (!read(&range))
  730. {
  731. LOG_ERROR_VARG("Failed to load point light %s in bundle '%s'.", "point", _path.c_str());
  732. }
  733. light = Light::createPoint(color, range);
  734. }
  735. else if (type == Light::SPOT)
  736. {
  737. float range, innerAngle, outerAngle;
  738. if (!read(&range) || !read(&innerAngle) || !read(&outerAngle))
  739. {
  740. LOG_ERROR_VARG("Failed to load spot light %s in bundle '%s'.", "spot", _path.c_str());
  741. }
  742. light = Light::createSpot(color, range, innerAngle, outerAngle);
  743. }
  744. else
  745. {
  746. LOG_ERROR_VARG("Failed to load light %s in bundle '%s'.", "type", _path.c_str());
  747. }
  748. return light;
  749. }
  750. Model* Bundle::readModel(const char* nodeId)
  751. {
  752. // Read mesh
  753. Mesh* mesh = NULL;
  754. std::string xref = readString(_file);
  755. if (xref.length() > 1 && xref[0] == '#') // TODO: Handle full xrefs
  756. {
  757. mesh = loadMesh(xref.c_str() + 1, nodeId);
  758. if (mesh)
  759. {
  760. Model* model = Model::create(mesh);
  761. SAFE_RELEASE(mesh);
  762. // Read skin
  763. unsigned char hasSkin;
  764. if (!read(&hasSkin))
  765. {
  766. LOG_ERROR_VARG("Failed to load hasSkin in bundle '%s'.", _path.c_str());
  767. return NULL;
  768. }
  769. if (hasSkin)
  770. {
  771. MeshSkin* skin = readMeshSkin();
  772. if (skin)
  773. {
  774. model->setSkin(skin);
  775. }
  776. }
  777. // Read material
  778. unsigned int materialCount;
  779. if (!read(&materialCount))
  780. {
  781. LOG_ERROR_VARG("Failed to load materialCount in bundle '%s'.", _path.c_str());
  782. return NULL;
  783. }
  784. if (materialCount > 0)
  785. {
  786. // TODO: Material loading not supported yet
  787. }
  788. return model;
  789. }
  790. }
  791. return NULL;
  792. }
  793. MeshSkin* Bundle::readMeshSkin()
  794. {
  795. MeshSkin* meshSkin = new MeshSkin();
  796. // Read bindShape
  797. float bindShape[16];
  798. if (!readMatrix(bindShape))
  799. {
  800. LOG_ERROR_VARG("Failed to load MeshSkin in bundle '%s'.", _path.c_str());
  801. SAFE_DELETE(meshSkin);
  802. return NULL;
  803. }
  804. meshSkin->setBindShape(bindShape);
  805. MeshSkinData* skinData = new MeshSkinData();
  806. skinData->skin = meshSkin;
  807. // Read joint count
  808. unsigned int jointCount;
  809. if (!read(&jointCount))
  810. {
  811. LOG_ERROR_VARG("Failed to load MeshSkin in bundle '%s'.", _path.c_str());
  812. SAFE_DELETE(meshSkin);
  813. SAFE_DELETE(skinData);
  814. return NULL;
  815. }
  816. if (jointCount == 0)
  817. {
  818. SAFE_DELETE(meshSkin);
  819. SAFE_DELETE(skinData);
  820. return NULL;
  821. }
  822. meshSkin->setJointCount(jointCount);
  823. // Read joint xref strings for all joints in the list
  824. for (unsigned int i = 0; i < jointCount; i++)
  825. {
  826. skinData->joints.push_back(readString(_file));
  827. }
  828. // read bindposes
  829. unsigned int jointsBindPosesCount;
  830. if (!read(&jointsBindPosesCount))
  831. {
  832. LOG_ERROR_VARG("Failed to load MeshSkin in bundle '%s'.", _path.c_str());
  833. SAFE_DELETE(meshSkin);
  834. SAFE_DELETE(skinData);
  835. return NULL;
  836. }
  837. if (jointsBindPosesCount > 0)
  838. {
  839. assert(jointCount * 16 == jointsBindPosesCount);
  840. float m[16];
  841. for (unsigned int i = 0; i < jointCount; i++)
  842. {
  843. if (!readMatrix(m))
  844. {
  845. LOG_ERROR_VARG("Failed to load MeshSkin in bundle '%s'.", _path.c_str());
  846. SAFE_DELETE(meshSkin);
  847. SAFE_DELETE(skinData);
  848. return NULL;
  849. }
  850. skinData->inverseBindPoseMatrices.push_back(m);
  851. }
  852. }
  853. // Store the MeshSkinData so we can go back and resolve all joint references later
  854. _meshSkins.push_back(skinData);
  855. return meshSkin;
  856. }
  857. void Bundle::resolveJointReferences(Scene* sceneContext, Node* nodeContext)
  858. {
  859. const unsigned int skinCount = _meshSkins.size();
  860. for (unsigned int i = 0; i < skinCount; ++i)
  861. {
  862. MeshSkinData* skinData = _meshSkins[i];
  863. // Resolve all joints in skin joint list
  864. const unsigned int jointCount = skinData->joints.size();
  865. for (unsigned int j = 0; j < jointCount; ++j)
  866. {
  867. // TODO: Handle full xrefs (not just local # xrefs)
  868. std::string jointId = skinData->joints[j];
  869. if (jointId.length() > 1 && jointId[0] == '#')
  870. {
  871. jointId = jointId.substr(1, jointId.length() - 1);
  872. Node* n = loadNode(jointId.c_str(), sceneContext, nodeContext);
  873. if (n && n->getType() == Node::JOINT)
  874. {
  875. Joint* joint = static_cast<Joint*>(n);
  876. joint->setInverseBindPose(skinData->inverseBindPoseMatrices[j]);
  877. skinData->skin->setJoint(joint, j);
  878. }
  879. }
  880. }
  881. // Set the root joint
  882. if (jointCount > 0)
  883. {
  884. Joint* rootJoint = skinData->skin->getJoint((unsigned int)0);
  885. Node* node = rootJoint;
  886. Node* parent = node->getParent();
  887. while (true)
  888. {
  889. if (parent)
  890. {
  891. if (skinData->skin->getJointIndex(static_cast<Joint*>(parent)) != -1)
  892. {
  893. // Parent is a joint in the MeshSkin, so treat it as the new root
  894. rootJoint = static_cast<Joint*>(parent);
  895. }
  896. node = parent;
  897. parent = node->getParent();
  898. }
  899. else
  900. {
  901. std::string nodeID = node->getId();
  902. while (true)
  903. {
  904. // Get the node's type.
  905. Reference* ref = find(nodeID.c_str());
  906. if (ref == NULL)
  907. {
  908. LOG_ERROR_VARG("No object with name '%s' in bundle '%s'.", nodeID.c_str(), _path.c_str());
  909. break;
  910. }
  911. // Seek to the current node in the file so we can get it's parent ID.
  912. seekTo(nodeID.c_str(), ref->type);
  913. // Skip over the node type (1 unsigned int) and transform (16 floats) and read the parent id.
  914. fseek(_file, sizeof(unsigned int) + sizeof(float)*16, SEEK_CUR);
  915. std::string parentID = readString(_file);
  916. if (parentID.size() > 0)
  917. nodeID = parentID;
  918. else
  919. break;
  920. }
  921. if (nodeID != rootJoint->getId())
  922. loadNode(nodeID.c_str(), sceneContext, nodeContext);
  923. break;
  924. }
  925. }
  926. skinData->skin->setRootJoint(rootJoint);
  927. }
  928. // Remove the joint hierarchy from the scene since it is owned by the mesh skin.
  929. if (sceneContext)
  930. sceneContext->removeNode(skinData->skin->_rootNode);
  931. // Done with this MeshSkinData entry
  932. SAFE_DELETE(_meshSkins[i]);
  933. }
  934. _meshSkins.clear();
  935. }
  936. void Bundle::readAnimation(Scene* scene)
  937. {
  938. const std::string animationId = readString(_file);
  939. // read the number of animation channels in this animation
  940. unsigned int animationChannelCount;
  941. if (!read(&animationChannelCount))
  942. {
  943. LOG_ERROR_VARG("Failed to read %s for %s: %s", "animationChannelCount", "animation", animationId.c_str());
  944. return;
  945. }
  946. Animation* animation = NULL;
  947. for (unsigned int i = 0; i < animationChannelCount; i++)
  948. {
  949. animation = readAnimationChannel(scene, animation, animationId.c_str());
  950. }
  951. }
  952. void Bundle::readAnimations(Scene* scene)
  953. {
  954. // read the number of animations in this object
  955. unsigned int animationCount;
  956. if (!read(&animationCount))
  957. {
  958. LOG_ERROR_VARG("Failed to read %s for %s: %s", "animationCount", "Animations");
  959. return;
  960. }
  961. for (unsigned int i = 0; i < animationCount; i++)
  962. {
  963. readAnimation(scene);
  964. }
  965. }
  966. Animation* Bundle::readAnimationChannel(Scene* scene, Animation* animation, const char* animationId)
  967. {
  968. const char* id = animationId;
  969. // read targetId
  970. std::string targetId = readString(_file);
  971. if (targetId.empty())
  972. {
  973. LOG_ERROR_VARG("Failed to read %s for %s: %s", "targetId", "animation", id);
  974. return NULL;
  975. }
  976. // read target attribute
  977. unsigned int targetAttribute;
  978. if (!read(&targetAttribute))
  979. {
  980. LOG_ERROR_VARG("Failed to read %s for %s: %s", "targetAttribute", "animation", id);
  981. return NULL;
  982. }
  983. //long position = ftell(_file);
  984. //fseek(_file, position, SEEK_SET);
  985. AnimationTarget* target = NULL;
  986. // Search for a node that matches target
  987. if (!target)
  988. {
  989. target = scene->findNode(targetId.c_str());
  990. if (!target)
  991. {
  992. LOG_ERROR_VARG("Failed to read %s for %s: %s", "animation target", targetId.c_str(), id);
  993. return NULL;
  994. }
  995. }
  996. return readAnimationChannelData(animation, animationId, target, targetAttribute);
  997. }
  998. Animation* Bundle::readAnimationChannelData(Animation* animation, const char* id, AnimationTarget* target, unsigned int targetAttribute)
  999. {
  1000. std::vector<unsigned long> keyTimes;
  1001. std::vector<float> values;
  1002. std::vector<float> tangentsIn;
  1003. std::vector<float> tangentsOut;
  1004. std::vector<unsigned long> interpolation;
  1005. // length of the arrays
  1006. unsigned int keyTimesCount;
  1007. unsigned int valuesCount;
  1008. unsigned int tangentsInCount;
  1009. unsigned int tangentsOutCount;
  1010. unsigned int interpolationCount;
  1011. // read key times
  1012. if (!readArray(&keyTimesCount, &keyTimes, sizeof(unsigned int)))
  1013. {
  1014. LOG_ERROR_VARG("Failed to read %s for %s: %s", "keyTimes", "animation", id);
  1015. return NULL;
  1016. }
  1017. // read key values
  1018. if (!readArray(&valuesCount, &values))
  1019. {
  1020. LOG_ERROR_VARG("Failed to read %s for %s: %s", "values", "animation", id);
  1021. return NULL;
  1022. }
  1023. // read tangentsIn
  1024. if (!readArray(&tangentsInCount, &tangentsIn))
  1025. {
  1026. LOG_ERROR_VARG("Failed to read %s for %s: %s", "tangentsIn", "animation", id);
  1027. return NULL;
  1028. }
  1029. // read tangent_out
  1030. if (!readArray(&tangentsOutCount, &tangentsOut))
  1031. {
  1032. LOG_ERROR_VARG("Failed to read %s for %s: %s", "tangentsOut", "animation", id);
  1033. return NULL;
  1034. }
  1035. // read interpolations
  1036. if (!readArray(&interpolationCount, &interpolation, sizeof(unsigned int)))
  1037. {
  1038. LOG_ERROR_VARG("Failed to read %s for %s: %s", "interpolation", "animation", id);
  1039. return NULL;
  1040. }
  1041. Game* game = Game::getInstance();
  1042. AnimationController* controller = game->getAnimationController();
  1043. // TODO: Handle other target attributes later.
  1044. if (targetAttribute > 0)
  1045. {
  1046. assert(keyTimes.size() > 0 && values.size() > 0);
  1047. if (animation == NULL)
  1048. {
  1049. // TODO: This code currently assumes LINEAR only
  1050. animation = target->createAnimation(id, targetAttribute, keyTimesCount, &keyTimes[0], &values[0], Curve::LINEAR);
  1051. }
  1052. else
  1053. {
  1054. animation->createChannel(target, targetAttribute, keyTimesCount, &keyTimes[0], &values[0], Curve::LINEAR);
  1055. }
  1056. }
  1057. return animation;
  1058. }
  1059. Mesh* Bundle::loadMesh(const char* id)
  1060. {
  1061. return loadMesh(id, false);
  1062. }
  1063. Mesh* Bundle::loadMesh(const char* id, const char* nodeId)
  1064. {
  1065. // Save the file position
  1066. long position = ftell(_file);
  1067. // Seek to the specified Mesh
  1068. Reference* ref = seekTo(id, BUNDLE_TYPE_MESH);
  1069. if (ref == NULL)
  1070. {
  1071. return NULL;
  1072. }
  1073. // Read mesh data
  1074. MeshData* meshData = readMeshData();
  1075. if (meshData == NULL)
  1076. {
  1077. return NULL;
  1078. }
  1079. // Create Mesh
  1080. Mesh* mesh = Mesh::createMesh(meshData->vertexFormat, meshData->vertexCount, false);
  1081. if (mesh == NULL)
  1082. {
  1083. LOG_ERROR_VARG("Failed to create mesh: %s", id);
  1084. SAFE_DELETE_ARRAY(meshData);
  1085. return NULL;
  1086. }
  1087. mesh->_url = _path;
  1088. mesh->_url += "#";
  1089. mesh->_url += id;
  1090. mesh->setVertexData(meshData->vertexData, 0, meshData->vertexCount);
  1091. mesh->_boundingBox.set(meshData->boundingBox);
  1092. mesh->_boundingSphere.set(meshData->boundingSphere);
  1093. // Create mesh parts
  1094. for (unsigned int i = 0; i < meshData->parts.size(); ++i)
  1095. {
  1096. MeshPartData* partData = meshData->parts[i];
  1097. MeshPart* part = mesh->addPart(partData->primitiveType, partData->indexFormat, partData->indexCount, false);
  1098. if (part == NULL)
  1099. {
  1100. LOG_ERROR_VARG("Failed to create mesh part (i=%d): %s", i, id);
  1101. SAFE_DELETE(meshData);
  1102. return NULL;
  1103. }
  1104. part->setIndexData(partData->indexData, 0, partData->indexCount);
  1105. }
  1106. SAFE_DELETE(meshData);
  1107. // Restore file pointer
  1108. fseek(_file, position, SEEK_SET);
  1109. return mesh;
  1110. }
  1111. Bundle::MeshData* Bundle::readMeshData()
  1112. {
  1113. // Read vertex format/elements
  1114. unsigned int vertexElementCount;
  1115. if (fread(&vertexElementCount, 4, 1, _file) != 1 || vertexElementCount < 1)
  1116. {
  1117. return NULL;
  1118. }
  1119. VertexFormat::Element* vertexElements = new VertexFormat::Element[vertexElementCount];
  1120. for (unsigned int i = 0; i < vertexElementCount; ++i)
  1121. {
  1122. unsigned int vUsage, vSize;
  1123. if (fread(&vUsage, 4, 1, _file) != 1 || fread(&vSize, 4, 1, _file) != 1)
  1124. {
  1125. SAFE_DELETE_ARRAY(vertexElements);
  1126. return NULL;
  1127. }
  1128. vertexElements[i].usage = (VertexFormat::Usage)vUsage;
  1129. vertexElements[i].size = vSize;
  1130. }
  1131. MeshData* meshData = new MeshData(VertexFormat(vertexElements, vertexElementCount));
  1132. SAFE_DELETE_ARRAY(vertexElements);
  1133. // Read vertex data
  1134. unsigned int vertexByteCount;
  1135. if (fread(&vertexByteCount, 4, 1, _file) != 1 || vertexByteCount == 0)
  1136. {
  1137. SAFE_DELETE(meshData);
  1138. return NULL;
  1139. }
  1140. meshData->vertexCount = vertexByteCount / meshData->vertexFormat.getVertexSize();
  1141. meshData->vertexData = new unsigned char[vertexByteCount];
  1142. if (fread(meshData->vertexData, 1, vertexByteCount, _file) != vertexByteCount)
  1143. {
  1144. SAFE_DELETE(meshData);
  1145. return NULL;
  1146. }
  1147. // Read mesh bounds (bounding box and bounding sphere)
  1148. if (fread(&meshData->boundingBox.min.x, 4, 3, _file) != 3 || fread(&meshData->boundingBox.max.x, 4, 3, _file) != 3)
  1149. {
  1150. SAFE_DELETE(meshData);
  1151. return NULL;
  1152. }
  1153. if (fread(&meshData->boundingSphere.center.x, 4, 3, _file) != 3 || fread(&meshData->boundingSphere.radius, 4, 1, _file) != 1)
  1154. {
  1155. SAFE_DELETE(meshData);
  1156. return NULL;
  1157. }
  1158. // Read mesh parts
  1159. unsigned int meshPartCount;
  1160. if (fread(&meshPartCount, 4, 1, _file) != 1)
  1161. {
  1162. SAFE_DELETE(meshData);
  1163. return NULL;
  1164. }
  1165. for (unsigned int i = 0; i < meshPartCount; ++i)
  1166. {
  1167. // Read primitive type, index format and index count
  1168. unsigned int pType, iFormat, iByteCount;
  1169. if (fread(&pType, 4, 1, _file) != 1 ||
  1170. fread(&iFormat, 4, 1, _file) != 1 ||
  1171. fread(&iByteCount, 4, 1, _file) != 1)
  1172. {
  1173. SAFE_DELETE(meshData);
  1174. return NULL;
  1175. }
  1176. MeshPartData* partData = new MeshPartData();
  1177. meshData->parts.push_back(partData);
  1178. partData->primitiveType = (Mesh::PrimitiveType)pType;
  1179. partData->indexFormat = (Mesh::IndexFormat)iFormat;
  1180. unsigned int indexSize = 0;
  1181. switch (partData->indexFormat)
  1182. {
  1183. case Mesh::INDEX8:
  1184. indexSize = 1;
  1185. break;
  1186. case Mesh::INDEX16:
  1187. indexSize = 2;
  1188. break;
  1189. case Mesh::INDEX32:
  1190. indexSize = 4;
  1191. break;
  1192. }
  1193. partData->indexCount = iByteCount / indexSize;
  1194. partData->indexData = new unsigned char[iByteCount];
  1195. if (fread(partData->indexData, 1, iByteCount, _file) != iByteCount)
  1196. {
  1197. SAFE_DELETE(meshData);
  1198. return NULL;
  1199. }
  1200. }
  1201. return meshData;
  1202. }
  1203. Bundle::MeshData* Bundle::readMeshData(const char* url)
  1204. {
  1205. assert(url);
  1206. unsigned int len = strlen(url);
  1207. if (len == 0)
  1208. return NULL;
  1209. // Parse URL (formatted as 'bundle#id')
  1210. std::string urlstring(url);
  1211. unsigned int pos = urlstring.find('#');
  1212. if (pos == std::string::npos)
  1213. return NULL;
  1214. std::string file = urlstring.substr(0, pos);
  1215. std::string id = urlstring.substr(pos + 1);
  1216. // Load bundle
  1217. Bundle* bundle = Bundle::create(file.c_str());
  1218. if (bundle == NULL)
  1219. return NULL;
  1220. // Seek to mesh with specified ID in bundle
  1221. Reference* ref = bundle->seekTo(id.c_str(), BUNDLE_TYPE_MESH);
  1222. if (ref == NULL)
  1223. return NULL;
  1224. // Read mesh data from current file position
  1225. MeshData* meshData = bundle->readMeshData();
  1226. SAFE_RELEASE(bundle);
  1227. return meshData;
  1228. }
  1229. Font* Bundle::loadFont(const char* id)
  1230. {
  1231. // Seek to the specified Font
  1232. Reference* ref = seekTo(id, BUNDLE_TYPE_FONT);
  1233. if (ref == NULL)
  1234. {
  1235. return NULL;
  1236. }
  1237. // Read font family
  1238. std::string family = readString(_file);
  1239. if (family.empty())
  1240. {
  1241. LOG_ERROR_VARG("Failed to read font family for font: %s", id);
  1242. return NULL;
  1243. }
  1244. // Read font style and size
  1245. unsigned int style, size;
  1246. if (fread(&style, 4, 1, _file) != 1 ||
  1247. fread(&size, 4, 1, _file) != 1)
  1248. {
  1249. LOG_ERROR_VARG("Failed to read style and/or size for font: %s", id);
  1250. return NULL;
  1251. }
  1252. // Read character set
  1253. std::string charset = readString(_file);
  1254. // Read font glyphs
  1255. unsigned int glyphCount;
  1256. if (fread(&glyphCount, 4, 1, _file) != 1 || glyphCount == 0)
  1257. {
  1258. LOG_ERROR_VARG("Failed to read glyph count for font: %s", id);
  1259. return NULL;
  1260. }
  1261. Font::Glyph* glyphs = new Font::Glyph[glyphCount];
  1262. if (fread(glyphs, sizeof(Font::Glyph), glyphCount, _file) != glyphCount)
  1263. {
  1264. LOG_ERROR_VARG("Failed to read %d glyphs for font: %s", glyphCount, id);
  1265. SAFE_DELETE_ARRAY(glyphs);
  1266. return NULL;
  1267. }
  1268. // Read texture
  1269. unsigned int width, height, textureByteCount;
  1270. if (fread(&width, 4, 1, _file) != 1 ||
  1271. fread(&height, 4, 1, _file) != 1 ||
  1272. fread(&textureByteCount, 4, 1, _file) != 1)
  1273. {
  1274. LOG_ERROR_VARG("Failed to read texture attributes for font: %s", id);
  1275. SAFE_DELETE_ARRAY(glyphs);
  1276. return NULL;
  1277. }
  1278. if (textureByteCount != (width * height))
  1279. {
  1280. LOG_ERROR_VARG("Invalid texture byte for font: %s", id);
  1281. SAFE_DELETE_ARRAY(glyphs);
  1282. return NULL;
  1283. }
  1284. unsigned char* textureData = new unsigned char[textureByteCount];
  1285. if (fread(textureData, 1, textureByteCount, _file) != textureByteCount)
  1286. {
  1287. LOG_ERROR_VARG("Failed to read %d texture bytes for font: %s", textureByteCount, id);
  1288. SAFE_DELETE_ARRAY(glyphs);
  1289. SAFE_DELETE_ARRAY(textureData);
  1290. return NULL;
  1291. }
  1292. // Load the texture for the font
  1293. Texture* texture = Texture::create(Texture::ALPHA, width, height, textureData, true);
  1294. // Free the texture data (no longer needed)
  1295. SAFE_DELETE_ARRAY(textureData);
  1296. if (texture == NULL)
  1297. {
  1298. LOG_ERROR_VARG("Failed to create texture for font: %s", id);
  1299. SAFE_DELETE_ARRAY(glyphs);
  1300. return NULL;
  1301. }
  1302. // Create the font
  1303. Font* font = Font::create(family.c_str(), Font::PLAIN, size, glyphs, glyphCount, texture);
  1304. // Free the glyph array
  1305. SAFE_DELETE_ARRAY(glyphs);
  1306. // Release the texture since the Font now owns it
  1307. SAFE_RELEASE(texture);
  1308. if (font)
  1309. {
  1310. font->_path = _path;
  1311. font->_id = id;
  1312. }
  1313. return font;
  1314. }
  1315. void Bundle::setTransform(const float* values, Transform* transform)
  1316. {
  1317. // Load array into transform
  1318. Matrix matrix(values);
  1319. Vector3 scale, translation;
  1320. Quaternion rotation;
  1321. matrix.decompose(&scale, &rotation, &translation);
  1322. transform->setScale(scale);
  1323. transform->setTranslation(translation);
  1324. transform->setRotation(rotation);
  1325. }
  1326. bool Bundle::contains(const char* id) const
  1327. {
  1328. return (find(id) != NULL);
  1329. }
  1330. unsigned int Bundle::getObjectCount() const
  1331. {
  1332. return _referenceCount;
  1333. }
  1334. const char* Bundle::getObjectID(unsigned int index) const
  1335. {
  1336. return (index >= _referenceCount ? NULL : _references[index].id.c_str());
  1337. }
  1338. Bundle::Reference::Reference()
  1339. : type(0), offset(0)
  1340. {
  1341. }
  1342. Bundle::Reference::~Reference()
  1343. {
  1344. }
  1345. Bundle::MeshPartData::MeshPartData() :
  1346. indexCount(0), indexData(NULL)
  1347. {
  1348. }
  1349. Bundle::MeshPartData::~MeshPartData()
  1350. {
  1351. SAFE_DELETE_ARRAY(indexData);
  1352. }
  1353. Bundle::MeshData::MeshData(const VertexFormat& vertexFormat)
  1354. : vertexFormat(vertexFormat), vertexCount(0), vertexData(NULL)
  1355. {
  1356. }
  1357. Bundle::MeshData::~MeshData()
  1358. {
  1359. SAFE_DELETE_ARRAY(vertexData);
  1360. for (unsigned int i = 0; i < parts.size(); ++i)
  1361. {
  1362. SAFE_DELETE(parts[i]);
  1363. }
  1364. }
  1365. }