LWOMaterial.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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 material oart of the LWO importer class */
  35. #ifndef ASSIMP_BUILD_NO_LWO_IMPORTER
  36. // internal headers
  37. #include "LWOLoader.h"
  38. #include <assimp/ByteSwapper.h>
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. template <class T>
  42. T lerp(const T &one, const T &two, float val) {
  43. return one + (two - one) * val;
  44. }
  45. // ------------------------------------------------------------------------------------------------
  46. // Convert a lightwave mapping mode to our's
  47. inline aiTextureMapMode GetMapMode(LWO::Texture::Wrap in) {
  48. switch (in) {
  49. case LWO::Texture::REPEAT:
  50. return aiTextureMapMode_Wrap;
  51. case LWO::Texture::MIRROR:
  52. return aiTextureMapMode_Mirror;
  53. case LWO::Texture::RESET:
  54. ASSIMP_LOG_WARN("LWO2: Unsupported texture map mode: RESET");
  55. // fall though here
  56. case LWO::Texture::EDGE:
  57. return aiTextureMapMode_Clamp;
  58. }
  59. return (aiTextureMapMode)0;
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. bool LWOImporter::HandleTextures(aiMaterial *pcMat, const TextureList &in, aiTextureType type) {
  63. ai_assert(nullptr != pcMat);
  64. unsigned int cur = 0, temp = 0;
  65. aiString s;
  66. bool ret = false;
  67. for (const auto &texture : in) {
  68. if (!texture.enabled || !texture.bCanUse)
  69. continue;
  70. ret = true;
  71. // Convert lightwave's mapping modes to ours. We let them
  72. // as they are, the GenUVcoords step will compute UV
  73. // channels if they're not there.
  74. aiTextureMapping mapping = aiTextureMapping_OTHER;
  75. switch (texture.mapMode) {
  76. case LWO::Texture::Planar:
  77. mapping = aiTextureMapping_PLANE;
  78. break;
  79. case LWO::Texture::Cylindrical:
  80. mapping = aiTextureMapping_CYLINDER;
  81. break;
  82. case LWO::Texture::Spherical:
  83. mapping = aiTextureMapping_SPHERE;
  84. break;
  85. case LWO::Texture::Cubic:
  86. mapping = aiTextureMapping_BOX;
  87. break;
  88. case LWO::Texture::FrontProjection:
  89. ASSIMP_LOG_ERROR("LWO2: Unsupported texture mapping: FrontProjection");
  90. mapping = aiTextureMapping_OTHER;
  91. break;
  92. case LWO::Texture::UV: {
  93. if (UINT_MAX == texture.mRealUVIndex) {
  94. // We have no UV index for this texture, so we can't display it
  95. continue;
  96. }
  97. // add the UV source index
  98. temp = texture.mRealUVIndex;
  99. pcMat->AddProperty<int>((int *)&temp, 1, AI_MATKEY_UVWSRC(type, cur));
  100. mapping = aiTextureMapping_UV;
  101. } break;
  102. default:
  103. ai_assert(false);
  104. };
  105. if (mapping != aiTextureMapping_UV) {
  106. // Setup the main axis
  107. aiVector3D v;
  108. switch (texture.majorAxis) {
  109. case Texture::AXIS_X:
  110. v = aiVector3D(1.0, 0.0, 0.0);
  111. break;
  112. case Texture::AXIS_Y:
  113. v = aiVector3D(0.0, 1.0, 0.0);
  114. break;
  115. default: // case Texture::AXIS_Z:
  116. v = aiVector3D(0.0, 0.0, 1.0);
  117. break;
  118. }
  119. pcMat->AddProperty(&v, 1, AI_MATKEY_TEXMAP_AXIS(type, cur));
  120. // Setup UV scalings for cylindric and spherical projections
  121. if (mapping == aiTextureMapping_CYLINDER || mapping == aiTextureMapping_SPHERE) {
  122. aiUVTransform trafo;
  123. trafo.mScaling.x = texture.wrapAmountW;
  124. trafo.mScaling.y = texture.wrapAmountH;
  125. static_assert(sizeof(aiUVTransform) / sizeof(ai_real) == 5, "sizeof(aiUVTransform)/sizeof(ai_real) == 5");
  126. pcMat->AddProperty(&trafo, 1, AI_MATKEY_UVTRANSFORM(type, cur));
  127. }
  128. ASSIMP_LOG_VERBOSE_DEBUG("LWO2: Setting up non-UV mapping");
  129. }
  130. // The older LWOB format does not use indirect references to clips.
  131. // The file name of a texture is directly specified in the tex chunk.
  132. if (mIsLWO2) {
  133. // find the corresponding clip (take the last one if multiple
  134. // share the same index)
  135. ClipList::iterator end = mClips.end(), candidate = end;
  136. temp = texture.mClipIdx;
  137. for (ClipList::iterator clip = mClips.begin(); clip != end; ++clip) {
  138. if ((*clip).idx == temp) {
  139. candidate = clip;
  140. }
  141. }
  142. if (candidate == end) {
  143. ASSIMP_LOG_ERROR("LWO2: Clip index is out of bounds");
  144. temp = 0;
  145. // fixme: apparently some LWO files shipping with Doom3 don't
  146. // have clips at all ... check whether that's true or whether
  147. // it's a bug in the loader.
  148. s.Set("$texture.png");
  149. //continue;
  150. } else {
  151. if (Clip::UNSUPPORTED == (*candidate).type) {
  152. ASSIMP_LOG_ERROR("LWO2: Clip type is not supported");
  153. continue;
  154. }
  155. AdjustTexturePath((*candidate).path);
  156. s.Set((*candidate).path);
  157. // Additional image settings
  158. int flags = 0;
  159. if ((*candidate).negate) {
  160. flags |= aiTextureFlags_Invert;
  161. }
  162. pcMat->AddProperty(&flags, 1, AI_MATKEY_TEXFLAGS(type, cur));
  163. }
  164. } else {
  165. std::string ss = texture.mFileName;
  166. if (!ss.length()) {
  167. ASSIMP_LOG_WARN("LWOB: Empty file name");
  168. continue;
  169. }
  170. AdjustTexturePath(ss);
  171. s.Set(ss);
  172. }
  173. pcMat->AddProperty(&s, AI_MATKEY_TEXTURE(type, cur));
  174. // add the blend factor
  175. pcMat->AddProperty<float>(&texture.mStrength, 1, AI_MATKEY_TEXBLEND(type, cur));
  176. // add the blend operation
  177. switch (texture.blendType) {
  178. case LWO::Texture::Normal:
  179. case LWO::Texture::Multiply:
  180. temp = (unsigned int)aiTextureOp_Multiply;
  181. break;
  182. case LWO::Texture::Subtractive:
  183. case LWO::Texture::Difference:
  184. temp = (unsigned int)aiTextureOp_Subtract;
  185. break;
  186. case LWO::Texture::Divide:
  187. temp = (unsigned int)aiTextureOp_Divide;
  188. break;
  189. case LWO::Texture::Additive:
  190. temp = (unsigned int)aiTextureOp_Add;
  191. break;
  192. default:
  193. temp = (unsigned int)aiTextureOp_Multiply;
  194. ASSIMP_LOG_WARN("LWO2: Unsupported texture blend mode: alpha or displacement");
  195. }
  196. // Setup texture operation
  197. pcMat->AddProperty<int>((int *)&temp, 1, AI_MATKEY_TEXOP(type, cur));
  198. // setup the mapping mode
  199. int mapping_ = static_cast<int>(mapping);
  200. pcMat->AddProperty<int>(&mapping_, 1, AI_MATKEY_MAPPING(type, cur));
  201. // add the u-wrapping
  202. temp = (unsigned int)GetMapMode(texture.wrapModeWidth);
  203. pcMat->AddProperty<int>((int *)&temp, 1, AI_MATKEY_MAPPINGMODE_U(type, cur));
  204. // add the v-wrapping
  205. temp = (unsigned int)GetMapMode(texture.wrapModeHeight);
  206. pcMat->AddProperty<int>((int *)&temp, 1, AI_MATKEY_MAPPINGMODE_V(type, cur));
  207. ++cur;
  208. }
  209. return ret;
  210. }
  211. // ------------------------------------------------------------------------------------------------
  212. void LWOImporter::ConvertMaterial(const LWO::Surface &surf, aiMaterial *pcMat) {
  213. // copy the name of the surface
  214. aiString st;
  215. st.Set(surf.mName);
  216. pcMat->AddProperty(&st, AI_MATKEY_NAME);
  217. const int i = surf.bDoubleSided ? 1 : 0;
  218. pcMat->AddProperty(&i, 1, AI_MATKEY_TWOSIDED);
  219. // add the refraction index and the bump intensity
  220. pcMat->AddProperty(&surf.mIOR, 1, AI_MATKEY_REFRACTI);
  221. pcMat->AddProperty(&surf.mBumpIntensity, 1, AI_MATKEY_BUMPSCALING);
  222. aiShadingMode m;
  223. if (surf.mSpecularValue && surf.mGlossiness) {
  224. float fGloss;
  225. if (mIsLWO2) {
  226. fGloss = std::pow(surf.mGlossiness * ai_real(10.0) + ai_real(2.0), ai_real(2.0));
  227. } else {
  228. if (16.0 >= surf.mGlossiness)
  229. fGloss = 6.0;
  230. else if (64.0 >= surf.mGlossiness)
  231. fGloss = 20.0;
  232. else if (256.0 >= surf.mGlossiness)
  233. fGloss = 50.0;
  234. else
  235. fGloss = 80.0;
  236. }
  237. pcMat->AddProperty(&surf.mSpecularValue, 1, AI_MATKEY_SHININESS_STRENGTH);
  238. pcMat->AddProperty(&fGloss, 1, AI_MATKEY_SHININESS);
  239. m = aiShadingMode_Phong;
  240. } else
  241. m = aiShadingMode_Gouraud;
  242. // specular color
  243. aiColor3D clr = lerp(aiColor3D(1.0, 1.0, 1.0), surf.mColor, surf.mColorHighlights);
  244. pcMat->AddProperty(&clr, 1, AI_MATKEY_COLOR_SPECULAR);
  245. pcMat->AddProperty(&surf.mSpecularValue, 1, AI_MATKEY_SHININESS_STRENGTH);
  246. // emissive color
  247. // luminosity is not really the same but it affects the surface in a similar way. Some scaling looks good.
  248. clr.g = clr.b = clr.r = surf.mLuminosity * ai_real(0.8);
  249. pcMat->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_EMISSIVE);
  250. // opacity ... either additive or default-blended, please
  251. if (0.0 != surf.mAdditiveTransparency) {
  252. const int add = aiBlendMode_Additive;
  253. pcMat->AddProperty(&surf.mAdditiveTransparency, 1, AI_MATKEY_OPACITY);
  254. pcMat->AddProperty(&add, 1, AI_MATKEY_BLEND_FUNC);
  255. } else if (10e10f != surf.mTransparency) {
  256. const int def = aiBlendMode_Default;
  257. const float f = 1.0f - surf.mTransparency;
  258. pcMat->AddProperty(&f, 1, AI_MATKEY_OPACITY);
  259. pcMat->AddProperty(&def, 1, AI_MATKEY_BLEND_FUNC);
  260. }
  261. // ADD TEXTURES to the material
  262. // TODO: find out how we can handle COLOR textures correctly...
  263. bool b = HandleTextures(pcMat, surf.mColorTextures, aiTextureType_DIFFUSE);
  264. b = (b || HandleTextures(pcMat, surf.mDiffuseTextures, aiTextureType_DIFFUSE));
  265. HandleTextures(pcMat, surf.mSpecularTextures, aiTextureType_SPECULAR);
  266. HandleTextures(pcMat, surf.mGlossinessTextures, aiTextureType_SHININESS);
  267. HandleTextures(pcMat, surf.mBumpTextures, aiTextureType_HEIGHT);
  268. HandleTextures(pcMat, surf.mOpacityTextures, aiTextureType_OPACITY);
  269. HandleTextures(pcMat, surf.mReflectionTextures, aiTextureType_REFLECTION);
  270. // Now we need to know which shader to use .. iterate through the shader list of
  271. // the surface and search for a name which we know ...
  272. for (const auto &shader : surf.mShaders) {
  273. if (shader.functionName == "LW_SuperCelShader" || shader.functionName == "AH_CelShader") {
  274. ASSIMP_LOG_INFO("LWO2: Mapping LW_SuperCelShader/AH_CelShader to aiShadingMode_Toon");
  275. m = aiShadingMode_Toon;
  276. break;
  277. } else if (shader.functionName == "LW_RealFresnel" || shader.functionName == "LW_FastFresnel") {
  278. ASSIMP_LOG_INFO("LWO2: Mapping LW_RealFresnel/LW_FastFresnel to aiShadingMode_Fresnel");
  279. m = aiShadingMode_Fresnel;
  280. break;
  281. } else {
  282. ASSIMP_LOG_WARN_F("LWO2: Unknown surface shader: ", shader.functionName);
  283. }
  284. }
  285. if (surf.mMaximumSmoothAngle <= 0.0)
  286. m = aiShadingMode_Flat;
  287. int m_ = static_cast<int>(m);
  288. pcMat->AddProperty(&m_, 1, AI_MATKEY_SHADING_MODEL);
  289. // (the diffuse value is just a scaling factor)
  290. // If a diffuse texture is set, we set this value to 1.0
  291. clr = (b && false ? aiColor3D(1.0, 1.0, 1.0) : surf.mColor);
  292. clr.r *= surf.mDiffuseValue;
  293. clr.g *= surf.mDiffuseValue;
  294. clr.b *= surf.mDiffuseValue;
  295. pcMat->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
  296. }
  297. // ------------------------------------------------------------------------------------------------
  298. char LWOImporter::FindUVChannels(LWO::TextureList &list,
  299. LWO::Layer & /*layer*/, LWO::UVChannel &uv, unsigned int next) {
  300. char ret = 0;
  301. for (auto &texture : list) {
  302. // Ignore textures with non-UV mappings for the moment.
  303. if (!texture.enabled || !texture.bCanUse || texture.mapMode != LWO::Texture::UV) {
  304. continue;
  305. }
  306. if (texture.mUVChannelIndex == uv.name) {
  307. ret = 1;
  308. // got it.
  309. if (texture.mRealUVIndex == UINT_MAX || texture.mRealUVIndex == next) {
  310. texture.mRealUVIndex = next;
  311. } else {
  312. // channel mismatch. need to duplicate the material.
  313. ASSIMP_LOG_WARN("LWO: Channel mismatch, would need to duplicate surface [design bug]");
  314. // TODO
  315. }
  316. }
  317. }
  318. return ret;
  319. }
  320. // ------------------------------------------------------------------------------------------------
  321. void LWOImporter::FindUVChannels(LWO::Surface &surf,
  322. LWO::SortedRep &sorted, LWO::Layer &layer,
  323. unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS]) {
  324. unsigned int next = 0, extra = 0, num_extra = 0;
  325. // Check whether we have an UV entry != 0 for one of the faces in 'sorted'
  326. for (unsigned int i = 0; i < layer.mUVChannels.size(); ++i) {
  327. LWO::UVChannel &uv = layer.mUVChannels[i];
  328. for (LWO::SortedRep::const_iterator it = sorted.begin(); it != sorted.end(); ++it) {
  329. LWO::Face &face = layer.mFaces[*it];
  330. for (unsigned int n = 0; n < face.mNumIndices; ++n) {
  331. unsigned int idx = face.mIndices[n];
  332. if (uv.abAssigned[idx] && ((aiVector2D *)&uv.rawData[0])[idx] != aiVector2D()) {
  333. if (extra >= AI_MAX_NUMBER_OF_TEXTURECOORDS) {
  334. ASSIMP_LOG_ERROR("LWO: Maximum number of UV channels for "
  335. "this mesh reached. Skipping channel \'" +
  336. uv.name + "\'");
  337. } else {
  338. // Search through all textures assigned to 'surf' and look for this UV channel
  339. char had = 0;
  340. had |= FindUVChannels(surf.mColorTextures, layer, uv, next);
  341. had |= FindUVChannels(surf.mDiffuseTextures, layer, uv, next);
  342. had |= FindUVChannels(surf.mSpecularTextures, layer, uv, next);
  343. had |= FindUVChannels(surf.mGlossinessTextures, layer, uv, next);
  344. had |= FindUVChannels(surf.mOpacityTextures, layer, uv, next);
  345. had |= FindUVChannels(surf.mBumpTextures, layer, uv, next);
  346. had |= FindUVChannels(surf.mReflectionTextures, layer, uv, next);
  347. // We have a texture referencing this UV channel so we have to take special care
  348. // and are willing to drop unreferenced channels in favour of it.
  349. if (had != 0) {
  350. if (num_extra) {
  351. for (unsigned int a = next; a < std::min(extra, AI_MAX_NUMBER_OF_TEXTURECOORDS - 1u); ++a) {
  352. out[a + 1] = out[a];
  353. }
  354. }
  355. ++extra;
  356. out[next++] = i;
  357. }
  358. // Bah ... seems not to be used at all. Push to end if enough space is available.
  359. else {
  360. out[extra++] = i;
  361. ++num_extra;
  362. }
  363. }
  364. it = sorted.end() - 1;
  365. break;
  366. }
  367. }
  368. }
  369. }
  370. if (extra < AI_MAX_NUMBER_OF_TEXTURECOORDS) {
  371. out[extra] = UINT_MAX;
  372. }
  373. }
  374. // ------------------------------------------------------------------------------------------------
  375. void LWOImporter::FindVCChannels(const LWO::Surface &surf, LWO::SortedRep &sorted, const LWO::Layer &layer,
  376. unsigned int out[AI_MAX_NUMBER_OF_COLOR_SETS]) {
  377. unsigned int next = 0;
  378. // Check whether we have an vc entry != 0 for one of the faces in 'sorted'
  379. for (unsigned int i = 0; i < layer.mVColorChannels.size(); ++i) {
  380. const LWO::VColorChannel &vc = layer.mVColorChannels[i];
  381. if (surf.mVCMap == vc.name) {
  382. // The vertex color map is explicitly requested by the surface so we need to take special care of it
  383. for (unsigned int a = 0; a < std::min(next, AI_MAX_NUMBER_OF_COLOR_SETS - 1u); ++a) {
  384. out[a + 1] = out[a];
  385. }
  386. out[0] = i;
  387. ++next;
  388. } else {
  389. for (LWO::SortedRep::iterator it = sorted.begin(); it != sorted.end(); ++it) {
  390. const LWO::Face &face = layer.mFaces[*it];
  391. for (unsigned int n = 0; n < face.mNumIndices; ++n) {
  392. unsigned int idx = face.mIndices[n];
  393. if (vc.abAssigned[idx] && ((aiColor4D *)&vc.rawData[0])[idx] != aiColor4D(0.0, 0.0, 0.0, 1.0)) {
  394. if (next >= AI_MAX_NUMBER_OF_COLOR_SETS) {
  395. ASSIMP_LOG_ERROR("LWO: Maximum number of vertex color channels for "
  396. "this mesh reached. Skipping channel \'" +
  397. vc.name + "\'");
  398. } else {
  399. out[next++] = i;
  400. }
  401. it = sorted.end() - 1;
  402. break;
  403. }
  404. }
  405. }
  406. }
  407. }
  408. if (next != AI_MAX_NUMBER_OF_COLOR_SETS) {
  409. out[next] = UINT_MAX;
  410. }
  411. }
  412. // ------------------------------------------------------------------------------------------------
  413. void LWOImporter::LoadLWO2ImageMap(unsigned int size, LWO::Texture &tex) {
  414. LE_NCONST uint8_t *const end = mFileBuffer + size;
  415. while (true) {
  416. if (mFileBuffer + 6 >= end) break;
  417. LE_NCONST IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
  418. if (mFileBuffer + head.length > end)
  419. throw DeadlyImportError("LWO2: Invalid SURF.BLOCK chunk length");
  420. uint8_t *const next = mFileBuffer + head.length;
  421. switch (head.type) {
  422. case AI_LWO_PROJ:
  423. tex.mapMode = (Texture::MappingMode)GetU2();
  424. break;
  425. case AI_LWO_WRAP:
  426. tex.wrapModeWidth = (Texture::Wrap)GetU2();
  427. tex.wrapModeHeight = (Texture::Wrap)GetU2();
  428. break;
  429. case AI_LWO_AXIS:
  430. tex.majorAxis = (Texture::Axes)GetU2();
  431. break;
  432. case AI_LWO_IMAG:
  433. tex.mClipIdx = GetU2();
  434. break;
  435. case AI_LWO_VMAP:
  436. GetS0(tex.mUVChannelIndex, head.length);
  437. break;
  438. case AI_LWO_WRPH:
  439. tex.wrapAmountH = GetF4();
  440. break;
  441. case AI_LWO_WRPW:
  442. tex.wrapAmountW = GetF4();
  443. break;
  444. }
  445. mFileBuffer = next;
  446. }
  447. }
  448. // ------------------------------------------------------------------------------------------------
  449. void LWOImporter::LoadLWO2Procedural(unsigned int /*size*/, LWO::Texture &tex) {
  450. // --- not supported at the moment
  451. ASSIMP_LOG_ERROR("LWO2: Found procedural texture, this is not supported");
  452. tex.bCanUse = false;
  453. }
  454. // ------------------------------------------------------------------------------------------------
  455. void LWOImporter::LoadLWO2Gradient(unsigned int /*size*/, LWO::Texture &tex) {
  456. // --- not supported at the moment
  457. ASSIMP_LOG_ERROR("LWO2: Found gradient texture, this is not supported");
  458. tex.bCanUse = false;
  459. }
  460. // ------------------------------------------------------------------------------------------------
  461. void LWOImporter::LoadLWO2TextureHeader(unsigned int size, LWO::Texture &tex) {
  462. LE_NCONST uint8_t *const end = mFileBuffer + size;
  463. // get the ordinal string
  464. GetS0(tex.ordinal, size);
  465. // we could crash later if this is an empty string ...
  466. if (!tex.ordinal.length()) {
  467. ASSIMP_LOG_ERROR("LWO2: Ill-formed SURF.BLOK ordinal string");
  468. tex.ordinal = "\x00";
  469. }
  470. while (true) {
  471. if (mFileBuffer + 6 >= end) break;
  472. const IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
  473. if (mFileBuffer + head.length > end)
  474. throw DeadlyImportError("LWO2: Invalid texture header chunk length");
  475. uint8_t *const next = mFileBuffer + head.length;
  476. switch (head.type) {
  477. case AI_LWO_CHAN:
  478. tex.type = GetU4();
  479. break;
  480. case AI_LWO_ENAB:
  481. tex.enabled = GetU2() ? true : false;
  482. break;
  483. case AI_LWO_OPAC:
  484. tex.blendType = (Texture::BlendType)GetU2();
  485. tex.mStrength = GetF4();
  486. break;
  487. }
  488. mFileBuffer = next;
  489. }
  490. }
  491. // ------------------------------------------------------------------------------------------------
  492. void LWOImporter::LoadLWO2TextureBlock(LE_NCONST IFF::SubChunkHeader *head, unsigned int size) {
  493. ai_assert(!mSurfaces->empty());
  494. LWO::Surface &surf = mSurfaces->back();
  495. LWO::Texture tex;
  496. // load the texture header
  497. LoadLWO2TextureHeader(head->length, tex);
  498. size -= head->length + 6;
  499. // now get the exact type of the texture
  500. switch (head->type) {
  501. case AI_LWO_PROC:
  502. LoadLWO2Procedural(size, tex);
  503. break;
  504. case AI_LWO_GRAD:
  505. LoadLWO2Gradient(size, tex);
  506. break;
  507. case AI_LWO_IMAP:
  508. LoadLWO2ImageMap(size, tex);
  509. }
  510. // get the destination channel
  511. TextureList *listRef = nullptr;
  512. switch (tex.type) {
  513. case AI_LWO_COLR:
  514. listRef = &surf.mColorTextures;
  515. break;
  516. case AI_LWO_DIFF:
  517. listRef = &surf.mDiffuseTextures;
  518. break;
  519. case AI_LWO_SPEC:
  520. listRef = &surf.mSpecularTextures;
  521. break;
  522. case AI_LWO_GLOS:
  523. listRef = &surf.mGlossinessTextures;
  524. break;
  525. case AI_LWO_BUMP:
  526. listRef = &surf.mBumpTextures;
  527. break;
  528. case AI_LWO_TRAN:
  529. listRef = &surf.mOpacityTextures;
  530. break;
  531. case AI_LWO_REFL:
  532. listRef = &surf.mReflectionTextures;
  533. break;
  534. default:
  535. ASSIMP_LOG_WARN("LWO2: Encountered unknown texture type");
  536. return;
  537. }
  538. // now attach the texture to the parent surface - sort by ordinal string
  539. for (TextureList::iterator it = listRef->begin(); it != listRef->end(); ++it) {
  540. if (::strcmp(tex.ordinal.c_str(), (*it).ordinal.c_str()) < 0) {
  541. listRef->insert(it, tex);
  542. return;
  543. }
  544. }
  545. listRef->push_back(tex);
  546. }
  547. // ------------------------------------------------------------------------------------------------
  548. void LWOImporter::LoadLWO2ShaderBlock(LE_NCONST IFF::SubChunkHeader * /*head*/, unsigned int size) {
  549. LE_NCONST uint8_t *const end = mFileBuffer + size;
  550. ai_assert(!mSurfaces->empty());
  551. LWO::Surface &surf = mSurfaces->back();
  552. LWO::Shader shader;
  553. // get the ordinal string
  554. GetS0(shader.ordinal, size);
  555. // we could crash later if this is an empty string ...
  556. if (!shader.ordinal.length()) {
  557. ASSIMP_LOG_ERROR("LWO2: Ill-formed SURF.BLOK ordinal string");
  558. shader.ordinal = "\x00";
  559. }
  560. // read the header
  561. while (true) {
  562. if (mFileBuffer + 6 >= end) break;
  563. const IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
  564. if (mFileBuffer + head.length > end)
  565. throw DeadlyImportError("LWO2: Invalid shader header chunk length");
  566. uint8_t *const next = mFileBuffer + head.length;
  567. switch (head.type) {
  568. case AI_LWO_ENAB:
  569. shader.enabled = GetU2() ? true : false;
  570. break;
  571. case AI_LWO_FUNC:
  572. GetS0(shader.functionName, head.length);
  573. }
  574. mFileBuffer = next;
  575. }
  576. // now attach the shader to the parent surface - sort by ordinal string
  577. for (ShaderList::iterator it = surf.mShaders.begin(); it != surf.mShaders.end(); ++it) {
  578. if (::strcmp(shader.ordinal.c_str(), (*it).ordinal.c_str()) < 0) {
  579. surf.mShaders.insert(it, shader);
  580. return;
  581. }
  582. }
  583. surf.mShaders.push_back(shader);
  584. }
  585. // ------------------------------------------------------------------------------------------------
  586. void LWOImporter::LoadLWO2Surface(unsigned int size) {
  587. LE_NCONST uint8_t *const end = mFileBuffer + size;
  588. mSurfaces->push_back(LWO::Surface());
  589. LWO::Surface &surf = mSurfaces->back();
  590. GetS0(surf.mName, size);
  591. // check whether this surface was derived from any other surface
  592. std::string derived;
  593. GetS0(derived, (unsigned int)(end - mFileBuffer));
  594. if (derived.length()) {
  595. // yes, find this surface
  596. for (SurfaceList::iterator it = mSurfaces->begin(), itEnd = mSurfaces->end() - 1; it != itEnd; ++it) {
  597. if ((*it).mName == derived) {
  598. // we have it ...
  599. surf = *it;
  600. derived.clear();
  601. break;
  602. }
  603. }
  604. if (derived.size()) {
  605. ASSIMP_LOG_WARN("LWO2: Unable to find source surface: " + derived);
  606. }
  607. }
  608. while (true) {
  609. if (mFileBuffer + 6 >= end)
  610. break;
  611. const IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
  612. if (mFileBuffer + head.length > end)
  613. throw DeadlyImportError("LWO2: Invalid surface chunk length");
  614. uint8_t *const next = mFileBuffer + head.length;
  615. switch (head.type) {
  616. // diffuse color
  617. case AI_LWO_COLR: {
  618. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, COLR, 12);
  619. surf.mColor.r = GetF4();
  620. surf.mColor.g = GetF4();
  621. surf.mColor.b = GetF4();
  622. break;
  623. }
  624. // diffuse strength ... hopefully
  625. case AI_LWO_DIFF: {
  626. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, DIFF, 4);
  627. surf.mDiffuseValue = GetF4();
  628. break;
  629. }
  630. // specular strength ... hopefully
  631. case AI_LWO_SPEC: {
  632. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, SPEC, 4);
  633. surf.mSpecularValue = GetF4();
  634. break;
  635. }
  636. // transparency
  637. case AI_LWO_TRAN: {
  638. // transparency explicitly disabled?
  639. if (surf.mTransparency == 10e10f)
  640. break;
  641. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, TRAN, 4);
  642. surf.mTransparency = GetF4();
  643. break;
  644. }
  645. // additive transparency
  646. case AI_LWO_ADTR: {
  647. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, ADTR, 4);
  648. surf.mAdditiveTransparency = GetF4();
  649. break;
  650. }
  651. // wireframe mode
  652. case AI_LWO_LINE: {
  653. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, LINE, 2);
  654. if (GetU2() & 0x1)
  655. surf.mWireframe = true;
  656. break;
  657. }
  658. // glossiness
  659. case AI_LWO_GLOS: {
  660. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, GLOS, 4);
  661. surf.mGlossiness = GetF4();
  662. break;
  663. }
  664. // bump intensity
  665. case AI_LWO_BUMP: {
  666. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, BUMP, 4);
  667. surf.mBumpIntensity = GetF4();
  668. break;
  669. }
  670. // color highlights
  671. case AI_LWO_CLRH: {
  672. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, CLRH, 4);
  673. surf.mColorHighlights = GetF4();
  674. break;
  675. }
  676. // index of refraction
  677. case AI_LWO_RIND: {
  678. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, RIND, 4);
  679. surf.mIOR = GetF4();
  680. break;
  681. }
  682. // polygon sidedness
  683. case AI_LWO_SIDE: {
  684. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, SIDE, 2);
  685. surf.bDoubleSided = (3 == GetU2());
  686. break;
  687. }
  688. // maximum smoothing angle
  689. case AI_LWO_SMAN: {
  690. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, SMAN, 4);
  691. surf.mMaximumSmoothAngle = std::fabs(GetF4());
  692. break;
  693. }
  694. // vertex color channel to be applied to the surface
  695. case AI_LWO_VCOL: {
  696. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, VCOL, 12);
  697. surf.mDiffuseValue *= GetF4(); // strength
  698. ReadVSizedIntLWO2(mFileBuffer); // skip envelope
  699. surf.mVCMapType = GetU4(); // type of the channel
  700. // name of the channel
  701. GetS0(surf.mVCMap, (unsigned int)(next - mFileBuffer));
  702. break;
  703. }
  704. // surface bock entry
  705. case AI_LWO_BLOK: {
  706. AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, BLOK, 4);
  707. IFF::SubChunkHeader head2 = IFF::LoadSubChunk(mFileBuffer);
  708. switch (head2.type) {
  709. case AI_LWO_PROC:
  710. case AI_LWO_GRAD:
  711. case AI_LWO_IMAP:
  712. LoadLWO2TextureBlock(&head2, head.length);
  713. break;
  714. case AI_LWO_SHDR:
  715. LoadLWO2ShaderBlock(&head2, head.length);
  716. break;
  717. default:
  718. ASSIMP_LOG_WARN("LWO2: Found an unsupported surface BLOK");
  719. };
  720. break;
  721. }
  722. }
  723. mFileBuffer = next;
  724. }
  725. }
  726. #endif // !! ASSIMP_BUILD_NO_X_IMPORTER