ColladaParser.cpp 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file ColladaParser.cpp
  35. * @brief Implementation of the Collada parser helper
  36. */
  37. #ifndef ASSIMP_BUILD_NO_COLLADA_IMPORTER
  38. #include "ColladaParser.h"
  39. #include <assimp/ParsingUtils.h>
  40. #include <assimp/StringUtils.h>
  41. #include <assimp/TinyFormatter.h>
  42. #include <assimp/ZipArchiveIOSystem.h>
  43. #include <assimp/commonMetaData.h>
  44. #include <assimp/fast_atof.h>
  45. #include <assimp/light.h>
  46. #include <stdarg.h>
  47. #include <assimp/DefaultLogger.hpp>
  48. #include <assimp/IOSystem.hpp>
  49. #include <sstream>
  50. #include <memory>
  51. using namespace Assimp;
  52. using namespace Assimp::Collada;
  53. using namespace Assimp::Formatter;
  54. // ------------------------------------------------------------------------------------------------
  55. // Constructor to be privately used by Importer
  56. ColladaParser::ColladaParser(IOSystem *pIOHandler, const std::string &pFile) :
  57. mFileName(pFile),
  58. mXmlParser(),
  59. mDataLibrary(),
  60. mAccessorLibrary(),
  61. mMeshLibrary(),
  62. mNodeLibrary(),
  63. mImageLibrary(),
  64. mEffectLibrary(),
  65. mMaterialLibrary(),
  66. mLightLibrary(),
  67. mCameraLibrary(),
  68. mControllerLibrary(),
  69. mRootNode(nullptr),
  70. mAnims(),
  71. mUnitSize(1.0f),
  72. mUpDirection(UP_Y),
  73. mFormat(FV_1_5_n) {
  74. // validate io-handler instance
  75. if (nullptr == pIOHandler) {
  76. throw DeadlyImportError("IOSystem is nullptr.");
  77. }
  78. std::unique_ptr<IOStream> daefile;
  79. std::unique_ptr<ZipArchiveIOSystem> zip_archive;
  80. // Determine type
  81. std::string extension = BaseImporter::GetExtension(pFile);
  82. if (extension != "dae") {
  83. zip_archive.reset(new ZipArchiveIOSystem(pIOHandler, pFile));
  84. }
  85. if (zip_archive && zip_archive->isOpen()) {
  86. std::string dae_filename = ReadZaeManifest(*zip_archive);
  87. if (dae_filename.empty()) {
  88. ThrowException(std::string("Invalid ZAE"));
  89. }
  90. daefile.reset(zip_archive->Open(dae_filename.c_str()));
  91. if (daefile == nullptr) {
  92. ThrowException(std::string("Invalid ZAE manifest: '") + std::string(dae_filename) + std::string("' is missing"));
  93. }
  94. } else {
  95. // attempt to open the file directly
  96. daefile.reset(pIOHandler->Open(pFile));
  97. if (daefile.get() == nullptr) {
  98. throw DeadlyImportError("Failed to open file '" + pFile + "'.");
  99. }
  100. }
  101. pugi::xml_node *root = mXmlParser.parse(daefile.get());
  102. // generate a XML reader for it
  103. if (nullptr == root) {
  104. ThrowException("Unable to read file, malformed XML");
  105. }
  106. /*std::unique_ptr<CIrrXML_IOStreamReader> mIOWrapper(new CIrrXML_IOStreamReader(daefile.get()));
  107. mReader = irr::io::createIrrXMLReader(mIOWrapper.get());
  108. if (!mReader) {
  109. ThrowException("Unable to read file, malformed XML");
  110. }*/
  111. // start reading
  112. ReadContents(*root);
  113. // read embedded textures
  114. if (zip_archive && zip_archive->isOpen()) {
  115. ReadEmbeddedTextures(*zip_archive);
  116. }
  117. }
  118. // ------------------------------------------------------------------------------------------------
  119. // Destructor, private as well
  120. ColladaParser::~ColladaParser() {
  121. for (NodeLibrary::iterator it = mNodeLibrary.begin(); it != mNodeLibrary.end(); ++it)
  122. delete it->second;
  123. for (MeshLibrary::iterator it = mMeshLibrary.begin(); it != mMeshLibrary.end(); ++it)
  124. delete it->second;
  125. }
  126. // ------------------------------------------------------------------------------------------------
  127. // Read a ZAE manifest and return the filename to attempt to open
  128. std::string ColladaParser::ReadZaeManifest(ZipArchiveIOSystem &zip_archive) {
  129. // Open the manifest
  130. std::unique_ptr<IOStream> manifestfile(zip_archive.Open("manifest.xml"));
  131. if (manifestfile == nullptr) {
  132. // No manifest, hope there is only one .DAE inside
  133. std::vector<std::string> file_list;
  134. zip_archive.getFileListExtension(file_list, "dae");
  135. if (file_list.empty())
  136. return std::string();
  137. return file_list.front();
  138. }
  139. std::unique_ptr<CIrrXML_IOStreamReader> mIOWrapper(new CIrrXML_IOStreamReader(manifestfile.get()));
  140. std::unique_ptr<irr::io::IrrXMLReader> manifest_reader(irr::io::createIrrXMLReader(mIOWrapper.get()));
  141. while (manifest_reader->read()) {
  142. // find the manifest "dae_root" element
  143. if (manifest_reader->getNodeType() == irr::io::EXN_ELEMENT) {
  144. if (::strcmp(manifest_reader->getNodeName(), "dae_root") == 0) {
  145. if (!manifest_reader->read())
  146. return std::string();
  147. if (manifest_reader->getNodeType() != irr::io::EXN_TEXT && manifest_reader->getNodeType() != irr::io::EXN_CDATA)
  148. return std::string();
  149. const char *filepath = manifest_reader->getNodeData();
  150. if (filepath == nullptr)
  151. return std::string();
  152. aiString ai_str(filepath);
  153. UriDecodePath(ai_str);
  154. return std::string(ai_str.C_Str());
  155. }
  156. }
  157. }
  158. return std::string();
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. // Convert a path read from a collada file to the usual representation
  162. void ColladaParser::UriDecodePath(aiString &ss) {
  163. // TODO: collada spec, p 22. Handle URI correctly.
  164. // For the moment we're just stripping the file:// away to make it work.
  165. // Windows doesn't seem to be able to find stuff like
  166. // 'file://..\LWO\LWO2\MappingModes\earthSpherical.jpg'
  167. if (0 == strncmp(ss.data, "file://", 7)) {
  168. ss.length -= 7;
  169. memmove(ss.data, ss.data + 7, ss.length);
  170. ss.data[ss.length] = '\0';
  171. }
  172. // Maxon Cinema Collada Export writes "file:///C:\andsoon" with three slashes...
  173. // I need to filter it without destroying linux paths starting with "/somewhere"
  174. #if defined(_MSC_VER)
  175. if (ss.data[0] == '/' && isalpha((unsigned char)ss.data[1]) && ss.data[2] == ':') {
  176. #else
  177. if (ss.data[0] == '/' && isalpha(ss.data[1]) && ss.data[2] == ':') {
  178. #endif
  179. --ss.length;
  180. ::memmove(ss.data, ss.data + 1, ss.length);
  181. ss.data[ss.length] = 0;
  182. }
  183. // find and convert all %xy special chars
  184. char *out = ss.data;
  185. for (const char *it = ss.data; it != ss.data + ss.length; /**/) {
  186. if (*it == '%' && (it + 3) < ss.data + ss.length) {
  187. // separate the number to avoid dragging in chars from behind into the parsing
  188. char mychar[3] = { it[1], it[2], 0 };
  189. size_t nbr = strtoul16(mychar);
  190. it += 3;
  191. *out++ = (char)(nbr & 0xFF);
  192. } else {
  193. *out++ = *it++;
  194. }
  195. }
  196. // adjust length and terminator of the shortened string
  197. *out = 0;
  198. ai_assert(out > ss.data);
  199. ss.length = static_cast<ai_uint32>(out - ss.data);
  200. }
  201. // ------------------------------------------------------------------------------------------------
  202. // Read bool from text contents of current element
  203. bool ColladaParser::ReadBoolFromTextContent() {
  204. const char *cur = GetTextContent();
  205. if (nullptr == cur) {
  206. return false;
  207. }
  208. return (!ASSIMP_strincmp(cur, "true", 4) || '0' != *cur);
  209. }
  210. // ------------------------------------------------------------------------------------------------
  211. // Read float from text contents of current element
  212. ai_real ColladaParser::ReadFloatFromTextContent() {
  213. const char *cur = GetTextContent();
  214. if (nullptr == cur) {
  215. return 0.0;
  216. }
  217. return fast_atof(cur);
  218. }
  219. // ------------------------------------------------------------------------------------------------
  220. // Reads the contents of the file
  221. void ColladaParser::ReadContents(XmlNode &node) {
  222. for (pugi::xml_node &curNode : node.children()) {
  223. pugi::xml_attribute attr = curNode.attribute("version");
  224. if (attr) {
  225. const char *version = attr.as_string();
  226. aiString v;
  227. v.Set(version);
  228. mAssetMetaData.emplace(AI_METADATA_SOURCE_FORMAT_VERSION, v);
  229. if (!::strncmp(version, "1.5", 3)) {
  230. mFormat = FV_1_5_n;
  231. ASSIMP_LOG_DEBUG("Collada schema version is 1.5.n");
  232. } else if (!::strncmp(version, "1.4", 3)) {
  233. mFormat = FV_1_4_n;
  234. ASSIMP_LOG_DEBUG("Collada schema version is 1.4.n");
  235. } else if (!::strncmp(version, "1.3", 3)) {
  236. mFormat = FV_1_3_n;
  237. ASSIMP_LOG_DEBUG("Collada schema version is 1.3.n");
  238. }
  239. }
  240. ReadStructure(curNode);
  241. }
  242. /*while (mReader->read()) {
  243. // handle the root element "COLLADA"
  244. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  245. if (IsElement("COLLADA")) {
  246. // check for 'version' attribute
  247. const int attrib = TestAttribute("version");
  248. if (attrib != -1) {
  249. const char *version = mReader->getAttributeValue(attrib);
  250. // Store declared format version string
  251. aiString v;
  252. v.Set(version);
  253. mAssetMetaData.emplace(AI_METADATA_SOURCE_FORMAT_VERSION, v);
  254. if (!::strncmp(version, "1.5", 3)) {
  255. mFormat = FV_1_5_n;
  256. ASSIMP_LOG_DEBUG("Collada schema version is 1.5.n");
  257. } else if (!::strncmp(version, "1.4", 3)) {
  258. mFormat = FV_1_4_n;
  259. ASSIMP_LOG_DEBUG("Collada schema version is 1.4.n");
  260. } else if (!::strncmp(version, "1.3", 3)) {
  261. mFormat = FV_1_3_n;
  262. ASSIMP_LOG_DEBUG("Collada schema version is 1.3.n");
  263. }
  264. }
  265. ReadStructure();
  266. } else {
  267. ASSIMP_LOG_VERBOSE_DEBUG_F("Ignoring global element <", mReader->getNodeName(), ">.");
  268. SkipElement();
  269. }
  270. } else {
  271. // skip everything else silently
  272. }
  273. }*/
  274. }
  275. // ------------------------------------------------------------------------------------------------
  276. // Reads the structure of the file
  277. <<<<<<< HEAD
  278. void ColladaParser::ReadStructure(XmlNode &node) {
  279. for (pugi::xml_node curNode : node.children()) {
  280. const std::string name = std::string(curNode.name());
  281. if (name == "asset")
  282. ReadAssetInfo(curNode);
  283. else if (name == "library_animations")
  284. ReadAnimationLibrary(curNode);
  285. else if (name == "library_animation_clips")
  286. ReadAnimationClipLibrary(curNode);
  287. else if (name == "library_controllers")
  288. ReadControllerLibrary(curNode);
  289. else if (name == "library_images")
  290. ReadImageLibrary(curNode);
  291. else if (name == "library_materials")
  292. ReadMaterialLibrary(curNode);
  293. else if (name == "library_effects")
  294. ReadEffectLibrary(curNode);
  295. else if (name == "library_geometries")
  296. ReadGeometryLibrary(curNode);
  297. else if (name == "library_visual_scenes")
  298. ReadSceneLibrary(curNode);
  299. else if (name == "library_lights")
  300. ReadLightLibrary(curNode);
  301. else if (name == "library_cameras")
  302. ReadCameraLibrary(curNode);
  303. else if (name == "library_nodes")
  304. ReadSceneNode(NULL); /* some hacking to reuse this piece of code */
  305. else if (name == "scene")
  306. ReadScene(curNode);
  307. //else
  308. // SkipElement();
  309. }
  310. /* while (mReader->read()) {
  311. // beginning of elements
  312. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  313. if (IsElement("asset"))
  314. ReadAssetInfo();
  315. else if (IsElement("library_animations"))
  316. ReadAnimationLibrary();
  317. else if (IsElement("library_animation_clips"))
  318. ReadAnimationClipLibrary();
  319. else if (IsElement("library_controllers"))
  320. ReadControllerLibrary();
  321. else if (IsElement("library_images"))
  322. ReadImageLibrary();
  323. else if (IsElement("library_materials"))
  324. ReadMaterialLibrary();
  325. else if (IsElement("library_effects"))
  326. ReadEffectLibrary();
  327. else if (IsElement("library_geometries"))
  328. ReadGeometryLibrary();
  329. else if (IsElement("library_visual_scenes"))
  330. ReadSceneLibrary();
  331. else if (IsElement("library_lights"))
  332. ReadLightLibrary();
  333. else if (IsElement("library_cameras"))
  334. ReadCameraLibrary();
  335. else if (IsElement("library_nodes"))
  336. ReadSceneNode(NULL); // some hacking to reuse this piece of code
  337. else if (IsElement("scene"))
  338. ReadScene();
  339. else
  340. SkipElement();
  341. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  342. break;
  343. }*/
  344. =======
  345. void ColladaParser::ReadStructure() {
  346. while (mReader->read()) {
  347. // beginning of elements
  348. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  349. if (IsElement("asset"))
  350. ReadAssetInfo();
  351. else if (IsElement("library_animations"))
  352. ReadAnimationLibrary();
  353. else if (IsElement("library_animation_clips"))
  354. ReadAnimationClipLibrary();
  355. else if (IsElement("library_controllers"))
  356. ReadControllerLibrary();
  357. else if (IsElement("library_images"))
  358. ReadImageLibrary();
  359. else if (IsElement("library_materials"))
  360. ReadMaterialLibrary();
  361. else if (IsElement("library_effects"))
  362. ReadEffectLibrary();
  363. else if (IsElement("library_geometries"))
  364. ReadGeometryLibrary();
  365. else if (IsElement("library_visual_scenes"))
  366. ReadSceneLibrary();
  367. else if (IsElement("library_lights"))
  368. ReadLightLibrary();
  369. else if (IsElement("library_cameras"))
  370. ReadCameraLibrary();
  371. else if (IsElement("library_nodes"))
  372. ReadSceneNode(nullptr); /* some hacking to reuse this piece of code */
  373. else if (IsElement("scene"))
  374. ReadScene();
  375. else
  376. SkipElement();
  377. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  378. break;
  379. }
  380. }
  381. >>>>>>> master
  382. PostProcessRootAnimations();
  383. PostProcessControllers();
  384. }
  385. // ------------------------------------------------------------------------------------------------
  386. // Reads asset information such as coordinate system information and legal blah
  387. void ColladaParser::ReadAssetInfo(XmlNode &node) {
  388. /* if (mReader->isEmptyElement())
  389. return;*/
  390. for (pugi::xml_node &curNode : node.children()) {
  391. const std::string name = std::string(curNode.name());
  392. if (name == "unit") {
  393. pugi::xml_attribute attr = curNode.attribute("meter");
  394. mUnitSize = 1.f;
  395. if (attr) {
  396. mUnitSize = attr.as_double();
  397. }
  398. } else if (name == "up_axis") {
  399. const char *content = curNode.value();
  400. if (strncmp(content, "X_UP", 4) == 0) {
  401. mUpDirection = UP_X;
  402. } else if (strncmp(content, "Z_UP", 4) == 0) {
  403. mUpDirection = UP_Z;
  404. } else {
  405. mUpDirection = UP_Y;
  406. }
  407. } else if (name == "contributor") {
  408. ReadMetaDataItem(curNode, mAssetMetaData);
  409. }
  410. }
  411. /*while (mReader->read()) {
  412. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  413. if (IsElement("unit")) {
  414. // read unit data from the element's attributes
  415. const int attrIndex = TestAttribute("meter");
  416. if (attrIndex == -1) {
  417. mUnitSize = 1.f;
  418. } else {
  419. mUnitSize = mReader->getAttributeValueAsFloat(attrIndex);
  420. }
  421. // consume the trailing stuff
  422. if (!mReader->isEmptyElement())
  423. SkipElement();
  424. } else if (IsElement("up_axis")) {
  425. // read content, strip whitespace, compare
  426. const char *content = GetTextContent();
  427. if (strncmp(content, "X_UP", 4) == 0)
  428. mUpDirection = UP_X;
  429. else if (strncmp(content, "Z_UP", 4) == 0)
  430. mUpDirection = UP_Z;
  431. else
  432. mUpDirection = UP_Y;
  433. // check element end
  434. TestClosing("up_axis");
  435. } else if (IsElement("contributor")) {
  436. ReadContributorInfo();
  437. } else {
  438. ReadMetaDataItem(mAssetMetaData);
  439. }
  440. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  441. if (strcmp(mReader->getNodeName(), "asset") != 0)
  442. ThrowException("Expected end of <asset> element.");
  443. break;
  444. }
  445. }*/
  446. }
  447. // ------------------------------------------------------------------------------------------------
  448. // Reads the contributor info
  449. /*void ColladaParser::ReadContributorInfo(XmlNode & node) {
  450. if (mReader->isEmptyElement())
  451. return;
  452. while (mReader->read()) {
  453. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  454. ReadMetaDataItem(mAssetMetaData);
  455. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  456. if (strcmp(mReader->getNodeName(), "contributor") != 0)
  457. ThrowException("Expected end of <contributor> element.");
  458. break;
  459. }
  460. }
  461. }*/
  462. static bool FindCommonKey(const std::string &collada_key, const MetaKeyPairVector &key_renaming, size_t &found_index) {
  463. for (size_t i = 0; i < key_renaming.size(); ++i) {
  464. if (key_renaming[i].first == collada_key) {
  465. found_index = i;
  466. return true;
  467. }
  468. }
  469. found_index = std::numeric_limits<size_t>::max();
  470. return false;
  471. }
  472. // ------------------------------------------------------------------------------------------------
  473. // Reads a single string metadata item
  474. void ColladaParser::ReadMetaDataItem(XmlNode &node, StringMetaData &metadata) {
  475. const Collada::MetaKeyPairVector &key_renaming = GetColladaAssimpMetaKeysCamelCase();
  476. const std::string name = node.name();
  477. if (!name.empty()) {
  478. const char *value_char = node.value();
  479. if (nullptr != value_char) {
  480. aiString aistr;
  481. aistr.Set(value_char);
  482. std::string camel_key_str(name);
  483. ToCamelCase(camel_key_str);
  484. size_t found_index;
  485. if (FindCommonKey(camel_key_str, key_renaming, found_index)) {
  486. metadata.emplace(key_renaming[found_index].second, aistr);
  487. } else {
  488. metadata.emplace(camel_key_str, aistr);
  489. }
  490. }
  491. }
  492. // Metadata such as created, keywords, subject etc
  493. /*const char *key_char = mReader->getNodeName();
  494. if (key_char != nullptr) {
  495. const std::string key_str(key_char);
  496. const char *value_char = TestTextContent();
  497. if (value_char != nullptr) {
  498. aiString aistr;
  499. aistr.Set(value_char);
  500. std::string camel_key_str(key_str);
  501. ToCamelCase(camel_key_str);
  502. size_t found_index;
  503. if (FindCommonKey(camel_key_str, key_renaming, found_index)) {
  504. metadata.emplace(key_renaming[found_index].second, aistr);
  505. } else {
  506. metadata.emplace(camel_key_str, aistr);
  507. }
  508. }
  509. TestClosing(key_str.c_str());
  510. } else
  511. SkipElement();*/
  512. }
  513. // ------------------------------------------------------------------------------------------------
  514. // Reads the animation clips
  515. void ColladaParser::ReadAnimationClipLibrary(XmlNode &node) {
  516. /*if (mReader->isEmptyElement())
  517. return;*/
  518. if (node.empty()) {
  519. return;
  520. }
  521. std::string animName;
  522. pugi::xml_attribute nameAttr = node.attribute("name");
  523. if (nameAttr) {
  524. animName = nameAttr.as_string();
  525. } else {
  526. pugi::xml_attribute idAttr = node.attribute("id");
  527. if (idAttr) {
  528. animName = idAttr.as_string();
  529. } else {
  530. animName = std::string("animation_") + to_string(mAnimationClipLibrary.size());
  531. }
  532. }
  533. std::pair<std::string, std::vector<std::string>> clip;
  534. clip.first = animName;
  535. for (pugi::xml_node &curNode : node.children()) {
  536. const std::string currentName = curNode.name();
  537. if (currentName == "instance_animation") {
  538. pugi::xml_attribute url = curNode.attribute("url");
  539. if (url) {
  540. const std::string urlName = url.as_string();
  541. if (urlName[0] != '#') {
  542. ThrowException("Unknown reference format");
  543. }
  544. clip.second.push_back(url);
  545. }
  546. }
  547. if (clip.second.size() > 0) {
  548. mAnimationClipLibrary.push_back(clip);
  549. }
  550. }
  551. /* while (mReader->read()) {
  552. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  553. if (IsElement("animation_clip")) {
  554. // optional name given as an attribute
  555. std::string animName;
  556. int indexName = TestAttribute("name");
  557. int indexID = TestAttribute("id");
  558. if (indexName >= 0)
  559. animName = mReader->getAttributeValue(indexName);
  560. else if (indexID >= 0)
  561. animName = mReader->getAttributeValue(indexID);
  562. else
  563. animName = std::string("animation_") + to_string(mAnimationClipLibrary.size());
  564. std::pair<std::string, std::vector<std::string>> clip;
  565. clip.first = animName;
  566. while (mReader->read()) {
  567. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  568. if (IsElement("instance_animation")) {
  569. int indexUrl = TestAttribute("url");
  570. if (indexUrl >= 0) {
  571. const char *url = mReader->getAttributeValue(indexUrl);
  572. if (url[0] != '#')
  573. ThrowException("Unknown reference format");
  574. url++;
  575. clip.second.push_back(url);
  576. }
  577. } else {
  578. // ignore the rest
  579. SkipElement();
  580. }
  581. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  582. if (strcmp(mReader->getNodeName(), "animation_clip") != 0)
  583. ThrowException("Expected end of <animation_clip> element.");
  584. break;
  585. }
  586. }
  587. if (clip.second.size() > 0) {
  588. mAnimationClipLibrary.push_back(clip);
  589. }
  590. } else {
  591. // ignore the rest
  592. SkipElement();
  593. }
  594. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  595. if (strcmp(mReader->getNodeName(), "library_animation_clips") != 0)
  596. ThrowException("Expected end of <library_animation_clips> element.");
  597. break;
  598. }
  599. }*/
  600. }
  601. void ColladaParser::PostProcessControllers() {
  602. std::string meshId;
  603. for (ControllerLibrary::iterator it = mControllerLibrary.begin(); it != mControllerLibrary.end(); ++it) {
  604. meshId = it->second.mMeshId;
  605. ControllerLibrary::iterator findItr = mControllerLibrary.find(meshId);
  606. while (findItr != mControllerLibrary.end()) {
  607. meshId = findItr->second.mMeshId;
  608. findItr = mControllerLibrary.find(meshId);
  609. }
  610. it->second.mMeshId = meshId;
  611. }
  612. }
  613. // ------------------------------------------------------------------------------------------------
  614. // Re-build animations from animation clip library, if present, otherwise combine single-channel animations
  615. void ColladaParser::PostProcessRootAnimations() {
  616. if (mAnimationClipLibrary.empty()) {
  617. mAnims.CombineSingleChannelAnimations();
  618. return;
  619. }
  620. Animation temp;
  621. for (AnimationClipLibrary::iterator it = mAnimationClipLibrary.begin(); it != mAnimationClipLibrary.end(); ++it) {
  622. std::string clipName = it->first;
  623. Animation *clip = new Animation();
  624. clip->mName = clipName;
  625. temp.mSubAnims.push_back(clip);
  626. for (std::vector<std::string>::iterator a = it->second.begin(); a != it->second.end(); ++a) {
  627. std::string animationID = *a;
  628. AnimationLibrary::iterator animation = mAnimationLibrary.find(animationID);
  629. if (animation != mAnimationLibrary.end()) {
  630. Animation *pSourceAnimation = animation->second;
  631. pSourceAnimation->CollectChannelsRecursively(clip->mChannels);
  632. }
  633. }
  634. }
  635. mAnims = temp;
  636. // Ensure no double deletes.
  637. temp.mSubAnims.clear();
  638. }
  639. // ------------------------------------------------------------------------------------------------
  640. // Reads the animation library
  641. void ColladaParser::ReadAnimationLibrary(XmlNode &node) {
  642. /*if (mReader->isEmptyElement())
  643. return;*/
  644. for (pugi::xml_node &curNode : node.children()) {
  645. const std::string currentName = curNode.name();
  646. if (currentName == "animation") {
  647. ReadAnimation(curNode, &mAnims);
  648. }
  649. }
  650. /*while (mReader->read()) {
  651. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  652. if (IsElement("animation")) {
  653. // delegate the reading. Depending on the inner elements it will be a container or a anim channel
  654. ReadAnimation(&mAnims);
  655. } else {
  656. // ignore the rest
  657. SkipElement();
  658. }
  659. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  660. if (strcmp(mReader->getNodeName(), "library_animations") != 0)
  661. ThrowException("Expected end of <library_animations> element.");
  662. break;
  663. }
  664. }*/
  665. }
  666. // ------------------------------------------------------------------------------------------------
  667. // Reads an animation into the given parent structure
  668. void ColladaParser::ReadAnimation(XmlNode &node, Collada::Animation *pParent) {
  669. if (node.empty()) {
  670. return;
  671. }
  672. /*if (mReader->isEmptyElement())
  673. return;*/
  674. // an <animation> element may be a container for grouping sub-elements or an animation channel
  675. // this is the channel collection by ID, in case it has channels
  676. typedef std::map<std::string, AnimationChannel> ChannelMap;
  677. ChannelMap channels;
  678. // this is the anim container in case we're a container
  679. Animation *anim = nullptr;
  680. // optional name given as an attribute
  681. std::string animName;
  682. pugi::xml_attribute nameAttr = node.attribute("name");
  683. if (nameAttr) {
  684. animName = nameAttr.as_string();
  685. } else {
  686. animName = "animation";
  687. }
  688. std::string animID;
  689. pugi::xml_attribute idAttr = node.attribute("id");
  690. if (idAttr) {
  691. animID = idAttr.as_string();
  692. }
  693. /* int indexName = TestAttribute("name");
  694. int indexID = TestAttribute("id");*/
  695. /*if (indexID >= 0)
  696. animID = mReader->getAttributeValue(indexID);
  697. if (indexName >= 0)
  698. animName = mReader->getAttributeValue(indexName);
  699. else if (indexID >= 0)
  700. animName = animID;
  701. else
  702. animName = "animation";
  703. */
  704. for (pugi::xml_node &curNode : node.children()) {
  705. const std::string currentName = curNode.name();
  706. if (currentName == "animation") {
  707. if (!anim) {
  708. anim = new Animation;
  709. anim->mName = animName;
  710. pParent->mSubAnims.push_back(anim);
  711. }
  712. // recurse into the sub-element
  713. ReadAnimation(curNode, anim);
  714. } else if (currentName == "source") {
  715. ReadSource(curNode);
  716. } else if (currentName == "sampler") {
  717. pugi::xml_attribute sampler_id = curNode.attribute("id");
  718. if (sampler_id) {
  719. std::string id = sampler_id.as_string();
  720. ChannelMap::iterator newChannel = channels.insert(std::make_pair(id, AnimationChannel())).first;
  721. // have it read into a channel
  722. ReadAnimationSampler(curNode, newChannel->second);
  723. } else if (currentName == "channel") {
  724. pugi::xml_attribute target = curNode.attribute("target");
  725. pugi::xml_attribute source = curNode.attribute("source");
  726. std::string source_name = source.as_string();
  727. if (source_name[0] == '#') {
  728. source_name = source_name.substr(1, source_name.size() - 1);
  729. }
  730. ChannelMap::iterator cit = channels.find(source_name);
  731. if (cit != channels.end()) {
  732. cit->second.mTarget = target.as_string();
  733. }
  734. }
  735. }
  736. }
  737. /*while (mReader->read()) {
  738. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  739. // we have subanimations
  740. if (IsElement("animation")) {
  741. // create container from our element
  742. if (!anim) {
  743. anim = new Animation;
  744. anim->mName = animName;
  745. pParent->mSubAnims.push_back(anim);
  746. }
  747. // recurse into the subelement
  748. ReadAnimation(anim);
  749. } else if (IsElement("source")) {
  750. // possible animation data - we'll never know. Better store it
  751. ReadSource();
  752. } else if (IsElement("sampler")) {
  753. // read the ID to assign the corresponding collada channel afterwards.
  754. int indexId = GetAttribute("id");
  755. std::string id = mReader->getAttributeValue(indexId);
  756. ChannelMap::iterator newChannel = channels.insert(std::make_pair(id, AnimationChannel())).first;
  757. // have it read into a channel
  758. ReadAnimationSampler(newChannel->second);
  759. } else if (IsElement("channel")) {
  760. // the binding element whose whole purpose is to provide the target to animate
  761. // Thanks, Collada! A directly posted information would have been too simple, I guess.
  762. // Better add another indirection to that! Can't have enough of those.
  763. int indexTarget = GetAttribute("target");
  764. int indexSource = GetAttribute("source");
  765. const char *sourceId = mReader->getAttributeValue(indexSource);
  766. if (sourceId[0] == '#')
  767. sourceId++;
  768. ChannelMap::iterator cit = channels.find(sourceId);
  769. if (cit != channels.end())
  770. cit->second.mTarget = mReader->getAttributeValue(indexTarget);
  771. if (!mReader->isEmptyElement())
  772. SkipElement();
  773. } else {
  774. // ignore the rest
  775. SkipElement();
  776. }
  777. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  778. if (strcmp(mReader->getNodeName(), "animation") != 0)
  779. ThrowException("Expected end of <animation> element.");
  780. break;
  781. }
  782. }*/
  783. // it turned out to have channels - add them
  784. if (!channels.empty()) {
  785. // FIXME: Is this essentially doing the same as "single-anim-node" codepath in
  786. // ColladaLoader::StoreAnimations? For now, this has been deferred to after
  787. // all animations and all clips have been read. Due to handling of
  788. // <library_animation_clips> this cannot be done here, as the channel owner
  789. // is lost, and some exporters make up animations by referring to multiple
  790. // single-channel animations from an <instance_animation>.
  791. /*
  792. // special filtering for stupid exporters packing each channel into a separate animation
  793. if( channels.size() == 1)
  794. {
  795. pParent->mChannels.push_back( channels.begin()->second);
  796. } else
  797. */
  798. {
  799. // else create the animation, if not done yet, and store the channels
  800. if (!anim) {
  801. anim = new Animation;
  802. anim->mName = animName;
  803. pParent->mSubAnims.push_back(anim);
  804. }
  805. for (ChannelMap::const_iterator it = channels.begin(); it != channels.end(); ++it)
  806. anim->mChannels.push_back(it->second);
  807. if (idAttr >= 0) {
  808. mAnimationLibrary[animID] = anim;
  809. }
  810. }
  811. }
  812. }
  813. // ------------------------------------------------------------------------------------------------
  814. // Reads an animation sampler into the given anim channel
  815. void ColladaParser::ReadAnimationSampler(XmlNode &node, Collada::AnimationChannel &pChannel) {
  816. for (pugi::xml_node &curNode : node.children()) {
  817. const std::string currentName = curNode.name();
  818. if (currentName == "input") {
  819. pugi::xml_attribute semantic = curNode.attribute("semantic");
  820. if (semantic) {
  821. }
  822. }
  823. }
  824. /*while (mReader->read()) {
  825. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  826. if (IsElement("input")) {
  827. int indexSemantic = GetAttribute("semantic");
  828. const char *semantic = mReader->getAttributeValue(indexSemantic);
  829. int indexSource = GetAttribute("source");
  830. const char *source = mReader->getAttributeValue(indexSource);
  831. if (source[0] != '#')
  832. ThrowException("Unsupported URL format");
  833. source++;
  834. if (strcmp(semantic, "INPUT") == 0)
  835. pChannel.mSourceTimes = source;
  836. else if (strcmp(semantic, "OUTPUT") == 0)
  837. pChannel.mSourceValues = source;
  838. else if (strcmp(semantic, "IN_TANGENT") == 0)
  839. pChannel.mInTanValues = source;
  840. else if (strcmp(semantic, "OUT_TANGENT") == 0)
  841. pChannel.mOutTanValues = source;
  842. else if (strcmp(semantic, "INTERPOLATION") == 0)
  843. pChannel.mInterpolationValues = source;
  844. if (!mReader->isEmptyElement())
  845. SkipElement();
  846. } else {
  847. // ignore the rest
  848. SkipElement();
  849. }
  850. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  851. if (strcmp(mReader->getNodeName(), "sampler") != 0)
  852. ThrowException("Expected end of <sampler> element.");
  853. break;
  854. }
  855. }*/
  856. }
  857. // ------------------------------------------------------------------------------------------------
  858. // Reads the skeleton controller library
  859. void ColladaParser::ReadControllerLibrary(XmlNode &node) {
  860. if (node.empty()) {
  861. return;
  862. }
  863. /*if (mReader->isEmptyElement())
  864. return;*/
  865. while (mReader->read()) {
  866. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  867. if (IsElement("controller")) {
  868. // read ID. Ask the spec if it's necessary or optional... you might be surprised.
  869. int attrID = GetAttribute("id");
  870. std::string id = mReader->getAttributeValue(attrID);
  871. // create an entry and store it in the library under its ID
  872. mControllerLibrary[id] = Controller();
  873. // read on from there
  874. ReadController(mControllerLibrary[id]);
  875. } else {
  876. // ignore the rest
  877. SkipElement();
  878. }
  879. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  880. if (strcmp(mReader->getNodeName(), "library_controllers") != 0)
  881. ThrowException("Expected end of <library_controllers> element.");
  882. break;
  883. }
  884. }
  885. }
  886. // ------------------------------------------------------------------------------------------------
  887. // Reads a controller into the given mesh structure
  888. void ColladaParser::ReadController(XmlNode &node, Collada::Controller &pController) {
  889. // initial values
  890. pController.mType = Skin;
  891. pController.mMethod = Normalized;
  892. while (mReader->read()) {
  893. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  894. // two types of controllers: "skin" and "morph". Only the first one is relevant, we skip the other
  895. if (IsElement("morph")) {
  896. pController.mType = Morph;
  897. int baseIndex = GetAttribute("source");
  898. pController.mMeshId = mReader->getAttributeValue(baseIndex) + 1;
  899. int methodIndex = GetAttribute("method");
  900. if (methodIndex > 0) {
  901. const char *method = mReader->getAttributeValue(methodIndex);
  902. if (strcmp(method, "RELATIVE") == 0)
  903. pController.mMethod = Relative;
  904. }
  905. } else if (IsElement("skin")) {
  906. // read the mesh it refers to. According to the spec this could also be another
  907. // controller, but I refuse to implement every single idea they've come up with
  908. int sourceIndex = GetAttribute("source");
  909. pController.mMeshId = mReader->getAttributeValue(sourceIndex) + 1;
  910. } else if (IsElement("bind_shape_matrix")) {
  911. // content is 16 floats to define a matrix... it seems to be important for some models
  912. const char *content = GetTextContent();
  913. // read the 16 floats
  914. for (unsigned int a = 0; a < 16; a++) {
  915. // read a number
  916. content = fast_atoreal_move<ai_real>(content, pController.mBindShapeMatrix[a]);
  917. // skip whitespace after it
  918. SkipSpacesAndLineEnd(&content);
  919. }
  920. TestClosing("bind_shape_matrix");
  921. } else if (IsElement("source")) {
  922. // data array - we have specialists to handle this
  923. ReadSource();
  924. } else if (IsElement("joints")) {
  925. ReadControllerJoints(pController);
  926. } else if (IsElement("vertex_weights")) {
  927. ReadControllerWeights(pController);
  928. } else if (IsElement("targets")) {
  929. while (mReader->read()) {
  930. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  931. if (IsElement("input")) {
  932. int semanticsIndex = GetAttribute("semantic");
  933. int sourceIndex = GetAttribute("source");
  934. const char *semantics = mReader->getAttributeValue(semanticsIndex);
  935. const char *source = mReader->getAttributeValue(sourceIndex);
  936. if (strcmp(semantics, "MORPH_TARGET") == 0) {
  937. pController.mMorphTarget = source + 1;
  938. } else if (strcmp(semantics, "MORPH_WEIGHT") == 0) {
  939. pController.mMorphWeight = source + 1;
  940. }
  941. }
  942. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  943. if (strcmp(mReader->getNodeName(), "targets") == 0)
  944. break;
  945. else
  946. ThrowException("Expected end of <targets> element.");
  947. }
  948. }
  949. } else {
  950. // ignore the rest
  951. SkipElement();
  952. }
  953. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  954. if (strcmp(mReader->getNodeName(), "controller") == 0)
  955. break;
  956. else if (strcmp(mReader->getNodeName(), "skin") != 0 && strcmp(mReader->getNodeName(), "morph") != 0)
  957. ThrowException("Expected end of <controller> element.");
  958. }
  959. }
  960. }
  961. // ------------------------------------------------------------------------------------------------
  962. // Reads the joint definitions for the given controller
  963. void ColladaParser::ReadControllerJoints(XmlNode &node, Collada::Controller &pController) {
  964. while (mReader->read()) {
  965. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  966. // Input channels for joint data. Two possible semantics: "JOINT" and "INV_BIND_MATRIX"
  967. if (IsElement("input")) {
  968. int indexSemantic = GetAttribute("semantic");
  969. const char *attrSemantic = mReader->getAttributeValue(indexSemantic);
  970. int indexSource = GetAttribute("source");
  971. const char *attrSource = mReader->getAttributeValue(indexSource);
  972. // local URLS always start with a '#'. We don't support global URLs
  973. if (attrSource[0] != '#')
  974. ThrowException(format() << "Unsupported URL format in \"" << attrSource << "\" in source attribute of <joints> data <input> element");
  975. attrSource++;
  976. // parse source URL to corresponding source
  977. if (strcmp(attrSemantic, "JOINT") == 0)
  978. pController.mJointNameSource = attrSource;
  979. else if (strcmp(attrSemantic, "INV_BIND_MATRIX") == 0)
  980. pController.mJointOffsetMatrixSource = attrSource;
  981. else
  982. ThrowException(format() << "Unknown semantic \"" << attrSemantic << "\" in <joints> data <input> element");
  983. // skip inner data, if present
  984. if (!mReader->isEmptyElement())
  985. SkipElement();
  986. } else {
  987. // ignore the rest
  988. SkipElement();
  989. }
  990. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  991. if (strcmp(mReader->getNodeName(), "joints") != 0)
  992. ThrowException("Expected end of <joints> element.");
  993. break;
  994. }
  995. }
  996. }
  997. // ------------------------------------------------------------------------------------------------
  998. // Reads the joint weights for the given controller
  999. void ColladaParser::ReadControllerWeights(XmlNode &node, Collada::Controller &pController) {
  1000. // read vertex count from attributes and resize the array accordingly
  1001. int indexCount = GetAttribute("count");
  1002. size_t vertexCount = (size_t)mReader->getAttributeValueAsInt(indexCount);
  1003. pController.mWeightCounts.resize(vertexCount);
  1004. while (mReader->read()) {
  1005. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1006. // Input channels for weight data. Two possible semantics: "JOINT" and "WEIGHT"
  1007. if (IsElement("input") && vertexCount > 0) {
  1008. InputChannel channel;
  1009. int indexSemantic = GetAttribute("semantic");
  1010. const char *attrSemantic = mReader->getAttributeValue(indexSemantic);
  1011. int indexSource = GetAttribute("source");
  1012. const char *attrSource = mReader->getAttributeValue(indexSource);
  1013. int indexOffset = TestAttribute("offset");
  1014. if (indexOffset >= 0)
  1015. channel.mOffset = mReader->getAttributeValueAsInt(indexOffset);
  1016. // local URLS always start with a '#'. We don't support global URLs
  1017. if (attrSource[0] != '#')
  1018. ThrowException(format() << "Unsupported URL format in \"" << attrSource << "\" in source attribute of <vertex_weights> data <input> element");
  1019. channel.mAccessor = attrSource + 1;
  1020. // parse source URL to corresponding source
  1021. if (strcmp(attrSemantic, "JOINT") == 0)
  1022. pController.mWeightInputJoints = channel;
  1023. else if (strcmp(attrSemantic, "WEIGHT") == 0)
  1024. pController.mWeightInputWeights = channel;
  1025. else
  1026. ThrowException(format() << "Unknown semantic \"" << attrSemantic << "\" in <vertex_weights> data <input> element");
  1027. // skip inner data, if present
  1028. if (!mReader->isEmptyElement())
  1029. SkipElement();
  1030. } else if (IsElement("vcount") && vertexCount > 0) {
  1031. // read weight count per vertex
  1032. const char *text = GetTextContent();
  1033. size_t numWeights = 0;
  1034. for (std::vector<size_t>::iterator it = pController.mWeightCounts.begin(); it != pController.mWeightCounts.end(); ++it) {
  1035. if (*text == 0)
  1036. ThrowException("Out of data while reading <vcount>");
  1037. *it = strtoul10(text, &text);
  1038. numWeights += *it;
  1039. SkipSpacesAndLineEnd(&text);
  1040. }
  1041. TestClosing("vcount");
  1042. // reserve weight count
  1043. pController.mWeights.resize(numWeights);
  1044. } else if (IsElement("v") && vertexCount > 0) {
  1045. // read JointIndex - WeightIndex pairs
  1046. const char *text = GetTextContent();
  1047. for (std::vector<std::pair<size_t, size_t>>::iterator it = pController.mWeights.begin(); it != pController.mWeights.end(); ++it) {
  1048. if (*text == 0)
  1049. ThrowException("Out of data while reading <vertex_weights>");
  1050. it->first = strtoul10(text, &text);
  1051. SkipSpacesAndLineEnd(&text);
  1052. if (*text == 0)
  1053. ThrowException("Out of data while reading <vertex_weights>");
  1054. it->second = strtoul10(text, &text);
  1055. SkipSpacesAndLineEnd(&text);
  1056. }
  1057. TestClosing("v");
  1058. } else {
  1059. // ignore the rest
  1060. SkipElement();
  1061. }
  1062. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1063. if (strcmp(mReader->getNodeName(), "vertex_weights") != 0)
  1064. ThrowException("Expected end of <vertex_weights> element.");
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. // ------------------------------------------------------------------------------------------------
  1070. // Reads the image library contents
  1071. void ColladaParser::ReadImageLibrary(XmlNode &node) {
  1072. if (node.empty()) {
  1073. return;
  1074. }
  1075. /*if (mReader->isEmptyElement())
  1076. return;*/
  1077. while (mReader->read()) {
  1078. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1079. if (IsElement("image")) {
  1080. // read ID. Another entry which is "optional" by design but obligatory in reality
  1081. int attrID = GetAttribute("id");
  1082. std::string id = mReader->getAttributeValue(attrID);
  1083. // create an entry and store it in the library under its ID
  1084. mImageLibrary[id] = Image();
  1085. // read on from there
  1086. ReadImage(mImageLibrary[id]);
  1087. } else {
  1088. // ignore the rest
  1089. SkipElement();
  1090. }
  1091. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1092. if (strcmp(mReader->getNodeName(), "library_images") != 0)
  1093. ThrowException("Expected end of <library_images> element.");
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. // ------------------------------------------------------------------------------------------------
  1099. // Reads an image entry into the given image
  1100. void ColladaParser::ReadImage(XmlNode &node, Collada::Image &pImage) {
  1101. while (mReader->read()) {
  1102. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1103. // Need to run different code paths here, depending on the Collada XSD version
  1104. if (IsElement("image")) {
  1105. SkipElement();
  1106. } else if (IsElement("init_from")) {
  1107. if (mFormat == FV_1_4_n) {
  1108. // FIX: C4D exporter writes empty <init_from/> tags
  1109. if (!mReader->isEmptyElement()) {
  1110. // element content is filename - hopefully
  1111. const char *sz = TestTextContent();
  1112. if (sz) {
  1113. aiString filepath(sz);
  1114. UriDecodePath(filepath);
  1115. pImage.mFileName = filepath.C_Str();
  1116. }
  1117. TestClosing("init_from");
  1118. }
  1119. if (!pImage.mFileName.length()) {
  1120. pImage.mFileName = "unknown_texture";
  1121. }
  1122. } else if (mFormat == FV_1_5_n) {
  1123. // make sure we skip over mip and array initializations, which
  1124. // we don't support, but which could confuse the loader if
  1125. // they're not skipped.
  1126. int attrib = TestAttribute("array_index");
  1127. if (attrib != -1 && mReader->getAttributeValueAsInt(attrib) > 0) {
  1128. ASSIMP_LOG_WARN("Collada: Ignoring texture array index");
  1129. continue;
  1130. }
  1131. attrib = TestAttribute("mip_index");
  1132. if (attrib != -1 && mReader->getAttributeValueAsInt(attrib) > 0) {
  1133. ASSIMP_LOG_WARN("Collada: Ignoring MIP map layer");
  1134. continue;
  1135. }
  1136. // TODO: correctly jump over cube and volume maps?
  1137. }
  1138. } else if (mFormat == FV_1_5_n) {
  1139. if (IsElement("ref")) {
  1140. // element content is filename - hopefully
  1141. const char *sz = TestTextContent();
  1142. if (sz) {
  1143. aiString filepath(sz);
  1144. UriDecodePath(filepath);
  1145. pImage.mFileName = filepath.C_Str();
  1146. }
  1147. TestClosing("ref");
  1148. } else if (IsElement("hex") && !pImage.mFileName.length()) {
  1149. // embedded image. get format
  1150. const int attrib = TestAttribute("format");
  1151. if (-1 == attrib)
  1152. ASSIMP_LOG_WARN("Collada: Unknown image file format");
  1153. else
  1154. pImage.mEmbeddedFormat = mReader->getAttributeValue(attrib);
  1155. const char *data = GetTextContent();
  1156. // hexadecimal-encoded binary octets. First of all, find the
  1157. // required buffer size to reserve enough storage.
  1158. const char *cur = data;
  1159. while (!IsSpaceOrNewLine(*cur))
  1160. cur++;
  1161. const unsigned int size = (unsigned int)(cur - data) * 2;
  1162. pImage.mImageData.resize(size);
  1163. for (unsigned int i = 0; i < size; ++i)
  1164. pImage.mImageData[i] = HexOctetToDecimal(data + (i << 1));
  1165. TestClosing("hex");
  1166. }
  1167. } else {
  1168. // ignore the rest
  1169. SkipElement();
  1170. }
  1171. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1172. if (strcmp(mReader->getNodeName(), "image") == 0)
  1173. break;
  1174. }
  1175. }
  1176. }
  1177. // ------------------------------------------------------------------------------------------------
  1178. // Reads the material library
  1179. void ColladaParser::ReadMaterialLibrary(XmlNode &node) {
  1180. if (node.empty()) {
  1181. return;
  1182. }
  1183. /*if (mReader->isEmptyElement())
  1184. return;*/
  1185. std::map<std::string, int> names;
  1186. while (mReader->read()) {
  1187. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1188. if (IsElement("material")) {
  1189. // read ID. By now you probably know my opinion about this "specification"
  1190. int attrID = GetAttribute("id");
  1191. std::string id = mReader->getAttributeValue(attrID);
  1192. std::string name;
  1193. int attrName = TestAttribute("name");
  1194. if (attrName >= 0)
  1195. name = mReader->getAttributeValue(attrName);
  1196. // create an entry and store it in the library under its ID
  1197. mMaterialLibrary[id] = Material();
  1198. if (!name.empty()) {
  1199. std::map<std::string, int>::iterator it = names.find(name);
  1200. if (it != names.end()) {
  1201. std::ostringstream strStream;
  1202. strStream << ++it->second;
  1203. name.append(" " + strStream.str());
  1204. } else {
  1205. names[name] = 0;
  1206. }
  1207. mMaterialLibrary[id].mName = name;
  1208. }
  1209. ReadMaterial(mMaterialLibrary[id]);
  1210. } else {
  1211. // ignore the rest
  1212. SkipElement();
  1213. }
  1214. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1215. if (strcmp(mReader->getNodeName(), "library_materials") != 0)
  1216. ThrowException("Expected end of <library_materials> element.");
  1217. break;
  1218. }
  1219. }
  1220. }
  1221. // ------------------------------------------------------------------------------------------------
  1222. // Reads the light library
  1223. void ColladaParser::ReadLightLibrary(XmlNode &node) {
  1224. if (node.empty()) {
  1225. return;
  1226. }
  1227. /*if (mReader->isEmptyElement())
  1228. return;*/
  1229. while (mReader->read()) {
  1230. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1231. if (IsElement("light")) {
  1232. // read ID. By now you probably know my opinion about this "specification"
  1233. int attrID = GetAttribute("id");
  1234. std::string id = mReader->getAttributeValue(attrID);
  1235. // create an entry and store it in the library under its ID
  1236. ReadLight(mLightLibrary[id] = Light());
  1237. } else {
  1238. // ignore the rest
  1239. SkipElement();
  1240. }
  1241. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1242. if (strcmp(mReader->getNodeName(), "library_lights") != 0)
  1243. ThrowException("Expected end of <library_lights> element.");
  1244. break;
  1245. }
  1246. }
  1247. }
  1248. // ------------------------------------------------------------------------------------------------
  1249. // Reads the camera library
  1250. void ColladaParser::ReadCameraLibrary(XmlNode &node) {
  1251. if (node.empty()) {
  1252. return;
  1253. }
  1254. /*if (mReader->isEmptyElement())
  1255. return;*/
  1256. while (mReader->read()) {
  1257. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1258. if (IsElement("camera")) {
  1259. // read ID. By now you probably know my opinion about this "specification"
  1260. int attrID = GetAttribute("id");
  1261. std::string id = mReader->getAttributeValue(attrID);
  1262. // create an entry and store it in the library under its ID
  1263. Camera &cam = mCameraLibrary[id];
  1264. attrID = TestAttribute("name");
  1265. if (attrID != -1)
  1266. cam.mName = mReader->getAttributeValue(attrID);
  1267. ReadCamera(cam);
  1268. } else {
  1269. // ignore the rest
  1270. SkipElement();
  1271. }
  1272. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1273. if (strcmp(mReader->getNodeName(), "library_cameras") != 0)
  1274. ThrowException("Expected end of <library_cameras> element.");
  1275. break;
  1276. }
  1277. }
  1278. }
  1279. // ------------------------------------------------------------------------------------------------
  1280. // Reads a material entry into the given material
  1281. void ColladaParser::ReadMaterial(XmlNode &node, Collada::Material &pMaterial) {
  1282. while (mReader->read()) {
  1283. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1284. if (IsElement("material")) {
  1285. SkipElement();
  1286. } else if (IsElement("instance_effect")) {
  1287. // referred effect by URL
  1288. int attrUrl = GetAttribute("url");
  1289. const char *url = mReader->getAttributeValue(attrUrl);
  1290. if (url[0] != '#')
  1291. ThrowException("Unknown reference format");
  1292. pMaterial.mEffect = url + 1;
  1293. SkipElement();
  1294. } else {
  1295. // ignore the rest
  1296. SkipElement();
  1297. }
  1298. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1299. if (strcmp(mReader->getNodeName(), "material") != 0)
  1300. ThrowException("Expected end of <material> element.");
  1301. break;
  1302. }
  1303. }
  1304. }
  1305. // ------------------------------------------------------------------------------------------------
  1306. // Reads a light entry into the given light
  1307. void ColladaParser::ReadLight(XmlNode &node, Collada::Light &pLight) {
  1308. while (mReader->read()) {
  1309. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1310. if (IsElement("light")) {
  1311. SkipElement();
  1312. } else if (IsElement("spot")) {
  1313. pLight.mType = aiLightSource_SPOT;
  1314. } else if (IsElement("ambient")) {
  1315. pLight.mType = aiLightSource_AMBIENT;
  1316. } else if (IsElement("directional")) {
  1317. pLight.mType = aiLightSource_DIRECTIONAL;
  1318. } else if (IsElement("point")) {
  1319. pLight.mType = aiLightSource_POINT;
  1320. } else if (IsElement("color")) {
  1321. // text content contains 3 floats
  1322. const char *content = GetTextContent();
  1323. content = fast_atoreal_move<ai_real>(content, (ai_real &)pLight.mColor.r);
  1324. SkipSpacesAndLineEnd(&content);
  1325. content = fast_atoreal_move<ai_real>(content, (ai_real &)pLight.mColor.g);
  1326. SkipSpacesAndLineEnd(&content);
  1327. content = fast_atoreal_move<ai_real>(content, (ai_real &)pLight.mColor.b);
  1328. SkipSpacesAndLineEnd(&content);
  1329. TestClosing("color");
  1330. } else if (IsElement("constant_attenuation")) {
  1331. pLight.mAttConstant = ReadFloatFromTextContent();
  1332. TestClosing("constant_attenuation");
  1333. } else if (IsElement("linear_attenuation")) {
  1334. pLight.mAttLinear = ReadFloatFromTextContent();
  1335. TestClosing("linear_attenuation");
  1336. } else if (IsElement("quadratic_attenuation")) {
  1337. pLight.mAttQuadratic = ReadFloatFromTextContent();
  1338. TestClosing("quadratic_attenuation");
  1339. } else if (IsElement("falloff_angle")) {
  1340. pLight.mFalloffAngle = ReadFloatFromTextContent();
  1341. TestClosing("falloff_angle");
  1342. } else if (IsElement("falloff_exponent")) {
  1343. pLight.mFalloffExponent = ReadFloatFromTextContent();
  1344. TestClosing("falloff_exponent");
  1345. }
  1346. // FCOLLADA extensions
  1347. // -------------------------------------------------------
  1348. else if (IsElement("outer_cone")) {
  1349. pLight.mOuterAngle = ReadFloatFromTextContent();
  1350. TestClosing("outer_cone");
  1351. }
  1352. // ... and this one is even deprecated
  1353. else if (IsElement("penumbra_angle")) {
  1354. pLight.mPenumbraAngle = ReadFloatFromTextContent();
  1355. TestClosing("penumbra_angle");
  1356. } else if (IsElement("intensity")) {
  1357. pLight.mIntensity = ReadFloatFromTextContent();
  1358. TestClosing("intensity");
  1359. } else if (IsElement("falloff")) {
  1360. pLight.mOuterAngle = ReadFloatFromTextContent();
  1361. TestClosing("falloff");
  1362. } else if (IsElement("hotspot_beam")) {
  1363. pLight.mFalloffAngle = ReadFloatFromTextContent();
  1364. TestClosing("hotspot_beam");
  1365. }
  1366. // OpenCOLLADA extensions
  1367. // -------------------------------------------------------
  1368. else if (IsElement("decay_falloff")) {
  1369. pLight.mOuterAngle = ReadFloatFromTextContent();
  1370. TestClosing("decay_falloff");
  1371. }
  1372. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1373. if (strcmp(mReader->getNodeName(), "light") == 0)
  1374. break;
  1375. }
  1376. }
  1377. }
  1378. // ------------------------------------------------------------------------------------------------
  1379. // Reads a camera entry into the given light
  1380. void ColladaParser::ReadCamera(XmlNode &node, Collada::Camera &camera) {
  1381. while (mReader->read()) {
  1382. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1383. if (IsElement("camera")) {
  1384. SkipElement();
  1385. } else if (IsElement("orthographic")) {
  1386. camera.mOrtho = true;
  1387. } else if (IsElement("xfov") || IsElement("xmag")) {
  1388. camera.mHorFov = ReadFloatFromTextContent();
  1389. TestClosing((camera.mOrtho ? "xmag" : "xfov"));
  1390. } else if (IsElement("yfov") || IsElement("ymag")) {
  1391. camera.mVerFov = ReadFloatFromTextContent();
  1392. TestClosing((camera.mOrtho ? "ymag" : "yfov"));
  1393. } else if (IsElement("aspect_ratio")) {
  1394. camera.mAspect = ReadFloatFromTextContent();
  1395. TestClosing("aspect_ratio");
  1396. } else if (IsElement("znear")) {
  1397. camera.mZNear = ReadFloatFromTextContent();
  1398. TestClosing("znear");
  1399. } else if (IsElement("zfar")) {
  1400. camera.mZFar = ReadFloatFromTextContent();
  1401. TestClosing("zfar");
  1402. }
  1403. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1404. if (strcmp(mReader->getNodeName(), "camera") == 0)
  1405. break;
  1406. }
  1407. }
  1408. }
  1409. // ------------------------------------------------------------------------------------------------
  1410. // Reads the effect library
  1411. void ColladaParser::ReadEffectLibrary(XmlNode &node) {
  1412. if (node.empty()) {
  1413. return;
  1414. }
  1415. /*if (mReader->isEmptyElement()) {
  1416. return;
  1417. }*/
  1418. while (mReader->read()) {
  1419. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1420. if (IsElement("effect")) {
  1421. // read ID. Do I have to repeat my ranting about "optional" attributes?
  1422. int attrID = GetAttribute("id");
  1423. std::string id = mReader->getAttributeValue(attrID);
  1424. // create an entry and store it in the library under its ID
  1425. mEffectLibrary[id] = Effect();
  1426. // read on from there
  1427. ReadEffect(mEffectLibrary[id]);
  1428. } else {
  1429. // ignore the rest
  1430. SkipElement();
  1431. }
  1432. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1433. if (strcmp(mReader->getNodeName(), "library_effects") != 0)
  1434. ThrowException("Expected end of <library_effects> element.");
  1435. break;
  1436. }
  1437. }
  1438. }
  1439. // ------------------------------------------------------------------------------------------------
  1440. // Reads an effect entry into the given effect
  1441. void ColladaParser::ReadEffect(XmlNode &node, Collada::Effect &pEffect) {
  1442. // for the moment we don't support any other type of effect.
  1443. while (mReader->read()) {
  1444. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1445. if (IsElement("profile_COMMON"))
  1446. ReadEffectProfileCommon(pEffect);
  1447. else
  1448. SkipElement();
  1449. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1450. if (strcmp(mReader->getNodeName(), "effect") != 0)
  1451. ThrowException("Expected end of <effect> element.");
  1452. break;
  1453. }
  1454. }
  1455. }
  1456. // ------------------------------------------------------------------------------------------------
  1457. // Reads an COMMON effect profile
  1458. void ColladaParser::ReadEffectProfileCommon(XmlNode &node, Collada::Effect &pEffect) {
  1459. while (mReader->read()) {
  1460. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1461. if (IsElement("newparam")) {
  1462. // save ID
  1463. int attrSID = GetAttribute("sid");
  1464. std::string sid = mReader->getAttributeValue(attrSID);
  1465. pEffect.mParams[sid] = EffectParam();
  1466. ReadEffectParam(pEffect.mParams[sid]);
  1467. } else if (IsElement("technique") || IsElement("extra")) {
  1468. // just syntactic sugar
  1469. }
  1470. else if (mFormat == FV_1_4_n && IsElement("image")) {
  1471. // read ID. Another entry which is "optional" by design but obligatory in reality
  1472. int attrID = GetAttribute("id");
  1473. std::string id = mReader->getAttributeValue(attrID);
  1474. // create an entry and store it in the library under its ID
  1475. mImageLibrary[id] = Image();
  1476. // read on from there
  1477. ReadImage(mImageLibrary[id]);
  1478. }
  1479. /* Shading modes */
  1480. else if (IsElement("phong"))
  1481. pEffect.mShadeType = Shade_Phong;
  1482. else if (IsElement("constant"))
  1483. pEffect.mShadeType = Shade_Constant;
  1484. else if (IsElement("lambert"))
  1485. pEffect.mShadeType = Shade_Lambert;
  1486. else if (IsElement("blinn"))
  1487. pEffect.mShadeType = Shade_Blinn;
  1488. /* Color + texture properties */
  1489. else if (IsElement("emission"))
  1490. ReadEffectColor(pEffect.mEmissive, pEffect.mTexEmissive);
  1491. else if (IsElement("ambient"))
  1492. ReadEffectColor(pEffect.mAmbient, pEffect.mTexAmbient);
  1493. else if (IsElement("diffuse"))
  1494. ReadEffectColor(pEffect.mDiffuse, pEffect.mTexDiffuse);
  1495. else if (IsElement("specular"))
  1496. ReadEffectColor(pEffect.mSpecular, pEffect.mTexSpecular);
  1497. else if (IsElement("reflective")) {
  1498. ReadEffectColor(pEffect.mReflective, pEffect.mTexReflective);
  1499. } else if (IsElement("transparent")) {
  1500. pEffect.mHasTransparency = true;
  1501. const char *opaque = mReader->getAttributeValueSafe("opaque");
  1502. if (::strcmp(opaque, "RGB_ZERO") == 0 || ::strcmp(opaque, "RGB_ONE") == 0) {
  1503. pEffect.mRGBTransparency = true;
  1504. }
  1505. // In RGB_ZERO mode, the transparency is interpreted in reverse, go figure...
  1506. if (::strcmp(opaque, "RGB_ZERO") == 0 || ::strcmp(opaque, "A_ZERO") == 0) {
  1507. pEffect.mInvertTransparency = true;
  1508. }
  1509. ReadEffectColor(pEffect.mTransparent, pEffect.mTexTransparent);
  1510. } else if (IsElement("shininess"))
  1511. ReadEffectFloat(pEffect.mShininess);
  1512. else if (IsElement("reflectivity"))
  1513. ReadEffectFloat(pEffect.mReflectivity);
  1514. /* Single scalar properties */
  1515. else if (IsElement("transparency"))
  1516. ReadEffectFloat(pEffect.mTransparency);
  1517. else if (IsElement("index_of_refraction"))
  1518. ReadEffectFloat(pEffect.mRefractIndex);
  1519. // GOOGLEEARTH/OKINO extensions
  1520. // -------------------------------------------------------
  1521. else if (IsElement("double_sided"))
  1522. pEffect.mDoubleSided = ReadBoolFromTextContent();
  1523. // FCOLLADA extensions
  1524. // -------------------------------------------------------
  1525. else if (IsElement("bump")) {
  1526. aiColor4D dummy;
  1527. ReadEffectColor(dummy, pEffect.mTexBump);
  1528. }
  1529. // MAX3D extensions
  1530. // -------------------------------------------------------
  1531. else if (IsElement("wireframe")) {
  1532. pEffect.mWireframe = ReadBoolFromTextContent();
  1533. TestClosing("wireframe");
  1534. } else if (IsElement("faceted")) {
  1535. pEffect.mFaceted = ReadBoolFromTextContent();
  1536. TestClosing("faceted");
  1537. } else {
  1538. // ignore the rest
  1539. SkipElement();
  1540. }
  1541. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1542. if (strcmp(mReader->getNodeName(), "profile_COMMON") == 0) {
  1543. break;
  1544. }
  1545. }
  1546. }
  1547. }
  1548. // ------------------------------------------------------------------------------------------------
  1549. // Read texture wrapping + UV transform settings from a profile==Maya chunk
  1550. void ColladaParser::ReadSamplerProperties(XmlNode &node, Sampler &out) {
  1551. if (node.empty()) {
  1552. return;
  1553. }
  1554. /*if (mReader->isEmptyElement()) {
  1555. return;
  1556. }*/
  1557. while (mReader->read()) {
  1558. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1559. // MAYA extensions
  1560. // -------------------------------------------------------
  1561. if (IsElement("wrapU")) {
  1562. out.mWrapU = ReadBoolFromTextContent();
  1563. TestClosing("wrapU");
  1564. } else if (IsElement("wrapV")) {
  1565. out.mWrapV = ReadBoolFromTextContent();
  1566. TestClosing("wrapV");
  1567. } else if (IsElement("mirrorU")) {
  1568. out.mMirrorU = ReadBoolFromTextContent();
  1569. TestClosing("mirrorU");
  1570. } else if (IsElement("mirrorV")) {
  1571. out.mMirrorV = ReadBoolFromTextContent();
  1572. TestClosing("mirrorV");
  1573. } else if (IsElement("repeatU")) {
  1574. out.mTransform.mScaling.x = ReadFloatFromTextContent();
  1575. TestClosing("repeatU");
  1576. } else if (IsElement("repeatV")) {
  1577. out.mTransform.mScaling.y = ReadFloatFromTextContent();
  1578. TestClosing("repeatV");
  1579. } else if (IsElement("offsetU")) {
  1580. out.mTransform.mTranslation.x = ReadFloatFromTextContent();
  1581. TestClosing("offsetU");
  1582. } else if (IsElement("offsetV")) {
  1583. out.mTransform.mTranslation.y = ReadFloatFromTextContent();
  1584. TestClosing("offsetV");
  1585. } else if (IsElement("rotateUV")) {
  1586. out.mTransform.mRotation = ReadFloatFromTextContent();
  1587. TestClosing("rotateUV");
  1588. } else if (IsElement("blend_mode")) {
  1589. const char *sz = GetTextContent();
  1590. // http://www.feelingsoftware.com/content/view/55/72/lang,en/
  1591. // NONE, OVER, IN, OUT, ADD, SUBTRACT, MULTIPLY, DIFFERENCE, LIGHTEN, DARKEN, SATURATE, DESATURATE and ILLUMINATE
  1592. if (0 == ASSIMP_strincmp(sz, "ADD", 3))
  1593. out.mOp = aiTextureOp_Add;
  1594. else if (0 == ASSIMP_strincmp(sz, "SUBTRACT", 8))
  1595. out.mOp = aiTextureOp_Subtract;
  1596. else if (0 == ASSIMP_strincmp(sz, "MULTIPLY", 8))
  1597. out.mOp = aiTextureOp_Multiply;
  1598. else {
  1599. ASSIMP_LOG_WARN("Collada: Unsupported MAYA texture blend mode");
  1600. }
  1601. TestClosing("blend_mode");
  1602. }
  1603. // OKINO extensions
  1604. // -------------------------------------------------------
  1605. else if (IsElement("weighting")) {
  1606. out.mWeighting = ReadFloatFromTextContent();
  1607. TestClosing("weighting");
  1608. } else if (IsElement("mix_with_previous_layer")) {
  1609. out.mMixWithPrevious = ReadFloatFromTextContent();
  1610. TestClosing("mix_with_previous_layer");
  1611. }
  1612. // MAX3D extensions
  1613. // -------------------------------------------------------
  1614. else if (IsElement("amount")) {
  1615. out.mWeighting = ReadFloatFromTextContent();
  1616. TestClosing("amount");
  1617. }
  1618. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1619. if (strcmp(mReader->getNodeName(), "technique") == 0)
  1620. break;
  1621. }
  1622. }
  1623. }
  1624. // ------------------------------------------------------------------------------------------------
  1625. // Reads an effect entry containing a color or a texture defining that color
  1626. void ColladaParser::ReadEffectColor(XmlNode &node, aiColor4D &pColor, Sampler &pSampler) {
  1627. if (node.empty()) {
  1628. return;
  1629. }
  1630. /*if (mReader->isEmptyElement())
  1631. return;*/
  1632. // Save current element name
  1633. const std::string curElem = mReader->getNodeName();
  1634. while (mReader->read()) {
  1635. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1636. if (IsElement("color")) {
  1637. // text content contains 4 floats
  1638. const char *content = GetTextContent();
  1639. content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.r);
  1640. SkipSpacesAndLineEnd(&content);
  1641. content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.g);
  1642. SkipSpacesAndLineEnd(&content);
  1643. content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.b);
  1644. SkipSpacesAndLineEnd(&content);
  1645. content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.a);
  1646. SkipSpacesAndLineEnd(&content);
  1647. TestClosing("color");
  1648. } else if (IsElement("texture")) {
  1649. // get name of source texture/sampler
  1650. int attrTex = GetAttribute("texture");
  1651. pSampler.mName = mReader->getAttributeValue(attrTex);
  1652. // get name of UV source channel. Specification demands it to be there, but some exporters
  1653. // don't write it. It will be the default UV channel in case it's missing.
  1654. attrTex = TestAttribute("texcoord");
  1655. if (attrTex >= 0)
  1656. pSampler.mUVChannel = mReader->getAttributeValue(attrTex);
  1657. //SkipElement();
  1658. // as we've read texture, the color needs to be 1,1,1,1
  1659. pColor = aiColor4D(1.f, 1.f, 1.f, 1.f);
  1660. } else if (IsElement("technique")) {
  1661. const int _profile = GetAttribute("profile");
  1662. const char *profile = mReader->getAttributeValue(_profile);
  1663. // Some extensions are quite useful ... ReadSamplerProperties processes
  1664. // several extensions in MAYA, OKINO and MAX3D profiles.
  1665. if (!::strcmp(profile, "MAYA") || !::strcmp(profile, "MAX3D") || !::strcmp(profile, "OKINO")) {
  1666. // get more information on this sampler
  1667. ReadSamplerProperties(pSampler);
  1668. } else
  1669. SkipElement();
  1670. } else if (!IsElement("extra")) {
  1671. // ignore the rest
  1672. SkipElement();
  1673. }
  1674. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1675. if (mReader->getNodeName() == curElem)
  1676. break;
  1677. }
  1678. }
  1679. }
  1680. // ------------------------------------------------------------------------------------------------
  1681. // Reads an effect entry containing a float
  1682. void ColladaParser::ReadEffectFloat(XmlNode &node, ai_real &pFloat) {
  1683. while (mReader->read()) {
  1684. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1685. if (IsElement("float")) {
  1686. // text content contains a single floats
  1687. const char *content = GetTextContent();
  1688. content = fast_atoreal_move<ai_real>(content, pFloat);
  1689. SkipSpacesAndLineEnd(&content);
  1690. TestClosing("float");
  1691. } else {
  1692. // ignore the rest
  1693. SkipElement();
  1694. }
  1695. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1696. break;
  1697. }
  1698. }
  1699. }
  1700. // ------------------------------------------------------------------------------------------------
  1701. // Reads an effect parameter specification of any kind
  1702. void ColladaParser::ReadEffectParam(XmlNode &node, Collada::EffectParam &pParam) {
  1703. while (mReader->read()) {
  1704. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1705. if (IsElement("surface")) {
  1706. // image ID given inside <init_from> tags
  1707. TestOpening("init_from");
  1708. const char *content = GetTextContent();
  1709. pParam.mType = Param_Surface;
  1710. pParam.mReference = content;
  1711. TestClosing("init_from");
  1712. // don't care for remaining stuff
  1713. SkipElement("surface");
  1714. } else if (IsElement("sampler2D") && (FV_1_4_n == mFormat || FV_1_3_n == mFormat)) {
  1715. // surface ID is given inside <source> tags
  1716. TestOpening("source");
  1717. const char *content = GetTextContent();
  1718. pParam.mType = Param_Sampler;
  1719. pParam.mReference = content;
  1720. TestClosing("source");
  1721. // don't care for remaining stuff
  1722. SkipElement("sampler2D");
  1723. } else if (IsElement("sampler2D")) {
  1724. // surface ID is given inside <instance_image> tags
  1725. TestOpening("instance_image");
  1726. int attrURL = GetAttribute("url");
  1727. const char *url = mReader->getAttributeValue(attrURL);
  1728. if (url[0] != '#')
  1729. ThrowException("Unsupported URL format in instance_image");
  1730. url++;
  1731. pParam.mType = Param_Sampler;
  1732. pParam.mReference = url;
  1733. SkipElement("sampler2D");
  1734. } else {
  1735. // ignore unknown element
  1736. SkipElement();
  1737. }
  1738. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1739. break;
  1740. }
  1741. }
  1742. }
  1743. // ------------------------------------------------------------------------------------------------
  1744. // Reads the geometry library contents
  1745. void ColladaParser::ReadGeometryLibrary(XmlNode &node) {
  1746. if (node.empty()) {
  1747. return;
  1748. }
  1749. /*if (mReader->isEmptyElement())
  1750. return;*/
  1751. while (mReader->read()) {
  1752. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1753. if (IsElement("geometry")) {
  1754. // read ID. Another entry which is "optional" by design but obligatory in reality
  1755. int indexID = GetAttribute("id");
  1756. std::string id = mReader->getAttributeValue(indexID);
  1757. // create a mesh and store it in the library under its (resolved) ID
  1758. // Skip and warn if ID is not unique
  1759. if (mMeshLibrary.find(id) == mMeshLibrary.cend()) {
  1760. std::unique_ptr<Mesh> mesh(new Mesh(id));
  1761. // read the mesh name if it exists
  1762. const int nameIndex = TestAttribute("name");
  1763. if (nameIndex != -1) {
  1764. mesh->mName = mReader->getAttributeValue(nameIndex);
  1765. }
  1766. // read on from there
  1767. ReadGeometry(*mesh);
  1768. // Read successfully, add to library
  1769. mMeshLibrary.insert({ id, mesh.release() });
  1770. } else {
  1771. ASSIMP_LOG_ERROR_F("Collada: Skipped duplicate geometry id: \"", id, "\"");
  1772. SkipElement();
  1773. }
  1774. } else {
  1775. // ignore the rest
  1776. SkipElement();
  1777. }
  1778. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1779. if (strcmp(mReader->getNodeName(), "library_geometries") != 0)
  1780. ThrowException("Expected end of <library_geometries> element.");
  1781. break;
  1782. }
  1783. }
  1784. }
  1785. // ------------------------------------------------------------------------------------------------
  1786. // Reads a geometry from the geometry library.
  1787. void ColladaParser::ReadGeometry(XmlNode &node, Collada::Mesh &pMesh) {
  1788. if (node.empty()) {
  1789. return;
  1790. }
  1791. /*if (mReader->isEmptyElement())
  1792. return;*/
  1793. while (mReader->read()) {
  1794. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1795. if (IsElement("mesh")) {
  1796. // read on from there
  1797. ReadMesh(pMesh);
  1798. } else {
  1799. // ignore the rest
  1800. SkipElement();
  1801. }
  1802. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1803. if (strcmp(mReader->getNodeName(), "geometry") != 0)
  1804. ThrowException("Expected end of <geometry> element.");
  1805. break;
  1806. }
  1807. }
  1808. }
  1809. // ------------------------------------------------------------------------------------------------
  1810. // Reads a mesh from the geometry library
  1811. void ColladaParser::ReadMesh(XmlNode &node, Mesh &pMesh) {
  1812. if (node.empty()) {
  1813. return;
  1814. }
  1815. /*if (mReader->isEmptyElement())
  1816. return;*/
  1817. while (mReader->read()) {
  1818. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1819. if (IsElement("source")) {
  1820. // we have professionals dealing with this
  1821. ReadSource();
  1822. } else if (IsElement("vertices")) {
  1823. // read per-vertex mesh data
  1824. ReadVertexData(pMesh);
  1825. } else if (IsElement("triangles") || IsElement("lines") || IsElement("linestrips") || IsElement("polygons") || IsElement("polylist") || IsElement("trifans") || IsElement("tristrips")) {
  1826. // read per-index mesh data and faces setup
  1827. ReadIndexData(pMesh);
  1828. } else {
  1829. // ignore the restf
  1830. SkipElement();
  1831. }
  1832. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1833. if (strcmp(mReader->getNodeName(), "technique_common") == 0) {
  1834. // end of another meaningless element - read over it
  1835. } else if (strcmp(mReader->getNodeName(), "mesh") == 0) {
  1836. // end of <mesh> element - we're done here
  1837. break;
  1838. } else {
  1839. // everything else should be punished
  1840. ThrowException("Expected end of <mesh> element.");
  1841. }
  1842. }
  1843. }
  1844. }
  1845. // ------------------------------------------------------------------------------------------------
  1846. // Reads a source element
  1847. void ColladaParser::ReadSource(XmlNode &node) {
  1848. int indexID = GetAttribute("id");
  1849. std::string sourceID = mReader->getAttributeValue(indexID);
  1850. while (mReader->read()) {
  1851. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1852. if (IsElement("float_array") || IsElement("IDREF_array") || IsElement("Name_array")) {
  1853. ReadDataArray();
  1854. } else if (IsElement("technique_common")) {
  1855. // I don't care for your profiles
  1856. } else if (IsElement("accessor")) {
  1857. ReadAccessor(sourceID);
  1858. } else {
  1859. // ignore the rest
  1860. SkipElement();
  1861. }
  1862. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  1863. if (strcmp(mReader->getNodeName(), "source") == 0) {
  1864. // end of <source> - we're done
  1865. break;
  1866. } else if (strcmp(mReader->getNodeName(), "technique_common") == 0) {
  1867. // end of another meaningless element - read over it
  1868. } else {
  1869. // everything else should be punished
  1870. ThrowException("Expected end of <source> element.");
  1871. }
  1872. }
  1873. }
  1874. }
  1875. // ------------------------------------------------------------------------------------------------
  1876. // Reads a data array holding a number of floats, and stores it in the global library
  1877. void ColladaParser::ReadDataArray(XmlNode &node) {
  1878. std::string elmName = mReader->getNodeName();
  1879. bool isStringArray = (elmName == "IDREF_array" || elmName == "Name_array");
  1880. bool isEmptyElement = mReader->isEmptyElement();
  1881. // read attributes
  1882. int indexID = GetAttribute("id");
  1883. std::string id = mReader->getAttributeValue(indexID);
  1884. int indexCount = GetAttribute("count");
  1885. unsigned int count = (unsigned int)mReader->getAttributeValueAsInt(indexCount);
  1886. const char *content = TestTextContent();
  1887. // read values and store inside an array in the data library
  1888. mDataLibrary[id] = Data();
  1889. Data &data = mDataLibrary[id];
  1890. data.mIsStringArray = isStringArray;
  1891. // some exporters write empty data arrays, but we need to conserve them anyways because others might reference them
  1892. if (content) {
  1893. if (isStringArray) {
  1894. data.mStrings.reserve(count);
  1895. std::string s;
  1896. for (unsigned int a = 0; a < count; a++) {
  1897. if (*content == 0)
  1898. ThrowException("Expected more values while reading IDREF_array contents.");
  1899. s.clear();
  1900. while (!IsSpaceOrNewLine(*content))
  1901. s += *content++;
  1902. data.mStrings.push_back(s);
  1903. SkipSpacesAndLineEnd(&content);
  1904. }
  1905. } else {
  1906. data.mValues.reserve(count);
  1907. for (unsigned int a = 0; a < count; a++) {
  1908. if (*content == 0)
  1909. ThrowException("Expected more values while reading float_array contents.");
  1910. ai_real value;
  1911. // read a number
  1912. content = fast_atoreal_move<ai_real>(content, value);
  1913. data.mValues.push_back(value);
  1914. // skip whitespace after it
  1915. SkipSpacesAndLineEnd(&content);
  1916. }
  1917. }
  1918. }
  1919. // test for closing tag
  1920. if (!isEmptyElement)
  1921. TestClosing(elmName.c_str());
  1922. }
  1923. // ------------------------------------------------------------------------------------------------
  1924. // Reads an accessor and stores it in the global library
  1925. void ColladaParser::ReadAccessor(XmlNode &node, const std::string &pID) {
  1926. // read accessor attributes
  1927. int attrSource = GetAttribute("source");
  1928. const char *source = mReader->getAttributeValue(attrSource);
  1929. if (source[0] != '#')
  1930. ThrowException(format() << "Unknown reference format in url \"" << source << "\" in source attribute of <accessor> element.");
  1931. int attrCount = GetAttribute("count");
  1932. unsigned int count = (unsigned int)mReader->getAttributeValueAsInt(attrCount);
  1933. int attrOffset = TestAttribute("offset");
  1934. unsigned int offset = 0;
  1935. if (attrOffset > -1)
  1936. offset = (unsigned int)mReader->getAttributeValueAsInt(attrOffset);
  1937. int attrStride = TestAttribute("stride");
  1938. unsigned int stride = 1;
  1939. if (attrStride > -1)
  1940. stride = (unsigned int)mReader->getAttributeValueAsInt(attrStride);
  1941. // store in the library under the given ID
  1942. mAccessorLibrary[pID] = Accessor();
  1943. Accessor &acc = mAccessorLibrary[pID];
  1944. acc.mCount = count;
  1945. acc.mOffset = offset;
  1946. acc.mStride = stride;
  1947. acc.mSource = source + 1; // ignore the leading '#'
  1948. acc.mSize = 0; // gets incremented with every param
  1949. // and read the components
  1950. while (mReader->read()) {
  1951. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  1952. if (IsElement("param")) {
  1953. // read data param
  1954. int attrName = TestAttribute("name");
  1955. std::string name;
  1956. if (attrName > -1) {
  1957. name = mReader->getAttributeValue(attrName);
  1958. // analyse for common type components and store it's sub-offset in the corresponding field
  1959. /* Cartesian coordinates */
  1960. if (name == "X")
  1961. acc.mSubOffset[0] = acc.mParams.size();
  1962. else if (name == "Y")
  1963. acc.mSubOffset[1] = acc.mParams.size();
  1964. else if (name == "Z")
  1965. acc.mSubOffset[2] = acc.mParams.size();
  1966. /* RGBA colors */
  1967. else if (name == "R")
  1968. acc.mSubOffset[0] = acc.mParams.size();
  1969. else if (name == "G")
  1970. acc.mSubOffset[1] = acc.mParams.size();
  1971. else if (name == "B")
  1972. acc.mSubOffset[2] = acc.mParams.size();
  1973. else if (name == "A")
  1974. acc.mSubOffset[3] = acc.mParams.size();
  1975. /* UVWQ (STPQ) texture coordinates */
  1976. else if (name == "S")
  1977. acc.mSubOffset[0] = acc.mParams.size();
  1978. else if (name == "T")
  1979. acc.mSubOffset[1] = acc.mParams.size();
  1980. else if (name == "P")
  1981. acc.mSubOffset[2] = acc.mParams.size();
  1982. // else if( name == "Q") acc.mSubOffset[3] = acc.mParams.size();
  1983. /* 4D uv coordinates are not supported in Assimp */
  1984. /* Generic extra data, interpreted as UV data, too*/
  1985. else if (name == "U")
  1986. acc.mSubOffset[0] = acc.mParams.size();
  1987. else if (name == "V")
  1988. acc.mSubOffset[1] = acc.mParams.size();
  1989. //else
  1990. // DefaultLogger::get()->warn( format() << "Unknown accessor parameter \"" << name << "\". Ignoring data channel." );
  1991. }
  1992. // read data type
  1993. int attrType = TestAttribute("type");
  1994. if (attrType > -1) {
  1995. // for the moment we only distinguish between a 4x4 matrix and anything else.
  1996. // TODO: (thom) I don't have a spec here at work. Check if there are other multi-value types
  1997. // which should be tested for here.
  1998. std::string type = mReader->getAttributeValue(attrType);
  1999. if (type == "float4x4")
  2000. acc.mSize += 16;
  2001. else
  2002. acc.mSize += 1;
  2003. }
  2004. acc.mParams.push_back(name);
  2005. // skip remaining stuff of this element, if any
  2006. SkipElement();
  2007. } else {
  2008. ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <accessor>");
  2009. }
  2010. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  2011. if (strcmp(mReader->getNodeName(), "accessor") != 0)
  2012. ThrowException("Expected end of <accessor> element.");
  2013. break;
  2014. }
  2015. }
  2016. }
  2017. // ------------------------------------------------------------------------------------------------
  2018. // Reads input declarations of per-vertex mesh data into the given mesh
  2019. void ColladaParser::ReadVertexData(XmlNode &node, Mesh &pMesh) {
  2020. // extract the ID of the <vertices> element. Not that we care, but to catch strange referencing schemes we should warn about
  2021. int attrID = GetAttribute("id");
  2022. pMesh.mVertexID = mReader->getAttributeValue(attrID);
  2023. // a number of <input> elements
  2024. while (mReader->read()) {
  2025. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  2026. if (IsElement("input")) {
  2027. ReadInputChannel(pMesh.mPerVertexData);
  2028. } else {
  2029. ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <vertices>");
  2030. }
  2031. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  2032. if (strcmp(mReader->getNodeName(), "vertices") != 0)
  2033. ThrowException("Expected end of <vertices> element.");
  2034. break;
  2035. }
  2036. }
  2037. }
  2038. // ------------------------------------------------------------------------------------------------
  2039. // Reads input declarations of per-index mesh data into the given mesh
  2040. void ColladaParser::ReadIndexData(XmlNode &node, Mesh &pMesh) {
  2041. std::vector<size_t> vcount;
  2042. std::vector<InputChannel> perIndexData;
  2043. // read primitive count from the attribute
  2044. int attrCount = GetAttribute("count");
  2045. size_t numPrimitives = (size_t)mReader->getAttributeValueAsInt(attrCount);
  2046. // some mesh types (e.g. tristrips) don't specify primitive count upfront,
  2047. // so we need to sum up the actual number of primitives while we read the <p>-tags
  2048. size_t actualPrimitives = 0;
  2049. // material subgroup
  2050. int attrMaterial = TestAttribute("material");
  2051. SubMesh subgroup;
  2052. if (attrMaterial > -1)
  2053. subgroup.mMaterial = mReader->getAttributeValue(attrMaterial);
  2054. // distinguish between polys and triangles
  2055. std::string elementName = mReader->getNodeName();
  2056. PrimitiveType primType = Prim_Invalid;
  2057. if (IsElement("lines"))
  2058. primType = Prim_Lines;
  2059. else if (IsElement("linestrips"))
  2060. primType = Prim_LineStrip;
  2061. else if (IsElement("polygons"))
  2062. primType = Prim_Polygon;
  2063. else if (IsElement("polylist"))
  2064. primType = Prim_Polylist;
  2065. else if (IsElement("triangles"))
  2066. primType = Prim_Triangles;
  2067. else if (IsElement("trifans"))
  2068. primType = Prim_TriFans;
  2069. else if (IsElement("tristrips"))
  2070. primType = Prim_TriStrips;
  2071. ai_assert(primType != Prim_Invalid);
  2072. // also a number of <input> elements, but in addition a <p> primitive collection and probably index counts for all primitives
  2073. while (mReader->read()) {
  2074. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  2075. if (IsElement("input")) {
  2076. ReadInputChannel(perIndexData);
  2077. } else if (IsElement("vcount")) {
  2078. if (!mReader->isEmptyElement()) {
  2079. if (numPrimitives) // It is possible to define a mesh without any primitives
  2080. {
  2081. // case <polylist> - specifies the number of indices for each polygon
  2082. const char *content = GetTextContent();
  2083. vcount.reserve(numPrimitives);
  2084. for (unsigned int a = 0; a < numPrimitives; a++) {
  2085. if (*content == 0)
  2086. ThrowException("Expected more values while reading <vcount> contents.");
  2087. // read a number
  2088. vcount.push_back((size_t)strtoul10(content, &content));
  2089. // skip whitespace after it
  2090. SkipSpacesAndLineEnd(&content);
  2091. }
  2092. }
  2093. TestClosing("vcount");
  2094. }
  2095. } else if (IsElement("p")) {
  2096. if (!mReader->isEmptyElement()) {
  2097. // now here the actual fun starts - these are the indices to construct the mesh data from
  2098. actualPrimitives += ReadPrimitives(pMesh, perIndexData, numPrimitives, vcount, primType);
  2099. }
  2100. } else if (IsElement("extra")) {
  2101. SkipElement("extra");
  2102. } else if (IsElement("ph")) {
  2103. SkipElement("ph");
  2104. } else {
  2105. ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <" << elementName << ">");
  2106. }
  2107. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  2108. if (mReader->getNodeName() != elementName)
  2109. ThrowException(format() << "Expected end of <" << elementName << "> element.");
  2110. break;
  2111. }
  2112. }
  2113. #ifdef ASSIMP_BUILD_DEBUG
  2114. if (primType != Prim_TriFans && primType != Prim_TriStrips && primType != Prim_LineStrip &&
  2115. primType != Prim_Lines) { // this is ONLY to workaround a bug in SketchUp 15.3.331 where it writes the wrong 'count' when it writes out the 'lines'.
  2116. ai_assert(actualPrimitives == numPrimitives);
  2117. }
  2118. #endif
  2119. // only when we're done reading all <p> tags (and thus know the final vertex count) can we commit the submesh
  2120. subgroup.mNumFaces = actualPrimitives;
  2121. pMesh.mSubMeshes.push_back(subgroup);
  2122. }
  2123. // ------------------------------------------------------------------------------------------------
  2124. // Reads a single input channel element and stores it in the given array, if valid
  2125. void ColladaParser::ReadInputChannel(XmlNode &node, std::vector<InputChannel> &poChannels) {
  2126. InputChannel channel;
  2127. // read semantic
  2128. int attrSemantic = GetAttribute("semantic");
  2129. std::string semantic = mReader->getAttributeValue(attrSemantic);
  2130. channel.mType = GetTypeForSemantic(semantic);
  2131. // read source
  2132. int attrSource = GetAttribute("source");
  2133. const char *source = mReader->getAttributeValue(attrSource);
  2134. if (source[0] != '#')
  2135. ThrowException(format() << "Unknown reference format in url \"" << source << "\" in source attribute of <input> element.");
  2136. channel.mAccessor = source + 1; // skipping the leading #, hopefully the remaining text is the accessor ID only
  2137. // read index offset, if per-index <input>
  2138. int attrOffset = TestAttribute("offset");
  2139. if (attrOffset > -1)
  2140. channel.mOffset = mReader->getAttributeValueAsInt(attrOffset);
  2141. // read set if texture coordinates
  2142. if (channel.mType == IT_Texcoord || channel.mType == IT_Color) {
  2143. int attrSet = TestAttribute("set");
  2144. if (attrSet > -1) {
  2145. attrSet = mReader->getAttributeValueAsInt(attrSet);
  2146. if (attrSet < 0)
  2147. ThrowException(format() << "Invalid index \"" << (attrSet) << "\" in set attribute of <input> element");
  2148. channel.mIndex = attrSet;
  2149. }
  2150. }
  2151. // store, if valid type
  2152. if (channel.mType != IT_Invalid)
  2153. poChannels.push_back(channel);
  2154. // skip remaining stuff of this element, if any
  2155. SkipElement();
  2156. }
  2157. // ------------------------------------------------------------------------------------------------
  2158. // Reads a <p> primitive index list and assembles the mesh data into the given mesh
  2159. size_t ColladaParser::ReadPrimitives(XmlNode &node, Mesh &pMesh, std::vector<InputChannel> &pPerIndexChannels,
  2160. size_t pNumPrimitives, const std::vector<size_t> &pVCount, PrimitiveType pPrimType) {
  2161. // determine number of indices coming per vertex
  2162. // find the offset index for all per-vertex channels
  2163. size_t numOffsets = 1;
  2164. size_t perVertexOffset = SIZE_MAX; // invalid value
  2165. for (const InputChannel &channel : pPerIndexChannels) {
  2166. numOffsets = std::max(numOffsets, channel.mOffset + 1);
  2167. if (channel.mType == IT_Vertex)
  2168. perVertexOffset = channel.mOffset;
  2169. }
  2170. // determine the expected number of indices
  2171. size_t expectedPointCount = 0;
  2172. switch (pPrimType) {
  2173. case Prim_Polylist: {
  2174. for (size_t i : pVCount)
  2175. expectedPointCount += i;
  2176. break;
  2177. }
  2178. case Prim_Lines:
  2179. expectedPointCount = 2 * pNumPrimitives;
  2180. break;
  2181. case Prim_Triangles:
  2182. expectedPointCount = 3 * pNumPrimitives;
  2183. break;
  2184. default:
  2185. // other primitive types don't state the index count upfront... we need to guess
  2186. break;
  2187. }
  2188. // and read all indices into a temporary array
  2189. std::vector<size_t> indices;
  2190. if (expectedPointCount > 0)
  2191. indices.reserve(expectedPointCount * numOffsets);
  2192. if (pNumPrimitives > 0) // It is possible to not contain any indices
  2193. {
  2194. const char *content = GetTextContent();
  2195. while (*content != 0) {
  2196. // read a value.
  2197. // Hack: (thom) Some exporters put negative indices sometimes. We just try to carry on anyways.
  2198. int value = std::max(0, strtol10(content, &content));
  2199. indices.push_back(size_t(value));
  2200. // skip whitespace after it
  2201. SkipSpacesAndLineEnd(&content);
  2202. }
  2203. }
  2204. // complain if the index count doesn't fit
  2205. if (expectedPointCount > 0 && indices.size() != expectedPointCount * numOffsets) {
  2206. if (pPrimType == Prim_Lines) {
  2207. // HACK: We just fix this number since SketchUp 15.3.331 writes the wrong 'count' for 'lines'
  2208. ReportWarning("Expected different index count in <p> element, %zu instead of %zu.", indices.size(), expectedPointCount * numOffsets);
  2209. pNumPrimitives = (indices.size() / numOffsets) / 2;
  2210. } else
  2211. ThrowException("Expected different index count in <p> element.");
  2212. } else if (expectedPointCount == 0 && (indices.size() % numOffsets) != 0)
  2213. ThrowException("Expected different index count in <p> element.");
  2214. // find the data for all sources
  2215. for (std::vector<InputChannel>::iterator it = pMesh.mPerVertexData.begin(); it != pMesh.mPerVertexData.end(); ++it) {
  2216. InputChannel &input = *it;
  2217. if (input.mResolved)
  2218. continue;
  2219. // find accessor
  2220. input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor);
  2221. // resolve accessor's data pointer as well, if necessary
  2222. const Accessor *acc = input.mResolved;
  2223. if (!acc->mData)
  2224. acc->mData = &ResolveLibraryReference(mDataLibrary, acc->mSource);
  2225. }
  2226. // and the same for the per-index channels
  2227. for (std::vector<InputChannel>::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it) {
  2228. InputChannel &input = *it;
  2229. if (input.mResolved)
  2230. continue;
  2231. // ignore vertex pointer, it doesn't refer to an accessor
  2232. if (input.mType == IT_Vertex) {
  2233. // warn if the vertex channel does not refer to the <vertices> element in the same mesh
  2234. if (input.mAccessor != pMesh.mVertexID)
  2235. ThrowException("Unsupported vertex referencing scheme.");
  2236. continue;
  2237. }
  2238. // find accessor
  2239. input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor);
  2240. // resolve accessor's data pointer as well, if necessary
  2241. const Accessor *acc = input.mResolved;
  2242. if (!acc->mData)
  2243. acc->mData = &ResolveLibraryReference(mDataLibrary, acc->mSource);
  2244. }
  2245. // For continued primitives, the given count does not come all in one <p>, but only one primitive per <p>
  2246. size_t numPrimitives = pNumPrimitives;
  2247. if (pPrimType == Prim_TriFans || pPrimType == Prim_Polygon)
  2248. numPrimitives = 1;
  2249. // For continued primitives, the given count is actually the number of <p>'s inside the parent tag
  2250. if (pPrimType == Prim_TriStrips) {
  2251. size_t numberOfVertices = indices.size() / numOffsets;
  2252. numPrimitives = numberOfVertices - 2;
  2253. }
  2254. if (pPrimType == Prim_LineStrip) {
  2255. size_t numberOfVertices = indices.size() / numOffsets;
  2256. numPrimitives = numberOfVertices - 1;
  2257. }
  2258. pMesh.mFaceSize.reserve(numPrimitives);
  2259. pMesh.mFacePosIndices.reserve(indices.size() / numOffsets);
  2260. size_t polylistStartVertex = 0;
  2261. for (size_t currentPrimitive = 0; currentPrimitive < numPrimitives; currentPrimitive++) {
  2262. // determine number of points for this primitive
  2263. size_t numPoints = 0;
  2264. switch (pPrimType) {
  2265. case Prim_Lines:
  2266. numPoints = 2;
  2267. for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
  2268. CopyVertex(currentVertex, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2269. break;
  2270. case Prim_LineStrip:
  2271. numPoints = 2;
  2272. for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
  2273. CopyVertex(currentVertex, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2274. break;
  2275. case Prim_Triangles:
  2276. numPoints = 3;
  2277. for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
  2278. CopyVertex(currentVertex, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2279. break;
  2280. case Prim_TriStrips:
  2281. numPoints = 3;
  2282. ReadPrimTriStrips(numOffsets, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2283. break;
  2284. case Prim_Polylist:
  2285. numPoints = pVCount[currentPrimitive];
  2286. for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
  2287. CopyVertex(polylistStartVertex + currentVertex, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, 0, indices);
  2288. polylistStartVertex += numPoints;
  2289. break;
  2290. case Prim_TriFans:
  2291. case Prim_Polygon:
  2292. numPoints = indices.size() / numOffsets;
  2293. for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
  2294. CopyVertex(currentVertex, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2295. break;
  2296. default:
  2297. // LineStrip is not supported due to expected index unmangling
  2298. ThrowException("Unsupported primitive type.");
  2299. break;
  2300. }
  2301. // store the face size to later reconstruct the face from
  2302. pMesh.mFaceSize.push_back(numPoints);
  2303. }
  2304. // if I ever get my hands on that guy who invented this steaming pile of indirection...
  2305. TestClosing("p");
  2306. return numPrimitives;
  2307. }
  2308. ///@note This function won't work correctly if both PerIndex and PerVertex channels have same channels.
  2309. ///For example if TEXCOORD present in both <vertices> and <polylist> tags this function will create wrong uv coordinates.
  2310. ///It's not clear from COLLADA documentation is this allowed or not. For now only exporter fixed to avoid such behavior
  2311. void ColladaParser::CopyVertex(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset, Mesh &pMesh,
  2312. std::vector<InputChannel> &pPerIndexChannels, size_t currentPrimitive, const std::vector<size_t> &indices) {
  2313. // calculate the base offset of the vertex whose attributes we ant to copy
  2314. size_t baseOffset = currentPrimitive * numOffsets * numPoints + currentVertex * numOffsets;
  2315. // don't overrun the boundaries of the index list
  2316. ai_assert((baseOffset + numOffsets - 1) < indices.size());
  2317. // extract per-vertex channels using the global per-vertex offset
  2318. for (std::vector<InputChannel>::iterator it = pMesh.mPerVertexData.begin(); it != pMesh.mPerVertexData.end(); ++it)
  2319. ExtractDataObjectFromChannel(*it, indices[baseOffset + perVertexOffset], pMesh);
  2320. // and extract per-index channels using there specified offset
  2321. for (std::vector<InputChannel>::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it)
  2322. ExtractDataObjectFromChannel(*it, indices[baseOffset + it->mOffset], pMesh);
  2323. // store the vertex-data index for later assignment of bone vertex weights
  2324. pMesh.mFacePosIndices.push_back(indices[baseOffset + perVertexOffset]);
  2325. }
  2326. void ColladaParser::ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, Mesh &pMesh, std::vector<InputChannel> &pPerIndexChannels,
  2327. size_t currentPrimitive, const std::vector<size_t> &indices) {
  2328. if (currentPrimitive % 2 != 0) {
  2329. //odd tristrip triangles need their indices mangled, to preserve winding direction
  2330. CopyVertex(1, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2331. CopyVertex(0, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2332. CopyVertex(2, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2333. } else { //for non tristrips or even tristrip triangles
  2334. CopyVertex(0, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2335. CopyVertex(1, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2336. CopyVertex(2, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
  2337. }
  2338. }
  2339. // ------------------------------------------------------------------------------------------------
  2340. // Extracts a single object from an input channel and stores it in the appropriate mesh data array
  2341. void ColladaParser::ExtractDataObjectFromChannel(const InputChannel &pInput, size_t pLocalIndex, Mesh &pMesh) {
  2342. // ignore vertex referrer - we handle them that separate
  2343. if (pInput.mType == IT_Vertex)
  2344. return;
  2345. const Accessor &acc = *pInput.mResolved;
  2346. if (pLocalIndex >= acc.mCount)
  2347. ThrowException(format() << "Invalid data index (" << pLocalIndex << "/" << acc.mCount << ") in primitive specification");
  2348. // get a pointer to the start of the data object referred to by the accessor and the local index
  2349. const ai_real *dataObject = &(acc.mData->mValues[0]) + acc.mOffset + pLocalIndex * acc.mStride;
  2350. // assemble according to the accessors component sub-offset list. We don't care, yet,
  2351. // what kind of object exactly we're extracting here
  2352. ai_real obj[4];
  2353. for (size_t c = 0; c < 4; ++c)
  2354. obj[c] = dataObject[acc.mSubOffset[c]];
  2355. // now we reinterpret it according to the type we're reading here
  2356. switch (pInput.mType) {
  2357. case IT_Position: // ignore all position streams except 0 - there can be only one position
  2358. if (pInput.mIndex == 0)
  2359. pMesh.mPositions.push_back(aiVector3D(obj[0], obj[1], obj[2]));
  2360. else
  2361. ASSIMP_LOG_ERROR("Collada: just one vertex position stream supported");
  2362. break;
  2363. case IT_Normal:
  2364. // pad to current vertex count if necessary
  2365. if (pMesh.mNormals.size() < pMesh.mPositions.size() - 1)
  2366. pMesh.mNormals.insert(pMesh.mNormals.end(), pMesh.mPositions.size() - pMesh.mNormals.size() - 1, aiVector3D(0, 1, 0));
  2367. // ignore all normal streams except 0 - there can be only one normal
  2368. if (pInput.mIndex == 0)
  2369. pMesh.mNormals.push_back(aiVector3D(obj[0], obj[1], obj[2]));
  2370. else
  2371. ASSIMP_LOG_ERROR("Collada: just one vertex normal stream supported");
  2372. break;
  2373. case IT_Tangent:
  2374. // pad to current vertex count if necessary
  2375. if (pMesh.mTangents.size() < pMesh.mPositions.size() - 1)
  2376. pMesh.mTangents.insert(pMesh.mTangents.end(), pMesh.mPositions.size() - pMesh.mTangents.size() - 1, aiVector3D(1, 0, 0));
  2377. // ignore all tangent streams except 0 - there can be only one tangent
  2378. if (pInput.mIndex == 0)
  2379. pMesh.mTangents.push_back(aiVector3D(obj[0], obj[1], obj[2]));
  2380. else
  2381. ASSIMP_LOG_ERROR("Collada: just one vertex tangent stream supported");
  2382. break;
  2383. case IT_Bitangent:
  2384. // pad to current vertex count if necessary
  2385. if (pMesh.mBitangents.size() < pMesh.mPositions.size() - 1)
  2386. pMesh.mBitangents.insert(pMesh.mBitangents.end(), pMesh.mPositions.size() - pMesh.mBitangents.size() - 1, aiVector3D(0, 0, 1));
  2387. // ignore all bitangent streams except 0 - there can be only one bitangent
  2388. if (pInput.mIndex == 0)
  2389. pMesh.mBitangents.push_back(aiVector3D(obj[0], obj[1], obj[2]));
  2390. else
  2391. ASSIMP_LOG_ERROR("Collada: just one vertex bitangent stream supported");
  2392. break;
  2393. case IT_Texcoord:
  2394. // up to 4 texture coord sets are fine, ignore the others
  2395. if (pInput.mIndex < AI_MAX_NUMBER_OF_TEXTURECOORDS) {
  2396. // pad to current vertex count if necessary
  2397. if (pMesh.mTexCoords[pInput.mIndex].size() < pMesh.mPositions.size() - 1)
  2398. pMesh.mTexCoords[pInput.mIndex].insert(pMesh.mTexCoords[pInput.mIndex].end(),
  2399. pMesh.mPositions.size() - pMesh.mTexCoords[pInput.mIndex].size() - 1, aiVector3D(0, 0, 0));
  2400. pMesh.mTexCoords[pInput.mIndex].push_back(aiVector3D(obj[0], obj[1], obj[2]));
  2401. if (0 != acc.mSubOffset[2] || 0 != acc.mSubOffset[3]) /* hack ... consider cleaner solution */
  2402. pMesh.mNumUVComponents[pInput.mIndex] = 3;
  2403. } else {
  2404. ASSIMP_LOG_ERROR("Collada: too many texture coordinate sets. Skipping.");
  2405. }
  2406. break;
  2407. case IT_Color:
  2408. // up to 4 color sets are fine, ignore the others
  2409. if (pInput.mIndex < AI_MAX_NUMBER_OF_COLOR_SETS) {
  2410. // pad to current vertex count if necessary
  2411. if (pMesh.mColors[pInput.mIndex].size() < pMesh.mPositions.size() - 1)
  2412. pMesh.mColors[pInput.mIndex].insert(pMesh.mColors[pInput.mIndex].end(),
  2413. pMesh.mPositions.size() - pMesh.mColors[pInput.mIndex].size() - 1, aiColor4D(0, 0, 0, 1));
  2414. aiColor4D result(0, 0, 0, 1);
  2415. for (size_t i = 0; i < pInput.mResolved->mSize; ++i) {
  2416. result[static_cast<unsigned int>(i)] = obj[pInput.mResolved->mSubOffset[i]];
  2417. }
  2418. pMesh.mColors[pInput.mIndex].push_back(result);
  2419. } else {
  2420. ASSIMP_LOG_ERROR("Collada: too many vertex color sets. Skipping.");
  2421. }
  2422. break;
  2423. default:
  2424. // IT_Invalid and IT_Vertex
  2425. ai_assert(false && "shouldn't ever get here");
  2426. }
  2427. }
  2428. // ------------------------------------------------------------------------------------------------
  2429. // Reads the library of node hierarchies and scene parts
  2430. void ColladaParser::ReadSceneLibrary(XmlNode &node) {
  2431. if (node.empty()) {
  2432. return;
  2433. }
  2434. /*if (mReader->isEmptyElement())
  2435. return;*/
  2436. while (mReader->read()) {
  2437. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  2438. // a visual scene - generate root node under its ID and let ReadNode() do the recursive work
  2439. if (IsElement("visual_scene")) {
  2440. // read ID. Is optional according to the spec, but how on earth should a scene_instance refer to it then?
  2441. int indexID = GetAttribute("id");
  2442. const char *attrID = mReader->getAttributeValue(indexID);
  2443. // read name if given.
  2444. int indexName = TestAttribute("name");
  2445. const char *attrName = "Scene";
  2446. if (indexName > -1)
  2447. attrName = mReader->getAttributeValue(indexName);
  2448. // create a node and store it in the library under its ID
  2449. Node *node = new Node;
  2450. node->mID = attrID;
  2451. node->mName = attrName;
  2452. mNodeLibrary[node->mID] = node;
  2453. ReadSceneNode(node);
  2454. } else {
  2455. // ignore the rest
  2456. SkipElement();
  2457. }
  2458. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  2459. if (strcmp(mReader->getNodeName(), "library_visual_scenes") == 0)
  2460. //ThrowException( "Expected end of \"library_visual_scenes\" element.");
  2461. break;
  2462. }
  2463. }
  2464. }
  2465. // ------------------------------------------------------------------------------------------------
  2466. // Reads a scene node's contents including children and stores it in the given node
  2467. void ColladaParser::ReadSceneNode(XmlNode &node, Node *pNode) {
  2468. // quit immediately on <bla/> elements
  2469. if (node.empty()) {
  2470. return;
  2471. }
  2472. /* if (mReader->isEmptyElement())
  2473. return;*/
  2474. while (mReader->read()) {
  2475. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  2476. if (IsElement("node")) {
  2477. Node *child = new Node;
  2478. int attrID = TestAttribute("id");
  2479. if (attrID > -1)
  2480. child->mID = mReader->getAttributeValue(attrID);
  2481. int attrSID = TestAttribute("sid");
  2482. if (attrSID > -1)
  2483. child->mSID = mReader->getAttributeValue(attrSID);
  2484. int attrName = TestAttribute("name");
  2485. if (attrName > -1)
  2486. child->mName = mReader->getAttributeValue(attrName);
  2487. if (pNode) {
  2488. pNode->mChildren.push_back(child);
  2489. child->mParent = pNode;
  2490. } else {
  2491. // no parent node given, probably called from <library_nodes> element.
  2492. // create new node in node library
  2493. mNodeLibrary[child->mID] = child;
  2494. }
  2495. // read on recursively from there
  2496. ReadSceneNode(child);
  2497. continue;
  2498. }
  2499. // For any further stuff we need a valid node to work on
  2500. else if (!pNode)
  2501. continue;
  2502. if (IsElement("lookat"))
  2503. ReadNodeTransformation(pNode, TF_LOOKAT);
  2504. else if (IsElement("matrix"))
  2505. ReadNodeTransformation(pNode, TF_MATRIX);
  2506. else if (IsElement("rotate"))
  2507. ReadNodeTransformation(pNode, TF_ROTATE);
  2508. else if (IsElement("scale"))
  2509. ReadNodeTransformation(pNode, TF_SCALE);
  2510. else if (IsElement("skew"))
  2511. ReadNodeTransformation(pNode, TF_SKEW);
  2512. else if (IsElement("translate"))
  2513. ReadNodeTransformation(pNode, TF_TRANSLATE);
  2514. else if (IsElement("render") && pNode->mParent == nullptr && 0 == pNode->mPrimaryCamera.length()) {
  2515. // ... scene evaluation or, in other words, postprocessing pipeline,
  2516. // or, again in other words, a turing-complete description how to
  2517. // render a Collada scene. The only thing that is interesting for
  2518. // us is the primary camera.
  2519. int attrId = TestAttribute("camera_node");
  2520. if (-1 != attrId) {
  2521. const char *s = mReader->getAttributeValue(attrId);
  2522. if (s[0] != '#')
  2523. ASSIMP_LOG_ERROR("Collada: Unresolved reference format of camera");
  2524. else
  2525. pNode->mPrimaryCamera = s + 1;
  2526. }
  2527. } else if (IsElement("instance_node")) {
  2528. // find the node in the library
  2529. int attrID = TestAttribute("url");
  2530. if (attrID != -1) {
  2531. const char *s = mReader->getAttributeValue(attrID);
  2532. if (s[0] != '#')
  2533. ASSIMP_LOG_ERROR("Collada: Unresolved reference format of node");
  2534. else {
  2535. pNode->mNodeInstances.push_back(NodeInstance());
  2536. pNode->mNodeInstances.back().mNode = s + 1;
  2537. }
  2538. }
  2539. } else if (IsElement("instance_geometry") || IsElement("instance_controller")) {
  2540. // Reference to a mesh or controller, with possible material associations
  2541. ReadNodeGeometry(pNode);
  2542. } else if (IsElement("instance_light")) {
  2543. // Reference to a light, name given in 'url' attribute
  2544. int attrID = TestAttribute("url");
  2545. if (-1 == attrID)
  2546. ASSIMP_LOG_WARN("Collada: Expected url attribute in <instance_light> element");
  2547. else {
  2548. const char *url = mReader->getAttributeValue(attrID);
  2549. if (url[0] != '#')
  2550. ThrowException("Unknown reference format in <instance_light> element");
  2551. pNode->mLights.push_back(LightInstance());
  2552. pNode->mLights.back().mLight = url + 1;
  2553. }
  2554. } else if (IsElement("instance_camera")) {
  2555. // Reference to a camera, name given in 'url' attribute
  2556. int attrID = TestAttribute("url");
  2557. if (-1 == attrID)
  2558. ASSIMP_LOG_WARN("Collada: Expected url attribute in <instance_camera> element");
  2559. else {
  2560. const char *url = mReader->getAttributeValue(attrID);
  2561. if (url[0] != '#')
  2562. ThrowException("Unknown reference format in <instance_camera> element");
  2563. pNode->mCameras.push_back(CameraInstance());
  2564. pNode->mCameras.back().mCamera = url + 1;
  2565. }
  2566. } else {
  2567. // skip everything else for the moment
  2568. SkipElement();
  2569. }
  2570. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  2571. break;
  2572. }
  2573. }
  2574. }
  2575. // ------------------------------------------------------------------------------------------------
  2576. // Reads a node transformation entry of the given type and adds it to the given node's transformation list.
  2577. void ColladaParser::ReadNodeTransformation(XmlNode &node, Node *pNode, TransformType pType) {
  2578. if (node.empty()) {
  2579. return;
  2580. }
  2581. /*if (mReader->isEmptyElement())
  2582. return;*/
  2583. std::string tagName = mReader->getNodeName();
  2584. Transform tf;
  2585. tf.mType = pType;
  2586. // read SID
  2587. int indexSID = TestAttribute("sid");
  2588. if (indexSID >= 0)
  2589. tf.mID = mReader->getAttributeValue(indexSID);
  2590. // how many parameters to read per transformation type
  2591. static const unsigned int sNumParameters[] = { 9, 4, 3, 3, 7, 16 };
  2592. const char *content = GetTextContent();
  2593. // read as many parameters and store in the transformation
  2594. for (unsigned int a = 0; a < sNumParameters[pType]; a++) {
  2595. // read a number
  2596. content = fast_atoreal_move<ai_real>(content, tf.f[a]);
  2597. // skip whitespace after it
  2598. SkipSpacesAndLineEnd(&content);
  2599. }
  2600. // place the transformation at the queue of the node
  2601. pNode->mTransforms.push_back(tf);
  2602. // and consume the closing tag
  2603. TestClosing(tagName.c_str());
  2604. }
  2605. // ------------------------------------------------------------------------------------------------
  2606. // Processes bind_vertex_input and bind elements
  2607. void ColladaParser::ReadMaterialVertexInputBinding(XmlNode &node, Collada::SemanticMappingTable &tbl) {
  2608. while (mReader->read()) {
  2609. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  2610. if (IsElement("bind_vertex_input")) {
  2611. Collada::InputSemanticMapEntry vn;
  2612. // effect semantic
  2613. int n = GetAttribute("semantic");
  2614. std::string s = mReader->getAttributeValue(n);
  2615. // input semantic
  2616. n = GetAttribute("input_semantic");
  2617. vn.mType = GetTypeForSemantic(mReader->getAttributeValue(n));
  2618. // index of input set
  2619. n = TestAttribute("input_set");
  2620. if (-1 != n)
  2621. vn.mSet = mReader->getAttributeValueAsInt(n);
  2622. tbl.mMap[s] = vn;
  2623. } else if (IsElement("bind")) {
  2624. ASSIMP_LOG_WARN("Collada: Found unsupported <bind> element");
  2625. }
  2626. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  2627. if (strcmp(mReader->getNodeName(), "instance_material") == 0)
  2628. break;
  2629. }
  2630. }
  2631. }
  2632. void ColladaParser::ReadEmbeddedTextures(ZipArchiveIOSystem &zip_archive) {
  2633. // Attempt to load any undefined Collada::Image in ImageLibrary
  2634. for (ImageLibrary::iterator it = mImageLibrary.begin(); it != mImageLibrary.end(); ++it) {
  2635. Collada::Image &image = (*it).second;
  2636. if (image.mImageData.empty()) {
  2637. std::unique_ptr<IOStream> image_file(zip_archive.Open(image.mFileName.c_str()));
  2638. if (image_file) {
  2639. image.mImageData.resize(image_file->FileSize());
  2640. image_file->Read(image.mImageData.data(), image_file->FileSize(), 1);
  2641. image.mEmbeddedFormat = BaseImporter::GetExtension(image.mFileName);
  2642. if (image.mEmbeddedFormat == "jpeg") {
  2643. image.mEmbeddedFormat = "jpg";
  2644. }
  2645. }
  2646. }
  2647. }
  2648. }
  2649. // ------------------------------------------------------------------------------------------------
  2650. // Reads a mesh reference in a node and adds it to the node's mesh list
  2651. void ColladaParser::ReadNodeGeometry(XmlNode &node, Node *pNode) {
  2652. // referred mesh is given as an attribute of the <instance_geometry> element
  2653. int attrUrl = GetAttribute("url");
  2654. const char *url = mReader->getAttributeValue(attrUrl);
  2655. if (url[0] != '#')
  2656. ThrowException("Unknown reference format");
  2657. Collada::MeshInstance instance;
  2658. instance.mMeshOrController = url + 1; // skipping the leading #
  2659. if (!mReader->isEmptyElement()) {
  2660. // read material associations. Ignore additional elements in between
  2661. while (mReader->read()) {
  2662. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  2663. if (IsElement("instance_material")) {
  2664. // read ID of the geometry subgroup and the target material
  2665. int attrGroup = GetAttribute("symbol");
  2666. std::string group = mReader->getAttributeValue(attrGroup);
  2667. int attrMaterial = GetAttribute("target");
  2668. const char *urlMat = mReader->getAttributeValue(attrMaterial);
  2669. Collada::SemanticMappingTable s;
  2670. if (urlMat[0] == '#')
  2671. urlMat++;
  2672. s.mMatName = urlMat;
  2673. // resolve further material details + THIS UGLY AND NASTY semantic mapping stuff
  2674. if (!mReader->isEmptyElement())
  2675. ReadMaterialVertexInputBinding(s);
  2676. // store the association
  2677. instance.mMaterials[group] = s;
  2678. }
  2679. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  2680. if (strcmp(mReader->getNodeName(), "instance_geometry") == 0 || strcmp(mReader->getNodeName(), "instance_controller") == 0)
  2681. break;
  2682. }
  2683. }
  2684. }
  2685. // store it
  2686. pNode->mMeshes.push_back(instance);
  2687. }
  2688. // ------------------------------------------------------------------------------------------------
  2689. // Reads the collada scene
  2690. void ColladaParser::ReadScene(XmlNode &node) {
  2691. if (node.empty()) {
  2692. return;
  2693. }
  2694. /*if (mReader->isEmptyElement())
  2695. return;*/
  2696. while (mReader->read()) {
  2697. if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
  2698. if (IsElement("instance_visual_scene")) {
  2699. // should be the first and only occurrence
  2700. if (mRootNode)
  2701. ThrowException("Invalid scene containing multiple root nodes in <instance_visual_scene> element");
  2702. // read the url of the scene to instance. Should be of format "#some_name"
  2703. int urlIndex = GetAttribute("url");
  2704. const char *url = mReader->getAttributeValue(urlIndex);
  2705. if (url[0] != '#')
  2706. ThrowException("Unknown reference format in <instance_visual_scene> element");
  2707. // find the referred scene, skip the leading #
  2708. NodeLibrary::const_iterator sit = mNodeLibrary.find(url + 1);
  2709. if (sit == mNodeLibrary.end())
  2710. ThrowException("Unable to resolve visual_scene reference \"" + std::string(url) + "\" in <instance_visual_scene> element.");
  2711. mRootNode = sit->second;
  2712. } else {
  2713. SkipElement();
  2714. }
  2715. } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  2716. break;
  2717. }
  2718. }
  2719. }
  2720. // ------------------------------------------------------------------------------------------------
  2721. // Aborts the file reading with an exception
  2722. AI_WONT_RETURN void ColladaParser::ThrowException(const std::string &pError) const {
  2723. throw DeadlyImportError(format() << "Collada: " << mFileName << " - " << pError);
  2724. }
  2725. void ColladaParser::ReportWarning(const char *msg, ...) {
  2726. ai_assert(nullptr != msg);
  2727. va_list args;
  2728. va_start(args, msg);
  2729. char szBuffer[3000];
  2730. const int iLen = vsprintf(szBuffer, msg, args);
  2731. ai_assert(iLen > 0);
  2732. va_end(args);
  2733. ASSIMP_LOG_WARN_F("Validation warning: ", std::string(szBuffer, iLen));
  2734. }
  2735. // ------------------------------------------------------------------------------------------------
  2736. // Skips all data until the end node of the current element
  2737. /*void ColladaParser::SkipElement() {
  2738. // nothing to skip if it's an <element />
  2739. if (mReader->isEmptyElement()) {
  2740. return;
  2741. }
  2742. // reroute
  2743. SkipElement(mReader->getNodeName());
  2744. }*/
  2745. // ------------------------------------------------------------------------------------------------
  2746. // Skips all data until the end node of the given element
  2747. /*void ColladaParser::SkipElement(const char *pElement) {
  2748. // copy the current node's name because it'a pointer to the reader's internal buffer,
  2749. // which is going to change with the upcoming parsing
  2750. std::string element = pElement;
  2751. while (mReader->read()) {
  2752. if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
  2753. if (mReader->getNodeName() == element) {
  2754. break;
  2755. }
  2756. }
  2757. }
  2758. }*/
  2759. // ------------------------------------------------------------------------------------------------
  2760. // Tests for an opening element of the given name, throws an exception if not found
  2761. /*void ColladaParser::TestOpening(const char *pName) {
  2762. // read element start
  2763. if (!mReader->read()) {
  2764. ThrowException(format() << "Unexpected end of file while beginning of <" << pName << "> element.");
  2765. }
  2766. // whitespace in front is ok, just read again if found
  2767. if (mReader->getNodeType() == irr::io::EXN_TEXT) {
  2768. if (!mReader->read()) {
  2769. ThrowException(format() << "Unexpected end of file while reading beginning of <" << pName << "> element.");
  2770. }
  2771. }
  2772. if (mReader->getNodeType() != irr::io::EXN_ELEMENT || strcmp(mReader->getNodeName(), pName) != 0) {
  2773. ThrowException(format() << "Expected start of <" << pName << "> element.");
  2774. }
  2775. }*/
  2776. // ------------------------------------------------------------------------------------------------
  2777. // Tests for the closing tag of the given element, throws an exception if not found
  2778. /*void ColladaParser::TestClosing(const char *pName) {
  2779. // check if we have an empty (self-closing) element
  2780. if (mReader->isEmptyElement()) {
  2781. return;
  2782. }
  2783. // check if we're already on the closing tag and return right away
  2784. if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END && strcmp(mReader->getNodeName(), pName) == 0) {
  2785. return;
  2786. }
  2787. // if not, read some more
  2788. if (!mReader->read()) {
  2789. ThrowException(format() << "Unexpected end of file while reading end of <" << pName << "> element.");
  2790. }
  2791. // whitespace in front is ok, just read again if found
  2792. if (mReader->getNodeType() == irr::io::EXN_TEXT) {
  2793. if (!mReader->read()) {
  2794. ThrowException(format() << "Unexpected end of file while reading end of <" << pName << "> element.");
  2795. }
  2796. }
  2797. // but this has the be the closing tag, or we're lost
  2798. if (mReader->getNodeType() != irr::io::EXN_ELEMENT_END || strcmp(mReader->getNodeName(), pName) != 0) {
  2799. ThrowException(format() << "Expected end of <" << pName << "> element.");
  2800. }
  2801. }*/
  2802. // ------------------------------------------------------------------------------------------------
  2803. // Returns the index of the named attribute or -1 if not found. Does not throw, therefore useful for optional attributes
  2804. int ColladaParser::GetAttribute(const char *pAttr) const {
  2805. int index = TestAttribute(pAttr);
  2806. if (index == -1) {
  2807. ThrowException(format() << "Expected attribute \"" << pAttr << "\" for element <" << mReader->getNodeName() << ">.");
  2808. }
  2809. // attribute not found -> throw an exception
  2810. return index;
  2811. }
  2812. // ------------------------------------------------------------------------------------------------
  2813. // Tests the present element for the presence of one attribute, returns its index or throws an exception if not found
  2814. int ColladaParser::TestAttribute(const char *pAttr) const {
  2815. for (int a = 0; a < mReader->getAttributeCount(); a++)
  2816. if (strcmp(mReader->getAttributeName(a), pAttr) == 0)
  2817. return a;
  2818. return -1;
  2819. }
  2820. // ------------------------------------------------------------------------------------------------
  2821. // Reads the text contents of an element, throws an exception if not given. Skips leading whitespace.
  2822. const char *ColladaParser::GetTextContent() {
  2823. const char *sz = TestTextContent();
  2824. if (!sz) {
  2825. ThrowException("Invalid contents in element \"n\".");
  2826. }
  2827. return sz;
  2828. }
  2829. // ------------------------------------------------------------------------------------------------
  2830. // Reads the text contents of an element, returns nullptr if not given. Skips leading whitespace.
  2831. const char *ColladaParser::TestTextContent() {
  2832. // present node should be the beginning of an element
  2833. if (mReader->getNodeType() != irr::io::EXN_ELEMENT || mReader->isEmptyElement())
  2834. return nullptr;
  2835. // read contents of the element
  2836. if (!mReader->read()) {
  2837. return nullptr;
  2838. }
  2839. if (mReader->getNodeType() != irr::io::EXN_TEXT && mReader->getNodeType() != irr::io::EXN_CDATA) {
  2840. return nullptr;
  2841. }
  2842. // skip leading whitespace
  2843. const char *text = mReader->getNodeData();
  2844. SkipSpacesAndLineEnd(&text);
  2845. return text;
  2846. }
  2847. // ------------------------------------------------------------------------------------------------
  2848. // Calculates the resulting transformation from all the given transform steps
  2849. aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector<Transform> &pTransforms) const {
  2850. aiMatrix4x4 res;
  2851. for (std::vector<Transform>::const_iterator it = pTransforms.begin(); it != pTransforms.end(); ++it) {
  2852. const Transform &tf = *it;
  2853. switch (tf.mType) {
  2854. case TF_LOOKAT: {
  2855. aiVector3D pos(tf.f[0], tf.f[1], tf.f[2]);
  2856. aiVector3D dstPos(tf.f[3], tf.f[4], tf.f[5]);
  2857. aiVector3D up = aiVector3D(tf.f[6], tf.f[7], tf.f[8]).Normalize();
  2858. aiVector3D dir = aiVector3D(dstPos - pos).Normalize();
  2859. aiVector3D right = (dir ^ up).Normalize();
  2860. res *= aiMatrix4x4(
  2861. right.x, up.x, -dir.x, pos.x,
  2862. right.y, up.y, -dir.y, pos.y,
  2863. right.z, up.z, -dir.z, pos.z,
  2864. 0, 0, 0, 1);
  2865. break;
  2866. }
  2867. case TF_ROTATE: {
  2868. aiMatrix4x4 rot;
  2869. ai_real angle = tf.f[3] * ai_real(AI_MATH_PI) / ai_real(180.0);
  2870. aiVector3D axis(tf.f[0], tf.f[1], tf.f[2]);
  2871. aiMatrix4x4::Rotation(angle, axis, rot);
  2872. res *= rot;
  2873. break;
  2874. }
  2875. case TF_TRANSLATE: {
  2876. aiMatrix4x4 trans;
  2877. aiMatrix4x4::Translation(aiVector3D(tf.f[0], tf.f[1], tf.f[2]), trans);
  2878. res *= trans;
  2879. break;
  2880. }
  2881. case TF_SCALE: {
  2882. aiMatrix4x4 scale(tf.f[0], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[1], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[2], 0.0f,
  2883. 0.0f, 0.0f, 0.0f, 1.0f);
  2884. res *= scale;
  2885. break;
  2886. }
  2887. case TF_SKEW:
  2888. // TODO: (thom)
  2889. ai_assert(false);
  2890. break;
  2891. case TF_MATRIX: {
  2892. aiMatrix4x4 mat(tf.f[0], tf.f[1], tf.f[2], tf.f[3], tf.f[4], tf.f[5], tf.f[6], tf.f[7],
  2893. tf.f[8], tf.f[9], tf.f[10], tf.f[11], tf.f[12], tf.f[13], tf.f[14], tf.f[15]);
  2894. res *= mat;
  2895. break;
  2896. }
  2897. default:
  2898. ai_assert(false);
  2899. break;
  2900. }
  2901. }
  2902. return res;
  2903. }
  2904. // ------------------------------------------------------------------------------------------------
  2905. // Determines the input data type for the given semantic string
  2906. Collada::InputType ColladaParser::GetTypeForSemantic(const std::string &semantic) {
  2907. if (semantic.empty()) {
  2908. ASSIMP_LOG_WARN("Vertex input type is empty.");
  2909. return IT_Invalid;
  2910. }
  2911. if (semantic == "POSITION")
  2912. return IT_Position;
  2913. else if (semantic == "TEXCOORD")
  2914. return IT_Texcoord;
  2915. else if (semantic == "NORMAL")
  2916. return IT_Normal;
  2917. else if (semantic == "COLOR")
  2918. return IT_Color;
  2919. else if (semantic == "VERTEX")
  2920. return IT_Vertex;
  2921. else if (semantic == "BINORMAL" || semantic == "TEXBINORMAL")
  2922. return IT_Bitangent;
  2923. else if (semantic == "TANGENT" || semantic == "TEXTANGENT")
  2924. return IT_Tangent;
  2925. ASSIMP_LOG_WARN_F("Unknown vertex input type \"", semantic, "\". Ignoring.");
  2926. return IT_Invalid;
  2927. }
  2928. #endif // !! ASSIMP_BUILD_NO_DAE_IMPORTER