You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

397 lines
11 KiB

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