LWOLoader.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the LWO importer class */
  35. #include "AssimpPCH.h"
  36. // internal headers
  37. #include "LWOLoader.h"
  38. #include "MaterialSystem.h"
  39. #include "StringComparison.h"
  40. #include "SGSpatialSort.h"
  41. #include "ByteSwap.h"
  42. #include "ProcessHelper.h"
  43. using namespace Assimp;
  44. // ------------------------------------------------------------------------------------------------
  45. // Constructor to be privately used by Importer
  46. LWOImporter::LWOImporter()
  47. {
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Destructor, private as well
  51. LWOImporter::~LWOImporter()
  52. {
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Returns whether the class can handle the format of the given file.
  56. bool LWOImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  57. {
  58. // simple check of file extension is enough for the moment
  59. std::string::size_type pos = pFile.find_last_of('.');
  60. // no file extension - can't read
  61. if( pos == std::string::npos)return false;
  62. std::string extension = pFile.substr( pos);
  63. if (extension.length() < 4)return false;
  64. if (extension[0] != '.')return false;
  65. return ! (extension[1] != 'l' && extension[1] != 'L' ||
  66. extension[2] != 'w' && extension[2] != 'W' &&
  67. extension[2] != 'x' && extension[2] != 'X' ||
  68. extension[3] != 'o' && extension[3] != 'O');
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // Setup configuration properties
  72. void LWOImporter::SetupProperties(const Importer* pImp)
  73. {
  74. configSpeedFlag = ( 0 != pImp->GetPropertyInteger(AI_CONFIG_FAVOUR_SPEED,0) ? true : false);
  75. configLayerIndex = pImp->GetPropertyInteger (AI_CONFIG_IMPORT_LWO_ONE_LAYER_ONLY,0xffffffff);
  76. configLayerName = pImp->GetPropertyString (AI_CONFIG_IMPORT_LWO_ONE_LAYER_ONLY,"");
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Imports the given file into the given scene structure.
  80. void LWOImporter::InternReadFile( const std::string& pFile,
  81. aiScene* pScene,
  82. IOSystem* pIOHandler)
  83. {
  84. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  85. // Check whether we can read from the file
  86. if( file.get() == NULL)
  87. throw new ImportErrorException( "Failed to open LWO file " + pFile + ".");
  88. if((this->fileSize = (unsigned int)file->FileSize()) < 12)
  89. throw new ImportErrorException("LWO: The file is too small to contain the IFF header");
  90. // allocate storage and copy the contents of the file to a memory buffer
  91. std::vector< uint8_t > mBuffer(fileSize);
  92. file->Read( &mBuffer[0], 1, fileSize);
  93. this->pScene = pScene;
  94. // determine the type of the file
  95. uint32_t fileType;
  96. const char* sz = IFF::ReadHeader(&mBuffer[0],fileType);
  97. if (sz)throw new ImportErrorException(sz);
  98. mFileBuffer = &mBuffer[0] + 12;
  99. fileSize -= 12;
  100. hasNamedLayer = false;
  101. // create temporary storage on the stack but store pointers to it in the class
  102. // instance. Therefore everything will be destructed properly if an exception
  103. // is thrown and we needn't take care of that.
  104. LayerList _mLayers;
  105. SurfaceList _mSurfaces;
  106. TagList _mTags;
  107. TagMappingTable _mMapping;
  108. mLayers = &_mLayers;
  109. mTags = &_mTags;
  110. mMapping = &_mMapping;
  111. mSurfaces = &_mSurfaces;
  112. // Allocate a default layer (layer indices are 1-based from now)
  113. mLayers->push_back(Layer());
  114. mCurLayer = &mLayers->back();
  115. mCurLayer->mName = "<LWODefault>";
  116. // old lightwave file format (prior to v6)
  117. if (AI_LWO_FOURCC_LWOB == fileType)
  118. {
  119. DefaultLogger::get()->info("LWO file format: LWOB (<= LightWave 5.5)");
  120. mIsLWO2 = false;
  121. LoadLWOBFile();
  122. }
  123. // New lightwave format
  124. else if (AI_LWO_FOURCC_LWO2 == fileType)
  125. {
  126. DefaultLogger::get()->info("LWO file format: LWO2 (>= LightWave 6)");
  127. }
  128. // MODO file format
  129. else if (AI_LWO_FOURCC_LXOB == fileType)
  130. {
  131. DefaultLogger::get()->info("LWO file format: LXOB (Modo)");
  132. }
  133. // we don't know this format
  134. else
  135. {
  136. char szBuff[5];
  137. szBuff[0] = (char)(fileType >> 24u);
  138. szBuff[1] = (char)(fileType >> 16u);
  139. szBuff[2] = (char)(fileType >> 8u);
  140. szBuff[3] = (char)(fileType);
  141. throw new ImportErrorException(std::string("Unknown LWO sub format: ") + szBuff);
  142. }
  143. if (AI_LWO_FOURCC_LWOB != fileType)
  144. {
  145. mIsLWO2 = true;
  146. LoadLWO2File();
  147. // The newer lightwave format allows the user to configure the
  148. // loader that just one layer is used. If this is the case
  149. // we need to check now whether the requested layer has been found.
  150. if (0xffffffff != configLayerIndex && configLayerIndex > mLayers->size())
  151. throw new ImportErrorException("LWO2: The requested layer was not found");
  152. if (configLayerName.length() && !hasNamedLayer)
  153. {
  154. throw new ImportErrorException("LWO2: Unable to find the requested layer: "
  155. + configLayerName);
  156. }
  157. }
  158. // now, as we have loaded all data, we can resolve cross-referenced tags and clips
  159. ResolveTags();
  160. ResolveClips();
  161. // now process all layers and build meshes and nodes
  162. std::vector<aiMesh*> apcMeshes;
  163. std::vector<aiNode*> apcNodes;
  164. apcNodes. reserve(mLayers->size());
  165. apcMeshes.reserve(mLayers->size()*std::min(((unsigned int)mSurfaces->size()/2u), 1u));
  166. unsigned int iDefaultSurface = 0xffffffff; // index of the default surface
  167. for (LayerList::iterator lit = mLayers->begin(), lend = mLayers->end();
  168. lit != lend;++lit)
  169. {
  170. LWO::Layer& layer = *lit;
  171. if (layer.skip)continue;
  172. // I don't know whether there could be dummy layers, but it would be possible
  173. const unsigned int meshStart = (unsigned int)apcMeshes.size();
  174. if (!layer.mFaces.empty() && !layer.mTempPoints.empty())
  175. {
  176. // now sort all faces by the surfaces assigned to them
  177. typedef std::vector<unsigned int> SortedRep;
  178. std::vector<SortedRep> pSorted(mSurfaces->size()+1);
  179. unsigned int i = 0;
  180. for (FaceList::iterator it = layer.mFaces.begin(), end = layer.mFaces.end();
  181. it != end;++it,++i)
  182. {
  183. unsigned int idx = (*it).surfaceIndex;
  184. if (idx >= mTags->size())
  185. {
  186. DefaultLogger::get()->warn("LWO: Invalid face surface index");
  187. idx = 0xffffffff;
  188. }
  189. if(0xffffffff == idx || 0xffffffff == (idx = _mMapping[idx]))
  190. {
  191. if (0xffffffff == iDefaultSurface)
  192. {
  193. iDefaultSurface = (unsigned int)mSurfaces->size();
  194. mSurfaces->push_back(LWO::Surface());
  195. LWO::Surface& surf = mSurfaces->back();
  196. surf.mColor.r = surf.mColor.g = surf.mColor.b = 0.6f;
  197. }
  198. idx = iDefaultSurface;
  199. }
  200. pSorted[idx].push_back(i);
  201. }
  202. if (0xffffffff == iDefaultSurface)pSorted.erase(pSorted.end()-1);
  203. for (unsigned int p = 0,i = 0;i < mSurfaces->size();++i)
  204. {
  205. SortedRep& sorted = pSorted[i];
  206. if (sorted.empty())continue;
  207. // generate the mesh
  208. aiMesh* mesh = new aiMesh();
  209. apcMeshes.push_back(mesh);
  210. mesh->mNumFaces = (unsigned int)sorted.size();
  211. // count the number of vertices
  212. SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
  213. for (;it != end;++it)
  214. {
  215. mesh->mNumVertices += layer.mFaces[*it].mNumIndices;
  216. }
  217. aiVector3D *nrm = NULL, * pv = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  218. aiFace* pf = mesh->mFaces = new aiFace[mesh->mNumFaces];
  219. mesh->mMaterialIndex = i;
  220. // find out which vertex color channels and which texture coordinate
  221. // channels are really required by the material attached to this mesh
  222. unsigned int vUVChannelIndices[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  223. unsigned int vVColorIndices[AI_MAX_NUMBER_OF_COLOR_SETS];
  224. #if _DEBUG
  225. for (unsigned int mui = 0; mui < AI_MAX_NUMBER_OF_TEXTURECOORDS;++mui )
  226. vUVChannelIndices[mui] = 0xffffffff;
  227. for (unsigned int mui = 0; mui < AI_MAX_NUMBER_OF_COLOR_SETS;++mui )
  228. vVColorIndices[mui] = 0xffffffff;
  229. #endif
  230. FindUVChannels(_mSurfaces[i],layer,vUVChannelIndices);
  231. FindVCChannels(_mSurfaces[i],layer,vVColorIndices);
  232. // allocate storage for UV and CV channels
  233. aiVector3D* pvUV[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  234. for (unsigned int mui = 0; mui < AI_MAX_NUMBER_OF_TEXTURECOORDS;++mui )
  235. {
  236. if (0xffffffff == vUVChannelIndices[mui])break;
  237. pvUV[mui] = mesh->mTextureCoords[mui] = new aiVector3D[mesh->mNumVertices];
  238. // LightWave doesn't support more than 2 UV components
  239. // so we can directly setup this value
  240. mesh->mNumUVComponents[0] = 2;
  241. }
  242. if (layer.mNormals.name.length())
  243. nrm = mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  244. aiColor4D* pvVC[AI_MAX_NUMBER_OF_COLOR_SETS];
  245. for (unsigned int mui = 0; mui < AI_MAX_NUMBER_OF_COLOR_SETS;++mui)
  246. {
  247. if (0xffffffff == vVColorIndices[mui])break;
  248. pvVC[mui] = mesh->mColors[mui] = new aiColor4D[mesh->mNumVertices];
  249. }
  250. // we would not need this extra array, but the code is much cleaner if we use it
  251. // FIX: we can use the referrer ID array here. invalidate its contents
  252. // before we resize it to avoid a unnecessary memcpy
  253. std::vector<unsigned int>& smoothingGroups = layer.mPointReferrers;
  254. smoothingGroups.erase (smoothingGroups.begin(),smoothingGroups.end());
  255. smoothingGroups.resize(mesh->mNumFaces,0);
  256. // now convert all faces
  257. unsigned int vert = 0;
  258. std::vector<unsigned int>::iterator outIt = smoothingGroups.begin();
  259. for (it = sorted.begin(); it != end;++it,++outIt)
  260. {
  261. const LWO::Face& face = layer.mFaces[*it];
  262. *outIt = face.smoothGroup;
  263. // copy all vertices
  264. for (unsigned int q = 0; q < face.mNumIndices;++q)
  265. {
  266. register unsigned int idx = face.mIndices[q];
  267. *pv = layer.mTempPoints[idx] + layer.mPivot;
  268. pv->z *= -1.0f; // DX to OGL
  269. pv++;
  270. // process UV coordinates
  271. for (unsigned int w = 0; w < AI_MAX_NUMBER_OF_TEXTURECOORDS;++w)
  272. {
  273. if (0xffffffff == vUVChannelIndices[w])break;
  274. aiVector3D*& pp = pvUV[w];
  275. const aiVector2D& src = ((aiVector2D*)&layer.mUVChannels[vUVChannelIndices[w]].rawData[0])[idx];
  276. pp->x = src.x;
  277. pp->y = src.y;
  278. pp++;
  279. }
  280. // process normals (MODO extension)
  281. if (nrm)
  282. {
  283. *nrm++ = ((aiVector3D*)&layer.mNormals.rawData[0])[idx];
  284. }
  285. // process vertex colors
  286. for (unsigned int w = 0; w < AI_MAX_NUMBER_OF_COLOR_SETS;++w)
  287. {
  288. if (0xffffffff == vVColorIndices[w])break;
  289. *pvVC[w] = ((aiColor4D*)&layer.mVColorChannels[vVColorIndices[w]].rawData[0])[idx];
  290. // If a RGB color map is explicitly requested delete the
  291. // alpha channel - it could theoretically be != 1.
  292. if(_mSurfaces[i].mVCMapType == AI_LWO_RGB)
  293. pvVC[w]->a = 1.f;
  294. pvVC[w]++;
  295. }
  296. #if 0
  297. // process vertex weights - not yet supported
  298. for (unsigned int w = 0; w < layer.mWeightChannels.size();++w)
  299. {
  300. }
  301. #endif
  302. face.mIndices[q] = vert + (face.mNumIndices-q-1);
  303. }
  304. vert += face.mNumIndices;
  305. pf->mIndices = face.mIndices;
  306. pf->mNumIndices = face.mNumIndices;
  307. unsigned int** p = (unsigned int**)&face.mIndices;*p = NULL; // make sure it won't be deleted
  308. pf++;
  309. }
  310. if (!mesh->mNormals)
  311. {
  312. // Compute normal vectors for the mesh - we can't use our GenSmoothNormal-
  313. // Step here since it wouldn't handle smoothing groups correctly for LWO.
  314. // So we use a separate implementation.
  315. ComputeNormals(mesh,smoothingGroups,_mSurfaces[i]);
  316. }
  317. else DefaultLogger::get()->debug("LWO2: No need to compute normals, they're already there");
  318. ++p;
  319. }
  320. }
  321. // Generate nodes to render the mesh. Store the parent index
  322. // in the mParent member of the nodes
  323. aiNode* pcNode = new aiNode();
  324. apcNodes.push_back(pcNode);
  325. pcNode->mName.Set(layer.mName);
  326. pcNode->mParent = (aiNode*)(uintptr_t)(layer.mParent);
  327. pcNode->mNumMeshes = (unsigned int)apcMeshes.size() - meshStart;
  328. pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
  329. for (unsigned int p = 0; p < pcNode->mNumMeshes;++p)
  330. pcNode->mMeshes[p] = p + meshStart;
  331. }
  332. if (apcNodes.empty() || apcMeshes.empty())
  333. throw new ImportErrorException("LWO: No meshes loaded");
  334. // The RemoveRedundantMaterials step will clean this up later
  335. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = (unsigned int)mSurfaces->size()];
  336. for (unsigned int mat = 0; mat < pScene->mNumMaterials;++mat)
  337. {
  338. MaterialHelper* pcMat = new MaterialHelper();
  339. pScene->mMaterials[mat] = pcMat;
  340. ConvertMaterial((*mSurfaces)[mat],pcMat);
  341. }
  342. // copy the meshes to the output structure
  343. if (apcMeshes.size()) // shouldn't occur, just to be sure we don't crash
  344. {
  345. pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes = (unsigned int)apcMeshes.size() ];
  346. ::memcpy(pScene->mMeshes,&apcMeshes[0],pScene->mNumMeshes*sizeof(void*));
  347. }
  348. // generate the final node graph
  349. GenerateNodeGraph(apcNodes);
  350. }
  351. // ------------------------------------------------------------------------------------------------
  352. void LWOImporter::ComputeNormals(aiMesh* mesh, const std::vector<unsigned int>& smoothingGroups,
  353. const LWO::Surface& surface)
  354. {
  355. // allocate output storage
  356. mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  357. // First generate per-face normals
  358. aiVector3D* out;
  359. std::vector<aiVector3D> faceNormals;
  360. if (!surface.mMaximumSmoothAngle)
  361. out = mesh->mNormals;
  362. else
  363. {
  364. faceNormals.resize(mesh->mNumVertices);
  365. out = &faceNormals[0];
  366. }
  367. aiFace* begin = mesh->mFaces, *const end = mesh->mFaces+mesh->mNumFaces;
  368. for (; begin != end; ++begin)
  369. {
  370. aiFace& face = *begin;
  371. // LWO doc: "the normal is defined as the cross product of the first and last edges"
  372. aiVector3D* pV1 = mesh->mVertices + face.mIndices[0];
  373. aiVector3D* pV2 = mesh->mVertices + face.mIndices[1];
  374. aiVector3D* pV3 = mesh->mVertices + face.mIndices[face.mNumIndices-1];
  375. aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).Normalize();
  376. for (unsigned int i = 0; i < face.mNumIndices;++i)
  377. out[face.mIndices[i]] = vNor;
  378. }
  379. if (!surface.mMaximumSmoothAngle)return;
  380. const float posEpsilon = ComputePositionEpsilon(mesh);
  381. // now generate the spatial sort tree
  382. SGSpatialSort sSort;
  383. std::vector<unsigned int>::const_iterator it = smoothingGroups.begin();
  384. for( begin = mesh->mFaces; begin != end; ++begin, ++it)
  385. {
  386. aiFace& face = *begin;
  387. for (unsigned int i = 0; i < face.mNumIndices;++i)
  388. {
  389. register unsigned int tt = face.mIndices[i];
  390. sSort.Add(mesh->mVertices[tt],tt,*it);
  391. }
  392. }
  393. // sort everything - this takes O(nlogn) time
  394. sSort.Prepare();
  395. std::vector<unsigned int> poResult;
  396. poResult.reserve(20);
  397. // generate vertex normals. We have O(logn) for the binary lookup, which we need
  398. // for n elements, thus the EXPECTED complexity is O(nlogn)
  399. if (surface.mMaximumSmoothAngle < 3.f && !configSpeedFlag)
  400. {
  401. const float fLimit = cos(surface.mMaximumSmoothAngle);
  402. for( begin = mesh->mFaces, it = smoothingGroups.begin(); begin != end; ++begin, ++it)
  403. {
  404. register unsigned int sg = *it;
  405. aiFace& face = *begin;
  406. unsigned int* beginIdx = face.mIndices, *const endIdx = face.mIndices+face.mNumIndices;
  407. for (; beginIdx != endIdx; ++beginIdx)
  408. {
  409. register unsigned int idx = *beginIdx;
  410. sSort.FindPositions(mesh->mVertices[idx],sg,posEpsilon,poResult,true);
  411. std::vector<unsigned int>::const_iterator a, end = poResult.end();
  412. aiVector3D vNormals;
  413. for (a = poResult.begin();a != end;++a)
  414. {
  415. const aiVector3D& v = faceNormals[*a];
  416. if (v * faceNormals[idx] < fLimit)continue;
  417. vNormals += v;
  418. }
  419. vNormals.Normalize();
  420. mesh->mNormals[idx] = vNormals;
  421. }
  422. }
  423. }
  424. else // faster code path in case there is no smooth angle
  425. {
  426. std::vector<bool> vertexDone(mesh->mNumVertices,false);
  427. for( begin = mesh->mFaces, it = smoothingGroups.begin(); begin != end; ++begin, ++it)
  428. {
  429. register unsigned int sg = *it;
  430. aiFace& face = *begin;
  431. unsigned int* beginIdx = face.mIndices, *const endIdx = face.mIndices+face.mNumIndices;
  432. for (; beginIdx != endIdx; ++beginIdx)
  433. {
  434. register unsigned int idx = *beginIdx;
  435. if (vertexDone[idx])continue;
  436. sSort.FindPositions(mesh->mVertices[idx],sg,posEpsilon,poResult,true);
  437. std::vector<unsigned int>::const_iterator a, end = poResult.end();
  438. aiVector3D vNormals;
  439. for (a = poResult.begin();a != end;++a)
  440. {
  441. const aiVector3D& v = faceNormals[*a];
  442. vNormals += v;
  443. }
  444. vNormals.Normalize();
  445. for (a = poResult.begin();a != end;++a)
  446. {
  447. mesh->mNormals[*a] = vNormals;
  448. vertexDone[*a] = true;
  449. }
  450. }
  451. }
  452. }
  453. }
  454. // ------------------------------------------------------------------------------------------------
  455. void LWOImporter::AddChildren(aiNode* node, uintptr_t parent, std::vector<aiNode*>& apcNodes)
  456. {
  457. for (uintptr_t i = 0; i < (uintptr_t)apcNodes.size();++i)
  458. {
  459. if (i == parent)continue;
  460. if (apcNodes[i] && (uintptr_t)apcNodes[i]->mParent == parent)++node->mNumChildren;
  461. }
  462. if (node->mNumChildren)
  463. {
  464. node->mChildren = new aiNode* [ node->mNumChildren ];
  465. for (uintptr_t i = 0, p = 0; i < (uintptr_t)apcNodes.size();++i)
  466. {
  467. if (i == parent)continue;
  468. if (apcNodes[i] && parent == (uintptr_t)(apcNodes[i]->mParent))
  469. {
  470. node->mChildren[p++] = apcNodes[i];
  471. apcNodes[i]->mParent = node;
  472. // recursively add more children
  473. AddChildren(apcNodes[i],i,apcNodes);
  474. apcNodes[i] = NULL;
  475. }
  476. }
  477. }
  478. }
  479. // ------------------------------------------------------------------------------------------------
  480. void LWOImporter::GenerateNodeGraph(std::vector<aiNode*>& apcNodes)
  481. {
  482. // now generate the final nodegraph - generate a root node
  483. pScene->mRootNode = new aiNode();
  484. pScene->mRootNode->mName.Set("<LWORoot>");
  485. AddChildren(pScene->mRootNode,0,apcNodes);
  486. unsigned int extra = 0;
  487. for (unsigned int i = 0; i < apcNodes.size();++i)
  488. if (apcNodes[i] && apcNodes[i]->mNumMeshes)++extra;
  489. if (extra)
  490. {
  491. // we need to add extra nodes to the root
  492. const unsigned int newSize = extra + pScene->mRootNode->mNumChildren;
  493. aiNode** const apcNewNodes = new aiNode*[newSize];
  494. if((extra = pScene->mRootNode->mNumChildren))
  495. ::memcpy(apcNewNodes,pScene->mRootNode->mChildren,extra*sizeof(void*));
  496. aiNode** cc = apcNewNodes+extra;
  497. for (unsigned int i = 0; i < apcNodes.size();++i)
  498. {
  499. if (apcNodes[i] && apcNodes[i]->mNumMeshes)
  500. {
  501. *cc++ = apcNodes[i];
  502. apcNodes[i]->mParent = pScene->mRootNode;
  503. // recursively add more children
  504. AddChildren(apcNodes[i],i,apcNodes);
  505. apcNodes[i] = NULL;
  506. }
  507. }
  508. delete[] pScene->mRootNode->mChildren;
  509. pScene->mRootNode->mChildren = apcNewNodes;
  510. pScene->mRootNode->mNumChildren = newSize;
  511. }
  512. if (!pScene->mRootNode->mNumChildren)throw new ImportErrorException("LWO: Unable to build a valid node graph");
  513. // remove a single root node
  514. // TODO: implement directly in the above loop, no need to deallocate here
  515. if (1 == pScene->mRootNode->mNumChildren)
  516. {
  517. aiNode* pc = pScene->mRootNode->mChildren[0];
  518. pc->mParent = pScene->mRootNode->mChildren[0] = NULL;
  519. delete pScene->mRootNode;
  520. pScene->mRootNode = pc;
  521. }
  522. }
  523. // ------------------------------------------------------------------------------------------------
  524. void LWOImporter::ResolveTags()
  525. {
  526. // --- this function is used for both LWO2 and LWOB
  527. mMapping->resize(mTags->size(),0xffffffff);
  528. for (unsigned int a = 0; a < mTags->size();++a)
  529. {
  530. for (unsigned int i = 0; i < mSurfaces->size();++i)
  531. {
  532. const std::string& c = (*mTags)[a];
  533. const std::string& d = (*mSurfaces)[i].mName;
  534. if (!ASSIMP_stricmp(c,d))
  535. {
  536. (*mMapping)[a] = i;
  537. break;
  538. }
  539. }
  540. }
  541. }
  542. // ------------------------------------------------------------------------------------------------
  543. void LWOImporter::ResolveClips()
  544. {
  545. for( unsigned int i = 0; i < mClips.size();++i)
  546. {
  547. Clip& clip = mClips[i];
  548. if (Clip::REF == clip.type)
  549. {
  550. if (clip.clipRef >= mClips.size())
  551. {
  552. DefaultLogger::get()->error("LWO2: Clip referrer index is out of range");
  553. clip.clipRef = 0;
  554. }
  555. Clip& dest = mClips[clip.clipRef];
  556. if (Clip::REF == dest.type)
  557. {
  558. DefaultLogger::get()->error("LWO2: Clip references another clip reference");
  559. clip.type = Clip::UNSUPPORTED;
  560. }
  561. else
  562. {
  563. clip.path = dest.path;
  564. clip.type = dest.type;
  565. }
  566. }
  567. }
  568. }
  569. // ------------------------------------------------------------------------------------------------
  570. void LWOImporter::AdjustTexturePath(std::string& out)
  571. {
  572. // --- this function is used for both LWO2 and LWOB
  573. if (!mIsLWO2 && ::strstr(out.c_str(), "(sequence)"))
  574. {
  575. // remove the (sequence) and append 000
  576. DefaultLogger::get()->info("LWOB: Sequence of animated texture found. It will be ignored");
  577. out = out.substr(0,out.length()-10) + "000";
  578. }
  579. // format: drive:path/file - we need to insert a slash after the drive
  580. std::string::size_type n = out.find_first_of(':');
  581. if (std::string::npos != n)
  582. {
  583. out.insert(n+1,"/");
  584. }
  585. }
  586. // ------------------------------------------------------------------------------------------------
  587. void LWOImporter::LoadLWOTags(unsigned int size)
  588. {
  589. // --- this function is used for both LWO2 and LWOB
  590. const char* szCur = (const char*)mFileBuffer, *szLast = szCur;
  591. const char* const szEnd = szLast+size;
  592. while (szCur < szEnd)
  593. {
  594. if (!(*szCur))
  595. {
  596. const unsigned int len = (unsigned int)(szCur-szLast);
  597. mTags->push_back(std::string(szLast,len));
  598. szCur += len & 1;
  599. szLast = szCur;
  600. }
  601. szCur++;
  602. }
  603. }
  604. // ------------------------------------------------------------------------------------------------
  605. void LWOImporter::LoadLWOPoints(unsigned int length)
  606. {
  607. // --- this function is used for both LWO2 and LWOB but for
  608. // LWO2 we need to allocate 25% more storage - it could be we'll
  609. // need to duplicate some points later.
  610. register unsigned int regularSize = (unsigned int)mCurLayer->mTempPoints.size() + length / 12;
  611. if (mIsLWO2)
  612. {
  613. mCurLayer->mTempPoints.reserve ( regularSize + (regularSize>>2u) );
  614. mCurLayer->mTempPoints.resize ( regularSize );
  615. // initialize all point referrers with the default values
  616. mCurLayer->mPointReferrers.reserve ( regularSize + (regularSize>>2u) );
  617. mCurLayer->mPointReferrers.resize ( regularSize, 0xffffffff );
  618. }
  619. else mCurLayer->mTempPoints.resize( regularSize );
  620. // perform endianess conversions
  621. #ifndef AI_BUILD_BIG_ENDIAN
  622. for (unsigned int i = 0; i < length>>2;++i)
  623. ByteSwap::Swap4( mFileBuffer + (i << 2));
  624. #endif
  625. ::memcpy(&mCurLayer->mTempPoints[0],mFileBuffer,length);
  626. }
  627. // ------------------------------------------------------------------------------------------------
  628. void LWOImporter::LoadLWO2Polygons(unsigned int length)
  629. {
  630. LE_NCONST uint16_t* const end = (LE_NCONST uint16_t*)(mFileBuffer+length);
  631. uint32_t type = GetU4();
  632. // Determine the type of the polygons
  633. switch (type)
  634. {
  635. case AI_LWO_PTCH:
  636. case AI_LWO_FACE:
  637. break;
  638. default:
  639. DefaultLogger::get()->warn("LWO2: Unsupported polygon type (PTCH and FACE are supported)");
  640. }
  641. // first find out how many faces and vertices we'll finally need
  642. uint16_t* cursor = (uint16_t*)mFileBuffer;
  643. unsigned int iNumFaces = 0,iNumVertices = 0;
  644. CountVertsAndFacesLWO2(iNumVertices,iNumFaces,cursor,end);
  645. // allocate the output array and copy face indices
  646. if (iNumFaces)
  647. {
  648. cursor = (uint16_t*)mFileBuffer;
  649. mCurLayer->mFaces.resize(iNumFaces);
  650. FaceList::iterator it = mCurLayer->mFaces.begin();
  651. CopyFaceIndicesLWO2(it,cursor,end);
  652. }
  653. }
  654. // ------------------------------------------------------------------------------------------------
  655. void LWOImporter::CountVertsAndFacesLWO2(unsigned int& verts, unsigned int& faces,
  656. uint16_t*& cursor, const uint16_t* const end, unsigned int max)
  657. {
  658. while (cursor < end && max--)
  659. {
  660. AI_LSWAP2P(cursor);
  661. uint16_t numIndices = *cursor++;
  662. numIndices &= 0x03FF;
  663. verts += numIndices;++faces;
  664. for(uint16_t i = 0; i < numIndices; i++)
  665. ReadVSizedIntLWO2((uint8_t*&)cursor);
  666. }
  667. }
  668. // ------------------------------------------------------------------------------------------------
  669. void LWOImporter::CopyFaceIndicesLWO2(FaceList::iterator& it,
  670. uint16_t*& cursor,
  671. const uint16_t* const end)
  672. {
  673. while (cursor < end)
  674. {
  675. LWO::Face& face = *it;++it;
  676. if((face.mNumIndices = (*cursor++) & 0x03FF)) // swapping has already been done
  677. {
  678. face.mIndices = new unsigned int[face.mNumIndices];
  679. for(unsigned int i = 0; i < face.mNumIndices; i++)
  680. {
  681. face.mIndices[i] = ReadVSizedIntLWO2((uint8_t*&)cursor) + mCurLayer->mPointIDXOfs;
  682. if(face.mIndices[i] > mCurLayer->mTempPoints.size())
  683. {
  684. DefaultLogger::get()->warn("LWO2: face index is out of range");
  685. face.mIndices[i] = (unsigned int)mCurLayer->mTempPoints.size()-1;
  686. }
  687. }
  688. }
  689. else DefaultLogger::get()->warn("LWO2: face has 0 indices");
  690. }
  691. }
  692. // ------------------------------------------------------------------------------------------------
  693. void LWOImporter::LoadLWO2PolygonTags(unsigned int length)
  694. {
  695. LE_NCONST uint8_t* const end = mFileBuffer+length;
  696. AI_LWO_VALIDATE_CHUNK_LENGTH(length,PTAG,4);
  697. uint32_t type = GetU4();
  698. if (type != AI_LWO_SURF && type != AI_LWO_SMGP)
  699. return;
  700. while (mFileBuffer < end)
  701. {
  702. unsigned int i = ReadVSizedIntLWO2(mFileBuffer) + mCurLayer->mFaceIDXOfs;
  703. unsigned int j = GetU2();
  704. if (i >= mCurLayer->mFaces.size())
  705. {
  706. DefaultLogger::get()->warn("LWO2: face index in PTAG is out of range");
  707. continue;
  708. }
  709. switch (type)
  710. {
  711. case AI_LWO_SURF:
  712. mCurLayer->mFaces[i].surfaceIndex = j;
  713. break;
  714. case AI_LWO_SMGP:
  715. mCurLayer->mFaces[i].smoothGroup = j;
  716. break;
  717. };
  718. }
  719. }
  720. // ------------------------------------------------------------------------------------------------
  721. template <class T>
  722. VMapEntry* FindEntry(std::vector< T >& list,const std::string& name, bool perPoly)
  723. {
  724. for (typename std::vector< T >::iterator it = list.begin(), end = list.end();
  725. it != end; ++it)
  726. {
  727. if ((*it).name == name)
  728. {
  729. if (!perPoly)
  730. {
  731. DefaultLogger::get()->warn("LWO2: Found two VMAP sections with equal names");
  732. }
  733. return &(*it);
  734. }
  735. }
  736. list.push_back( T() );
  737. VMapEntry* p = &list.back();
  738. p->name = name;
  739. return p;
  740. }
  741. // ------------------------------------------------------------------------------------------------
  742. template <class T>
  743. inline void CreateNewEntry(T& chan, unsigned int srcIdx)
  744. {
  745. if (!chan.name.length())return;
  746. chan.abAssigned[srcIdx] = true;
  747. chan.abAssigned.resize(chan.abAssigned.size()+1,false);
  748. for (unsigned int a = 0; a < chan.dims;++a)
  749. chan.rawData.push_back(chan.rawData[srcIdx*chan.dims+a]);
  750. }
  751. // ------------------------------------------------------------------------------------------------
  752. template <class T>
  753. inline void CreateNewEntry(std::vector< T >& list, unsigned int srcIdx)
  754. {
  755. for (typename std::vector< T >::iterator
  756. it = list.begin(), end = list.end();
  757. it != end;++it)
  758. {
  759. CreateNewEntry( *it, srcIdx );
  760. }
  761. }
  762. // ------------------------------------------------------------------------------------------------
  763. void LWOImporter::DoRecursiveVMAPAssignment(VMapEntry* base, unsigned int numRead,
  764. unsigned int idx, float* data)
  765. {
  766. ai_assert(NULL != data);
  767. LWO::ReferrerList& refList = mCurLayer->mPointReferrers;
  768. unsigned int i;
  769. base->abAssigned[idx] = true;
  770. for (i = 0; i < numRead;++i)
  771. base->rawData[idx*base->dims+i]= data[i];
  772. if (0xffffffff != (i = refList[idx]))
  773. DoRecursiveVMAPAssignment(base,numRead,i,data);
  774. }
  775. // ------------------------------------------------------------------------------------------------
  776. void AddToSingleLinkedList(ReferrerList& refList, unsigned int srcIdx, unsigned int destIdx)
  777. {
  778. if(0xffffffff == refList[srcIdx])
  779. {
  780. refList[srcIdx] = destIdx;
  781. return;
  782. }
  783. AddToSingleLinkedList(refList,refList[srcIdx],destIdx);
  784. }
  785. // ------------------------------------------------------------------------------------------------
  786. void LWOImporter::LoadLWO2VertexMap(unsigned int length, bool perPoly)
  787. {
  788. LE_NCONST uint8_t* const end = mFileBuffer+length;
  789. AI_LWO_VALIDATE_CHUNK_LENGTH(length,VMAP,6);
  790. unsigned int type = GetU4();
  791. unsigned int dims = GetU2();
  792. VMapEntry* base;
  793. // read the name of the vertex map
  794. std::string name;
  795. GetS0(name,length);
  796. switch (type)
  797. {
  798. case AI_LWO_TXUV:
  799. if (dims != 2)
  800. {
  801. DefaultLogger::get()->warn("LWO2: Found UV channel with != 2 components");
  802. }
  803. base = FindEntry(mCurLayer->mUVChannels,name,perPoly);
  804. break;
  805. case AI_LWO_WGHT:
  806. if (dims != 1)
  807. {
  808. DefaultLogger::get()->warn("LWO2: found vertex weight map with != 1 components");
  809. }
  810. base = FindEntry(mCurLayer->mWeightChannels,name,perPoly);
  811. break;
  812. case AI_LWO_RGB:
  813. case AI_LWO_RGBA:
  814. if (dims != 3 && dims != 4)
  815. {
  816. DefaultLogger::get()->warn("LWO2: found vertex color map with != 3&4 components");
  817. }
  818. base = FindEntry(mCurLayer->mVColorChannels,name,perPoly);
  819. break;
  820. case AI_LWO_MODO_NORM:
  821. /* This is a non-standard extension chunk used by Luxology's MODO.
  822. * It stores per-vertex normals. This VMAP exists just once, has
  823. * 3 dimensions and is btw extremely beautiful.
  824. */
  825. if (name != "vert_normals" || dims != 3 || mCurLayer->mNormals.name.length())
  826. return;
  827. DefaultLogger::get()->info("Non-standard extension: MODO VMAP.NORM.vert_normals");
  828. mCurLayer->mNormals.name = name;
  829. base = & mCurLayer->mNormals;
  830. break;
  831. default:
  832. return;
  833. };
  834. base->Allocate((unsigned int)mCurLayer->mTempPoints.size());
  835. // now read all entries in the map
  836. type = std::min(dims,base->dims);
  837. const unsigned int diff = (dims - type)<<2;
  838. LWO::FaceList& list = mCurLayer->mFaces;
  839. LWO::PointList& pointList = mCurLayer->mTempPoints;
  840. LWO::ReferrerList& refList = mCurLayer->mPointReferrers;
  841. float temp[4];
  842. const unsigned int numPoints = (unsigned int)pointList.size();
  843. const unsigned int numFaces = (unsigned int)list.size();
  844. while (mFileBuffer < end)
  845. {
  846. unsigned int idx = ReadVSizedIntLWO2(mFileBuffer) + mCurLayer->mPointIDXOfs;
  847. if (idx >= numPoints)
  848. {
  849. DefaultLogger::get()->warn("LWO2: vertex index in vmap/vmad is out of range");
  850. mFileBuffer += base->dims*4;continue;
  851. }
  852. if (perPoly)
  853. {
  854. unsigned int polyIdx = ReadVSizedIntLWO2(mFileBuffer) + mCurLayer->mFaceIDXOfs;
  855. if (base->abAssigned[idx])
  856. {
  857. // we have already a VMAP entry for this vertex - thus
  858. // we need to duplicate the corresponding polygon.
  859. if (polyIdx >= numFaces)
  860. {
  861. DefaultLogger::get()->warn("LWO2: VMAD polygon index is out of range");
  862. mFileBuffer += base->dims*4;
  863. continue;
  864. }
  865. LWO::Face& src = list[polyIdx];
  866. // generate a new unique vertex for the corresponding index - but only
  867. // if we can find the index in the face
  868. for (unsigned int i = 0; i < src.mNumIndices;++i)
  869. {
  870. register unsigned int srcIdx = src.mIndices[i];
  871. if (idx != srcIdx)continue;
  872. refList.resize(refList.size()+1, 0xffffffff);
  873. idx = (unsigned int)pointList.size();
  874. src.mIndices[i] = (unsigned int)pointList.size();
  875. // store the index of the new vertex in the old vertex
  876. // so we get a single linked list we can traverse in
  877. // only one direction
  878. AddToSingleLinkedList(refList,srcIdx,src.mIndices[i]);
  879. pointList.push_back(pointList[srcIdx]);
  880. CreateNewEntry(mCurLayer->mVColorChannels, srcIdx );
  881. CreateNewEntry(mCurLayer->mUVChannels, srcIdx );
  882. CreateNewEntry(mCurLayer->mWeightChannels, srcIdx );
  883. CreateNewEntry(mCurLayer->mNormals, srcIdx );
  884. }
  885. }
  886. }
  887. for (unsigned int l = 0; l < type;++l)
  888. temp[l] = GetF4();
  889. DoRecursiveVMAPAssignment(base,type,idx, temp);
  890. mFileBuffer += diff;
  891. }
  892. }
  893. // ------------------------------------------------------------------------------------------------
  894. void LWOImporter::LoadLWO2Clip(unsigned int length)
  895. {
  896. AI_LWO_VALIDATE_CHUNK_LENGTH(length,CLIP,10);
  897. mClips.push_back(LWO::Clip());
  898. LWO::Clip& clip = mClips.back();
  899. // first - get the index of the clip
  900. clip.idx = GetU4();
  901. IFF::SubChunkHeader* const head = IFF::LoadSubChunk(mFileBuffer);
  902. switch (head->type)
  903. {
  904. case AI_LWO_STIL:
  905. // "Normal" texture
  906. GetS0(clip.path,head->length);
  907. clip.type = Clip::STILL;
  908. break;
  909. case AI_LWO_ISEQ:
  910. // Image sequence. We'll later take the first.
  911. {
  912. uint8_t digits = GetU1(); mFileBuffer++;
  913. int16_t offset = GetU2(); mFileBuffer+=4;
  914. int16_t start = GetU2(); mFileBuffer+=4;
  915. std::string s;std::stringstream ss;
  916. GetS0(s,head->length);
  917. head->length -= (unsigned int)s.length()+1;
  918. ss << s;
  919. ss << std::setw(digits) << offset + start;
  920. GetS0(s,head->length);
  921. ss << s;
  922. clip.path = ss.str();
  923. clip.type = Clip::SEQ;
  924. }
  925. break;
  926. case AI_LWO_STCC:
  927. DefaultLogger::get()->warn("LWO2: Color shifted images are not supported");
  928. break;
  929. case AI_LWO_ANIM:
  930. DefaultLogger::get()->warn("LWO2: Animated textures are not supported");
  931. break;
  932. case AI_LWO_XREF:
  933. // Just a cross-reference to another CLIp
  934. clip.type = Clip::REF;
  935. clip.clipRef = GetU4();
  936. break;
  937. default:
  938. DefaultLogger::get()->warn("LWO2: Encountered unknown CLIP subchunk");
  939. }
  940. }
  941. // ------------------------------------------------------------------------------------------------
  942. void LWOImporter::LoadLWO2File()
  943. {
  944. bool skip = false;
  945. LE_NCONST uint8_t* const end = mFileBuffer + fileSize;
  946. while (true)
  947. {
  948. if (mFileBuffer + sizeof(IFF::ChunkHeader) > end)break;
  949. IFF::ChunkHeader* const head = IFF::LoadChunk(mFileBuffer);
  950. if (mFileBuffer + head->length > end)
  951. {
  952. throw new ImportErrorException("LWO2: Chunk length points behind the file");
  953. break;
  954. }
  955. uint8_t* const next = mFileBuffer+head->length;
  956. unsigned int iUnnamed = 0;
  957. switch (head->type)
  958. {
  959. // new layer
  960. case AI_LWO_LAYR:
  961. {
  962. // add a new layer to the list ....
  963. mLayers->push_back ( LWO::Layer() );
  964. LWO::Layer& layer = mLayers->back();
  965. mCurLayer = &layer;
  966. // load this layer or ignore it? Check the layer index property
  967. // NOTE: The first layer is the default layer, so the layer
  968. // index is one-based now
  969. if (0xffffffff != configLayerIndex && configLayerIndex != mLayers->size())
  970. {
  971. skip = true;
  972. }
  973. else skip = false;
  974. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,LAYR,16);
  975. // and parse its properties, e.g. the pivot point
  976. mFileBuffer += 2;
  977. mCurLayer->mPivot.x = GetF4();
  978. mCurLayer->mPivot.y = GetF4();
  979. mCurLayer->mPivot.z = GetF4();
  980. mFileBuffer += 2;
  981. GetS0(layer.mName,head->length-16);
  982. // if the name is empty, generate a default name
  983. if (layer.mName.empty())
  984. {
  985. char buffer[128]; // should be sufficiently large
  986. ::sprintf(buffer,"Layer_%i", iUnnamed++);
  987. layer.mName = buffer;
  988. }
  989. // load this layer or ignore it? Check the layer name property
  990. if (configLayerName.length() && configLayerName != layer.mName)
  991. {
  992. skip = true;
  993. }
  994. else hasNamedLayer = true;
  995. if (mFileBuffer + 2 <= next)
  996. layer.mParent = GetU2();
  997. break;
  998. }
  999. // vertex list
  1000. case AI_LWO_PNTS:
  1001. {
  1002. if (skip)break;
  1003. unsigned int old = (unsigned int)mCurLayer->mTempPoints.size();
  1004. LoadLWOPoints(head->length);
  1005. mCurLayer->mPointIDXOfs = old;
  1006. break;
  1007. }
  1008. // vertex tags
  1009. case AI_LWO_VMAD:
  1010. if (mCurLayer->mFaces.empty())
  1011. {
  1012. DefaultLogger::get()->warn("LWO2: Unexpected VMAD chunk");
  1013. break;
  1014. }
  1015. // --- intentionally no break here
  1016. case AI_LWO_VMAP:
  1017. {
  1018. if (skip)break;
  1019. if (mCurLayer->mTempPoints.empty())
  1020. DefaultLogger::get()->warn("LWO2: Unexpected VMAP chunk");
  1021. else LoadLWO2VertexMap(head->length,head->type == AI_LWO_VMAD);
  1022. break;
  1023. }
  1024. // face list
  1025. case AI_LWO_POLS:
  1026. {
  1027. if (skip)break;
  1028. unsigned int old = (unsigned int)mCurLayer->mFaces.size();
  1029. LoadLWO2Polygons(head->length);
  1030. mCurLayer->mFaceIDXOfs = old;
  1031. break;
  1032. }
  1033. // polygon tags
  1034. case AI_LWO_PTAG:
  1035. {
  1036. if (skip)break;
  1037. if (mCurLayer->mFaces.empty())
  1038. DefaultLogger::get()->warn("LWO2: Unexpected PTAG");
  1039. else LoadLWO2PolygonTags(head->length);
  1040. break;
  1041. }
  1042. // list of tags
  1043. case AI_LWO_TAGS:
  1044. {
  1045. if (!mTags->empty())
  1046. DefaultLogger::get()->warn("LWO2: SRFS chunk encountered twice");
  1047. else LoadLWOTags(head->length);
  1048. break;
  1049. }
  1050. // surface chunk
  1051. case AI_LWO_SURF:
  1052. {
  1053. LoadLWO2Surface(head->length);
  1054. break;
  1055. }
  1056. // clip chunk
  1057. case AI_LWO_CLIP:
  1058. {
  1059. LoadLWO2Clip(head->length);
  1060. break;
  1061. }
  1062. }
  1063. mFileBuffer = next;
  1064. }
  1065. }