2
0

LWOLoader.cpp 42 KB

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