LWOMaterial.cpp 25 KB

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