ColladaParser.cpp 114 KB

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