DAESceneEncoder.cpp 72 KB

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