LWOLoader.cpp 39 KB

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