LWOMaterial.cpp 31 KB

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