ColladaParser.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /** Implementation of the Collada parser helper*/
  2. /*
  3. ---------------------------------------------------------------------------
  4. Open Asset Import Library (ASSIMP)
  5. ---------------------------------------------------------------------------
  6. Copyright (c) 2006-2008, ASSIMP Development Team
  7. All rights reserved.
  8. Redistribution and use of this software in source and binary forms,
  9. with or without modification, are permitted provided that the following
  10. conditions are met:
  11. * Redistributions of source code must retain the above
  12. copyright notice, this list of conditions and the
  13. following disclaimer.
  14. * Redistributions in binary form must reproduce the above
  15. copyright notice, this list of conditions and the
  16. following disclaimer in the documentation and/or other
  17. materials provided with the distribution.
  18. * Neither the name of the ASSIMP team, nor the names of its
  19. contributors may be used to endorse or promote products
  20. derived from this software without specific prior
  21. written permission of the ASSIMP Development Team.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. ---------------------------------------------------------------------------
  34. */
  35. #include "AssimpPCH.h"
  36. #include "ColladaParser.h"
  37. #include "fast_atof.h"
  38. #include "ParsingUtils.h"
  39. using namespace Assimp;
  40. using namespace Assimp::Collada;
  41. // ------------------------------------------------------------------------------------------------
  42. // Constructor to be privately used by Importer
  43. ColladaParser::ColladaParser( const std::string& pFile)
  44. : mFileName( pFile)
  45. {
  46. mRootNode = NULL;
  47. mUnitSize = 1.0f;
  48. mUpDirection = UP_Z;
  49. // generate a XML reader for it
  50. mReader = irr::io::createIrrXMLReader( pFile.c_str());
  51. if( !mReader)
  52. ThrowException( "Unable to open file.");
  53. // start reading
  54. ReadContents();
  55. }
  56. // ------------------------------------------------------------------------------------------------
  57. // Destructor, private as well
  58. ColladaParser::~ColladaParser()
  59. {
  60. delete mReader;
  61. for( NodeLibrary::iterator it = mNodeLibrary.begin(); it != mNodeLibrary.end(); ++it)
  62. delete it->second;
  63. for( MeshLibrary::iterator it = mMeshLibrary.begin(); it != mMeshLibrary.end(); ++it)
  64. delete it->second;
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Reads the contents of the file
  68. void ColladaParser::ReadContents()
  69. {
  70. while( mReader->read())
  71. {
  72. // handle the root element "COLLADA"
  73. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  74. {
  75. if( IsElement( "COLLADA"))
  76. {
  77. ReadStructure();
  78. } else
  79. {
  80. DefaultLogger::get()->debug( boost::str( boost::format( "Ignoring global element \"%s\".") % mReader->getNodeName()));
  81. SkipElement();
  82. }
  83. } else
  84. {
  85. // skip everything else silently
  86. }
  87. }
  88. }
  89. // ------------------------------------------------------------------------------------------------
  90. // Reads the structure of the file
  91. void ColladaParser::ReadStructure()
  92. {
  93. while( mReader->read())
  94. {
  95. // beginning of elements
  96. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  97. {
  98. if( IsElement( "asset"))
  99. ReadAssetInfo();
  100. else if( IsElement( "library_geometries"))
  101. ReadGeometryLibrary();
  102. else if( IsElement( "library_visual_scenes"))
  103. ReadSceneLibrary();
  104. else if( IsElement( "scene"))
  105. ReadScene();
  106. else
  107. SkipElement();
  108. }
  109. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  110. {
  111. break;
  112. }
  113. }
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. // Reads asset informations such as coordinate system informations and legal blah
  117. void ColladaParser::ReadAssetInfo()
  118. {
  119. while( mReader->read())
  120. {
  121. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  122. {
  123. if( IsElement( "unit"))
  124. {
  125. // read unit data from the element's attributes
  126. int attrIndex = GetAttribute( "meter");
  127. mUnitSize = mReader->getAttributeValueAsFloat( attrIndex);
  128. // consume the trailing stuff
  129. if( !mReader->isEmptyElement())
  130. SkipElement();
  131. }
  132. else if( IsElement( "up_axis"))
  133. {
  134. // read content, strip whitespace, compare
  135. const char* content = GetTextContent();
  136. if( strncmp( content, "X_UP", 4) == 0)
  137. mUpDirection = UP_X;
  138. else if( strncmp( content, "Y_UP", 4) == 0)
  139. mUpDirection = UP_Y;
  140. else
  141. mUpDirection = UP_Z;
  142. // check element end
  143. TestClosing( "up_axis");
  144. } else
  145. {
  146. SkipElement();
  147. }
  148. }
  149. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  150. {
  151. if( strcmp( mReader->getNodeName(), "asset") != 0)
  152. ThrowException( "Expected end of \"asset\" element.");
  153. break;
  154. }
  155. }
  156. }
  157. // ------------------------------------------------------------------------------------------------
  158. // Reads the image library contents
  159. void ColladaParser::ReadImageLibrary()
  160. {
  161. while( mReader->read())
  162. {
  163. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  164. {
  165. if( IsElement( "image"))
  166. {
  167. // read ID. Another entry which is "optional" by design but obligatory in reality
  168. int attrID = GetAttribute( "id");
  169. std::string id = mReader->getAttributeValue( attrID);
  170. // create an entry and store it in the library under its ID
  171. mImageLibrary[id] = Image();
  172. // read on from there
  173. ReadImage( mImageLibrary[id]);
  174. } else
  175. {
  176. // ignore the rest
  177. SkipElement();
  178. }
  179. }
  180. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  181. {
  182. if( strcmp( mReader->getNodeName(), "library_images") != 0)
  183. ThrowException( "Expected end of \"library_images\" element.");
  184. break;
  185. }
  186. }
  187. }
  188. // ------------------------------------------------------------------------------------------------
  189. // Reads an image entry into the given image
  190. void ColladaParser::ReadImage( Collada::Image& pImage)
  191. {
  192. while( mReader->read())
  193. {
  194. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  195. {
  196. if( IsElement( "init_from"))
  197. {
  198. // element content is filename - hopefully
  199. const char* content = GetTextContent();
  200. pImage.mFileName = content;
  201. TestClosing( "init_from");
  202. } else
  203. {
  204. // ignore the rest
  205. SkipElement();
  206. }
  207. }
  208. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  209. {
  210. if( strcmp( mReader->getNodeName(), "image") != 0)
  211. ThrowException( "Expected end of \"image\" element.");
  212. break;
  213. }
  214. }
  215. }
  216. // ------------------------------------------------------------------------------------------------
  217. // Reads the material library
  218. void ColladaParser::ReadMaterialLibrary()
  219. {
  220. while( mReader->read())
  221. {
  222. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  223. {
  224. if( IsElement( "material"))
  225. {
  226. // read ID. By now you propably know my opinion about this "specification"
  227. int attrID = GetAttribute( "id");
  228. std::string id = mReader->getAttributeValue( attrID);
  229. // create an entry and store it in the library under its ID
  230. mMaterialLibrary[id] = Material();
  231. // read on from there
  232. ReadMaterial( mMaterialLibrary[id]);
  233. } else
  234. {
  235. // ignore the rest
  236. SkipElement();
  237. }
  238. }
  239. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  240. {
  241. if( strcmp( mReader->getNodeName(), "library_materials") != 0)
  242. ThrowException( "Expected end of \"library_materials\" element.");
  243. break;
  244. }
  245. }
  246. }
  247. // ------------------------------------------------------------------------------------------------
  248. // Reads a material entry into the given material
  249. void ColladaParser::ReadMaterial( Collada::Material& pMaterial)
  250. {
  251. while( mReader->read())
  252. {
  253. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  254. {
  255. if( IsElement( "instance_effect"))
  256. {
  257. // referred effect by URL
  258. int attrUrl = GetAttribute( "url");
  259. const char* url = mReader->getAttributeValue( attrUrl);
  260. if( url[0] != '#')
  261. ThrowException( "Unknown reference format");
  262. pMaterial.mEffect = url;
  263. SkipElement();
  264. } else
  265. {
  266. // ignore the rest
  267. SkipElement();
  268. }
  269. }
  270. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  271. {
  272. if( strcmp( mReader->getNodeName(), "material") != 0)
  273. ThrowException( "Expected end of \"image\" element.");
  274. break;
  275. }
  276. }
  277. }
  278. // ------------------------------------------------------------------------------------------------
  279. // Reads the effect library
  280. void ColladaParser::ReadEffectLibrary()
  281. {
  282. }
  283. // ------------------------------------------------------------------------------------------------
  284. // Reads an effect entry into the given effect
  285. void ColladaParser::ReadEffect( Collada::Effect* pEffect)
  286. {
  287. }
  288. // ------------------------------------------------------------------------------------------------
  289. // Reads the geometry library contents
  290. void ColladaParser::ReadGeometryLibrary()
  291. {
  292. while( mReader->read())
  293. {
  294. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  295. {
  296. if( IsElement( "geometry"))
  297. {
  298. // read ID. Another entry which is "optional" by design but obligatory in reality
  299. int indexID = GetAttribute( "id");
  300. std::string id = mReader->getAttributeValue( indexID);
  301. // TODO: (thom) support SIDs
  302. assert( TestAttribute( "sid") == -1);
  303. // a <geometry> always contains a single <mesh> element inside, so we just skip that element in advance
  304. TestOpening( "mesh");
  305. // create a mesh and store it in the library under its ID
  306. Mesh* mesh = new Mesh;
  307. mMeshLibrary[id] = mesh;
  308. // read on from there
  309. ReadMesh( mesh);
  310. // check for the closing tag of the outer <geometry> element, the inner closing of <mesh> has been consumed by ReadMesh()
  311. TestClosing( "geometry");
  312. } else
  313. {
  314. // ignore the rest
  315. SkipElement();
  316. }
  317. }
  318. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  319. {
  320. if( strcmp( mReader->getNodeName(), "library_geometries") != 0)
  321. ThrowException( "Expected end of \"library_geometries\" element.");
  322. break;
  323. }
  324. }
  325. }
  326. // ------------------------------------------------------------------------------------------------
  327. // Reads a mesh from the geometry library
  328. void ColladaParser::ReadMesh( Mesh* pMesh)
  329. {
  330. // I'm doing a dirty state parsing here because I don't want to open another submethod for it.
  331. // There's a <source> tag defining the name for the accessor inside, and possible a <float_array>
  332. // with it's own ID. This string contains the current source's ID if parsing is inside a <source> element.
  333. std::string presentSourceID;
  334. while( mReader->read())
  335. {
  336. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  337. {
  338. if( IsElement( "source"))
  339. {
  340. // beginning of a source element - store ID for the inner elements
  341. int attrID = GetAttribute( "id");
  342. presentSourceID = mReader->getAttributeValue( attrID);
  343. }
  344. else if( IsElement( "float_array"))
  345. {
  346. ReadFloatArray();
  347. }
  348. else if( IsElement( "technique_common"))
  349. {
  350. // I don't fucking care for your profiles bullshit
  351. }
  352. else if( IsElement( "accessor"))
  353. {
  354. ReadAccessor( presentSourceID);
  355. }
  356. else if( IsElement( "vertices"))
  357. {
  358. // read per-vertex mesh data
  359. ReadVertexData( pMesh);
  360. }
  361. else if( IsElement( "triangles") || IsElement( "lines") || IsElement( "linestrips")
  362. || IsElement( "polygons") || IsElement( "polylist") || IsElement( "trifans") || IsElement( "tristrips"))
  363. {
  364. // read per-index mesh data and faces setup
  365. ReadIndexData( pMesh);
  366. } else
  367. {
  368. // ignore the rest
  369. SkipElement();
  370. }
  371. }
  372. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  373. {
  374. if( strcmp( mReader->getNodeName(), "source") == 0)
  375. {
  376. // end of <source> - reset present source ID
  377. presentSourceID.clear();
  378. }
  379. else if( strcmp( mReader->getNodeName(), "technique_common") == 0)
  380. {
  381. // end of another meaningless element - read over it
  382. }
  383. else if( strcmp( mReader->getNodeName(), "mesh") == 0)
  384. {
  385. // end of <mesh> element - we're done here
  386. break;
  387. } else
  388. {
  389. // everything else should be punished
  390. ThrowException( "Expected end of \"mesh\" element.");
  391. }
  392. }
  393. }
  394. }
  395. // ------------------------------------------------------------------------------------------------
  396. // Reads a data array holding a number of floats, and stores it in the global library
  397. void ColladaParser::ReadFloatArray()
  398. {
  399. // read attributes
  400. int indexID = GetAttribute( "id");
  401. std::string id = mReader->getAttributeValue( indexID);
  402. int indexCount = GetAttribute( "count");
  403. unsigned int count = (unsigned int) mReader->getAttributeValueAsInt( indexCount);
  404. const char* content = GetTextContent();
  405. // read values and store inside an array in the data library
  406. mDataLibrary[id] = Data();
  407. Data& data = mDataLibrary[id];
  408. data.mValues.reserve( count);
  409. for( unsigned int a = 0; a < count; a++)
  410. {
  411. if( *content == 0)
  412. ThrowException( "Expected more values while reading float_array contents.");
  413. float value;
  414. // read a number
  415. content = fast_atof_move( content, value);
  416. data.mValues.push_back( value);
  417. // skip whitespace after it
  418. SkipSpacesAndLineEnd( &content);
  419. }
  420. // test for closing tag
  421. TestClosing( "float_array");
  422. }
  423. // ------------------------------------------------------------------------------------------------
  424. // Reads an accessor and stores it in the global library
  425. void ColladaParser::ReadAccessor( const std::string& pID)
  426. {
  427. // read accessor attributes
  428. int attrSource = GetAttribute( "source");
  429. const char* source = mReader->getAttributeValue( attrSource);
  430. if( source[0] != '#')
  431. ThrowException( boost::str( boost::format( "Unknown reference format in url \"%s\".") % source));
  432. int attrCount = GetAttribute( "count");
  433. unsigned int count = (unsigned int) mReader->getAttributeValueAsInt( attrCount);
  434. int attrOffset = TestAttribute( "offset");
  435. unsigned int offset = 0;
  436. if( attrOffset > -1)
  437. offset = (unsigned int) mReader->getAttributeValueAsInt( attrOffset);
  438. int attrStride = TestAttribute( "stride");
  439. unsigned int stride = 1;
  440. if( attrStride > -1)
  441. stride = (unsigned int) mReader->getAttributeValueAsInt( attrStride);
  442. // store in the library under the given ID
  443. mAccessorLibrary[pID] = Accessor();
  444. Accessor& acc = mAccessorLibrary[pID];
  445. acc.mCount = count;
  446. acc.mOffset = offset;
  447. acc.mStride = stride;
  448. acc.mSource = source+1; // ignore the leading '#'
  449. // and read the components
  450. while( mReader->read())
  451. {
  452. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  453. {
  454. if( IsElement( "param"))
  455. {
  456. // read data param
  457. int attrName = TestAttribute( "name");
  458. std::string name;
  459. if( attrName > -1)
  460. {
  461. name = mReader->getAttributeValue( attrName);
  462. // analyse for common type components and store it's sub-offset in the corresponding field
  463. if( name == "X") acc.mSubOffset[0] = acc.mParams.size();
  464. else if( name == "Y") acc.mSubOffset[1] = acc.mParams.size();
  465. else if( name == "Z") acc.mSubOffset[2] = acc.mParams.size();
  466. else if( name == "R") acc.mSubOffset[0] = acc.mParams.size();
  467. else if( name == "G") acc.mSubOffset[1] = acc.mParams.size();
  468. else if( name == "B") acc.mSubOffset[2] = acc.mParams.size();
  469. else if( name == "A") acc.mSubOffset[3] = acc.mParams.size();
  470. else if( name == "S") acc.mSubOffset[0] = acc.mParams.size();
  471. else if( name == "T") acc.mSubOffset[1] = acc.mParams.size();
  472. else
  473. DefaultLogger::get()->warn( boost::str( boost::format( "Unknown accessor parameter \"%s\". Ignoring data channel.") % name));
  474. }
  475. acc.mParams.push_back( name);
  476. // skip remaining stuff of this element, if any
  477. SkipElement();
  478. } else
  479. {
  480. ThrowException( "Unexpected sub element in tag \"accessor\".");
  481. }
  482. }
  483. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  484. {
  485. if( strcmp( mReader->getNodeName(), "accessor") != 0)
  486. ThrowException( "Expected end of \"accessor\" element.");
  487. break;
  488. }
  489. }
  490. }
  491. // ------------------------------------------------------------------------------------------------
  492. // Reads input declarations of per-vertex mesh data into the given mesh
  493. void ColladaParser::ReadVertexData( Mesh* pMesh)
  494. {
  495. // extract the ID of the <vertices> element. Not that we care, but to catch strange referencing schemes we should warn about
  496. int attrID= GetAttribute( "id");
  497. pMesh->mVertexID = mReader->getAttributeValue( attrID);
  498. // a number of <input> elements
  499. while( mReader->read())
  500. {
  501. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  502. {
  503. if( IsElement( "input"))
  504. {
  505. ReadInputChannel( pMesh->mPerVertexData);
  506. } else
  507. {
  508. ThrowException( "Unexpected sub element in tag \"vertices\".");
  509. }
  510. }
  511. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  512. {
  513. if( strcmp( mReader->getNodeName(), "vertices") != 0)
  514. ThrowException( "Expected end of \"vertices\" element.");
  515. break;
  516. }
  517. }
  518. }
  519. // ------------------------------------------------------------------------------------------------
  520. // Reads input declarations of per-index mesh data into the given mesh
  521. void ColladaParser::ReadIndexData( Mesh* pMesh)
  522. {
  523. std::vector<size_t> vcount;
  524. std::vector<InputChannel> perIndexData;
  525. // read primitive count from the attribute
  526. int attrCount = GetAttribute( "count");
  527. size_t numPrimitives = (size_t) mReader->getAttributeValueAsInt( attrCount);
  528. // distinguish between polys and triangles
  529. std::string elementName = mReader->getNodeName();
  530. PrimitiveType primType = Prim_Invalid;
  531. if( IsElement( "lines"))
  532. primType = Prim_Lines;
  533. else if( IsElement( "linestrips"))
  534. primType = Prim_LineStrip;
  535. else if( IsElement( "polygons"))
  536. primType = Prim_Polygon;
  537. else if( IsElement( "polylist"))
  538. primType = Prim_Polylist;
  539. else if( IsElement( "triangles"))
  540. primType = Prim_Triangles;
  541. else if( IsElement( "trifans"))
  542. primType = Prim_TriFans;
  543. else if( IsElement( "tristrips"))
  544. primType = Prim_TriStrips;
  545. assert( primType != Prim_Invalid);
  546. // also a number of <input> elements, but in addition a <p> primitive collection and propably index counts for all primitives
  547. while( mReader->read())
  548. {
  549. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  550. {
  551. if( IsElement( "input"))
  552. {
  553. ReadInputChannel( perIndexData);
  554. }
  555. else if( IsElement( "vcount"))
  556. {
  557. // case <polylist> - specifies the number of indices for each polygon
  558. const char* content = GetTextContent();
  559. vcount.reserve( numPrimitives);
  560. for( unsigned int a = 0; a < numPrimitives; a++)
  561. {
  562. if( *content == 0)
  563. ThrowException( "Expected more values while reading vcount contents.");
  564. // read a number
  565. vcount.push_back( (size_t) strtol10( content, &content));
  566. // skip whitespace after it
  567. SkipSpacesAndLineEnd( &content);
  568. }
  569. TestClosing( "vcount");
  570. }
  571. else if( IsElement( "p"))
  572. {
  573. // now here the actual fun starts - these are the indices to construct the mesh data from
  574. ReadPrimitives( pMesh, perIndexData, numPrimitives, vcount, primType);
  575. } else
  576. {
  577. ThrowException( "Unexpected sub element in tag \"vertices\".");
  578. }
  579. }
  580. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  581. {
  582. if( mReader->getNodeName() != elementName)
  583. ThrowException( boost::str( boost::format( "Expected end of \"%s\" element.") % elementName));
  584. break;
  585. }
  586. }
  587. }
  588. // ------------------------------------------------------------------------------------------------
  589. // Reads a single input channel element and stores it in the given array, if valid
  590. void ColladaParser::ReadInputChannel( std::vector<InputChannel>& poChannels)
  591. {
  592. InputChannel channel;
  593. // read semantic
  594. int attrSemantic = GetAttribute( "semantic");
  595. std::string semantic = mReader->getAttributeValue( attrSemantic);
  596. channel.mType = GetTypeForSemantic( semantic);
  597. // read source
  598. int attrSource = GetAttribute( "source");
  599. const char* source = mReader->getAttributeValue( attrSource);
  600. if( source[0] != '#')
  601. ThrowException( boost::str( boost::format( "Unknown reference format in url \"%s\".") % source));
  602. channel.mAccessor = source+1; // skipping the leading #, hopefully the remaining text is the accessor ID only
  603. // read index offset, if per-index <input>
  604. int attrOffset = TestAttribute( "offset");
  605. if( attrOffset > -1)
  606. channel.mOffset = mReader->getAttributeValueAsInt( attrOffset);
  607. // store, if valid type
  608. if( channel.mType != IT_Invalid)
  609. poChannels.push_back( channel);
  610. // skip remaining stuff of this element, if any
  611. SkipElement();
  612. }
  613. // ------------------------------------------------------------------------------------------------
  614. // Reads a <p> primitive index list and assembles the mesh data into the given mesh
  615. void ColladaParser::ReadPrimitives( Mesh* pMesh, std::vector<InputChannel>& pPerIndexChannels,
  616. size_t pNumPrimitives, const std::vector<size_t>& pVCount, PrimitiveType pPrimType)
  617. {
  618. // determine number of indices coming per vertex
  619. // find the offset index for all per-vertex channels
  620. size_t numOffsets = 1;
  621. size_t perVertexOffset = -1; // invalid value
  622. BOOST_FOREACH( const InputChannel& channel, pPerIndexChannels)
  623. {
  624. numOffsets = std::max( numOffsets, channel.mOffset+1);
  625. if( channel.mType == IT_Vertex)
  626. perVertexOffset = channel.mOffset;
  627. }
  628. // determine the expected number of indices
  629. size_t expectedPointCount = 0;
  630. switch( pPrimType)
  631. {
  632. case Prim_Polylist:
  633. {
  634. BOOST_FOREACH( size_t i, pVCount)
  635. expectedPointCount += i;
  636. break;
  637. }
  638. case Prim_Lines:
  639. expectedPointCount = 2 * pNumPrimitives;
  640. break;
  641. case Prim_Triangles:
  642. expectedPointCount = 3 * pNumPrimitives;
  643. break;
  644. default:
  645. // other primitive types don't state the index count upfront... we need to guess
  646. break;
  647. }
  648. // and read all indices into a temporary array
  649. std::vector<size_t> indices;
  650. if( expectedPointCount > 0)
  651. indices.reserve( expectedPointCount * numOffsets);
  652. const char* content = GetTextContent();
  653. while( *content != 0)
  654. {
  655. // read a value
  656. unsigned int value = strtol10( content, &content);
  657. indices.push_back( size_t( value));
  658. // skip whitespace after it
  659. SkipSpacesAndLineEnd( &content);
  660. }
  661. // complain if the index count doesn't fit
  662. if( expectedPointCount > 0 && indices.size() != expectedPointCount * numOffsets)
  663. ThrowException( "Expected different index count in <p> element.");
  664. else if( expectedPointCount == 0 && (indices.size() % numOffsets) != 0)
  665. ThrowException( "Expected different index count in <p> element.");
  666. // find the data for all sources
  667. BOOST_FOREACH( InputChannel& input, pMesh->mPerVertexData)
  668. {
  669. if( input.mResolved)
  670. continue;
  671. // find accessor
  672. input.mResolved = &ResolveLibraryReference( mAccessorLibrary, input.mAccessor);
  673. // resolve accessor's data pointer as well, if neccessary
  674. const Accessor* acc = input.mResolved;
  675. if( !acc->mData)
  676. acc->mData = &ResolveLibraryReference( mDataLibrary, acc->mSource);
  677. }
  678. // and the same for the per-index channels
  679. BOOST_FOREACH( InputChannel& input, pPerIndexChannels)
  680. {
  681. if( input.mResolved)
  682. continue;
  683. // ignore vertex pointer, it doesn't refer to an accessor
  684. if( input.mType == IT_Vertex)
  685. {
  686. // warn if the vertex channel does not refer to the <vertices> element in the same mesh
  687. if( input.mAccessor != pMesh->mVertexID)
  688. ThrowException( "Unsupported vertex referencing scheme. I fucking hate Collada.");
  689. continue;
  690. }
  691. // find accessor
  692. input.mResolved = &ResolveLibraryReference( mAccessorLibrary, input.mAccessor);
  693. // resolve accessor's data pointer as well, if neccessary
  694. const Accessor* acc = input.mResolved;
  695. if( !acc->mData)
  696. acc->mData = &ResolveLibraryReference( mDataLibrary, acc->mSource);
  697. }
  698. // now assemble vertex data according to those indices
  699. std::vector<size_t>::const_iterator idx = indices.begin();
  700. // For continued primitives, the given count does not come all in one <p>, but only one primitive per <p>
  701. size_t numPrimitives = pNumPrimitives;
  702. if( pPrimType == Prim_TriFans || pPrimType == Prim_Polygon)
  703. numPrimitives = 1;
  704. for( size_t a = 0; a < numPrimitives; a++)
  705. {
  706. // determine number of points for this primitive
  707. size_t numPoints = 0;
  708. switch( pPrimType)
  709. {
  710. case Prim_Lines: numPoints = 2; break;
  711. case Prim_Triangles: numPoints = 3; break;
  712. case Prim_Polylist: numPoints = pVCount[a]; break;
  713. case Prim_TriFans:
  714. case Prim_Polygon: numPoints = indices.size() / numOffsets; break;
  715. default:
  716. // LineStrip and TriStrip not supported due to expected index unmangling
  717. ThrowException( "Unsupported primitive type.");
  718. break;
  719. }
  720. // store the face size to later reconstruct the face from
  721. pMesh->mFaceSize.push_back( numPoints);
  722. // gather that number of vertices
  723. for( size_t b = 0; b < numPoints; b++)
  724. {
  725. // read all indices for this vertex. Yes, in a hacky static array
  726. assert( numOffsets < 20);
  727. static size_t vindex[20];
  728. for( size_t offsets = 0; offsets < numOffsets; ++offsets)
  729. vindex[offsets] = *idx++;
  730. // extract per-vertex channels using the global per-vertex offset
  731. BOOST_FOREACH( const InputChannel& input, pMesh->mPerVertexData)
  732. ExtractDataObjectFromChannel( input, vindex[perVertexOffset], pMesh);
  733. // and extract per-index channels using there specified offset
  734. BOOST_FOREACH( const InputChannel& input, pPerIndexChannels)
  735. ExtractDataObjectFromChannel( input, vindex[input.mOffset], pMesh);
  736. }
  737. }
  738. // if I ever get my hands on that guy who invented this steaming pile of indirection...
  739. TestClosing( "p");
  740. }
  741. // ------------------------------------------------------------------------------------------------
  742. // Extracts a single object from an input channel and stores it in the appropriate mesh data array
  743. void ColladaParser::ExtractDataObjectFromChannel( const InputChannel& pInput, size_t pLocalIndex, Mesh* pMesh)
  744. {
  745. // ignore vertex referrer - we handle them that separate
  746. if( pInput.mType == IT_Vertex)
  747. return;
  748. const Accessor& acc = *pInput.mResolved;
  749. if( pLocalIndex >= acc.mCount)
  750. ThrowException( boost::str( boost::format( "Invalid data index (%d/%d) in primitive specification") % pLocalIndex % acc.mCount));
  751. // get a pointer to the start of the data object referred to by the accessor and the local index
  752. const float* dataObject = &(acc.mData->mValues[0]) + acc.mOffset + pLocalIndex* acc.mStride;
  753. // assemble according to the accessors component sub-offset list. We don't care, yet, what kind of object exactly we're extracting here
  754. float obj[4];
  755. for( size_t c = 0; c < 4; ++c)
  756. obj[c] = dataObject[acc.mSubOffset[c]];
  757. // now we reinterpret it according to the type we're reading here
  758. switch( pInput.mType)
  759. {
  760. case IT_Position: // ignore all position streams except 0 - there can be only one position
  761. if( pInput.mIndex == 0)
  762. pMesh->mPositions.push_back( aiVector3D( obj[0], obj[1], obj[2]));
  763. break;
  764. case IT_Normal: // ignore all normal streams except 0 - there can be only one normal
  765. if( pInput.mIndex == 0)
  766. pMesh->mNormals.push_back( aiVector3D( obj[0], obj[1], obj[2]));
  767. break;
  768. case IT_Texcoord: // up to 4 texture coord sets are fine, ignore the others
  769. if( pInput.mIndex < AI_MAX_NUMBER_OF_TEXTURECOORDS)
  770. pMesh->mTexCoords[pInput.mIndex].push_back( aiVector2D( obj[0], obj[1]));
  771. break;
  772. case IT_Color: // up to 4 color sets are fine, ignore the others
  773. if( pInput.mIndex < AI_MAX_NUMBER_OF_COLOR_SETS)
  774. pMesh->mColors[pInput.mIndex].push_back( aiColor4D( obj[0], obj[1], obj[2], obj[3]));
  775. break;
  776. }
  777. }
  778. // ------------------------------------------------------------------------------------------------
  779. // Reads the library of node hierarchies and scene parts
  780. void ColladaParser::ReadSceneLibrary()
  781. {
  782. while( mReader->read())
  783. {
  784. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  785. {
  786. // a visual scene - generate root node under its ID and let ReadNode() do the recursive work
  787. if( IsElement( "visual_scene"))
  788. {
  789. // read ID. Is optional according to the spec, but how on earth should a scene_instance refer to it then?
  790. int indexID = GetAttribute( "id");
  791. const char* attrID = mReader->getAttributeValue( indexID);
  792. // read name if given.
  793. int indexName = TestAttribute( "name");
  794. const char* attrName = "unnamed";
  795. if( indexName > -1)
  796. attrName = mReader->getAttributeValue( indexName);
  797. // TODO: (thom) support SIDs
  798. assert( TestAttribute( "sid") == -1);
  799. // create a node and store it in the library under its ID
  800. Node* node = new Node;
  801. node->mID = attrID;
  802. node->mName = attrName;
  803. mNodeLibrary[node->mID] = node;
  804. ReadSceneNode( node);
  805. } else
  806. {
  807. // ignore the rest
  808. SkipElement();
  809. }
  810. }
  811. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  812. {
  813. if( strcmp( mReader->getNodeName(), "library_visual_scenes") != 0)
  814. ThrowException( "Expected end of \"library_visual_scenes\" element.");
  815. break;
  816. }
  817. }
  818. }
  819. // ------------------------------------------------------------------------------------------------
  820. // Reads a scene node's contents including children and stores it in the given node
  821. void ColladaParser::ReadSceneNode( Node* pNode)
  822. {
  823. // quit immediately on <bla/> elements
  824. if( mReader->isEmptyElement())
  825. return;
  826. while( mReader->read())
  827. {
  828. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  829. {
  830. if( IsElement( "lookat"))
  831. ReadNodeTransformation( pNode, TF_LOOKAT);
  832. else if( IsElement( "matrix"))
  833. ReadNodeTransformation( pNode, TF_MATRIX);
  834. else if( IsElement( "rotate"))
  835. ReadNodeTransformation( pNode, TF_ROTATE);
  836. else if( IsElement( "scale"))
  837. ReadNodeTransformation( pNode, TF_SCALE);
  838. else if( IsElement( "skew"))
  839. ReadNodeTransformation( pNode, TF_SKEW);
  840. else if( IsElement( "translate"))
  841. ReadNodeTransformation( pNode, TF_TRANSLATE);
  842. else if( IsElement( "node"))
  843. {
  844. Node* child = new Node;
  845. int attrID = TestAttribute( "id");
  846. if( attrID > -1)
  847. child->mID = mReader->getAttributeValue( attrID);
  848. int attrName = TestAttribute( "name");
  849. if( attrName > -1)
  850. child->mName = mReader->getAttributeValue( attrName);
  851. // TODO: (thom) support SIDs
  852. // assert( TestAttribute( "sid") == -1);
  853. pNode->mChildren.push_back( child);
  854. child->mParent = pNode;
  855. // read on recursively from there
  856. ReadSceneNode( child);
  857. } else if( IsElement( "instance_node"))
  858. {
  859. // test for it, in case we need to implement it
  860. assert( false);
  861. SkipElement();
  862. } else if( IsElement( "instance_geometry"))
  863. {
  864. // Reference to a mesh, we possible material associations
  865. ReadNodeGeometry( pNode);
  866. } else
  867. {
  868. // skip everything else for the moment
  869. SkipElement();
  870. }
  871. }
  872. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  873. {
  874. break;
  875. }
  876. }
  877. }
  878. // ------------------------------------------------------------------------------------------------
  879. // Reads a node transformation entry of the given type and adds it to the given node's transformation list.
  880. void ColladaParser::ReadNodeTransformation( Node* pNode, TransformType pType)
  881. {
  882. std::string tagName = mReader->getNodeName();
  883. // how many parameters to read per transformation type
  884. static const unsigned int sNumParameters[] = { 9, 4, 3, 3, 7, 16 };
  885. const char* content = GetTextContent();
  886. // read as many parameters and store in the transformation
  887. Transform tf;
  888. tf.mType = pType;
  889. for( unsigned int a = 0; a < sNumParameters[pType]; a++)
  890. {
  891. // read a number
  892. content = fast_atof_move( content, tf.f[a]);
  893. // skip whitespace after it
  894. SkipSpacesAndLineEnd( &content);
  895. }
  896. // place the transformation at the queue of the node
  897. pNode->mTransforms.push_back( tf);
  898. // and consum the closing tag
  899. TestClosing( tagName.c_str());
  900. }
  901. // ------------------------------------------------------------------------------------------------
  902. // Reads a mesh reference in a node and adds it to the node's mesh list
  903. void ColladaParser::ReadNodeGeometry( Node* pNode)
  904. {
  905. // referred mesh is given as an attribute of the <instance_geometry> element
  906. int attrUrl = GetAttribute( "url");
  907. const char* url = mReader->getAttributeValue( attrUrl);
  908. if( url[0] != '#')
  909. ThrowException( "Unknown reference format");
  910. Collada::MeshInstance instance;
  911. instance.mMesh = url+1; // skipping the leading #
  912. // read material associations. Ignore additional elements inbetween
  913. while( mReader->read())
  914. {
  915. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  916. {
  917. if( IsElement( "instance_material"))
  918. {
  919. // read ID of the geometry subgroup and the target material
  920. int attrGroup = GetAttribute( "symbol");
  921. std::string group = mReader->getAttributeValue( attrGroup);
  922. int attrMaterial = GetAttribute( "target");
  923. const char* urlMat = mReader->getAttributeValue( attrMaterial);
  924. if( urlMat[0] != '#')
  925. ThrowException( "Unknown reference format");
  926. std::string mat = mReader->getAttributeValue( attrMaterial+1);
  927. // store the association
  928. instance.mMaterials[group] = mat;
  929. } else
  930. {
  931. SkipElement();
  932. }
  933. }
  934. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  935. {
  936. if( strcmp( mReader->getNodeName(), "instance_geometry") == 0)
  937. break;
  938. }
  939. }
  940. // store it
  941. pNode->mMeshes.push_back( instance);
  942. }
  943. // ------------------------------------------------------------------------------------------------
  944. // Reads the collada scene
  945. void ColladaParser::ReadScene()
  946. {
  947. while( mReader->read())
  948. {
  949. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  950. {
  951. if( IsElement( "instance_visual_scene"))
  952. {
  953. // should be the first and only occurence
  954. if( mRootNode)
  955. ThrowException( "Invalid scene containing multiple root nodes");
  956. // read the url of the scene to instance. Should be of format "#some_name"
  957. int urlIndex = GetAttribute( "url");
  958. const char* url = mReader->getAttributeValue( urlIndex);
  959. if( url[0] != '#')
  960. ThrowException( "Unknown reference format");
  961. // find the referred scene, skip the leading #
  962. NodeLibrary::const_iterator sit = mNodeLibrary.find( url+1);
  963. if( sit == mNodeLibrary.end())
  964. ThrowException( boost::str( boost::format( "Unable to resolve visual_scene reference \"%s\".") % url));
  965. mRootNode = sit->second;
  966. } else
  967. {
  968. SkipElement();
  969. }
  970. }
  971. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  972. {
  973. break;
  974. }
  975. }
  976. }
  977. // ------------------------------------------------------------------------------------------------
  978. // Aborts the file reading with an exception
  979. void ColladaParser::ThrowException( const std::string& pError) const
  980. {
  981. throw new ImportErrorException( boost::str( boost::format( "%s - %s") % mFileName % pError));
  982. }
  983. // ------------------------------------------------------------------------------------------------
  984. // Skips all data until the end node of the current element
  985. void ColladaParser::SkipElement()
  986. {
  987. // nothing to skip if it's an <element />
  988. if( mReader->isEmptyElement())
  989. return;
  990. // copy the current node's name because it'a pointer to the reader's internal buffer,
  991. // which is going to change with the upcoming parsing
  992. std::string element = mReader->getNodeName();
  993. while( mReader->read())
  994. {
  995. if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  996. if( mReader->getNodeName() == element)
  997. break;
  998. }
  999. }
  1000. // ------------------------------------------------------------------------------------------------
  1001. // Tests for an opening element of the given name, throws an exception if not found
  1002. void ColladaParser::TestOpening( const char* pName)
  1003. {
  1004. // read element start
  1005. if( !mReader->read())
  1006. ThrowException( boost::str( boost::format( "Unexpected end of file while beginning of \"%s\" element.") % pName));
  1007. // whitespace in front is ok, just read again if found
  1008. if( mReader->getNodeType() == irr::io::EXN_TEXT)
  1009. if( !mReader->read())
  1010. ThrowException( boost::str( boost::format( "Unexpected end of file while reading beginning of \"%s\" element.") % pName));
  1011. if( mReader->getNodeType() != irr::io::EXN_ELEMENT || strcmp( mReader->getNodeName(), pName) != 0)
  1012. ThrowException( boost::str( boost::format( "Expected start of \"%s\" element.") % pName));
  1013. }
  1014. // ------------------------------------------------------------------------------------------------
  1015. // Tests for the closing tag of the given element, throws an exception if not found
  1016. void ColladaParser::TestClosing( const char* pName)
  1017. {
  1018. // read closing tag
  1019. if( !mReader->read())
  1020. ThrowException( boost::str( boost::format( "Unexpected end of file while reading end of \"%s\" element.") % pName));
  1021. // whitespace in front is ok, just read again if found
  1022. if( mReader->getNodeType() == irr::io::EXN_TEXT)
  1023. if( !mReader->read())
  1024. ThrowException( boost::str( boost::format( "Unexpected end of file while reading end of \"%s\" element.") % pName));
  1025. if( mReader->getNodeType() != irr::io::EXN_ELEMENT_END || strcmp( mReader->getNodeName(), pName) != 0)
  1026. ThrowException( boost::str( boost::format( "Expected end of \"%s\" element.") % pName));
  1027. }
  1028. // ------------------------------------------------------------------------------------------------
  1029. // Returns the index of the named attribute or -1 if not found. Does not throw, therefore useful for optional attributes
  1030. int ColladaParser::GetAttribute( const char* pAttr) const
  1031. {
  1032. int index = TestAttribute( pAttr);
  1033. if( index != -1)
  1034. return index;
  1035. // attribute not found -> throw an exception
  1036. ThrowException( boost::str( boost::format( "Expected attribute \"%s\" at element \"%s\".") % pAttr % mReader->getNodeName()));
  1037. return -1;
  1038. }
  1039. // ------------------------------------------------------------------------------------------------
  1040. // Tests the present element for the presence of one attribute, returns its index or throws an exception if not found
  1041. int ColladaParser::TestAttribute( const char* pAttr) const
  1042. {
  1043. for( int a = 0; a < mReader->getAttributeCount(); a++)
  1044. if( strcmp( mReader->getAttributeName( a), pAttr) == 0)
  1045. return a;
  1046. return -1;
  1047. }
  1048. // ------------------------------------------------------------------------------------------------
  1049. // Reads the text contents of an element, throws an exception if not given. Skips leading whitespace.
  1050. const char* ColladaParser::GetTextContent()
  1051. {
  1052. // present node should be the beginning of an element
  1053. if( mReader->getNodeType() != irr::io::EXN_ELEMENT || mReader->isEmptyElement())
  1054. ThrowException( "Expected opening element");
  1055. // read contents of the element
  1056. if( !mReader->read())
  1057. ThrowException( "Unexpected end of file while reading asset up_axis element.");
  1058. if( mReader->getNodeType() != irr::io::EXN_TEXT)
  1059. ThrowException( "Invalid contents in element \"up_axis\".");
  1060. // skip leading whitespace
  1061. const char* text = mReader->getNodeData();
  1062. SkipSpacesAndLineEnd( &text);
  1063. return text;
  1064. }
  1065. // ------------------------------------------------------------------------------------------------
  1066. // Calculates the resulting transformation fromm all the given transform steps
  1067. aiMatrix4x4 ColladaParser::CalculateResultTransform( const std::vector<Transform>& pTransforms) const
  1068. {
  1069. aiMatrix4x4 res;
  1070. for( std::vector<Transform>::const_iterator it = pTransforms.begin(); it != pTransforms.end(); ++it)
  1071. {
  1072. const Transform& tf = *it;
  1073. switch( tf.mType)
  1074. {
  1075. case TF_LOOKAT:
  1076. // TODO: (thom)
  1077. assert( false);
  1078. break;
  1079. case TF_ROTATE:
  1080. {
  1081. aiMatrix4x4 rot;
  1082. float angle = tf.f[3] * float( AI_MATH_PI) / 180.0f;
  1083. aiVector3D axis( tf.f[0], tf.f[1], tf.f[2]);
  1084. aiMatrix4x4::Rotation( angle, axis, rot);
  1085. res *= rot;
  1086. break;
  1087. }
  1088. case TF_TRANSLATE:
  1089. {
  1090. aiMatrix4x4 trans;
  1091. aiMatrix4x4::Translation( aiVector3D( tf.f[0], tf.f[1], tf.f[2]), trans);
  1092. res *= trans;
  1093. break;
  1094. }
  1095. case TF_SCALE:
  1096. {
  1097. 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,
  1098. 0.0f, 0.0f, 0.0f, 1.0f);
  1099. res *= scale;
  1100. break;
  1101. }
  1102. case TF_SKEW:
  1103. // TODO: (thom)
  1104. assert( false);
  1105. break;
  1106. case TF_MATRIX:
  1107. {
  1108. 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],
  1109. tf.f[8], tf.f[9], tf.f[10], tf.f[11], tf.f[12], tf.f[13], tf.f[14], tf.f[15]);
  1110. res *= mat;
  1111. break;
  1112. }
  1113. default:
  1114. assert( false);
  1115. break;
  1116. }
  1117. }
  1118. return res;
  1119. }
  1120. // ------------------------------------------------------------------------------------------------
  1121. // Determines the input data type for the given semantic string
  1122. Collada::InputType ColladaParser::GetTypeForSemantic( const std::string& pSemantic)
  1123. {
  1124. if( pSemantic == "POSITION")
  1125. return IT_Position;
  1126. else if( pSemantic == "TEXCOORD")
  1127. return IT_Texcoord;
  1128. else if( pSemantic == "NORMAL")
  1129. return IT_Normal;
  1130. else if( pSemantic == "COLOR")
  1131. return IT_Color;
  1132. else if( pSemantic == "VERTEX")
  1133. return IT_Vertex;
  1134. DefaultLogger::get()->warn( boost::str( boost::format( "Unknown vertex input type \"%s\". Ignoring.") % pSemantic));
  1135. return IT_Invalid;
  1136. }