FBXDocument.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXDocument.cpp
  34. * @brief Implementation of the FBX DOM classes
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include "FBXParser.h"
  39. #include "FBXDocument.h"
  40. #include "FBXUtil.h"
  41. #include "FBXImporter.h"
  42. #include "FBXImportSettings.h"
  43. namespace Assimp {
  44. namespace FBX {
  45. namespace {
  46. // ------------------------------------------------------------------------------------------------
  47. // signal DOM construction error, this is always unrecoverable. Throws DeadlyImportError.
  48. void DOMError(const std::string& message, const Token& token)
  49. {
  50. throw DeadlyImportError(Util::AddTokenText("FBX-DOM",message,&token));
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. void DOMError(const std::string& message, const Element* element = NULL)
  54. {
  55. if(element) {
  56. DOMError(message,element->KeyToken());
  57. }
  58. throw DeadlyImportError("FBX-DOM " + message);
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. // extract required compound scope
  62. const Scope& GetRequiredScope(const Element& el)
  63. {
  64. const Scope* const s = el.Compound();
  65. if(!s) {
  66. DOMError("expected compound scope",&el);
  67. }
  68. return *s;
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // get token at a particular index
  72. const Token& GetRequiredToken(const Element& el, unsigned int index)
  73. {
  74. const TokenList& t = el.Tokens();
  75. if(index >= t.size()) {
  76. DOMError(Formatter::format( "missing token at index " ) << index,&el);
  77. }
  78. return *t[index];
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // wrapper around ParseTokenAsID() with DOMError handling
  82. uint64_t ParseTokenAsID(const Token& t)
  83. {
  84. const char* err;
  85. const uint64_t i = ParseTokenAsID(t,err);
  86. if(err) {
  87. DOMError(err,t);
  88. }
  89. return i;
  90. }
  91. // ------------------------------------------------------------------------------------------------
  92. // wrapper around ParseTokenAsDim() with DOMError handling
  93. size_t ParseTokenAsDim(const Token& t)
  94. {
  95. const char* err;
  96. const size_t i = ParseTokenAsDim(t,err);
  97. if(err) {
  98. DOMError(err,t);
  99. }
  100. return i;
  101. }
  102. // ------------------------------------------------------------------------------------------------
  103. // wrapper around ParseTokenAsFloat() with DOMError handling
  104. float ParseTokenAsFloat(const Token& t)
  105. {
  106. const char* err;
  107. const float i = ParseTokenAsFloat(t,err);
  108. if(err) {
  109. DOMError(err,t);
  110. }
  111. return i;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // wrapper around ParseTokenAsInt() with DOMError handling
  115. int ParseTokenAsInt(const Token& t)
  116. {
  117. const char* err;
  118. const int i = ParseTokenAsInt(t,err);
  119. if(err) {
  120. DOMError(err,t);
  121. }
  122. return i;
  123. }
  124. // ------------------------------------------------------------------------------------------------
  125. // wrapper around ParseTokenAsString() with DOMError handling
  126. std::string ParseTokenAsString(const Token& t)
  127. {
  128. const char* err;
  129. const std::string& i = ParseTokenAsString(t,err);
  130. if(err) {
  131. DOMError(err,t);
  132. }
  133. return i;
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. // extract a required element from a scope, abort if the element cannot be found
  137. const Element& GetRequiredElement(const Scope& sc, const std::string& index, const Element* element = NULL)
  138. {
  139. const Element* el = sc[index];
  140. if(!el) {
  141. DOMError("did not find required element \"" + index + "\"",element);
  142. }
  143. return *el;
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. // read an array of float3 tuples
  147. void ReadVectorDataArray(std::vector<aiVector3D>& out, const Element& el)
  148. {
  149. out.clear();
  150. const TokenList& tok = el.Tokens();
  151. const size_t dim = ParseTokenAsDim(*tok[0]);
  152. // may throw bad_alloc if the input is rubbish, but this need
  153. // not to be prevented - importing would fail but we wouldn't
  154. // crash since assimp handles this case properly.
  155. out.reserve(dim);
  156. const Scope& scope = GetRequiredScope(el);
  157. const Element& a = GetRequiredElement(scope,"a",&el);
  158. if (a.Tokens().size() % 3 != 0) {
  159. DOMError("number of floats is not a multiple of three (3)",&el);
  160. }
  161. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  162. aiVector3D v;
  163. v.x = ParseTokenAsFloat(**it++);
  164. v.y = ParseTokenAsFloat(**it++);
  165. v.z = ParseTokenAsFloat(**it++);
  166. out.push_back(v);
  167. }
  168. }
  169. // ------------------------------------------------------------------------------------------------
  170. // read an array of color4 tuples
  171. void ReadVectorDataArray(std::vector<aiColor4D>& out, const Element& el)
  172. {
  173. out.clear();
  174. const TokenList& tok = el.Tokens();
  175. const size_t dim = ParseTokenAsDim(*tok[0]);
  176. // see notes in ReadVectorDataArray() above
  177. out.reserve(dim);
  178. const Scope& scope = GetRequiredScope(el);
  179. const Element& a = GetRequiredElement(scope,"a",&el);
  180. if (a.Tokens().size() % 4 != 0) {
  181. DOMError("number of floats is not a multiple of four (4)",&el);
  182. }
  183. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  184. aiColor4D v;
  185. v.r = ParseTokenAsFloat(**it++);
  186. v.g = ParseTokenAsFloat(**it++);
  187. v.b = ParseTokenAsFloat(**it++);
  188. v.a = ParseTokenAsFloat(**it++);
  189. out.push_back(v);
  190. }
  191. }
  192. // ------------------------------------------------------------------------------------------------
  193. // read an array of float2 tuples
  194. void ReadVectorDataArray(std::vector<aiVector2D>& out, const Element& el)
  195. {
  196. out.clear();
  197. const TokenList& tok = el.Tokens();
  198. const size_t dim = ParseTokenAsDim(*tok[0]);
  199. // see notes in ReadVectorDataArray() above
  200. out.reserve(dim);
  201. const Scope& scope = GetRequiredScope(el);
  202. const Element& a = GetRequiredElement(scope,"a",&el);
  203. if (a.Tokens().size() % 2 != 0) {
  204. DOMError("number of floats is not a multiple of two (2)",&el);
  205. }
  206. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  207. aiVector2D v;
  208. v.x = ParseTokenAsFloat(**it++);
  209. v.y = ParseTokenAsFloat(**it++);
  210. out.push_back(v);
  211. }
  212. }
  213. // ------------------------------------------------------------------------------------------------
  214. // read an array of ints
  215. void ReadVectorDataArray(std::vector<int>& out, const Element& el)
  216. {
  217. out.clear();
  218. const TokenList& tok = el.Tokens();
  219. const size_t dim = ParseTokenAsDim(*tok[0]);
  220. // see notes in ReadVectorDataArray()
  221. out.reserve(dim);
  222. const Scope& scope = GetRequiredScope(el);
  223. const Element& a = GetRequiredElement(scope,"a",&el);
  224. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  225. const int ival = ParseTokenAsInt(**it++);
  226. out.push_back(ival);
  227. }
  228. }
  229. // ------------------------------------------------------------------------------------------------
  230. // read an array of uints
  231. void ReadVectorDataArray(std::vector<unsigned int>& out, const Element& el)
  232. {
  233. out.clear();
  234. const TokenList& tok = el.Tokens();
  235. const size_t dim = ParseTokenAsDim(*tok[0]);
  236. // see notes in ReadVectorDataArray()
  237. out.reserve(dim);
  238. const Scope& scope = GetRequiredScope(el);
  239. const Element& a = GetRequiredElement(scope,"a",&el);
  240. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  241. const int ival = ParseTokenAsInt(**it++);
  242. if(ival < 0) {
  243. DOMError("encountered negative integer index");
  244. }
  245. out.push_back(static_cast<unsigned int>(ival));
  246. }
  247. }
  248. } // end anon.
  249. // ------------------------------------------------------------------------------------------------
  250. LazyObject::LazyObject(const Element& element, const ImportSettings& settings)
  251. : settings(settings)
  252. , element(element)
  253. {
  254. }
  255. // ------------------------------------------------------------------------------------------------
  256. LazyObject::~LazyObject()
  257. {
  258. }
  259. // ------------------------------------------------------------------------------------------------
  260. const Object* LazyObject::Get()
  261. {
  262. if (object.get()) {
  263. return object.get();
  264. }
  265. const Token& key = element.KeyToken();
  266. const TokenList& tokens = element.Tokens();
  267. if(tokens.size() < 3) {
  268. DOMError("expected at least 3 tokens: id, name and class tag",&element);
  269. }
  270. const char* err;
  271. const std::string name = ParseTokenAsString(*tokens[1],err);
  272. if (err) {
  273. DOMError(err,&element);
  274. }
  275. const std::string classtag = ParseTokenAsString(*tokens[2],err);
  276. if (err) {
  277. DOMError(err,&element);
  278. }
  279. // this needs to be relatively fast since we do it a lot,
  280. // so avoid constructing strings all the time.
  281. const char* obtype = key.begin();
  282. if (!strncmp(obtype,"Geometry",static_cast<size_t>(key.end()-key.begin()))) {
  283. if (!strcmp(classtag.c_str(),"Mesh")) {
  284. object.reset(new MeshGeometry(element,name,settings));
  285. }
  286. }
  287. if (!object.get()) {
  288. //DOMError("failed to convert element to DOM object, class: " + classtag + ", name: " + name,&element);
  289. }
  290. return object.get();
  291. }
  292. // ------------------------------------------------------------------------------------------------
  293. Object::Object(const Element& element, const std::string& name)
  294. : element(element)
  295. , name(name)
  296. {
  297. }
  298. // ------------------------------------------------------------------------------------------------
  299. Object::~Object()
  300. {
  301. }
  302. // ------------------------------------------------------------------------------------------------
  303. Geometry::Geometry(const Element& element, const std::string& name)
  304. : Object(element,name)
  305. {
  306. }
  307. // ------------------------------------------------------------------------------------------------
  308. Geometry::~Geometry()
  309. {
  310. }
  311. // ------------------------------------------------------------------------------------------------
  312. MeshGeometry::MeshGeometry(const Element& element, const std::string& name, const ImportSettings& settings)
  313. : Geometry(element,name)
  314. {
  315. const Scope* sc = element.Compound();
  316. if (!sc) {
  317. DOMError("failed to read Geometry object (class: Mesh), no data scope found");
  318. }
  319. // must have Mesh elements:
  320. const Element& Vertices = GetRequiredElement(*sc,"Vertices",&element);
  321. const Element& PolygonVertexIndex = GetRequiredElement(*sc,"PolygonVertexIndex",&element);
  322. // optional Mesh elements:
  323. const ElementCollection& Layer = sc->GetCollection("Layer");
  324. const ElementCollection& LayerElementMaterial = sc->GetCollection("LayerElementMaterial");
  325. const ElementCollection& LayerElementUV = sc->GetCollection("LayerElementUV");
  326. const ElementCollection& LayerElementNormal = sc->GetCollection("LayerElementNormal");
  327. std::vector<aiVector3D> tempVerts;
  328. ReadVectorDataArray(tempVerts,Vertices);
  329. if(tempVerts.empty()) {
  330. FBXImporter::LogWarn("encountered mesh with no vertices");
  331. return;
  332. }
  333. std::vector<int> tempFaces;
  334. ReadVectorDataArray(tempFaces,PolygonVertexIndex);
  335. if(tempFaces.empty()) {
  336. FBXImporter::LogWarn("encountered mesh with no faces");
  337. return;
  338. }
  339. vertices.reserve(tempFaces.size());
  340. faces.reserve(tempFaces.size() / 3);
  341. mapping_offsets.resize(tempVerts.size());
  342. mapping_counts.resize(tempVerts.size(),0);
  343. mappings.resize(tempFaces.size());
  344. const size_t vertex_count = tempVerts.size();
  345. // generate output vertices, computing an adjacency table to
  346. // preserve the mapping from fbx indices to *this* indexing.
  347. unsigned int count = 0;
  348. BOOST_FOREACH(int index, tempFaces) {
  349. const int absi = index < 0 ? (-index - 1) : index;
  350. if(static_cast<size_t>(absi) >= vertex_count) {
  351. DOMError("polygon vertex index out of range",&PolygonVertexIndex);
  352. }
  353. vertices.push_back(tempVerts[absi]);
  354. ++count;
  355. ++mapping_counts[absi];
  356. if (index < 0) {
  357. faces.push_back(count);
  358. count = 0;
  359. }
  360. }
  361. unsigned int cursor = 0;
  362. for (size_t i = 0, e = tempVerts.size(); i < e; ++i) {
  363. mapping_offsets[i] = cursor;
  364. cursor += mapping_counts[i];
  365. mapping_counts[i] = 0;
  366. }
  367. cursor = 0;
  368. BOOST_FOREACH(int index, tempFaces) {
  369. const int absi = index < 0 ? (-index - 1) : index;
  370. mappings[mapping_offsets[absi] + mapping_counts[absi]++] = cursor;
  371. }
  372. // if settings.readAllLayers is true:
  373. // * read all layers, try to load as many vertex channels as possible
  374. // if settings.readAllLayers is false:
  375. // * read only the layer with index 0, but warn about any further layers
  376. for (ElementMap::const_iterator it = Layer.first; it != Layer.second; ++it) {
  377. const TokenList& tokens = (*it).second->Tokens();
  378. const char* err;
  379. const int index = ParseTokenAsInt(*tokens[0], err);
  380. if(err) {
  381. DOMError(err,&element);
  382. }
  383. if(settings.readAllLayers || index == 0) {
  384. const Scope& layer = GetRequiredScope(*(*it).second);
  385. ReadLayer(layer);
  386. }
  387. else {
  388. FBXImporter::LogWarn("ignoring additional geometry layers");
  389. }
  390. }
  391. }
  392. // ------------------------------------------------------------------------------------------------
  393. MeshGeometry::~MeshGeometry()
  394. {
  395. }
  396. // ------------------------------------------------------------------------------------------------
  397. void MeshGeometry::ReadLayer(const Scope& layer)
  398. {
  399. const ElementCollection& LayerElement = layer.GetCollection("LayerElement");
  400. for (ElementMap::const_iterator eit = LayerElement.first; eit != LayerElement.second; ++eit) {
  401. const Scope& elayer = GetRequiredScope(*(*eit).second);
  402. ReadLayerElement(elayer);
  403. }
  404. }
  405. // ------------------------------------------------------------------------------------------------
  406. void MeshGeometry::ReadLayerElement(const Scope& layerElement)
  407. {
  408. const Element& Type = GetRequiredElement(layerElement,"Type");
  409. const Element& TypedIndex = GetRequiredElement(layerElement,"TypedIndex");
  410. const std::string& type = ParseTokenAsString(GetRequiredToken(Type,0));
  411. const int typedIndex = ParseTokenAsInt(GetRequiredToken(TypedIndex,0));
  412. const Scope& top = GetRequiredScope(element);
  413. const ElementCollection candidates = top.GetCollection(type);
  414. for (ElementMap::const_iterator it = candidates.first; it != candidates.second; ++it) {
  415. const int index = ParseTokenAsInt(GetRequiredToken(*(*it).second,0));
  416. if(index == typedIndex) {
  417. ReadVertexData(type,typedIndex,GetRequiredScope(*(*it).second));
  418. return;
  419. }
  420. }
  421. FBXImporter::LogError(Formatter::format("failed to resolve vertex layer element: ")
  422. << type << ", index: " << typedIndex);
  423. }
  424. // ------------------------------------------------------------------------------------------------
  425. void MeshGeometry::ReadVertexData(const std::string& type, int index, const Scope& source)
  426. {
  427. const std::string& MappingInformationType = ParseTokenAsString(GetRequiredToken(
  428. GetRequiredElement(source,"MappingInformationType"),0)
  429. );
  430. const std::string& ReferenceInformationType = ParseTokenAsString(GetRequiredToken(
  431. GetRequiredElement(source,"ReferenceInformationType"),0)
  432. );
  433. if (type == "LayerElementUV") {
  434. if(index >= AI_MAX_NUMBER_OF_TEXTURECOORDS) {
  435. FBXImporter::LogError(Formatter::format("ignoring UV layer, maximum number of UV channels exceeded: ")
  436. << index << " (limit is " << AI_MAX_NUMBER_OF_TEXTURECOORDS << ")" );
  437. return;
  438. }
  439. ReadVertexDataUV(uvs[index],source,
  440. MappingInformationType,
  441. ReferenceInformationType
  442. );
  443. }
  444. else if (type == "LayerElementMaterial") {
  445. if (materials.size() > 0) {
  446. FBXImporter::LogError("ignoring additional material layer");
  447. return;
  448. }
  449. ReadVertexDataMaterials(materials,source,
  450. MappingInformationType,
  451. ReferenceInformationType
  452. );
  453. }
  454. else if (type == "LayerElementNormal") {
  455. if (normals.size() > 0) {
  456. FBXImporter::LogError("ignoring additional normal layer");
  457. return;
  458. }
  459. ReadVertexDataNormals(normals,source,
  460. MappingInformationType,
  461. ReferenceInformationType
  462. );
  463. }
  464. else if (type == "LayerElementTangent") {
  465. if (tangents.size() > 0) {
  466. FBXImporter::LogError("ignoring additional tangent layer");
  467. return;
  468. }
  469. ReadVertexDataTangents(tangents,source,
  470. MappingInformationType,
  471. ReferenceInformationType
  472. );
  473. }
  474. else if (type == "LayerElementBinormal") {
  475. if (binormals.size() > 0) {
  476. FBXImporter::LogError("ignoring additional binormal layer");
  477. return;
  478. }
  479. ReadVertexDataBinormals(binormals,source,
  480. MappingInformationType,
  481. ReferenceInformationType
  482. );
  483. }
  484. else if (type == "LayerElementColor") {
  485. if(index >= AI_MAX_NUMBER_OF_COLOR_SETS) {
  486. FBXImporter::LogError(Formatter::format("ignoring vertex color layer, maximum number of color sets exceeded: ")
  487. << index << " (limit is " << AI_MAX_NUMBER_OF_COLOR_SETS << ")" );
  488. return;
  489. }
  490. ReadVertexDataColors(colors[index],source,
  491. MappingInformationType,
  492. ReferenceInformationType
  493. );
  494. }
  495. }
  496. // ------------------------------------------------------------------------------------------------
  497. // Lengthy utility function to read and resolve a FBX vertex data array - that is, the
  498. // output is in polygon vertex order. This logic is used for reading normals, UVs, colors,
  499. // tangents ..
  500. template <typename T>
  501. void ResolveVertexDataArray(std::vector<T>& data_out, const Scope& source,
  502. const std::string& MappingInformationType,
  503. const std::string& ReferenceInformationType,
  504. const char* dataElementName,
  505. const char* indexDataElementName,
  506. size_t vertex_count,
  507. const std::vector<unsigned int>& mapping_counts,
  508. const std::vector<unsigned int>& mapping_offsets,
  509. const std::vector<unsigned int>& mappings)
  510. {
  511. std::vector<T> tempUV;
  512. ReadVectorDataArray(tempUV,GetRequiredElement(source,dataElementName));
  513. // handle permutations of Mapping and Reference type - it would be nice to
  514. // deal with this more elegantly and with less redundancy, but right
  515. // now it seems unavoidable.
  516. if (MappingInformationType == "ByVertice" && ReferenceInformationType == "Direct") {
  517. data_out.resize(vertex_count);
  518. for (size_t i = 0, e = tempUV.size(); i < e; ++i) {
  519. const unsigned int istart = mapping_offsets[i], iend = istart + mapping_counts[i];
  520. for (unsigned int j = istart; j < iend; ++j) {
  521. data_out[mappings[j]] = tempUV[i];
  522. }
  523. }
  524. }
  525. else if (MappingInformationType == "ByVertice" && ReferenceInformationType == "IndexToDirect") {
  526. data_out.resize(vertex_count);
  527. std::vector<int> uvIndices;
  528. ReadVectorDataArray(uvIndices,GetRequiredElement(source,indexDataElementName));
  529. for (size_t i = 0, e = uvIndices.size(); i < e; ++i) {
  530. const unsigned int istart = mapping_offsets[i], iend = istart + mapping_counts[i];
  531. for (unsigned int j = istart; j < iend; ++j) {
  532. if(static_cast<size_t>(uvIndices[i]) >= tempUV.size()) {
  533. DOMError("index out of range",&GetRequiredElement(source,indexDataElementName));
  534. }
  535. data_out[mappings[j]] = tempUV[uvIndices[i]];
  536. }
  537. }
  538. }
  539. else if (MappingInformationType == "ByPolygonVertex" && ReferenceInformationType == "Direct") {
  540. if (tempUV.size() != vertex_count) {
  541. FBXImporter::LogError(Formatter::format("length of input data unexpected for ByPolygon mapping: ")
  542. << tempUV.size() << ", expected " << vertex_count
  543. );
  544. return;
  545. }
  546. data_out.swap(tempUV);
  547. }
  548. else if (MappingInformationType == "ByPolygonVertex" && ReferenceInformationType == "IndexToDirect") {
  549. data_out.resize(vertex_count);
  550. std::vector<int> uvIndices;
  551. ReadVectorDataArray(uvIndices,GetRequiredElement(source,indexDataElementName));
  552. if (uvIndices.size() != vertex_count) {
  553. FBXImporter::LogError("length of input data unexpected for ByPolygonVertex mapping");
  554. return;
  555. }
  556. unsigned int next = 0;
  557. BOOST_FOREACH(int i, uvIndices) {
  558. if(static_cast<size_t>(i) >= tempUV.size()) {
  559. DOMError("index out of range",&GetRequiredElement(source,indexDataElementName));
  560. }
  561. data_out[next++] = tempUV[i];
  562. }
  563. }
  564. else {
  565. FBXImporter::LogError(Formatter::format("ignoring vertex data channel, access type not implemented: ")
  566. << MappingInformationType << "," << ReferenceInformationType);
  567. }
  568. }
  569. // ------------------------------------------------------------------------------------------------
  570. void MeshGeometry::ReadVertexDataNormals(std::vector<aiVector3D>& normals_out, const Scope& source,
  571. const std::string& MappingInformationType,
  572. const std::string& ReferenceInformationType)
  573. {
  574. ResolveVertexDataArray(normals_out,source,MappingInformationType,ReferenceInformationType,
  575. "Normals",
  576. "NormalsIndex",
  577. vertices.size(),
  578. mapping_counts,
  579. mapping_offsets,
  580. mappings);
  581. }
  582. // ------------------------------------------------------------------------------------------------
  583. void MeshGeometry::ReadVertexDataUV(std::vector<aiVector2D>& uv_out, const Scope& source,
  584. const std::string& MappingInformationType,
  585. const std::string& ReferenceInformationType)
  586. {
  587. ResolveVertexDataArray(uv_out,source,MappingInformationType,ReferenceInformationType,
  588. "UV",
  589. "UVIndex",
  590. vertices.size(),
  591. mapping_counts,
  592. mapping_offsets,
  593. mappings);
  594. }
  595. // ------------------------------------------------------------------------------------------------
  596. void MeshGeometry::ReadVertexDataColors(std::vector<aiColor4D>& colors_out, const Scope& source,
  597. const std::string& MappingInformationType,
  598. const std::string& ReferenceInformationType)
  599. {
  600. ResolveVertexDataArray(colors_out,source,MappingInformationType,ReferenceInformationType,
  601. "Color",
  602. "ColorIndex",
  603. vertices.size(),
  604. mapping_counts,
  605. mapping_offsets,
  606. mappings);
  607. }
  608. // ------------------------------------------------------------------------------------------------
  609. void MeshGeometry::ReadVertexDataTangents(std::vector<aiVector3D>& tangents_out, const Scope& source,
  610. const std::string& MappingInformationType,
  611. const std::string& ReferenceInformationType)
  612. {
  613. ResolveVertexDataArray(tangents_out,source,MappingInformationType,ReferenceInformationType,
  614. "Tangent",
  615. "TangentIndex",
  616. vertices.size(),
  617. mapping_counts,
  618. mapping_offsets,
  619. mappings);
  620. }
  621. // ------------------------------------------------------------------------------------------------
  622. void MeshGeometry::ReadVertexDataBinormals(std::vector<aiVector3D>& binormals_out, const Scope& source,
  623. const std::string& MappingInformationType,
  624. const std::string& ReferenceInformationType)
  625. {
  626. ResolveVertexDataArray(binormals_out,source,MappingInformationType,ReferenceInformationType,
  627. "Binormal",
  628. "BinormalIndex",
  629. vertices.size(),
  630. mapping_counts,
  631. mapping_offsets,
  632. mappings);
  633. }
  634. // ------------------------------------------------------------------------------------------------
  635. void MeshGeometry::ReadVertexDataMaterials(std::vector<unsigned int>& materials_out, const Scope& source,
  636. const std::string& MappingInformationType,
  637. const std::string& ReferenceInformationType)
  638. {
  639. const size_t face_count = faces.size();
  640. ai_assert(face_count);
  641. // materials are handled separately. First of all, they are assigned per-face
  642. // and not per polyvert. Secondly, ReferenceInformationType=IndexToDirect
  643. // has a slightly different meaning for materials.
  644. ReadVectorDataArray(materials_out,GetRequiredElement(source,"Materials"));
  645. if (MappingInformationType == "AllSame") {
  646. // easy - same material for all faces
  647. if (materials_out.empty()) {
  648. FBXImporter::LogError(Formatter::format("expected material index, ignoring"));
  649. return;
  650. }
  651. else if (materials_out.size() > 1) {
  652. FBXImporter::LogWarn(Formatter::format("expected only a single material index, ignoring all except the first one"));
  653. materials_out.clear();
  654. }
  655. materials.assign(vertices.size(),materials_out[0]);
  656. }
  657. else if (MappingInformationType == "ByPolygon" && ReferenceInformationType == "IndexToDirect") {
  658. materials.resize(face_count);
  659. if(materials_out.size() != face_count) {
  660. FBXImporter::LogError(Formatter::format("length of input data unexpected for ByPolygon mapping: ")
  661. << materials_out.size() << ", expected " << face_count
  662. );
  663. return;
  664. }
  665. }
  666. else {
  667. FBXImporter::LogError(Formatter::format("ignoring material assignments, access type not implemented: ")
  668. << MappingInformationType << "," << ReferenceInformationType);
  669. }
  670. }
  671. // ------------------------------------------------------------------------------------------------
  672. Document::Document(const Parser& parser, const ImportSettings& settings)
  673. : parser(parser)
  674. , settings(settings)
  675. {
  676. const Scope& sc = parser.GetRootScope();
  677. const Element* const eobjects = sc["Objects"];
  678. if(!eobjects || !eobjects->Compound()) {
  679. DOMError("no Objects dictionary found");
  680. }
  681. const Scope* const sobjects = eobjects->Compound();
  682. BOOST_FOREACH(const ElementMap::value_type& el, sobjects->Elements()) {
  683. // extract ID
  684. const TokenList& tok = el.second->Tokens();
  685. if (tok.empty()) {
  686. DOMError("expected ID after object key",el.second);
  687. }
  688. const char* err;
  689. const uint64_t id = ParseTokenAsID(*tok[0], err);
  690. if(err) {
  691. DOMError(err,el.second);
  692. }
  693. objects[id] = new LazyObject(*el.second, settings);
  694. // DEBUG - evaluate all objects
  695. const Object* o = objects[id]->Get();
  696. }
  697. }
  698. // ------------------------------------------------------------------------------------------------
  699. Document::~Document()
  700. {
  701. }
  702. } // !FBX
  703. } // !Assimp
  704. #endif