LWOMaterial.cpp 23 KB

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