DAESceneEncoder.cpp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  1. /*
  2. * DAESceneEncoder.h
  3. */
  4. #include "Base.h"
  5. #include "DAESceneEncoder.h"
  6. #include "DAEOptimizer.h"
  7. //#define ENCODER_PRINT_TIME 1
  8. namespace gameplay
  9. {
  10. DAESceneEncoder::DAESceneEncoder()
  11. : _collada(NULL), _dom(NULL), file(NULL), _vertexBlendWeights(NULL), _vertexBlendIndices(NULL)
  12. {
  13. }
  14. DAESceneEncoder::~DAESceneEncoder()
  15. {
  16. }
  17. unsigned int getMaxOffset(domInputLocalOffset_Array& inputArray)
  18. {
  19. unsigned int maxOffset = 0;
  20. for (unsigned int i = 0; i < (int)inputArray.getCount(); ++i)
  21. {
  22. if ( inputArray[i]->getOffset() > maxOffset )
  23. {
  24. maxOffset = (unsigned int)inputArray[i]->getOffset();
  25. }
  26. }
  27. return maxOffset;
  28. }
  29. void DAESceneEncoder::optimizeCOLLADA(const EncoderArguments& arguments, domCOLLADA* dom)
  30. {
  31. DAEOptimizer optimizer(dom);
  32. const std::vector<std::string>& groupAnimatioNodeIds = arguments.getGroupAnimationNodeId();
  33. const std::vector<std::string>& groupAnimatioIds = arguments.getGroupAnimationAnimationId();
  34. assert(groupAnimatioNodeIds.size() == groupAnimatioIds.size());
  35. size_t size = groupAnimatioNodeIds.size();
  36. if (size > 0)
  37. {
  38. begin();
  39. for (size_t i = 0; i < size; ++i)
  40. {
  41. optimizer.combineAnimations(groupAnimatioNodeIds[i], groupAnimatioIds[i]);
  42. }
  43. end("groupAnimation");
  44. }
  45. if (arguments.DAEOutputEnabled())
  46. {
  47. if (!_collada->writeTo(arguments.getFilePath(), arguments.getDAEOutputPath()))
  48. {
  49. fprintf(stderr,"Error: COLLADA failed to write the dom for file:%s\n", arguments.getDAEOutputPath().c_str());
  50. }
  51. }
  52. }
  53. void DAESceneEncoder::triangulate(DAE* dae)
  54. {
  55. daeDatabase* dataBase = dae->getDatabase();
  56. int geometryCount = (int)(dataBase->getElementCount(0, "geometry"));
  57. for (int i = 0; i < geometryCount; ++i)
  58. {
  59. // Find the next geometry element.
  60. domGeometry* domGeometry;
  61. dataBase->getElement((daeElement**)&domGeometry, i, 0, "geometry");
  62. // Get the mesh out of the geometry.
  63. const domMeshRef domMesh = domGeometry->getMesh();
  64. if (!domMesh)
  65. {
  66. continue;
  67. }
  68. // Loop over all the polygons elements.
  69. int polygonsCount = (int)(domMesh->getPolygons_array().getCount());
  70. for (int j = 0; j < polygonsCount; ++j)
  71. {
  72. // Get the polygons out of the mesh.
  73. domPolygons* domPolygons = domMesh->getPolygons_array()[j];
  74. // Create the triangles from the polygons
  75. createTrianglesFromPolygons(domMesh, domPolygons);
  76. }
  77. while (domMesh->getPolygons_array().getCount() > 0)
  78. {
  79. domPolygons* domPolygons = domMesh->getPolygons_array().get(0);
  80. // Remove the polygons from the mesh.
  81. domMesh->removeChildElement(domPolygons);
  82. }
  83. // Loop over all the polylist elements.
  84. int polylistCount = (int)(domMesh->getPolylist_array().getCount());
  85. for (int j = 0; j < polylistCount; ++j)
  86. {
  87. // Get the polylist out of the mesh.
  88. domPolylist* domPolylist = domMesh->getPolylist_array()[j];
  89. // Create the triangles from the polygon list
  90. createTrianglesFromPolylist(domMesh, domPolylist);
  91. }
  92. while (domMesh->getPolylist_array().getCount() > 0)
  93. {
  94. domPolylist* domPolylist = domMesh->getPolylist_array().get(0);
  95. // Remove the polylist from the mesh.
  96. domMesh->removeChildElement(domPolylist);
  97. }
  98. }
  99. }
  100. void DAESceneEncoder::createTrianglesFromPolygons(domMesh* domMesh, domPolygons* domPolygons)
  101. {
  102. // Create a new <triangles> inside the mesh that has the same material as the <polygons>.
  103. domTriangles* triangles = (domTriangles*)domMesh->createAndPlace("triangles");
  104. triangles->setCount(0);
  105. triangles->setMaterial(domPolygons->getMaterial());
  106. domP* domTrianglesP = (domP*)triangles->createAndPlace("p");
  107. // Give the new <triangles> the same <_dae> and <parameters> as the old <polygons>.
  108. for (unsigned int i = 0; i < domPolygons->getInput_array().getCount(); ++i)
  109. {
  110. triangles->placeElement(domPolygons->getInput_array()[i]->clone());
  111. }
  112. // Get the number of inputs and primitives for the polygons array.
  113. unsigned int inputCount = getMaxOffset(domPolygons->getInput_array()) + 1;
  114. unsigned int primitiveCount = domPolygons->getP_array().getCount();
  115. // Triangulate all the primitives, this generates all the triangles in a single <p> element.
  116. for (unsigned int j = 0; j < primitiveCount; ++j)
  117. {
  118. // Check the polygons for consistancy (some exported files have had the wrong number of indices).
  119. domP* domCurrentP = domPolygons->getP_array()[j];
  120. int elementCount = (int)(domCurrentP->getValue().getCount());
  121. if ( (elementCount % inputCount) != 0 )
  122. {
  123. // Skip this case.
  124. }
  125. else
  126. {
  127. unsigned int triangleCount = (elementCount / inputCount) - 2;
  128. // Write out the primitives as triangles, just fan using the first element as the base.
  129. unsigned int index = inputCount;
  130. for (unsigned int k = 0; k < triangleCount; ++k)
  131. {
  132. // First vertex.
  133. for (unsigned int l = 0; l < inputCount; ++l)
  134. {
  135. domTrianglesP->getValue().append(domCurrentP->getValue()[l]);
  136. }
  137. // Second vertex.
  138. for (unsigned int l = 0; l < inputCount; ++l)
  139. {
  140. domTrianglesP->getValue().append(domCurrentP->getValue()[index + l]);
  141. }
  142. // Third vertex.
  143. index += inputCount;
  144. for (unsigned int l = 0; l < inputCount; ++l)
  145. {
  146. domTrianglesP->getValue().append(domCurrentP->getValue()[index + l]);
  147. }
  148. triangles->setCount(triangles->getCount() + 1);
  149. }
  150. }
  151. }
  152. }
  153. void DAESceneEncoder::createTrianglesFromPolylist(domMesh* domMesh, domPolylist* domPolylist)
  154. {
  155. // Create a new <triangles> inside the mesh that has the same material as the <polylist>.
  156. domTriangles* triangles = (domTriangles*)domMesh->createAndPlace("triangles");
  157. triangles->setMaterial(domPolylist->getMaterial());
  158. domP* domTrianglesP = (domP*)triangles->createAndPlace("p");
  159. // Give the new <triangles> the same <_dae> and <parameters> as the old <polylist>.
  160. for (int i = 0; i < (int)(domPolylist->getInput_array().getCount()); ++i)
  161. {
  162. triangles->placeElement(domPolylist->getInput_array()[i]->clone());
  163. }
  164. // Get the number of inputs and primitives for the polygons array.
  165. unsigned int inputCount = getMaxOffset(domPolylist->getInput_array()) + 1;
  166. unsigned int primitiveCount = domPolylist->getVcount()->getValue().getCount();
  167. unsigned int offset = 0;
  168. unsigned int trianglesProcessed = 0;
  169. // Triangulate all the primitives, this generates all the triangles in a single <p> element.
  170. for (unsigned int j = 0; j < primitiveCount; ++j)
  171. {
  172. unsigned int triangleCount = (unsigned int)domPolylist->getVcount()->getValue()[j] - 2;
  173. // Write out the primitives as triangles, just fan using the first element as the base.
  174. int index = inputCount;
  175. for (unsigned int k = 0; k < triangleCount; ++k)
  176. {
  177. // First vertex.
  178. for (unsigned int l = 0; l < inputCount; ++l)
  179. {
  180. domTrianglesP->getValue().append(domPolylist->getP()->getValue()[offset + l]);
  181. }
  182. // Second vertex.
  183. for (unsigned int l = 0; l < inputCount; ++l)
  184. {
  185. domTrianglesP->getValue().append(domPolylist->getP()->getValue()[offset + index + l]);
  186. }
  187. // Third vertex.
  188. index += inputCount;
  189. for (unsigned int l = 0; l < inputCount; ++l)
  190. {
  191. domTrianglesP->getValue().append(domPolylist->getP()->getValue()[offset + index + l]);
  192. }
  193. trianglesProcessed++;
  194. }
  195. offset += (unsigned int)domPolylist->getVcount()->getValue()[j] * inputCount;
  196. }
  197. triangles->setCount(trianglesProcessed);
  198. }
  199. void DAESceneEncoder::write(const std::string& filepath, const EncoderArguments& arguments)
  200. {
  201. _begin = clock();
  202. const char* nodeId = arguments.getNodeId();
  203. bool text = arguments.textOutputEnabled();
  204. std::string filenameOnly = getFilenameFromFilePath(filepath);
  205. std::string dstPath = filepath.substr(0, filepath.find_last_of('/'));
  206. // Load the collada document
  207. _collada = new DAE();
  208. begin();
  209. _dom = _collada->open(filepath);
  210. end("Open file");
  211. if (!_dom)
  212. {
  213. fprintf(stderr,"Error: COLLADA failed to open file:%s\n", filepath.c_str());
  214. if (_collada)
  215. {
  216. delete _collada;
  217. _collada = NULL;
  218. }
  219. return;
  220. }
  221. // Run collada conditioners
  222. begin();
  223. triangulate(_collada);
  224. end("triangulate");
  225. // Optimize the dom before encoding
  226. optimizeCOLLADA(arguments, _dom);
  227. // Find the <visual_scene> element within the <scene>
  228. const domCOLLADA::domSceneRef& domScene = _dom->getScene();
  229. daeElement* scene = NULL;
  230. if (domScene && domScene->getInstance_visual_scene())
  231. {
  232. scene = getVisualScene(domScene);
  233. if (scene)
  234. {
  235. if (nodeId == NULL)
  236. {
  237. // If the -i <node_id> parameter was not passed then write out the entire scene.
  238. begin();
  239. loadScene((domVisual_scene*)scene);
  240. end("load scene");
  241. }
  242. else
  243. {
  244. // Resolve/Search for the node the user specified with the -i <node_id> parameter.
  245. daeSIDResolver resolver(scene, nodeId);
  246. domNode* nodeElement = daeSafeCast<domNode>(resolver.getElement());
  247. if (nodeElement)
  248. {
  249. Node* node = loadNode(nodeElement, NULL);
  250. if (node)
  251. {
  252. _gamePlayFile.addScenelessNode(node);
  253. }
  254. else
  255. {
  256. fprintf(stderr,"COLLADA File loaded to the dom, but failed to load node %s.\n", nodeId);
  257. }
  258. }
  259. else
  260. {
  261. fprintf(stderr,"COLLADA File loaded to the dom, but node was not found with node ID %s.\n", nodeId);
  262. }
  263. }
  264. }
  265. else
  266. {
  267. fprintf(stderr,"COLLADA File loaded to the dom, but query for the dom assets failed.\n");
  268. }
  269. }
  270. else
  271. {
  272. fprintf(stderr, "COLLADA File loaded to the dom, but missing <visual_scene>.\n");
  273. }
  274. // The animations should be loaded last
  275. begin();
  276. loadAnimations(_dom);
  277. end("loadAnimations");
  278. std::string dstFilename = dstPath;
  279. dstFilename.append(1, '/');
  280. dstFilename.append(getFilenameNoExt(filenameOnly));
  281. _gamePlayFile.adjust();
  282. if (text)
  283. {
  284. std::string outFile = dstFilename + ".xml";
  285. fprintf(stderr, "Saving debug file: %s\n", outFile.c_str());
  286. _gamePlayFile.saveText(outFile);
  287. }
  288. else
  289. {
  290. std::string outFile = dstFilename + ".gpb";
  291. fprintf(stderr, "Saving binary file: %s\n", outFile.c_str());
  292. begin();
  293. _gamePlayFile.saveBinary(outFile);
  294. end("save binary");
  295. }
  296. // Cleanup
  297. if (file)
  298. {
  299. fclose(file);
  300. }
  301. if (_collada)
  302. {
  303. delete _collada;
  304. _collada = NULL;
  305. }
  306. }
  307. void DAESceneEncoder::loadAnimations(const domCOLLADA* dom)
  308. {
  309. // Call loadAnimation on all <animation> elements in all <library_animations>
  310. const domLibrary_animations_Array& animationLibrarys = dom->getLibrary_animations_array();
  311. size_t animationLibrarysCount = animationLibrarys.getCount();
  312. for (size_t i = 0; i < animationLibrarysCount; ++i)
  313. {
  314. const domLibrary_animationsRef& libraryAnimation = animationLibrarys.get(i);
  315. const domAnimation_Array& animationArray = libraryAnimation->getAnimation_array();
  316. size_t animationCount = animationArray.getCount();
  317. for (size_t j = 0; j < animationCount; ++j)
  318. {
  319. const domAnimationRef& animationRef = animationArray.get(j);
  320. loadAnimation(animationRef);
  321. }
  322. }
  323. }
  324. void DAESceneEncoder::loadAnimation(const domAnimationRef animationRef, const char* altId)
  325. {
  326. // Animations can contain other animations.
  327. const domAnimation_Array& animationArray = animationRef->getAnimation_array();
  328. unsigned int animationCount = animationArray.getCount();
  329. if (animationCount == 1)
  330. {
  331. // DAE_FBX nests 1 animation within another animation for some reason.
  332. loadAnimation(animationArray.get(0), animationRef->getId());
  333. }
  334. else if ( animationCount > 1)
  335. {
  336. loadAnimation(animationArray.get(0));
  337. }
  338. // <channel> points to one <sampler>
  339. // <sampler> points to multiple <input> elements
  340. // <channel>
  341. const domChannel_Array& channelArray = animationRef->getChannel_array();
  342. size_t channelArrayCount = channelArray.getCount();
  343. if (channelArrayCount > 0)
  344. {
  345. Animation* animation = new Animation();
  346. const char* str = animationRef->getId();
  347. if (str)
  348. {
  349. animation->setId(str);
  350. }
  351. else if (altId)
  352. {
  353. animation->setId(altId);
  354. }
  355. for (size_t i = 0; i < channelArrayCount; ++i)
  356. {
  357. AnimationChannel* animationChannel = new AnimationChannel();
  358. const domChannelRef& channelRef = channelArray.get(i);
  359. // <sampler>
  360. const domSamplerRef sampler = getSampler(channelRef);
  361. assert(sampler);
  362. // <input>
  363. const domInputLocal_Array& inputArray = sampler->getInput_array();
  364. size_t inputArrayCount = inputArray.getCount();
  365. for (size_t j = 0; j < inputArrayCount; ++j)
  366. {
  367. const domInputLocalRef& inputLocal = inputArray.get(j);
  368. // <source>
  369. const domSourceRef source = getSource(inputLocal, animationRef);
  370. std::string semantic = inputLocal->getSemantic();
  371. if (equals(semantic, "INTERPOLATION"))
  372. {
  373. // Interpolation source is a list of strings
  374. loadInterpolation(source, animationChannel);
  375. }
  376. else
  377. {
  378. // The other sources are lists of floats.
  379. std::vector<float> floats;
  380. copyFloats(source->getFloat_array(), &floats);
  381. if (equals(semantic, "INPUT"))
  382. {
  383. // TODO: Ensure param name is TIME?
  384. for (std::vector<float>::iterator k = floats.begin(); k != floats.end(); ++k)
  385. {
  386. // Convert seconds to milliseconds
  387. *k = *k * 1000.0f;
  388. }
  389. animationChannel->setKeyTimes(floats);
  390. }
  391. else if (equals(semantic, "OUTPUT"))
  392. {
  393. animationChannel->setKeyValues(floats);
  394. }
  395. else if (equals(semantic, "IN_TANGENT"))
  396. {
  397. animationChannel->setTangentsIn(floats);
  398. }
  399. else if (equals(semantic, "OUT_TANGENT"))
  400. {
  401. animationChannel->setTangentsOut(floats);
  402. }
  403. }
  404. }
  405. // get target attribute enum value
  406. if (loadTarget(channelRef, animationChannel))
  407. {
  408. if (animationChannel->getKeyTimes().size() > 0)
  409. {
  410. animation->add(animationChannel);
  411. }
  412. }
  413. }
  414. if (animation->getAnimationChannelCount() > 0)
  415. {
  416. _gamePlayFile.addAnimation(animation);
  417. }
  418. else
  419. {
  420. delete animation;
  421. }
  422. }
  423. }
  424. void DAESceneEncoder::loadInterpolation(const domSourceRef source, AnimationChannel* animationChannel)
  425. {
  426. // COLLADA stores the interpolations as a list of strings while GBP uses unsigned int
  427. std::vector<unsigned int> values;
  428. const domName_arrayRef nameArray = getSourceNameArray(source);
  429. assert(nameArray);
  430. const domListOfNames& names = nameArray->getValue();
  431. size_t count = (size_t)names.getCount();
  432. values.resize(count);
  433. if (count > 0)
  434. {
  435. for (size_t i = 0; i < count; ++i)
  436. {
  437. values[i] = AnimationChannel::getInterpolationType(names.get(i));
  438. }
  439. // If all of the interpolation types are the same then only store the interpolation once
  440. // instead of storing the same type for each key frame.
  441. unsigned int firstType = values[0];
  442. bool allEqual = true;
  443. for (size_t i = 1; i < count; ++i)
  444. {
  445. if (firstType != values[i])
  446. {
  447. allEqual = false;
  448. break;
  449. }
  450. }
  451. if (allEqual)
  452. {
  453. values.resize(1);
  454. }
  455. }
  456. animationChannel->setInterpolations(values);
  457. }
  458. bool DAESceneEncoder::loadTarget(const domChannelRef& channelRef, AnimationChannel* animationChannel)
  459. {
  460. // GamePlay requires that animations are baked. Use "Bake Transforms" in your 3D modeling tool.
  461. // If the target of an animation is not a matrix then an error will be printed.
  462. const static char* TRANSFORM_WARNING_FORMAT = "Warning: Node \"%s\":\n %s %s\n";
  463. const static char* TRANSFORM_MESSAGE = "transform found but not supported.\n Use \"Bake Transforms\" option when exporting.";
  464. unsigned int targetProperty = 0;
  465. DAEChannelTarget channelTarget(channelRef);
  466. const char* targetId = channelTarget.getTargetId().c_str();
  467. // TODO: Do we want to support more than one? If yes then this needs to be fixed.
  468. for (size_t i = 0; i < channelTarget.getTargetAttributeCount(); ++i)
  469. {
  470. std::string prop;
  471. channelTarget.getPropertyName(i, &prop);
  472. daeElement* attributeElement = channelTarget.getTargetAttribute(i);
  473. if (attributeElement)
  474. {
  475. daeInt type = attributeElement->typeID();
  476. if (type == domRotate::ID())
  477. {
  478. printf(TRANSFORM_WARNING_FORMAT, targetId, "Rotate", TRANSFORM_MESSAGE);
  479. return false;
  480. /*
  481. // <rotate>
  482. const domRotate* rotate = daeSafeCast<domRotate>(attributeElement);
  483. if (prop.size() > 0)
  484. {
  485. if (equalsIgnoreCase(prop, "ANGLE"))
  486. {
  487. targetProperty = Transform::ANIMATE_ROTATE;
  488. // get the rotation axis
  489. const domFloat4& f = rotate->getValue();
  490. float x = (float)f.get(0);
  491. float y = (float)f.get(1);
  492. float z = (float)f.get(2);
  493. // Get the angle values that were already read
  494. const std::vector<float>& keyValues = animationChannel->getKeyValues();
  495. size_t size = keyValues.size();
  496. assert(size > 0);
  497. // COLLADA only targeted a single prop but GBP requires all 4 rotate values.
  498. // Convert (ANGLE ANGLE ANGLE) to (X Y Z ANGLE X Y Z ANGLE X Y Z ANGLE)
  499. std::vector<float> floats(size * 4);
  500. // Duplicate rotation axis. We will replace only the angle that COLLADA is targeting.
  501. for (size_t j = 0; j < size; ++j)
  502. {
  503. size_t k = j * 4;
  504. floats[k+0] = x;
  505. floats[k+1] = y;
  506. floats[k+2] = z;
  507. floats[k+3] = keyValues[j]; // angle
  508. }
  509. animationChannel->setKeyValues(floats);
  510. }
  511. }
  512. */
  513. }
  514. else if (type == domScale::ID())
  515. {
  516. printf(TRANSFORM_WARNING_FORMAT, targetId, "Scale", TRANSFORM_MESSAGE);
  517. return false;
  518. /*
  519. // <scale>
  520. //const domScale* scale = daeSafeCast<domScale>(attributeElement);
  521. if (equalsIgnoreCase(prop, "X"))
  522. {
  523. targetProperty = Transform::ANIMATE_SCALE_X;
  524. }
  525. else if (equalsIgnoreCase(prop, "Y"))
  526. {
  527. targetProperty = Transform::ANIMATE_SCALE_Y;
  528. }
  529. else if (equalsIgnoreCase(prop, "Z"))
  530. {
  531. targetProperty = Transform::ANIMATE_SCALE_Z;
  532. }
  533. else
  534. {
  535. targetProperty = Transform::ANIMATE_SCALE;
  536. }
  537. */
  538. }
  539. else if (type == domTranslate::ID())
  540. {
  541. printf(TRANSFORM_WARNING_FORMAT, targetId, "Translate", TRANSFORM_MESSAGE);
  542. return false;
  543. /*
  544. // <translate>
  545. //const domTranslate* translate = daeSafeCast<domTranslate>(attributeElement);
  546. if (equalsIgnoreCase(prop, "X"))
  547. {
  548. targetProperty = Transform::ANIMATE_TRANSLATE_X;
  549. }
  550. else if (equalsIgnoreCase(prop, "Y"))
  551. {
  552. targetProperty = Transform::ANIMATE_TRANSLATE_Y;
  553. }
  554. else if (equalsIgnoreCase(prop, "Z"))
  555. {
  556. targetProperty = Transform::ANIMATE_TRANSLATE_Z;
  557. }
  558. else
  559. {
  560. targetProperty = Transform::ANIMATE_TRANSLATE;
  561. }
  562. */
  563. }
  564. else if (type == domMatrix::ID())
  565. {
  566. // If the animation is targetting a matrix then convert it into
  567. // a scale, rotate, translate animation by decomposing the matrix.
  568. targetProperty = Transform::ANIMATE_SCALE_ROTATE_TRANSLATE;
  569. const std::vector<float>& keyValues = animationChannel->getKeyValues();
  570. assert(keyValues.size() % 16 == 0);
  571. // The matrix was 16 floats and the new values will be 10 floats
  572. size_t newSize = keyValues.size() / 16 * 10;
  573. std::vector<float> floats(newSize);
  574. size_t matrixCount = keyValues.size() / 16;
  575. for (size_t i = 0; i < matrixCount; ++i)
  576. {
  577. size_t j = i * 16;
  578. // COLLADA used row-major but the Matrix class uses column-major
  579. Matrix matrix(
  580. keyValues[j+0], keyValues[j+4], keyValues[j+8], keyValues[j+12],
  581. keyValues[j+1], keyValues[j+5], keyValues[j+9], keyValues[j+13],
  582. keyValues[j+2], keyValues[j+6], keyValues[j+10], keyValues[j+14],
  583. keyValues[j+3], keyValues[j+7], keyValues[j+11], keyValues[j+15]);
  584. Vector3 scale;
  585. Quaternion rotation;
  586. Vector3 translation;
  587. matrix.decompose(&scale, &rotation, &translation);
  588. rotation.normalize();
  589. size_t k = i * 10;
  590. floats[k+0] = scale.x;
  591. floats[k+1] = scale.y;
  592. floats[k+2] = scale.z;
  593. floats[k+3] = rotation.x;
  594. floats[k+4] = rotation.y;
  595. floats[k+5] = rotation.z;
  596. floats[k+6] = rotation.w;
  597. floats[k+7] = translation.x;
  598. floats[k+8] = translation.y;
  599. floats[k+9] = translation.z;
  600. }
  601. animationChannel->setKeyValues(floats);
  602. }
  603. }
  604. }
  605. animationChannel->setTargetAttribute(targetProperty);
  606. animationChannel->setTargetId(channelTarget.getTargetId());
  607. //animationChannel->removeDuplicates();
  608. return true;
  609. }
  610. void DAESceneEncoder::begin()
  611. {
  612. #ifdef ENCODER_PRINT_TIME
  613. _begin = clock();
  614. #endif
  615. }
  616. void DAESceneEncoder::end(const char* str)
  617. {
  618. #ifdef ENCODER_PRINT_TIME
  619. clock_t time = clock() - _begin;
  620. fprintf(stderr,"%5d %s\n", time, str);
  621. #endif
  622. }
  623. void DAESceneEncoder::copyFloats(const domFloat_array* source, std::vector<float>* target)
  624. {
  625. std::vector<float>& t = *target;
  626. size_t count = (size_t)source->getCount();
  627. t.resize(count);
  628. const domListOfFloats& listOfFloats = source->getValue();
  629. for (size_t i = 0; i < count; ++i)
  630. {
  631. t[i] = (float)listOfFloats.get(i);
  632. }
  633. }
  634. void DAESceneEncoder::loadScene(const domVisual_scene* visualScene)
  635. {
  636. Scene* scene = new Scene();
  637. const domNode_Array& nodes = visualScene->getNode_array();
  638. scene->setId(visualScene->getId());
  639. if (scene->getId().length() == 0)
  640. {
  641. scene->setId("__SCENE__");
  642. }
  643. size_t childCount = nodes.getCount();
  644. for (size_t i = 0; i < childCount; ++i)
  645. {
  646. scene->add(loadNode(nodes[i], NULL));
  647. }
  648. Node* activeCameraNode = findSceneActiveCameraNode(visualScene, scene);
  649. if (activeCameraNode)
  650. {
  651. scene->setActiveCameraNode(activeCameraNode);
  652. }
  653. _gamePlayFile.addScene(scene);
  654. }
  655. Node* DAESceneEncoder::findSceneActiveCameraNode(const domVisual_scene* visualScene, Scene* scene)
  656. {
  657. // Loops through each evaluate_scene's render until an active camera node is found.
  658. // Returns the first one found.
  659. // Find the active camera
  660. const domVisual_scene::domEvaluate_scene_Array& evaluateScenes = visualScene->getEvaluate_scene_array();
  661. size_t evaluateSceneCount = evaluateScenes.getCount();
  662. for (size_t i = 0; i < evaluateSceneCount; ++i)
  663. {
  664. const domVisual_scene::domEvaluate_scene::domRender_Array& renders = evaluateScenes[i]->getRender_array();
  665. size_t renderCount = renders.getCount();
  666. for (size_t j = 0; j < renderCount; ++j)
  667. {
  668. xsAnyURI cameraNodeURI = renders[i]->getCamera_node();
  669. domNode* nodeRef = daeSafeCast<domNode>(cameraNodeURI.getElement());
  670. if (nodeRef)
  671. {
  672. std::string id = nodeRef->getId();
  673. Node* node = _gamePlayFile.getNode(id.c_str());
  674. if (node)
  675. {
  676. return node;
  677. }
  678. }
  679. }
  680. }
  681. // Find the first node in the scene that contains a camera.
  682. return scene->getFirstCameraNode();
  683. }
  684. Node* DAESceneEncoder::loadNode(domNode* n, Node* parent)
  685. {
  686. Node* node = NULL;
  687. // Check if this node has already been loaded
  688. const char* id = n->getID();
  689. if (id && strlen(id) > 0)
  690. {
  691. node = _gamePlayFile.getNode(n->getID());
  692. if (node)
  693. {
  694. return node;
  695. }
  696. }
  697. // Load the node
  698. node = new Node();
  699. if (parent)
  700. {
  701. parent->addChild(node);
  702. }
  703. if (n->getType() == NODETYPE_JOINT)
  704. {
  705. node->setIsJoint(true);
  706. }
  707. // Set node id
  708. node->setId(n->getId());
  709. // If this node has an id then add it to the ref table
  710. _gamePlayFile.addNode(node);
  711. transformNode(n, node);
  712. loadControllerInstance(n, node);
  713. loadCameraInstance(n, node);
  714. loadLightInstance(n, node);
  715. loadGeometryInstance(n, node);
  716. // Load child nodes
  717. const domNode_Array& childNodes = n->getNode_array();
  718. size_t childCount = childNodes.getCount();
  719. for (size_t i = 0; i < childCount; ++i)
  720. {
  721. loadNode(childNodes.get(i), node);
  722. }
  723. return node;
  724. }
  725. void DAESceneEncoder::transformNode(domNode* domNode, Node* node)
  726. {
  727. // Apply the transform.
  728. // Note that we only honor the first matrix transform specified for the DOM node.
  729. const domMatrix_Array& matrixArray = domNode->getMatrix_array();
  730. if (matrixArray.getCount() > 0)
  731. {
  732. const domMatrixRef& matrix = matrixArray.get(0);
  733. if (!matrix)
  734. {
  735. return;
  736. }
  737. const domFloat4x4& tx = matrix->getValue();
  738. float transform[] = {(float)tx.get(0), (float)tx.get(4), (float)tx.get(8), (float)tx.get(12),
  739. (float)tx.get(1), (float)tx.get(5), (float)tx.get(9), (float)tx.get(13),
  740. (float)tx.get(2), (float)tx.get(6), (float)tx.get(10), (float)tx.get(14),
  741. (float)tx.get(3), (float)tx.get(7), (float)tx.get(11), (float)tx.get(15)};
  742. node->setTransformMatrix(transform);
  743. }
  744. else
  745. {
  746. Matrix transform;
  747. calcTransform(domNode, transform);
  748. node->setTransformMatrix(transform.m);
  749. }
  750. // TODO: Handle transforming by other types (SRT, etc) (see "Node" child elements spec)
  751. /*Vector3 scale;
  752. Quaternion rotation;
  753. Vector3 translation;
  754. localTransform.Decompose(&scale, &rotation, &translation);
  755. node->SetScale(scale);
  756. node->SetRotation(rotation);
  757. node->SetTranslation(translation);*/
  758. }
  759. void DAESceneEncoder::calcTransform(domNode* domNode, Matrix& dstTransform)
  760. {
  761. daeTArray<daeSmartRef<daeElement> > children;
  762. domNode->getChildren(children);
  763. size_t childCount = children.getCount();
  764. for (size_t i = 0; i < childCount; ++i)
  765. {
  766. daeElementRef childElement = children[i];
  767. switch (childElement->getElementType())
  768. {
  769. case COLLADA_TYPE::TRANSLATE:
  770. {
  771. domTranslateRef translateNode = daeSafeCast<domTranslate>(childElement);
  772. float x = (float)translateNode->getValue().get(0);
  773. float y = (float)translateNode->getValue().get(1);
  774. float z = (float)translateNode->getValue().get(2);
  775. dstTransform.translate(x, y, z);
  776. break;
  777. }
  778. case COLLADA_TYPE::ROTATE:
  779. {
  780. domRotateRef rotateNode = daeSafeCast<domRotate>(childElement);
  781. float x = (float)rotateNode->getValue().get(0);
  782. float y = (float)rotateNode->getValue().get(1);
  783. float z = (float)rotateNode->getValue().get(2);
  784. float angle = MATH_DEG_TO_RAD((float)rotateNode->getValue().get(3)); // COLLADA uses degrees, gameplay uses radians
  785. if (x == 1.0f && y == 0.0f && z == 0.0f)
  786. {
  787. dstTransform.rotateX(angle);
  788. }
  789. else if (x == 0.0f && y == 1.0f && z == 0.0f)
  790. {
  791. dstTransform.rotateY(angle);
  792. }
  793. else if (x == 0.0f && y == 0.0f && z == 1.0f)
  794. {
  795. dstTransform.rotateZ(angle);
  796. }
  797. else
  798. {
  799. dstTransform.rotate(x, y, z, angle);
  800. }
  801. break;
  802. }
  803. case COLLADA_TYPE::SCALE:
  804. {
  805. domScaleRef scaleNode = daeSafeCast<domScale>(childElement);
  806. float x = (float)scaleNode->getValue().get(0);
  807. float y = (float)scaleNode->getValue().get(1);
  808. float z = (float)scaleNode->getValue().get(2);
  809. dstTransform.scale(x, y, z);
  810. break;
  811. }
  812. case COLLADA_TYPE::SKEW:
  813. warning("Skew transform found but not supported.");
  814. break;
  815. case COLLADA_TYPE::LOOKAT:
  816. warning("Lookat transform found but not supported.");
  817. break;
  818. default:
  819. break;
  820. }
  821. }
  822. }
  823. void DAESceneEncoder::loadCameraInstance(const domNode* n, Node* node)
  824. {
  825. // Does this node have any camera instances?
  826. const domInstance_camera_Array& instanceCameras = n->getInstance_camera_array();
  827. size_t instanceCameraCount = instanceCameras.getCount();
  828. for (size_t i = 0; i < instanceCameraCount; ++i)
  829. {
  830. // Get the camrea object
  831. const domInstance_camera* cameraInstanceRef = instanceCameras.get(i);
  832. xsAnyURI cameraURI = cameraInstanceRef->getUrl();
  833. domCamera* cameraRef = daeSafeCast<domCamera>(cameraURI.getElement());
  834. if (cameraRef)
  835. {
  836. Camera* camera = loadCamera(cameraRef);
  837. if (camera)
  838. {
  839. node->setCamera(camera);
  840. }
  841. }
  842. else
  843. {
  844. // warning
  845. }
  846. }
  847. }
  848. void DAESceneEncoder::loadLightInstance(const domNode* n, Node* node)
  849. {
  850. // Does this node have any light instances?
  851. const domInstance_light_Array& instanceLights = n->getInstance_light_array();
  852. size_t instanceLightCount = instanceLights.getCount();
  853. for (size_t i = 0; i < instanceLightCount; ++i)
  854. {
  855. // Get the camrea object
  856. const domInstance_light* lightInstanceRef = instanceLights.get(i);
  857. xsAnyURI lightURI = lightInstanceRef->getUrl();
  858. domLight* lightRef = daeSafeCast<domLight>(lightURI.getElement());
  859. if (lightRef)
  860. {
  861. Light* light = loadLight(lightRef);
  862. if (light)
  863. {
  864. node->setLight(light);
  865. }
  866. }
  867. else
  868. {
  869. // warning
  870. }
  871. }
  872. }
  873. void DAESceneEncoder::loadGeometryInstance(const domNode* n, Node* node)
  874. {
  875. // Does this node have any geometry instances?
  876. const domInstance_geometry_Array& instanceGeometries = n->getInstance_geometry_array();
  877. size_t instanceGeometryCount = instanceGeometries.getCount();
  878. for (size_t i = 0; i < instanceGeometryCount; ++i)
  879. {
  880. // Get the geometry object
  881. const domInstance_geometryRef geometryInstanceRef = instanceGeometries.get(i);
  882. xsAnyURI geometryURI = geometryInstanceRef->getUrl();
  883. domGeometry* geometry = daeSafeCast<domGeometry>(geometryURI.getElement());
  884. // Load the model from this geometry
  885. if (geometry)
  886. {
  887. Model* model = loadGeometry(geometry, geometryInstanceRef->getBind_material());
  888. if (model)
  889. {
  890. node->setModel(model);
  891. }
  892. }
  893. else
  894. {
  895. warning(std::string("Failed to resolve geometry url: ") + geometryURI.getURI());
  896. }
  897. }
  898. }
  899. void DAESceneEncoder::loadControllerInstance(const domNode* n, Node* node)
  900. {
  901. // Does this node have any controller instances?
  902. const domInstance_controller_Array& instanceControllers = n->getInstance_controller_array();
  903. size_t instanceControllerCount = instanceControllers.getCount();
  904. for (size_t i = 0; i < instanceControllerCount; ++i)
  905. {
  906. const domInstance_controllerRef instanceControllerRef = instanceControllers.get(i);
  907. xsAnyURI controllerURI = instanceControllerRef->getUrl();
  908. domController* controllerRef = daeSafeCast<domController>(controllerURI.getElement());
  909. if (controllerRef)
  910. {
  911. const domSkin* skinElement = controllerRef->getSkin();
  912. if (skinElement)
  913. {
  914. Model* model = loadSkin(skinElement);
  915. if (model)
  916. {
  917. domInstance_controller::domSkeleton_Array& skeletons = instanceControllerRef->getSkeleton_array();
  918. if (skeletons.getCount() == 0)
  919. {
  920. domNode* rootJoint = getRootJointNode(skinElement);
  921. if (rootJoint)
  922. {
  923. loadSkeleton(rootJoint, model->getSkin());
  924. node->setModel(model);
  925. }
  926. }
  927. else
  928. {
  929. // Load the skeleton for this skin
  930. domInstance_controller::domSkeletonRef skeleton = getSkeleton(instanceControllerRef);
  931. assert(skeleton);
  932. loadSkeleton(skeleton, model->getSkin());
  933. node->setModel(model);
  934. }
  935. }
  936. }
  937. }
  938. else
  939. {
  940. // warning
  941. }
  942. _jointLookupTable.clear();
  943. _jointInverseBindPoseMatrices.clear();
  944. }
  945. }
  946. Camera* DAESceneEncoder::loadCamera(const domCamera* cameraRef)
  947. {
  948. Camera* camera = new Camera();
  949. camera->setId(cameraRef->getId());
  950. // Optics
  951. const domCamera::domOpticsRef opticsRef = cameraRef->getOptics();
  952. if (opticsRef.cast())
  953. {
  954. const domCamera::domOptics::domTechnique_commonRef techRef = opticsRef->getTechnique_common();
  955. // Orthographics
  956. const domCamera::domOptics::domTechnique_common::domOrthographicRef orthographicRef = techRef->getOrthographic();
  957. if (orthographicRef.cast())
  958. {
  959. camera->setOrthographic();
  960. camera->setAspectRatio((float)orthographicRef->getAspect_ratio()->getValue());
  961. camera->setNearPlane((float)orthographicRef->getZnear()->getValue());
  962. camera->setFarPlane((float)orthographicRef->getZfar()->getValue());
  963. const domTargetableFloatRef xmag = orthographicRef->getXmag();
  964. const domTargetableFloatRef ymag = orthographicRef->getYmag();
  965. // Viewport width
  966. if (xmag.cast())
  967. {
  968. camera->setViewportWidth((float)xmag->getValue());
  969. }
  970. // Viewport height
  971. if (ymag.cast())
  972. {
  973. camera->setViewportHeight((float)ymag->getValue());
  974. }
  975. // TODO: Viewport x and y?
  976. }
  977. // Perspective
  978. const domCamera::domOptics::domTechnique_common::domPerspectiveRef perspectiveRef = techRef->getPerspective();
  979. if (perspectiveRef.cast())
  980. {
  981. camera->setPerspective();
  982. camera->setNearPlane((float)perspectiveRef->getZnear()->getValue());
  983. camera->setFarPlane((float)perspectiveRef->getZfar()->getValue());
  984. float aspectRatio = -1.0f;
  985. if (perspectiveRef->getAspect_ratio().cast())
  986. {
  987. aspectRatio = (float)perspectiveRef->getAspect_ratio()->getValue();
  988. camera->setAspectRatio(aspectRatio);
  989. }
  990. if (perspectiveRef->getYfov().cast())
  991. {
  992. camera->setFieldOfView((float)perspectiveRef->getYfov()->getValue());
  993. }
  994. else if (perspectiveRef->getXfov().cast() && aspectRatio > 0.0f)
  995. {
  996. // The gameplaybinary stores the yfov but collada might have specified
  997. // an xfov and an aspect ratio. So use those to calculate the yfov.
  998. float xfov = (float)perspectiveRef->getXfov()->getValue();
  999. float yfov = xfov / aspectRatio;
  1000. camera->setFieldOfView(yfov);
  1001. }
  1002. }
  1003. }
  1004. _gamePlayFile.addCamera(camera);
  1005. return camera;
  1006. }
  1007. Light* DAESceneEncoder::loadLight(const domLight* lightRef)
  1008. {
  1009. Light* light = new Light();
  1010. light->setId(lightRef->getId());
  1011. const domLight::domTechnique_commonRef techRef = lightRef->getTechnique_common();
  1012. // Ambient light
  1013. {
  1014. const domLight::domTechnique_common::domAmbientRef ambientRef = techRef->getAmbient();
  1015. if (ambientRef.cast())
  1016. {
  1017. light->setAmbientLight();
  1018. // color
  1019. const domTargetableFloat3Ref float3Ref = ambientRef->getColor();
  1020. const domFloat3& color3 = float3Ref->getValue();
  1021. light->setColor((float)color3.get(0), (float)color3.get(1), (float)color3.get(2));
  1022. }
  1023. }
  1024. // Directional light
  1025. {
  1026. const domLight::domTechnique_common::domDirectionalRef direcitonalRef = techRef->getDirectional();
  1027. if (direcitonalRef.cast())
  1028. {
  1029. light->setDirectionalLight();
  1030. // color
  1031. const domTargetableFloat3Ref float3Ref = direcitonalRef->getColor();
  1032. const domFloat3& color3 = float3Ref->getValue();
  1033. light->setColor((float)color3.get(0), (float)color3.get(1), (float)color3.get(2));
  1034. }
  1035. }
  1036. // Spot light
  1037. {
  1038. const domLight::domTechnique_common::domSpotRef spotRef = techRef->getSpot();
  1039. if (spotRef.cast())
  1040. {
  1041. light->setSpotLight();
  1042. // color
  1043. const domTargetableFloat3Ref float3Ref = spotRef->getColor();
  1044. const domFloat3& color3 = float3Ref->getValue();
  1045. light->setColor((float)color3.get(0), (float)color3.get(1), (float)color3.get(2));
  1046. const domTargetableFloatRef& constAtt = spotRef->getConstant_attenuation();
  1047. if (constAtt.cast())
  1048. {
  1049. light->setConstantAttenuation((float)constAtt->getValue());
  1050. }
  1051. const domTargetableFloatRef& linearAtt = spotRef->getLinear_attenuation();
  1052. if (linearAtt.cast())
  1053. {
  1054. light->setLinearAttenuation((float)linearAtt->getValue());
  1055. }
  1056. const domTargetableFloatRef& quadAtt = spotRef->getQuadratic_attenuation();
  1057. if (quadAtt.cast())
  1058. {
  1059. light->setQuadraticAttenuation((float)quadAtt->getValue());
  1060. }
  1061. const domTargetableFloatRef& falloffAngle = spotRef->getFalloff_angle();
  1062. if (falloffAngle.cast())
  1063. {
  1064. light->setFalloffAngle((float)falloffAngle->getValue());
  1065. }
  1066. const domTargetableFloatRef& falloffExp = spotRef->getFalloff_exponent();
  1067. if (falloffExp.cast())
  1068. {
  1069. light->setFalloffExponent((float)falloffExp->getValue());
  1070. }
  1071. }
  1072. }
  1073. // Point light
  1074. {
  1075. const domLight::domTechnique_common::domPointRef pointRef = techRef->getPoint();
  1076. if (pointRef.cast())
  1077. {
  1078. light->setPointLight();
  1079. // color
  1080. const domTargetableFloat3Ref float3Ref = pointRef->getColor();
  1081. const domFloat3& color3 = float3Ref->getValue();
  1082. light->setColor((float)color3.get(0), (float)color3.get(1), (float)color3.get(2));
  1083. const domTargetableFloatRef& constAtt = pointRef->getConstant_attenuation();
  1084. if (constAtt.cast())
  1085. {
  1086. light->setConstantAttenuation((float)constAtt->getValue());
  1087. }
  1088. const domTargetableFloatRef& linearAtt = pointRef->getLinear_attenuation();
  1089. if (linearAtt.cast())
  1090. {
  1091. light->setLinearAttenuation((float)linearAtt->getValue());
  1092. }
  1093. const domTargetableFloatRef& quadAtt = pointRef->getQuadratic_attenuation();
  1094. if (quadAtt.cast())
  1095. {
  1096. light->setQuadraticAttenuation((float)quadAtt->getValue());
  1097. }
  1098. // When Maya exports DAE_FBX, the ambient lights are converted into point lights but with not attenuation elements.
  1099. // If this point light has no attenuation then assume it is ambient.
  1100. if (!(constAtt.cast() && linearAtt.cast() && quadAtt.cast()))
  1101. {
  1102. light->setAmbientLight();
  1103. }
  1104. }
  1105. }
  1106. _gamePlayFile.addLight(light);
  1107. return light;
  1108. }
  1109. void DAESceneEncoder::loadSkeleton(domInstance_controller::domSkeleton* skeletonElement, MeshSkin* skin)
  1110. {
  1111. xsAnyURI skeletonUri = skeletonElement->getValue();
  1112. daeString skeletonId = skeletonUri.getID();
  1113. daeSIDResolver resolver(skeletonUri.getElement(), skeletonId);
  1114. domNode* rootNode = daeSafeCast<domNode>(resolver.getElement());
  1115. loadSkeleton(rootNode, skin);
  1116. }
  1117. void DAESceneEncoder::loadSkeleton(domNode* rootNode, MeshSkin* skin)
  1118. {
  1119. // Get the lookup scene id (sid) and joint index.
  1120. std::string id = std::string(rootNode->getId());
  1121. // Has the skeleton (root joint) been loaded yet?
  1122. Node* skeleton = (Node*)_gamePlayFile.getFromRefTable(id);
  1123. // The skeleton node is not loaded yet, so let's load it now
  1124. if (skeleton == NULL)
  1125. {
  1126. // Find the top most parent of rootNode that has not yet been loaded
  1127. domNode* topLevelParent = rootNode;
  1128. while (
  1129. topLevelParent->getParent() &&
  1130. topLevelParent->getParent()->getElementType() == COLLADA_TYPE::NODE &&
  1131. _gamePlayFile.getFromRefTable(topLevelParent->getParent()->getID()) == NULL)
  1132. {
  1133. topLevelParent = (domNode*)topLevelParent->getParent();
  1134. }
  1135. // Is the parent of this node loaded yet?
  1136. Node* parentNode = NULL;
  1137. if (topLevelParent->getParent() &&
  1138. topLevelParent->getParent()->getElementType() == COLLADA_TYPE::NODE &&
  1139. _gamePlayFile.getFromRefTable(topLevelParent->getParent()->getID()) != NULL)
  1140. {
  1141. parentNode = (Node*)_gamePlayFile.getFromRefTable(topLevelParent->getParent()->getID());
  1142. }
  1143. // Finally, load the node hierarchy that includes the skeleton
  1144. skeleton = loadNode(topLevelParent, parentNode);
  1145. }
  1146. if (skeleton == NULL)
  1147. {
  1148. // This shouldn't really happen..
  1149. skeleton = new Node();
  1150. skeleton->setId(id);
  1151. _gamePlayFile.addNode(skeleton);
  1152. }
  1153. // Resolve and set joints array for skin
  1154. std::vector<Node*> _joints;
  1155. const std::vector<std::string>& jointNames = skin->getJointNames();
  1156. for (std::vector<std::string>::const_iterator i = jointNames.begin(); i != jointNames.end(); i++)
  1157. {
  1158. Object* obj = _gamePlayFile.getFromRefTable(*i);
  1159. if (obj && obj->getTypeId() == Object::NODE_ID)
  1160. {
  1161. Node* node = static_cast<Node*>(obj);
  1162. _joints.push_back(node);
  1163. }
  1164. }
  1165. skin->setJoints(_joints);
  1166. }
  1167. Model* DAESceneEncoder::loadSkin(const domSkin* skinElement)
  1168. {
  1169. ///////////////////////////// SKIN
  1170. Model* model = new Model();
  1171. MeshSkin* skin = new MeshSkin();
  1172. // Bind Shape Matrix
  1173. const domSkin::domBind_shape_matrix* bindShapeMatrix = skinElement->getBind_shape_matrix();
  1174. if (bindShapeMatrix)
  1175. {
  1176. const domFloat4x4& m = bindShapeMatrix->getValue();
  1177. float transform[] = {(float)m.get(0), (float)m.get(4), (float)m.get(8), (float)m.get(12),
  1178. (float)m.get(1), (float)m.get(5), (float)m.get(9), (float)m.get(13),
  1179. (float)m.get(2), (float)m.get(6), (float)m.get(10), (float)m.get(14),
  1180. (float)m.get(3), (float)m.get(7), (float)m.get(11), (float)m.get(15)};
  1181. skin->setBindShape(transform);
  1182. }
  1183. // Read and set our joints
  1184. domSkin::domJointsRef _joints = skinElement->getJoints();
  1185. domInputLocal_Array& jointInputs = _joints->getInput_array();
  1186. // Process "JOINT" input semantic first (we need to do this to set the joint count)
  1187. unsigned int jointCount = 0;
  1188. for (unsigned int i = 0; i < jointInputs.getCount(); ++i)
  1189. {
  1190. domInputLocalRef input = jointInputs.get(i);
  1191. std::string inputSemantic = std::string(input->getSemantic());
  1192. domURIFragmentType* sourceURI = &input->getSource();
  1193. sourceURI->resolveElement();
  1194. const domSourceRef source = (domSource*)(daeElement*)sourceURI->getElement();
  1195. if (equals(inputSemantic, "JOINT"))
  1196. {
  1197. // Get the joint Ids's
  1198. std::vector<std::string> list;
  1199. getJointNames(source, list);
  1200. // Go through the joint list and conver them from sid to id because the sid information is
  1201. // lost when converting to the gameplay binary format.
  1202. for (std::vector<std::string>::iterator i = list.begin(); i != list.end(); i++)
  1203. {
  1204. daeSIDResolver resolver(source->getDocument()->getDomRoot(), i->c_str());
  1205. daeElement* element = resolver.getElement();
  1206. if (element && element->getElementType() == COLLADA_TYPE::NODE)
  1207. {
  1208. domNodeRef node = daeSafeCast<domNode>(element);
  1209. const char* nodeId = node->getId();
  1210. if (nodeId && !equals(*i, nodeId))
  1211. {
  1212. *i = nodeId;
  1213. }
  1214. }
  1215. }
  1216. // Get the joint count and set the capacities for both the
  1217. jointCount = list.size();
  1218. _jointInverseBindPoseMatrices.reserve(jointCount);
  1219. unsigned int j = 0;
  1220. for (std::vector<std::string>::const_iterator i = list.begin(); i != list.end(); i++)
  1221. {
  1222. _jointLookupTable[*i] = j++;
  1223. }
  1224. skin->setJointNames(list);
  1225. }
  1226. }
  1227. // Make sure we have some joints
  1228. if (jointCount == 0)
  1229. {
  1230. warning("No joints found for skin: ");
  1231. return NULL;
  1232. }
  1233. // Process "INV_BIND_MATRIX" next
  1234. for (unsigned int i = 0; i < jointInputs.getCount(); ++i)
  1235. {
  1236. domInputLocalRef input = jointInputs.get(i);
  1237. std::string inputSemantic = std::string(input->getSemantic());
  1238. domURIFragmentType* sourceURI = &input->getSource();
  1239. sourceURI->resolveElement();
  1240. domSource* source = (domSource*)(daeElement*)sourceURI->getElement();
  1241. if (equals(inputSemantic, "INV_BIND_MATRIX"))
  1242. {
  1243. domListOfFloats& matrixFloats = source->getFloat_array()->getValue();
  1244. //unsigned int matrixFloatsCount = (unsigned int)source->getFloat_array()->getCount();
  1245. unsigned int jointIndex = 0;
  1246. for (unsigned int j = 0; j < jointCount; ++j)
  1247. {
  1248. Matrix matrix((float)matrixFloats.get(jointIndex + 0), (float)matrixFloats.get(jointIndex + 4), (float)matrixFloats.get(jointIndex + 8), (float)matrixFloats.get(jointIndex + 12),
  1249. (float)matrixFloats.get(jointIndex + 1), (float)matrixFloats.get(jointIndex + 5), (float)matrixFloats.get(jointIndex + 9), (float)matrixFloats.get(jointIndex + 13),
  1250. (float)matrixFloats.get(jointIndex + 2), (float)matrixFloats.get(jointIndex + 6), (float)matrixFloats.get(jointIndex + 10), (float)matrixFloats.get(jointIndex + 14),
  1251. (float)matrixFloats.get(jointIndex + 3), (float)matrixFloats.get(jointIndex + 7), (float)matrixFloats.get(jointIndex + 11), (float)matrixFloats.get(jointIndex + 15));
  1252. _jointInverseBindPoseMatrices.push_back(matrix);
  1253. jointIndex += 16;
  1254. }
  1255. }
  1256. }
  1257. skin->setBindPoses(_jointInverseBindPoseMatrices);
  1258. // Get the vertex weights inputs
  1259. domSkin::domVertex_weights* vertexWeights = skinElement->getVertex_weights();
  1260. domInputLocalOffset_Array& vertexWeightsInputs = vertexWeights->getInput_array();
  1261. unsigned int vertexWeightsCount = (unsigned int)vertexWeights->getCount();
  1262. domListOfFloats jointWeights;
  1263. for (unsigned int i = 0; i < jointInputs.getCount(); ++i)
  1264. {
  1265. domInputLocalOffsetRef input = vertexWeightsInputs.get(i);
  1266. std::string inputSemantic = std::string(input->getSemantic());
  1267. domURIFragmentType* sourceURI = &input->getSource();
  1268. sourceURI->resolveElement();
  1269. domSource* source = (domSource*)(daeElement*)sourceURI->getElement();
  1270. if (equals(inputSemantic, "WEIGHT"))
  1271. {
  1272. domFloat_array* weights = source->getFloat_array();
  1273. if (weights)
  1274. {
  1275. jointWeights = weights->getValue();
  1276. }
  1277. }
  1278. }
  1279. // Get the number of joint influences per vertex
  1280. domSkin::domVertex_weights::domVcount* vCountElement = vertexWeights->getVcount();
  1281. domListOfUInts skinVertexInfluenceCounts = vCountElement->getValue();
  1282. // Get the joint/weight pair data.
  1283. domSkin::domVertex_weights::domV* vElement = vertexWeights->getV();
  1284. domListOfInts skinVertexJointWeightPairIndices = vElement->getValue();
  1285. // Get the vertex influence count for any given vertex (up to max of 4)
  1286. unsigned int maxVertexInfluencesCount = SCENE_SKIN_VERTEXINFLUENCES_MAX;
  1287. skin->setVertexInfluenceCount(maxVertexInfluencesCount);
  1288. // Get the vertex blend weights and joint indices and
  1289. // allocate our vertex blend weights and blend indices arrays.
  1290. // These will be used and cleaned up later in LoadMesh
  1291. int skinVertexInfluenceCountTotal = skinVertexInfluenceCounts.getCount();
  1292. int totalVertexInfluencesCount = vertexWeightsCount * maxVertexInfluencesCount;
  1293. _vertexBlendWeights = new float[totalVertexInfluencesCount];
  1294. _vertexBlendIndices = new unsigned int[totalVertexInfluencesCount];
  1295. // Preset the default blend weights to 0.0f (no effect) and blend indices to 0 (uses the first which when multiplied
  1296. // will have no effect anyhow.
  1297. memset(_vertexBlendWeights, 0, totalVertexInfluencesCount * sizeof(float));
  1298. memset(_vertexBlendIndices , 0, totalVertexInfluencesCount * sizeof(unsigned int));
  1299. int vOffset = 0;
  1300. int weightOffset = 0;
  1301. // Go through all the skin vertex influence weights from the indexed data.
  1302. for (int i = 0; i < skinVertexInfluenceCountTotal; ++i)
  1303. {
  1304. // Get the influence count and directly get the vertext blend weights and indices.
  1305. unsigned int vertexInfluenceCount = (unsigned int)skinVertexInfluenceCounts.get(i);
  1306. float vertexInfluencesTotalWeights = 0.0f;
  1307. std::vector<SkinnedVertexWeightPair> vertexInfluences;
  1308. //vertexInfluences.SetCapacity(vertexInfluenceCount);
  1309. // Get the index/weight pairs and some the weight totals while at it.
  1310. for (unsigned int j = 0; j < vertexInfluenceCount; ++j)
  1311. {
  1312. float weight = (float)jointWeights.get((unsigned int)skinVertexJointWeightPairIndices[vOffset + 1]);
  1313. int index = (int)skinVertexJointWeightPairIndices[vOffset];
  1314. // Set invalid index corresponding weights to zero
  1315. if (index < 0 || index > (int)vertexWeightsCount)
  1316. {
  1317. weight = 0.0f;
  1318. index = 0;
  1319. }
  1320. SkinnedVertexWeightPair pair(weight, index);
  1321. vertexInfluences.push_back(pair);
  1322. vertexInfluencesTotalWeights += weight;
  1323. vOffset+=2;
  1324. }
  1325. // Get up the the maximum vertex weight influence count.
  1326. for (unsigned int j = 0; j < maxVertexInfluencesCount; ++j)
  1327. {
  1328. if (j < vertexInfluenceCount)
  1329. {
  1330. SkinnedVertexWeightPair pair = vertexInfluences[j];
  1331. _vertexBlendIndices[weightOffset] = pair.BlendIndex;
  1332. if (vertexInfluencesTotalWeights > 0.0f)
  1333. {
  1334. _vertexBlendWeights[weightOffset] = pair.BlendWeight;
  1335. }
  1336. else
  1337. {
  1338. if (j == 0)
  1339. {
  1340. _vertexBlendWeights[weightOffset] = 1.0f;
  1341. }
  1342. else
  1343. {
  1344. _vertexBlendWeights[weightOffset] = 0.0f;
  1345. }
  1346. }
  1347. }
  1348. weightOffset++;
  1349. }
  1350. }
  1351. model->setSkin(skin);
  1352. ///////////////////////////////////////////////////////////
  1353. // get geometry
  1354. xsAnyURI geometryURI = skinElement->getSource();
  1355. domGeometry* geometry = daeSafeCast<domGeometry>(geometryURI.getElement());
  1356. if (geometry)
  1357. {
  1358. const domMesh* meshElement = geometry->getMesh();
  1359. if (meshElement)
  1360. {
  1361. Mesh* mesh = loadMesh(meshElement, geometry->getId());
  1362. if (mesh)
  1363. {
  1364. model->setMesh(mesh);
  1365. }
  1366. }
  1367. }
  1368. ///////////////////////////////////////////////////////////
  1369. return model;
  1370. }
  1371. Model* DAESceneEncoder::loadGeometry(const domGeometry* geometry, const domBind_materialRef bindMaterial)
  1372. {
  1373. // Does this geometry have a valid mesh?
  1374. // Get the mesh for the geometry (if it has one)
  1375. const domMesh* meshElement = geometry->getMesh();
  1376. if (meshElement == NULL)
  1377. {
  1378. warning(std::string("No mesh found for geometry: ") + geometry->getId());
  1379. return NULL;
  1380. }
  1381. ///////////////////////////// GEOMETRY
  1382. // Load the mesh for this model
  1383. Mesh* mesh = loadMesh(meshElement, geometry->getId());
  1384. if (mesh == NULL)
  1385. {
  1386. return NULL;
  1387. }
  1388. // Mesh instance
  1389. Model* model = new Model();
  1390. model->setMesh(mesh);
  1391. return model;
  1392. }
  1393. Mesh* DAESceneEncoder::loadMesh(const domMesh* meshElement, const std::string& geometryId)
  1394. {
  1395. const domTriangles_Array& trianglesArray = meshElement->getTriangles_array();
  1396. unsigned int trianglesArrayCount = (unsigned int)trianglesArray.getCount();
  1397. // Ensure the data is exported as triangles.
  1398. if (trianglesArrayCount == 0)
  1399. {
  1400. warning(std::string("Geometry mesh has no triangles: ") + geometryId);
  1401. return NULL;
  1402. }
  1403. // Check if this mesh already exists
  1404. Mesh* mesh = _gamePlayFile.getMesh(geometryId.c_str());
  1405. if (mesh)
  1406. {
  1407. return mesh;
  1408. }
  1409. mesh = new Mesh();
  1410. mesh->setId(geometryId.c_str());
  1411. std::vector<DAEPolygonInput*> polygonInputs;
  1412. // Quickly just go through each triangles array and make sure they have the same number of inputs
  1413. // with the same layout.
  1414. // const domSource_Array& sourceArray = meshElement->getSource_array();
  1415. const domInputLocal_Array& vertexArray = meshElement->getVertices()->getInput_array();
  1416. unsigned int inputCount = (unsigned int)-1;
  1417. // Loop through our set of triangle lists (each list of triangles corresponds to a single MeshPart)
  1418. for (unsigned int i = 0; i < trianglesArrayCount; ++i)
  1419. {
  1420. const domTrianglesRef& triangles = trianglesArray.get(i);
  1421. const domInputLocalOffset_Array& inputArray = triangles->getInput_array();
  1422. // If not set then determine the number of input for all the triangles.
  1423. if (inputCount == -1)
  1424. {
  1425. inputCount = (unsigned int)inputArray.getCount();
  1426. int texCoordCount = 0;
  1427. for (unsigned int j = 0; j < inputCount; ++j)
  1428. {
  1429. const domInputLocalOffsetRef& input = inputArray.get(j);
  1430. std::string inputSemantic = input->getSemantic();
  1431. // If its a vertex first do an extra lookup for the inclusive inputs
  1432. if (equals(inputSemantic, "VERTEX"))
  1433. {
  1434. unsigned int vertexArrayCount = (unsigned int)vertexArray.getCount();
  1435. for (unsigned int k = 0; k < vertexArrayCount; ++k)
  1436. {
  1437. const domInputLocalRef& vertexInput = vertexArray.get(k);
  1438. std::string semantic = std::string(vertexInput->getSemantic());
  1439. int type = getVertexUsageType(semantic);
  1440. if (type == -1)
  1441. {
  1442. warning(std::string("Vertex semantic (") + semantic + ") is invalid/unsupported for geometry mesh: " + geometryId);
  1443. }
  1444. DAEPolygonInput* polygonInput = new DAEPolygonInput();
  1445. domURIFragmentType& sourceURI = vertexInput->getSource();
  1446. sourceURI.resolveElement();
  1447. domSource* source = (domSource*)(daeElement*)sourceURI.getElement();
  1448. polygonInput->offset = 0;
  1449. polygonInput->sourceValues = source->getFloat_array()->getValue();
  1450. polygonInput->type = type;
  1451. polygonInputs.push_back(polygonInput);
  1452. }
  1453. }
  1454. else
  1455. {
  1456. std::string semantic = input->getSemantic();
  1457. int type = getVertexUsageType(semantic);
  1458. if (type == -1)
  1459. {
  1460. warning(std::string("Semantic (") + semantic + ") is invalid/unsupported for geometry mesh: " + geometryId);
  1461. break;
  1462. }
  1463. if (type == TEXCOORD0)
  1464. {
  1465. // Some meshes have multiple texture coordinates
  1466. assert(texCoordCount <= 7);
  1467. type += texCoordCount;
  1468. ++texCoordCount;
  1469. }
  1470. DAEPolygonInput* polygonInput = new DAEPolygonInput();
  1471. domURIFragmentType& sourceURI = input->getSource();
  1472. sourceURI.resolveElement();
  1473. domSource* source = (domSource*)(daeElement*)sourceURI.getElement();
  1474. polygonInput->offset = (unsigned int)input->getOffset();
  1475. polygonInput->sourceValues = source->getFloat_array()->getValue();
  1476. polygonInput->type = type;
  1477. // Get the accessor info
  1478. const domSource::domTechnique_commonRef& technique = source->getTechnique_common();
  1479. if (technique.cast())
  1480. {
  1481. const domAccessorRef& accessor = technique->getAccessor();
  1482. polygonInput->accessor = accessor;
  1483. }
  1484. polygonInputs.push_back(polygonInput);
  1485. }
  1486. }
  1487. }
  1488. else
  1489. {
  1490. // If there is a triangle array with a different number of inputs, this is not supported.
  1491. if (inputCount != (unsigned int)inputArray.getCount())
  1492. {
  1493. for (size_t j = 0; j < polygonInputs.size(); ++j)
  1494. {
  1495. delete polygonInputs[j];
  1496. }
  1497. warning(std::string("Triangles do not all have the same number of input sources for geometry mesh: ") + geometryId);
  1498. return NULL;
  1499. }
  1500. else
  1501. {
  1502. // TODO: Check if they are in the same order...
  1503. }
  1504. }
  1505. }
  1506. // Now we have validated that all input in all triangles are the same and in the same input layout.
  1507. // Lets start to read them and build our subsets.
  1508. for (unsigned int i = 0; i < trianglesArrayCount; ++i)
  1509. {
  1510. // Subset to be built.
  1511. MeshPart* subset = new MeshPart();
  1512. // All of the information about the triangles and the sources to access the data from.
  1513. domTriangles* triangles = daeSafeCast<domTriangles>(trianglesArray.get(i));
  1514. // Parse the material for this subset
  1515. //string materialName = triangles->getMaterial() == NULL ? _T("") : triangles->getMaterial();
  1516. //if (materialName.size() > 0)
  1517. /// subset->material = ParseMaterial(bindMaterial, materialName);
  1518. //const domInputLocalOffset_Array& inputArray = triangles->getInput_array();
  1519. const domListOfUInts& polyInts = triangles->getP()->getValue();
  1520. unsigned int polyIntsCount = (unsigned int)polyInts.getCount();
  1521. unsigned int poly = 0;
  1522. unsigned int inputSourceCount = (unsigned int)polygonInputs.size();
  1523. unsigned int maxOffset = 0;
  1524. // Go through the polygon indices for each input source retrieve the values
  1525. // and iterate by its offset.
  1526. Vertex vertex;
  1527. for (unsigned int k = 0; k < inputSourceCount && poly < polyIntsCount;)
  1528. {
  1529. const domListOfFloats& source = polygonInputs[k]->sourceValues;
  1530. unsigned int offset = polygonInputs[k]->offset;
  1531. if (offset > maxOffset)
  1532. {
  1533. maxOffset = offset;
  1534. }
  1535. int polyIndexInt = (int) polyInts.get(poly + offset);
  1536. unsigned int polyIndex = (unsigned int) polyInts.get(poly + offset);
  1537. switch (polygonInputs[k]->type)
  1538. {
  1539. case POSITION:
  1540. vertex = Vertex(); // TODO
  1541. if (_vertexBlendWeights && _vertexBlendIndices)
  1542. {
  1543. vertex.hasWeights = true;
  1544. vertex.blendWeights.x = _vertexBlendWeights[polyIndex * 4];
  1545. vertex.blendWeights.y = _vertexBlendWeights[polyIndex * 4 + 1];
  1546. vertex.blendWeights.z = _vertexBlendWeights[polyIndex * 4 + 2];
  1547. vertex.blendWeights.w = _vertexBlendWeights[polyIndex * 4 + 3];
  1548. vertex.blendIndices.x = (float)_vertexBlendIndices[polyIndex * 4];
  1549. vertex.blendIndices.y = (float)_vertexBlendIndices[polyIndex * 4 + 1];
  1550. vertex.blendIndices.z = (float)_vertexBlendIndices[polyIndex * 4 + 2];
  1551. vertex.blendIndices.w = (float)_vertexBlendIndices[polyIndex * 4 + 3];
  1552. }
  1553. vertex.position.x = (float)source.get(polyIndex * 3);
  1554. vertex.position.y = (float)source.get(polyIndex * 3 + 1);
  1555. vertex.position.z = (float)source.get(polyIndex * 3 + 2);
  1556. break;
  1557. case NORMAL:
  1558. vertex.hasNormal = true;
  1559. vertex.normal.x = (float)source.get(polyIndex * 3);
  1560. vertex.normal.y = (float)source.get(polyIndex * 3 + 1);
  1561. vertex.normal.z = (float)source.get(polyIndex * 3 + 2);
  1562. break;
  1563. // TODO: We must examine the Collada input accessor and read the stride/count to verify this - not ONLY for Color, but we should be doing this for ALL components (i.e. Position, Normal, etc).
  1564. case COLOR:
  1565. {
  1566. domAccessor* accessor = polygonInputs[k]->accessor;
  1567. if (accessor)
  1568. {
  1569. vertex.hasDiffuse = true;
  1570. vertex.diffuse.w = 1.0f;
  1571. unsigned int stride = (unsigned int)polygonInputs[k]->accessor->getStride();
  1572. unsigned int index = polyIndex * stride;
  1573. const domParam_Array& paramArray = accessor->getParam_array();
  1574. const size_t paramArrayCount = paramArray.getCount();
  1575. for (size_t i = 0; i < paramArrayCount; ++i)
  1576. {
  1577. const domParamRef& param = paramArray.get(i);
  1578. const char* name = param->getName();
  1579. if (name)
  1580. {
  1581. switch (name[0])
  1582. {
  1583. case 'r':
  1584. case 'R':
  1585. vertex.diffuse.x = (float)source.get(index + i); // red
  1586. break;
  1587. case 'g':
  1588. case 'G':
  1589. vertex.diffuse.y = (float)source.get(index + i); // green
  1590. break;
  1591. case 'b':
  1592. case 'B':
  1593. vertex.diffuse.z = (float)source.get(index + i); // blue
  1594. break;
  1595. case 'a':
  1596. case 'A':
  1597. vertex.diffuse.w = (float)source.get(index + i); // alpha
  1598. break;
  1599. default:
  1600. break;
  1601. }
  1602. }
  1603. }
  1604. }
  1605. break;
  1606. }
  1607. case TANGENT:
  1608. vertex.hasTangent = true;
  1609. vertex.tangent.x = (float)source.get(polyIndex * 3);
  1610. vertex.tangent.y = (float)source.get(polyIndex * 3 + 1);
  1611. vertex.tangent.z = (float)source.get(polyIndex * 3 + 2);
  1612. break;
  1613. case BINORMAL:
  1614. vertex.hasBinormal = true;
  1615. vertex.binormal.x = (float)source.get(polyIndex * 3);
  1616. vertex.binormal.y = (float)source.get(polyIndex * 3 + 1);
  1617. vertex.binormal.z = (float)source.get(polyIndex * 3 + 2);
  1618. break;
  1619. case TEXCOORD0:
  1620. vertex.hasTexCoord = true;
  1621. if (polygonInputs[k]->accessor)
  1622. {
  1623. // TODO: This assumes (s, t) are first
  1624. unsigned int stride = (unsigned int)polygonInputs[k]->accessor->getStride();
  1625. if (polyIndexInt < 0)
  1626. {
  1627. unsigned int i = (unsigned int)((int)polygonInputs[k]->accessor->getCount()) + polyIndexInt;
  1628. vertex.texCoord.x = (float)source.get(i * stride);
  1629. vertex.texCoord.y = (float)source.get(i * stride + 1);
  1630. }
  1631. else
  1632. {
  1633. vertex.texCoord.x = (float)source.get(polyIndex * stride);
  1634. vertex.texCoord.y = (float)source.get(polyIndex * stride + 1);
  1635. }
  1636. }
  1637. else
  1638. {
  1639. vertex.texCoord.x = (float)source.get(polyIndex * 2);
  1640. vertex.texCoord.y = (float)source.get(polyIndex * 2 + 1);
  1641. }
  1642. break;
  1643. case TEXCOORD1:
  1644. // TODO
  1645. break;
  1646. default:
  1647. break;
  1648. }
  1649. // On the last input source attempt to add the vertex or index an existing one.
  1650. if (k == (inputSourceCount - 1))
  1651. {
  1652. // Only add unique vertices, use a hashtable and compare the hash functions of the
  1653. // vertices. If they exist simply lookup the index of the existing ones.
  1654. // otherwise add and new one and index it.
  1655. unsigned int index;
  1656. if (mesh->contains(vertex))
  1657. {
  1658. index = mesh->getVertexIndex(vertex);
  1659. }
  1660. else
  1661. {
  1662. index = mesh->addVertex(vertex);
  1663. }
  1664. subset->addIndex(index);
  1665. poly += (maxOffset+1);
  1666. k = 0;
  1667. }
  1668. else
  1669. {
  1670. k++;
  1671. }
  1672. }
  1673. // Add our new subset for the mesh.
  1674. mesh->addMeshPart(subset);
  1675. }
  1676. bool hasNormals = mesh->vertices[0].hasNormal;
  1677. bool hasDiffuses = mesh->vertices[0].hasDiffuse;
  1678. bool hasTangents = mesh->vertices[0].hasTangent;
  1679. bool hasBinormals = mesh->vertices[0].hasBinormal;
  1680. bool hasTexCoords = mesh->vertices[0].hasTexCoord;
  1681. bool hasWeights = mesh->vertices[0].hasWeights;
  1682. // The order that the vertex elements are add to the list matters.
  1683. // It should be the same order as how the Vertex data is written.
  1684. // Position
  1685. mesh->addVetexAttribute(POSITION, Vertex::POSITION_COUNT);
  1686. // Normals
  1687. if (hasNormals)
  1688. {
  1689. mesh->addVetexAttribute(NORMAL, Vertex::NORMAL_COUNT);
  1690. }
  1691. // Tangents
  1692. if (hasTangents)
  1693. {
  1694. mesh->addVetexAttribute(TANGENT, Vertex::TANGENT_COUNT);
  1695. }
  1696. // Binormals
  1697. if (hasBinormals)
  1698. {
  1699. mesh->addVetexAttribute(BINORMAL, Vertex::BINORMAL_COUNT);
  1700. }
  1701. // Texture Coordinates
  1702. if (hasTexCoords)
  1703. {
  1704. mesh->addVetexAttribute(TEXCOORD0, Vertex::TEXCOORD_COUNT);
  1705. }
  1706. // Diffuse Color
  1707. if (hasDiffuses)
  1708. {
  1709. mesh->addVetexAttribute(COLOR, Vertex::DIFFUSE_COUNT);
  1710. }
  1711. // Skinning BlendWeights BlendIndices
  1712. if (hasWeights)
  1713. {
  1714. mesh->addVetexAttribute(BLENDWEIGHTS, Vertex::BLEND_WEIGHTS_COUNT);
  1715. mesh->addVetexAttribute(BLENDINDICES, Vertex::BLEND_INDICES_COUNT);
  1716. }
  1717. _gamePlayFile.addMesh(mesh);
  1718. return mesh;
  1719. }
  1720. void DAESceneEncoder::warning(const std::string& message)
  1721. {
  1722. printf("Warning: %s\n", message.c_str());
  1723. }
  1724. void DAESceneEncoder::warning(const char* message)
  1725. {
  1726. printf("Warning: %s\n", message);
  1727. }
  1728. int DAESceneEncoder::getVertexUsageType(const std::string& semantic)
  1729. {
  1730. if (semantic.length() > 0)
  1731. {
  1732. switch (semantic[0])
  1733. {
  1734. case 'P':
  1735. if (equals(semantic, "POSITION"))
  1736. {
  1737. return POSITION;
  1738. }
  1739. case 'N':
  1740. if (equals(semantic, "NORMAL"))
  1741. {
  1742. return NORMAL;
  1743. }
  1744. case 'C':
  1745. if (equals(semantic, "COLOR"))
  1746. {
  1747. return COLOR;
  1748. }
  1749. case 'T':
  1750. if (equals(semantic, "TANGENT"))
  1751. {
  1752. return TANGENT;
  1753. }
  1754. else if (equals(semantic, "TEXCOORD"))
  1755. {
  1756. return TEXCOORD0;
  1757. }
  1758. else if (equals(semantic, "TEXTANGENT"))
  1759. {
  1760. // Treat TEXTANGENT as TANGENT
  1761. return TANGENT;
  1762. }
  1763. else if (equals(semantic, "TEXBINORMAL"))
  1764. {
  1765. // Treat TEXBINORMAL as BINORMAL
  1766. return BINORMAL;
  1767. }
  1768. case 'B':
  1769. if (equals(semantic, "BINORMAL"))
  1770. {
  1771. return BINORMAL;
  1772. }
  1773. default:
  1774. return -1;
  1775. }
  1776. }
  1777. return -1;
  1778. }
  1779. DAESceneEncoder::DAEPolygonInput::DAEPolygonInput(void) :
  1780. offset(0),
  1781. type(0),
  1782. accessor(NULL)
  1783. {
  1784. }
  1785. DAESceneEncoder::DAEPolygonInput::~DAEPolygonInput(void)
  1786. {
  1787. }
  1788. }