2
0

LWOMaterial.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. 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(MaterialHelper* pcMat, const TextureList& in, const char* type)
  66. {
  67. ai_assert(NULL != pcMat && NULL != type);
  68. unsigned int cur = 0, temp = 0;
  69. char buffer[512];
  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 || 0xffffffff == (*it).mRealUVIndex)continue;
  76. ret = true;
  77. // add the path to the texture
  78. sprintf(buffer,"$tex.file.%s[%i]",type,cur);
  79. // The older LWOB format does not use indirect references to clips.
  80. // The file name of a texture is directly specified in the tex chunk.
  81. if (mIsLWO2)
  82. {
  83. // find the corresponding clip
  84. ClipList::iterator clip = mClips.begin();
  85. temp = (*it).mClipIdx;
  86. for (ClipList::iterator end = mClips.end(); clip != end; ++clip)
  87. {
  88. if ((*clip).idx == temp)
  89. {
  90. break;
  91. }
  92. }
  93. if (mClips.end() == clip)
  94. {
  95. DefaultLogger::get()->error("LWO2: Clip index is out of bounds");
  96. temp = 0;
  97. }
  98. if (Clip::UNSUPPORTED == (*clip).type)
  99. {
  100. DefaultLogger::get()->error("LWO2: Clip type is not supported");
  101. continue;
  102. }
  103. AdjustTexturePath((*clip).path);
  104. s.Set((*clip).path);
  105. }
  106. else
  107. {
  108. std::string ss = (*it).mFileName;
  109. if (!ss.length())
  110. {
  111. DefaultLogger::get()->error("LWOB: Empty file name");
  112. continue;
  113. }
  114. AdjustTexturePath(ss);
  115. s.Set(ss);
  116. }
  117. pcMat->AddProperty(&s,buffer);
  118. // add the blend factor
  119. sprintf(buffer,"$tex.blend.%s[%i]",type,cur);
  120. pcMat->AddProperty(&(*it).mStrength,1,buffer);
  121. // add the blend operation
  122. sprintf(buffer,"$tex.op.%s[%i]",type,cur);
  123. switch ((*it).blendType)
  124. {
  125. case LWO::Texture::Normal:
  126. case LWO::Texture::Multiply:
  127. temp = (unsigned int)aiTextureOp_Multiply;
  128. break;
  129. case LWO::Texture::Subtractive:
  130. case LWO::Texture::Difference:
  131. temp = (unsigned int)aiTextureOp_Subtract;
  132. break;
  133. case LWO::Texture::Divide:
  134. temp = (unsigned int)aiTextureOp_Divide;
  135. break;
  136. case LWO::Texture::Additive:
  137. temp = (unsigned int)aiTextureOp_Add;
  138. break;
  139. default:
  140. temp = (unsigned int)aiTextureOp_Multiply;
  141. DefaultLogger::get()->warn("LWO2: Unsupported texture blend mode: alpha or displacement");
  142. }
  143. pcMat->AddProperty<int>((int*)&temp,1,buffer);
  144. // add the UV source index
  145. sprintf(buffer,"$tex.uvw.%s[%i]",type,cur);
  146. temp = (*it).mRealUVIndex;
  147. pcMat->AddProperty<int>((int*)&temp,1,buffer);
  148. // add the u-wrapping
  149. sprintf(buffer,"$tex.mapmodeu.%s[%i]",type,cur);
  150. temp = (unsigned int)GetMapMode((*it).wrapModeWidth);
  151. pcMat->AddProperty<int>((int*)&temp,1,buffer);
  152. // add the v-wrapping
  153. sprintf(buffer,"$tex.mapmodev.%s[%i]",type,cur);
  154. temp = (unsigned int)GetMapMode((*it).wrapModeHeight);
  155. pcMat->AddProperty<int>((int*)&temp,1,buffer);
  156. ++cur;
  157. }
  158. return ret;
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. void LWOImporter::ConvertMaterial(const LWO::Surface& surf,MaterialHelper* pcMat)
  162. {
  163. // copy the name of the surface
  164. aiString st;
  165. st.Set(surf.mName);
  166. pcMat->AddProperty(&st,AI_MATKEY_NAME);
  167. int i = surf.bDoubleSided ? 1 : 0;
  168. pcMat->AddProperty<int>(&i,1,AI_MATKEY_TWOSIDED);
  169. // add the refraction index and the bump intensity
  170. pcMat->AddProperty<float>(&surf.mIOR,1,AI_MATKEY_REFRACTI);
  171. pcMat->AddProperty<float>(&surf.mBumpIntensity,1,AI_MATKEY_BUMPSCALING);
  172. aiShadingMode m;
  173. if (surf.mSpecularValue && surf.mGlossiness)
  174. {
  175. float fGloss;
  176. if (mIsLWO2)
  177. {
  178. fGloss = pow( surf.mGlossiness*10.0f+2.0f, 2.0f);
  179. }
  180. else
  181. {
  182. if (16.0f >= surf.mGlossiness)fGloss = 6.0f;
  183. else if (64.0f >= surf.mGlossiness)fGloss = 20.0f;
  184. else if (256.0f >= surf.mGlossiness)fGloss = 50.0f;
  185. else fGloss = 80.0f;
  186. }
  187. pcMat->AddProperty<float>(&surf.mSpecularValue,1,AI_MATKEY_SHININESS_STRENGTH);
  188. pcMat->AddProperty<float>(&fGloss,1,AI_MATKEY_SHININESS);
  189. m = aiShadingMode_Phong;
  190. }
  191. else m = aiShadingMode_Gouraud;
  192. // specular color
  193. aiColor3D clr = lerp( aiColor3D(1.f,1.f,1.f), surf.mColor, surf.mColorHighlights );
  194. pcMat->AddProperty<aiColor3D>(&clr,1,AI_MATKEY_COLOR_SPECULAR);
  195. pcMat->AddProperty<float>(&surf.mSpecularValue,1,AI_MATKEY_SHININESS_STRENGTH);
  196. // emissive color
  197. // (luminosity is not really the same but it affects the surface in
  198. // a similar way. However, some scalings seems to be necessary)
  199. clr.g = clr.b = clr.r = surf.mLuminosity*0.8f;
  200. pcMat->AddProperty<aiColor3D>(&clr,1,AI_MATKEY_COLOR_EMISSIVE);
  201. // opacity
  202. float f = 1.0f-surf.mTransparency;
  203. pcMat->AddProperty<float>(&f,1,AI_MATKEY_OPACITY);
  204. // ADD TEXTURES to the material
  205. // TODO: find out how we can handle COLOR textures correctly...
  206. bool b = HandleTextures(pcMat,surf.mColorTextures,"diffuse");
  207. b = (b || HandleTextures(pcMat,surf.mDiffuseTextures,"diffuse"));
  208. HandleTextures(pcMat,surf.mSpecularTextures,"specular");
  209. HandleTextures(pcMat,surf.mGlossinessTextures,"shininess");
  210. HandleTextures(pcMat,surf.mBumpTextures,"height");
  211. HandleTextures(pcMat,surf.mOpacityTextures,"opacity");
  212. // now we need to know which shader we must use
  213. // iterate through the shader list of the surface and
  214. // search for a name we know
  215. for (ShaderList::const_iterator it = surf.mShaders.begin(), end = surf.mShaders.end();
  216. it != end;++it)
  217. {
  218. if (!(*it).enabled)continue;
  219. if ((*it).functionName == "LW_SuperCelShader" ||
  220. (*it).functionName == "AH_CelShader")
  221. {
  222. DefaultLogger::get()->info("Mapping LW_SuperCelShader/AH_CelShader "
  223. "to aiShadingMode_Toon");
  224. m = aiShadingMode_Toon;
  225. break;
  226. }
  227. else if ((*it).functionName == "LW_RealFresnel" ||
  228. (*it).functionName == "LW_FastFresnel")
  229. {
  230. DefaultLogger::get()->info("Mapping LW_RealFresnel/LW_FastFresnel "
  231. "to aiShadingMode_Fresnel");
  232. m = aiShadingMode_Fresnel;
  233. break;
  234. }
  235. else
  236. {
  237. DefaultLogger::get()->warn("LWO2: Unknown surface shader: " + (*it).functionName);
  238. }
  239. }
  240. if (surf.mMaximumSmoothAngle <= 0.0f)m = aiShadingMode_Flat;
  241. pcMat->AddProperty((int*)&m,1,AI_MATKEY_SHADING_MODEL);
  242. // (the diffuse value is just a scaling factor)
  243. // If a diffuse texture is set, we set this value to 1.0
  244. clr = (b ? aiColor3D(1.f,1.f,1.f) : surf.mColor);
  245. clr.r *= surf.mDiffuseValue;
  246. clr.g *= surf.mDiffuseValue;
  247. clr.b *= surf.mDiffuseValue;
  248. pcMat->AddProperty<aiColor3D>(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
  249. }
  250. // ------------------------------------------------------------------------------------------------
  251. void LWOImporter::FindUVChannels(LWO::TextureList& list, LWO::Layer& layer,
  252. unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS], unsigned int& next)
  253. {
  254. for (TextureList::iterator it = list.begin(), end = list.end();
  255. it != end;++it)
  256. {
  257. if (!(*it).enabled || !(*it).bCanUse || 0xffffffff != (*it).mRealUVIndex)continue;
  258. switch ((*it).mapMode)
  259. {
  260. // TODO: implement these special mappings ...
  261. case LWO::Texture::Spherical:
  262. case LWO::Texture::Cylindrical:
  263. case LWO::Texture::Cubic:
  264. case LWO::Texture::Planar:
  265. case LWO::Texture::FrontProjection:
  266. DefaultLogger::get()->warn("LWO2: Only UV mapping is currently supported.");
  267. continue;
  268. default: ;
  269. }
  270. for (unsigned int i = 0; i < layer.mUVChannels.size();++i)
  271. {
  272. if ((*it).mUVChannelIndex == layer.mUVChannels[i].name)
  273. {
  274. // check whether we have this channel already
  275. for (unsigned int m = 0; m < next;++m)
  276. {
  277. if (i == out[m])
  278. {
  279. (*it).mRealUVIndex = m;
  280. }
  281. }
  282. if (0xffffffff == (*it).mRealUVIndex)
  283. {
  284. (*it).mRealUVIndex = next;
  285. out[next++] = i;
  286. if (AI_MAX_NUMBER_OF_TEXTURECOORDS != next)
  287. out[next] = 0xffffffff;
  288. break;
  289. }
  290. }
  291. }
  292. if (0xffffffff == (*it).mRealUVIndex)
  293. DefaultLogger::get()->error("LWO2: Unable to find matching UV channel for a texture");
  294. }
  295. }
  296. // ------------------------------------------------------------------------------------------------
  297. void LWOImporter::FindUVChannels(LWO::Surface& surf, LWO::Layer& layer,
  298. unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS])
  299. {
  300. out[0] = 0xffffffff;
  301. unsigned int next = 0;
  302. FindUVChannels(surf.mColorTextures,layer,out,next);
  303. FindUVChannels(surf.mDiffuseTextures,layer,out,next);
  304. FindUVChannels(surf.mSpecularTextures,layer,out,next);
  305. FindUVChannels(surf.mGlossinessTextures,layer,out,next);
  306. FindUVChannels(surf.mOpacityTextures,layer,out,next);
  307. FindUVChannels(surf.mBumpTextures,layer,out,next);
  308. }
  309. // ------------------------------------------------------------------------------------------------
  310. void LWOImporter::FindVCChannels(const LWO::Surface& surf, const LWO::Layer& layer,
  311. unsigned int out[AI_MAX_NUMBER_OF_COLOR_SETS])
  312. {
  313. out[0] = 0xffffffff;
  314. if (surf.mVCMap.length())
  315. {
  316. for (unsigned int i = 0; i < layer.mVColorChannels.size();++i)
  317. {
  318. if (surf.mVCMap == layer.mVColorChannels[i].name)
  319. {
  320. out[0] = i;
  321. out[1] = 0xffffffff;
  322. return;
  323. }
  324. }
  325. DefaultLogger::get()->warn("LWO2: Unable to find vertex color channel: " + surf.mVCMap);
  326. }
  327. }
  328. // ------------------------------------------------------------------------------------------------
  329. void LWOImporter::LoadLWO2ImageMap(unsigned int size, LWO::Texture& tex )
  330. {
  331. LE_NCONST uint8_t* const end = mFileBuffer + size;
  332. while (true)
  333. {
  334. if (mFileBuffer + 6 >= end)break;
  335. LE_NCONST IFF::SubChunkHeader* const head = IFF::LoadSubChunk(mFileBuffer);
  336. if (mFileBuffer + head->length > end)
  337. throw new ImportErrorException("LWO2: Invalid SURF.BLOCK chunk length");
  338. uint8_t* const next = mFileBuffer+head->length;
  339. switch (head->type)
  340. {
  341. case AI_LWO_PROJ:
  342. tex.wrapModeWidth = (Texture::Wrap)GetU2();
  343. tex.wrapModeHeight = (Texture::Wrap)GetU2();
  344. break;
  345. case AI_LWO_AXIS:
  346. tex.majorAxis = (Texture::Axes)GetU2();
  347. break;
  348. case AI_LWO_IMAG:
  349. tex.mClipIdx = GetU2();
  350. break;
  351. case AI_LWO_VMAP:
  352. GetS0(tex.mUVChannelIndex,head->length);
  353. break;
  354. case AI_LWO_WRPH:
  355. tex.wrapAmountH = GetF4();
  356. break;
  357. case AI_LWO_WRPW:
  358. tex.wrapAmountW = GetF4();
  359. break;
  360. }
  361. mFileBuffer = next;
  362. }
  363. }
  364. // ------------------------------------------------------------------------------------------------
  365. void LWOImporter::LoadLWO2Procedural(unsigned int size, LWO::Texture& tex )
  366. {
  367. // --- not supported at the moment
  368. DefaultLogger::get()->error("LWO2: Found procedural texture, this is not supported");
  369. tex.bCanUse = false;
  370. }
  371. // ------------------------------------------------------------------------------------------------
  372. void LWOImporter::LoadLWO2Gradient(unsigned int size, LWO::Texture& tex )
  373. {
  374. // --- not supported at the moment
  375. DefaultLogger::get()->error("LWO2: Found gradient texture, this is not supported");
  376. tex.bCanUse = false;
  377. }
  378. // ------------------------------------------------------------------------------------------------
  379. void LWOImporter::LoadLWO2TextureHeader(unsigned int size, LWO::Texture& tex )
  380. {
  381. LE_NCONST uint8_t* const end = mFileBuffer + size;
  382. // get the ordinal string
  383. GetS0( tex.ordinal, size);
  384. // we could crash later if this is an empty string ...
  385. if (!tex.ordinal.length())
  386. {
  387. DefaultLogger::get()->error("LWO2: Ill-formed SURF.BLOK ordinal string");
  388. tex.ordinal = "\x00";
  389. }
  390. while (true)
  391. {
  392. if (mFileBuffer + 6 >= end)break;
  393. LE_NCONST IFF::SubChunkHeader* const head = IFF::LoadSubChunk(mFileBuffer);
  394. if (mFileBuffer + head->length > end)
  395. throw new ImportErrorException("LWO2: Invalid texture header chunk length");
  396. uint8_t* const next = mFileBuffer+head->length;
  397. switch (head->type)
  398. {
  399. case AI_LWO_CHAN:
  400. tex.type = GetU4();
  401. break;
  402. case AI_LWO_ENAB:
  403. tex.enabled = GetU2() ? true : false;
  404. break;
  405. case AI_LWO_OPAC:
  406. tex.blendType = (Texture::BlendType)GetU2();
  407. tex.mStrength = GetF4();
  408. break;
  409. }
  410. mFileBuffer = next;
  411. }
  412. }
  413. // ------------------------------------------------------------------------------------------------
  414. void LWOImporter::LoadLWO2TextureBlock(LE_NCONST IFF::SubChunkHeader* head, unsigned int size )
  415. {
  416. ai_assert(!mSurfaces->empty());
  417. LWO::Surface& surf = mSurfaces->back();
  418. LWO::Texture tex;
  419. // load the texture header
  420. LoadLWO2TextureHeader(head->length,tex);
  421. size -= head->length + 6;
  422. // now get the exact type of the texture
  423. switch (head->type)
  424. {
  425. case AI_LWO_PROC:
  426. LoadLWO2Procedural(size,tex);
  427. break;
  428. case AI_LWO_GRAD:
  429. LoadLWO2Gradient(size,tex);
  430. break;
  431. case AI_LWO_IMAP:
  432. LoadLWO2ImageMap(size,tex);
  433. }
  434. // get the destination channel
  435. TextureList* listRef = NULL;
  436. switch (tex.type)
  437. {
  438. case AI_LWO_COLR:
  439. listRef = &surf.mColorTextures;break;
  440. case AI_LWO_DIFF:
  441. listRef = &surf.mDiffuseTextures;break;
  442. case AI_LWO_SPEC:
  443. listRef = &surf.mSpecularTextures;break;
  444. case AI_LWO_GLOS:
  445. listRef = &surf.mGlossinessTextures;break;
  446. case AI_LWO_BUMP:
  447. listRef = &surf.mBumpTextures;break;
  448. case AI_LWO_TRAN:
  449. listRef = &surf.mOpacityTextures;break;
  450. default:
  451. DefaultLogger::get()->warn("LWO2: Encountered unknown texture type");
  452. return;
  453. }
  454. // now attach the texture to the parent surface - sort by ordinal string
  455. for (TextureList::iterator it = listRef->begin();
  456. it != listRef->end(); ++it)
  457. {
  458. if (::strcmp(tex.ordinal.c_str(),(*it).ordinal.c_str()) < 0)
  459. {
  460. listRef->insert(it,tex);
  461. return;
  462. }
  463. }
  464. listRef->push_back(tex);
  465. }
  466. // ------------------------------------------------------------------------------------------------
  467. void LWOImporter::LoadLWO2ShaderBlock(LE_NCONST IFF::SubChunkHeader* head, unsigned int size )
  468. {
  469. LE_NCONST uint8_t* const end = mFileBuffer + size;
  470. ai_assert(!mSurfaces->empty());
  471. LWO::Surface& surf = mSurfaces->back();
  472. LWO::Shader shader;
  473. // get the ordinal string
  474. GetS0( shader.ordinal, size);
  475. // we could crash later if this is an empty string ...
  476. if (!shader.ordinal.length())
  477. {
  478. DefaultLogger::get()->error("LWO2: Ill-formed SURF.BLOK ordinal string");
  479. shader.ordinal = "\x00";
  480. }
  481. // read the header
  482. while (true)
  483. {
  484. if (mFileBuffer + 6 >= end)break;
  485. LE_NCONST IFF::SubChunkHeader* const head = IFF::LoadSubChunk(mFileBuffer);
  486. if (mFileBuffer + head->length > end)
  487. throw new ImportErrorException("LWO2: Invalid shader header chunk length");
  488. uint8_t* const next = mFileBuffer+head->length;
  489. switch (head->type)
  490. {
  491. case AI_LWO_ENAB:
  492. shader.enabled = GetU2() ? true : false;
  493. break;
  494. case AI_LWO_FUNC:
  495. GetS0( shader.functionName, head->length );
  496. }
  497. mFileBuffer = next;
  498. }
  499. // now attach the shader to the parent surface - sort by ordinal string
  500. for (ShaderList::iterator it = surf.mShaders.begin();
  501. it != surf.mShaders.end(); ++it)
  502. {
  503. if (::strcmp(shader.ordinal.c_str(),(*it).ordinal.c_str()) < 0)
  504. {
  505. surf.mShaders.insert(it,shader);
  506. return;
  507. }
  508. }
  509. surf.mShaders.push_back(shader);
  510. }
  511. // ------------------------------------------------------------------------------------------------
  512. void LWOImporter::LoadLWO2Surface(unsigned int size)
  513. {
  514. LE_NCONST uint8_t* const end = mFileBuffer + size;
  515. mSurfaces->push_back( LWO::Surface () );
  516. LWO::Surface& surf = mSurfaces->back();
  517. GetS0(surf.mName,size);
  518. // check whether this surface was derived from any other surface
  519. std::string derived;
  520. GetS0(derived,(unsigned int)(end - mFileBuffer));
  521. if (derived.length())
  522. {
  523. // yes, find this surface
  524. for (SurfaceList::iterator it = mSurfaces->begin(), end = mSurfaces->end()-1;
  525. it != end; ++it)
  526. {
  527. if ((*it).mName == derived)
  528. {
  529. // we have it ...
  530. surf = *it;
  531. derived.clear();
  532. }
  533. }
  534. if (!derived.size())
  535. DefaultLogger::get()->warn("LWO2: Unable to find source surface: " + derived);
  536. }
  537. while (true)
  538. {
  539. if (mFileBuffer + 6 >= end)break;
  540. LE_NCONST IFF::SubChunkHeader* const head = IFF::LoadSubChunk(mFileBuffer);
  541. if (mFileBuffer + head->length > end)
  542. throw new ImportErrorException("LWO2: Invalid surface chunk length");
  543. uint8_t* const next = mFileBuffer+head->length;
  544. switch (head->type)
  545. {
  546. // diffuse color
  547. case AI_LWO_COLR:
  548. {
  549. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,COLR,12);
  550. surf.mColor.r = GetF4();
  551. surf.mColor.g = GetF4();
  552. surf.mColor.b = GetF4();
  553. break;
  554. }
  555. // diffuse strength ... hopefully
  556. case AI_LWO_DIFF:
  557. {
  558. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,DIFF,4);
  559. surf.mDiffuseValue = GetF4();
  560. break;
  561. }
  562. // specular strength ... hopefully
  563. case AI_LWO_SPEC:
  564. {
  565. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,SPEC,4);
  566. surf.mSpecularValue = GetF4();
  567. break;
  568. }
  569. // transparency
  570. case AI_LWO_TRAN:
  571. {
  572. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,TRAN,4);
  573. surf.mTransparency = GetF4();
  574. break;
  575. }
  576. // glossiness
  577. case AI_LWO_GLOS:
  578. {
  579. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,GLOS,4);
  580. surf.mGlossiness = GetF4();
  581. break;
  582. }
  583. // bump intensity
  584. case AI_LWO_BUMP:
  585. {
  586. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,BUMP,4);
  587. surf.mBumpIntensity = GetF4();
  588. break;
  589. }
  590. // color highlights
  591. case AI_LWO_CLRH:
  592. {
  593. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,CLRH,4);
  594. surf.mColorHighlights = GetF4();
  595. break;
  596. }
  597. // index of refraction
  598. case AI_LWO_RIND:
  599. {
  600. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,RIND,4);
  601. surf.mIOR = GetF4();
  602. break;
  603. }
  604. // polygon sidedness
  605. case AI_LWO_SIDE:
  606. {
  607. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,SIDE,2);
  608. surf.bDoubleSided = (3 == GetU2());
  609. break;
  610. }
  611. // maximum smoothing angle
  612. case AI_LWO_SMAN:
  613. {
  614. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,SMAN,4);
  615. surf.mMaximumSmoothAngle = GetF4();
  616. break;
  617. }
  618. // surface bock entry
  619. case AI_LWO_BLOK:
  620. {
  621. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,BLOK,4);
  622. LE_NCONST IFF::SubChunkHeader* head2 = IFF::LoadSubChunk(mFileBuffer);
  623. switch (head2->type)
  624. {
  625. case AI_LWO_PROC:
  626. case AI_LWO_GRAD:
  627. case AI_LWO_IMAP:
  628. LoadLWO2TextureBlock(head2, head->length);
  629. break;
  630. case AI_LWO_SHDR:
  631. LoadLWO2ShaderBlock(head2, head->length);
  632. break;
  633. default:
  634. DefaultLogger::get()->warn("LWO2: Found an unsupported surface BLOK");
  635. };
  636. break;
  637. }
  638. }
  639. mFileBuffer = next;
  640. }
  641. }