ColladaParser.cpp 113 KB

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