LWOBLoader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp 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 team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the LWO importer class for the older LWOB
  35. file formats, including materials */
  36. #ifndef ASSIMP_BUILD_NO_LWO_IMPORTER
  37. // Internal headers
  38. #include "LWOLoader.h"
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. void LWOImporter::LoadLWOBFile() {
  42. LE_NCONST uint8_t* const end = mFileBuffer + fileSize;
  43. bool running = true;
  44. while (running) {
  45. if (mFileBuffer + sizeof(IFF::ChunkHeader) > end)
  46. break;
  47. const IFF::ChunkHeader head = IFF::LoadChunk(mFileBuffer);
  48. if (mFileBuffer + head.length > end) {
  49. throw DeadlyImportError("LWOB: Invalid chunk length");
  50. }
  51. uint8_t* const next = mFileBuffer+head.length;
  52. switch (head.type) {
  53. // vertex list
  54. case AI_LWO_PNTS: {
  55. if (!mCurLayer->mTempPoints.empty())
  56. ASSIMP_LOG_WARN("LWO: PNTS chunk encountered twice");
  57. else
  58. LoadLWOPoints(head.length);
  59. } break;
  60. case AI_LWO_POLS: { // face list
  61. if (!mCurLayer->mFaces.empty())
  62. ASSIMP_LOG_WARN("LWO: POLS chunk encountered twice");
  63. else
  64. LoadLWOBPolygons(head.length);
  65. } break;
  66. case AI_LWO_SRFS: // list of tags
  67. {
  68. if (!mTags->empty())
  69. ASSIMP_LOG_WARN("LWO: SRFS chunk encountered twice");
  70. else
  71. LoadLWOTags(head.length);
  72. } break;
  73. case AI_LWO_SURF: // surface chunk
  74. {
  75. LoadLWOBSurface(head.length);
  76. } break;
  77. default:
  78. break;
  79. }
  80. mFileBuffer = next;
  81. }
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. void LWOImporter::LoadLWOBPolygons(unsigned int length) {
  85. // first find out how many faces and vertices we'll finally need
  86. LE_NCONST uint16_t* const end = (LE_NCONST uint16_t*)(mFileBuffer+length);
  87. LE_NCONST uint16_t* cursor = (LE_NCONST uint16_t*)mFileBuffer;
  88. // perform endianness conversions
  89. #ifndef AI_BUILD_BIG_ENDIAN
  90. while (cursor < end)ByteSwap::Swap2(cursor++);
  91. cursor = (LE_NCONST uint16_t*)mFileBuffer;
  92. #endif
  93. unsigned int iNumFaces = 0,iNumVertices = 0;
  94. CountVertsAndFacesLWOB(iNumVertices,iNumFaces,cursor,end);
  95. // allocate the output array and copy face indices
  96. if (iNumFaces) {
  97. cursor = (LE_NCONST uint16_t*)mFileBuffer;
  98. mCurLayer->mFaces.resize(iNumFaces);
  99. FaceList::iterator it = mCurLayer->mFaces.begin();
  100. CopyFaceIndicesLWOB(it,cursor,end);
  101. }
  102. }
  103. // ------------------------------------------------------------------------------------------------
  104. void LWOImporter::CountVertsAndFacesLWOB(unsigned int& verts, unsigned int& faces,
  105. LE_NCONST uint16_t*& cursor, const uint16_t* const end, unsigned int max) {
  106. while (cursor < end && max--) {
  107. uint16_t numIndices;
  108. // must have 2 shorts left for numIndices and surface
  109. if (end - cursor < 2) {
  110. throw DeadlyImportError("LWOB: Unexpected end of file");
  111. }
  112. ::memcpy(&numIndices, cursor++, 2);
  113. // must have enough left for indices and surface
  114. if (end - cursor < (1 + numIndices)) {
  115. throw DeadlyImportError("LWOB: Unexpected end of file");
  116. }
  117. verts += numIndices;
  118. faces++;
  119. cursor += numIndices;
  120. int16_t surface;
  121. ::memcpy(&surface, cursor++, 2);
  122. if (surface < 0) {
  123. // there are detail polygons
  124. ::memcpy(&numIndices, cursor++, 2);
  125. CountVertsAndFacesLWOB(verts,faces,cursor,end,numIndices);
  126. }
  127. }
  128. }
  129. // ------------------------------------------------------------------------------------------------
  130. void LWOImporter::CopyFaceIndicesLWOB(FaceList::iterator &it,
  131. LE_NCONST uint16_t*& cursor,
  132. const uint16_t* const end,
  133. unsigned int max) {
  134. while (cursor < end && max--) {
  135. LWO::Face& face = *it;++it;
  136. uint16_t numIndices;
  137. ::memcpy(&numIndices, cursor++, 2);
  138. face.mNumIndices = numIndices;
  139. if(face.mNumIndices) {
  140. if (cursor + face.mNumIndices >= end) {
  141. break;
  142. }
  143. face.mIndices = new unsigned int[face.mNumIndices];
  144. for (unsigned int i = 0; i < face.mNumIndices;++i) {
  145. unsigned int & mi = face.mIndices[i];
  146. uint16_t index;
  147. ::memcpy(&index, cursor++, 2);
  148. mi = index;
  149. if (mi > mCurLayer->mTempPoints.size()) {
  150. ASSIMP_LOG_WARN("LWOB: face index is out of range");
  151. mi = (unsigned int)mCurLayer->mTempPoints.size()-1;
  152. }
  153. }
  154. } else {
  155. ASSIMP_LOG_WARN("LWOB: Face has 0 indices");
  156. }
  157. int16_t surface;
  158. ::memcpy(&surface, cursor++, 2);
  159. if (surface < 0) {
  160. surface = -surface;
  161. // there are detail polygons.
  162. uint16_t numPolygons;
  163. ::memcpy(&numPolygons, cursor++, 2);
  164. if (cursor < end) {
  165. CopyFaceIndicesLWOB(it,cursor,end,numPolygons);
  166. }
  167. }
  168. face.surfaceIndex = surface-1;
  169. }
  170. }
  171. // ------------------------------------------------------------------------------------------------
  172. LWO::Texture* LWOImporter::SetupNewTextureLWOB(LWO::TextureList& list,unsigned int size) {
  173. list.emplace_back();
  174. LWO::Texture* tex = &list.back();
  175. std::string type;
  176. GetS0(type,size);
  177. const char* s = type.c_str();
  178. if(strstr(s, "Image Map")) {
  179. // Determine mapping type
  180. if(strstr(s, "Planar"))
  181. tex->mapMode = LWO::Texture::Planar;
  182. else if(strstr(s, "Cylindrical"))
  183. tex->mapMode = LWO::Texture::Cylindrical;
  184. else if(strstr(s, "Spherical"))
  185. tex->mapMode = LWO::Texture::Spherical;
  186. else if(strstr(s, "Cubic"))
  187. tex->mapMode = LWO::Texture::Cubic;
  188. else if(strstr(s, "Front"))
  189. tex->mapMode = LWO::Texture::FrontProjection;
  190. } else {
  191. // procedural or gradient, not supported
  192. ASSIMP_LOG_ERROR("LWOB: Unsupported legacy texture: ", type);
  193. }
  194. return tex;
  195. }
  196. // ------------------------------------------------------------------------------------------------
  197. void LWOImporter::LoadLWOBSurface(unsigned int size) {
  198. LE_NCONST uint8_t* const end = mFileBuffer + size;
  199. mSurfaces->push_back( LWO::Surface () );
  200. LWO::Surface& surf = mSurfaces->back();
  201. LWO::Texture *pTex = nullptr;
  202. GetS0(surf.mName,size);
  203. bool running = true;
  204. while (running) {
  205. if (mFileBuffer + 6 >= end)
  206. break;
  207. IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
  208. /* A single test file (sonycam.lwo) seems to have invalid surface chunks.
  209. * I'm assuming it's the fault of a single, unknown exporter so there are
  210. * probably THOUSANDS of them. Here's a dirty workaround:
  211. *
  212. * We don't break if the chunk limit is exceeded. Instead, we're computing
  213. * how much storage is actually left and work with this value from now on.
  214. */
  215. if (mFileBuffer + head.length > end) {
  216. ASSIMP_LOG_ERROR("LWOB: Invalid surface chunk length. Trying to continue.");
  217. head.length = (uint16_t) (end - mFileBuffer);
  218. }
  219. uint8_t* const next = mFileBuffer+head.length;
  220. switch (head.type) {
  221. // diffuse color
  222. case AI_LWO_COLR:
  223. {
  224. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,COLR,3);
  225. surf.mColor.r = GetU1() / 255.0f;
  226. surf.mColor.g = GetU1() / 255.0f;
  227. surf.mColor.b = GetU1() / 255.0f;
  228. break;
  229. }
  230. // diffuse strength ...
  231. case AI_LWO_DIFF:
  232. {
  233. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,DIFF,2);
  234. surf.mDiffuseValue = GetU2() / 255.0f;
  235. break;
  236. }
  237. // specular strength ...
  238. case AI_LWO_SPEC:
  239. {
  240. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,SPEC,2);
  241. surf.mSpecularValue = GetU2() / 255.0f;
  242. break;
  243. }
  244. // luminosity ...
  245. case AI_LWO_LUMI:
  246. {
  247. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,LUMI,2);
  248. surf.mLuminosity = GetU2() / 255.0f;
  249. break;
  250. }
  251. // transparency
  252. case AI_LWO_TRAN:
  253. {
  254. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,TRAN,2);
  255. surf.mTransparency = GetU2() / 255.0f;
  256. break;
  257. }
  258. // surface flags
  259. case AI_LWO_FLAG:
  260. {
  261. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,FLAG,2);
  262. uint16_t flag = GetU2();
  263. if (flag & 0x4 ) surf.mMaximumSmoothAngle = 1.56207f;
  264. if (flag & 0x8 ) surf.mColorHighlights = 1.f;
  265. if (flag & 0x100) surf.bDoubleSided = true;
  266. break;
  267. }
  268. // maximum smoothing angle
  269. case AI_LWO_SMAN:
  270. {
  271. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,SMAN,4);
  272. surf.mMaximumSmoothAngle = std::fabs( GetF4() );
  273. break;
  274. }
  275. // glossiness
  276. case AI_LWO_GLOS:
  277. {
  278. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,GLOS,2);
  279. surf.mGlossiness = (float)GetU2();
  280. break;
  281. }
  282. // color texture
  283. case AI_LWO_CTEX:
  284. {
  285. pTex = SetupNewTextureLWOB(surf.mColorTextures,
  286. head.length);
  287. break;
  288. }
  289. // diffuse texture
  290. case AI_LWO_DTEX:
  291. {
  292. pTex = SetupNewTextureLWOB(surf.mDiffuseTextures,
  293. head.length);
  294. break;
  295. }
  296. // specular texture
  297. case AI_LWO_STEX:
  298. {
  299. pTex = SetupNewTextureLWOB(surf.mSpecularTextures,
  300. head.length);
  301. break;
  302. }
  303. // bump texture
  304. case AI_LWO_BTEX:
  305. {
  306. pTex = SetupNewTextureLWOB(surf.mBumpTextures,
  307. head.length);
  308. break;
  309. }
  310. // transparency texture
  311. case AI_LWO_TTEX:
  312. {
  313. pTex = SetupNewTextureLWOB(surf.mOpacityTextures,
  314. head.length);
  315. break;
  316. }
  317. // texture path
  318. case AI_LWO_TIMG:
  319. {
  320. if (pTex) {
  321. GetS0(pTex->mFileName,head.length);
  322. } else {
  323. ASSIMP_LOG_WARN("LWOB: Unexpected TIMG chunk");
  324. }
  325. break;
  326. }
  327. // texture strength
  328. case AI_LWO_TVAL:
  329. {
  330. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,TVAL,1);
  331. if (pTex) {
  332. pTex->mStrength = (float)GetU1()/ 255.f;
  333. } else {
  334. ASSIMP_LOG_ERROR("LWOB: Unexpected TVAL chunk");
  335. }
  336. break;
  337. }
  338. // texture flags
  339. case AI_LWO_TFLG:
  340. {
  341. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length,TFLG,2);
  342. if (nullptr != pTex) {
  343. const uint16_t s = GetU2();
  344. if (s & 1)
  345. pTex->majorAxis = LWO::Texture::AXIS_X;
  346. else if (s & 2)
  347. pTex->majorAxis = LWO::Texture::AXIS_Y;
  348. else if (s & 4)
  349. pTex->majorAxis = LWO::Texture::AXIS_Z;
  350. if (s & 16) {
  351. ASSIMP_LOG_WARN("LWOB: Ignoring \'negate\' flag on texture");
  352. }
  353. }
  354. else {
  355. ASSIMP_LOG_WARN("LWOB: Unexpected TFLG chunk");
  356. }
  357. break;
  358. }
  359. }
  360. mFileBuffer = next;
  361. }
  362. }
  363. #endif // !! ASSIMP_BUILD_NO_LWO_IMPORTER