LWOMaterial.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // internal headers
  36. #include "LWOLoader.h"
  37. #include "MaterialSystem.h"
  38. #include "ByteSwap.h"
  39. // public assimp headers
  40. #include "../include/IOStream.h"
  41. #include "../include/IOSystem.h"
  42. #include "../include/aiScene.h"
  43. #include "../include/aiAssert.h"
  44. #include "../include/DefaultLogger.h"
  45. // boost headers
  46. #include <boost/scoped_ptr.hpp>
  47. using namespace Assimp;
  48. // ------------------------------------------------------------------------------------------------
  49. void LWOImporter::ConvertMaterial(const LWO::Surface& surf,MaterialHelper* pcMat)
  50. {
  51. // copy the name of the surface
  52. aiString st;
  53. st.Set(surf.mName);
  54. pcMat->AddProperty(&st,AI_MATKEY_NAME);
  55. int i = surf.bDoubleSided ? 1 : 0;
  56. pcMat->AddProperty<int>(&i,1,AI_MATKEY_TWOSIDED);
  57. if (surf.mSpecularValue && surf.mGlossiness)
  58. {
  59. // this is only an assumption, needs to be confirmed.
  60. // the values have been tweaked by hand and seem to be correct.
  61. float fGloss;
  62. if (mIsLWO2)fGloss = surf.mGlossiness * 0.8f;
  63. else
  64. {
  65. if (16.0f >= surf.mGlossiness)fGloss = 6.0f;
  66. else if (64.0f >= surf.mGlossiness)fGloss = 20.0f;
  67. else if (256.0f >= surf.mGlossiness)fGloss = 50.0f;
  68. else fGloss = 80.0f;
  69. }
  70. pcMat->AddProperty<float>(&surf.mSpecularValue,1,AI_MATKEY_SHININESS_STRENGTH);
  71. pcMat->AddProperty<float>(&fGloss,1,AI_MATKEY_SHININESS);
  72. }
  73. // (the diffuse value is just a scaling factor)
  74. aiColor3D clr = surf.mColor;
  75. clr.r *= surf.mDiffuseValue;
  76. clr.g *= surf.mDiffuseValue;
  77. clr.b *= surf.mDiffuseValue;
  78. pcMat->AddProperty<aiColor3D>(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
  79. // specular color
  80. clr.b = clr.g = clr.r = surf.mSpecularValue;
  81. pcMat->AddProperty<aiColor3D>(&clr,1,AI_MATKEY_COLOR_SPECULAR);
  82. // emissive color
  83. // (luminosity is not really the same but it affects the surface in
  84. // a similar way. However, some scalings seems to be necessary)
  85. clr.g = clr.b = clr.r = surf.mLuminosity*0.8f;
  86. pcMat->AddProperty<aiColor3D>(&clr,1,AI_MATKEY_COLOR_EMISSIVE);
  87. // opacity
  88. float f = 1.0f-surf.mTransparency;
  89. pcMat->AddProperty<float>(&f,1,AI_MATKEY_OPACITY);
  90. // now handle all textures ...
  91. // TODO
  92. }
  93. // ------------------------------------------------------------------------------------------------
  94. void LWOImporter::LoadLWOBSurface(unsigned int size)
  95. {
  96. LE_NCONST uint8_t* const end = mFileBuffer + size;
  97. uint32_t iCursor = 0;
  98. mSurfaces->push_back( LWO::Surface () );
  99. LWO::Surface& surf = mSurfaces->back();
  100. LWO::Texture* pTex = NULL;
  101. ParseString(surf.mName,size);
  102. mFileBuffer+=surf.mName.length()+1;
  103. // skip one byte if the length of the surface name is odd
  104. if (!(surf.mName.length() & 1))++mFileBuffer;
  105. while (true)
  106. {
  107. if (mFileBuffer + 6 > end)break;
  108. // no proper IFF header here - the chunk length is specified as int16
  109. uint32_t head_type = *((LE_NCONST uint32_t*)mFileBuffer);mFileBuffer+=4;
  110. uint16_t head_length = *((LE_NCONST uint16_t*)mFileBuffer);mFileBuffer+=2;
  111. AI_LSWAP4(head_type);
  112. AI_LSWAP2(head_length);
  113. if (mFileBuffer + head_length > end)
  114. {
  115. throw new ImportErrorException("LWOB: Invalid file, the size attribute of "
  116. "a surface sub chunk points behind the end of the file");
  117. }
  118. LE_NCONST uint8_t* const next = mFileBuffer+head_length;
  119. switch (head_type)
  120. {
  121. // diffuse color
  122. case AI_LWO_COLR:
  123. {
  124. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,COLR,3);
  125. surf.mColor.r = *mFileBuffer++ / 255.0f;
  126. surf.mColor.g = *mFileBuffer++ / 255.0f;
  127. surf.mColor.b = *mFileBuffer / 255.0f;
  128. break;
  129. }
  130. // diffuse strength ...
  131. case AI_LWO_DIFF:
  132. {
  133. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,DIFF,2);
  134. AI_LSWAP2P(mFileBuffer);
  135. surf.mDiffuseValue = *((int16_t*)mFileBuffer) / 255.0f;
  136. break;
  137. }
  138. // specular strength ...
  139. case AI_LWO_SPEC:
  140. {
  141. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,SPEC,2);
  142. AI_LSWAP2P(mFileBuffer);
  143. surf.mSpecularValue = *((int16_t*)mFileBuffer) / 255.0f;
  144. break;
  145. }
  146. // luminosity ...
  147. case AI_LWO_LUMI:
  148. {
  149. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,LUMI,2);
  150. AI_LSWAP2P(mFileBuffer);
  151. surf.mLuminosity = *((int16_t*)mFileBuffer) / 255.0f;
  152. break;
  153. }
  154. // transparency
  155. case AI_LWO_TRAN:
  156. {
  157. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,TRAN,2);
  158. AI_LSWAP2P(mFileBuffer);
  159. surf.mTransparency = *((int16_t*)mFileBuffer) / 255.0f;
  160. break;
  161. }
  162. // glossiness
  163. case AI_LWO_GLOS:
  164. {
  165. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,GLOS,2);
  166. AI_LSWAP2P(mFileBuffer);
  167. surf.mGlossiness = float(*((int16_t*)mFileBuffer));
  168. break;
  169. }
  170. // color texture
  171. case AI_LWO_CTEX:
  172. {
  173. pTex = &surf.mColorTexture;
  174. break;
  175. }
  176. // diffuse texture
  177. case AI_LWO_DTEX:
  178. {
  179. pTex = &surf.mDiffuseTexture;
  180. break;
  181. }
  182. // specular texture
  183. case AI_LWO_STEX:
  184. {
  185. pTex = &surf.mSpecularTexture;
  186. break;
  187. }
  188. // bump texture
  189. case AI_LWO_BTEX:
  190. {
  191. pTex = &surf.mBumpTexture;
  192. break;
  193. }
  194. // transparency texture
  195. case AI_LWO_TTEX:
  196. {
  197. pTex = &surf.mTransparencyTexture;
  198. break;
  199. }
  200. // texture path
  201. case AI_LWO_TIMG:
  202. {
  203. if (pTex)
  204. {
  205. ParseString(pTex->mFileName,head_length);
  206. AdjustTexturePath(pTex->mFileName);
  207. mFileBuffer += pTex->mFileName.length();
  208. }
  209. else DefaultLogger::get()->warn("LWOB: TIMG tag was encuntered although "
  210. "there was no xTEX tag before");
  211. break;
  212. }
  213. // texture strength
  214. case AI_LWO_TVAL:
  215. {
  216. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,TVAL,1);
  217. if (pTex)pTex->mStrength = *mFileBuffer / 255.0f;
  218. else DefaultLogger::get()->warn("LWOB: TVAL tag was encuntered "
  219. "although there was no xTEX tag before");
  220. break;
  221. }
  222. }
  223. mFileBuffer = next;
  224. }
  225. }
  226. // ------------------------------------------------------------------------------------------------
  227. void LWOImporter::LoadLWO2ImageMap(unsigned int size, LWO::Texture& tex )
  228. {
  229. LE_NCONST uint8_t* const end = mFileBuffer + size;
  230. }
  231. // ------------------------------------------------------------------------------------------------
  232. void LWOImporter::LoadLWO2Procedural(unsigned int size, LWO::Texture& tex )
  233. {
  234. LE_NCONST uint8_t* const end = mFileBuffer + size;
  235. }
  236. // ------------------------------------------------------------------------------------------------
  237. void LWOImporter::LoadLWO2Gradient(unsigned int size, LWO::Texture& tex )
  238. {
  239. LE_NCONST uint8_t* const end = mFileBuffer + size;
  240. }
  241. // ------------------------------------------------------------------------------------------------
  242. void LWOImporter::LoadLWO2TextureHeader(unsigned int size, LWO::Texture& tex )
  243. {
  244. LE_NCONST uint8_t* const end = mFileBuffer + size;
  245. }
  246. // ------------------------------------------------------------------------------------------------
  247. void LWOImporter::LoadLWO2TextureBlock(uint32_t type, unsigned int size )
  248. {
  249. LE_NCONST uint8_t* const end = mFileBuffer + size;
  250. LWO::Surface& surf = mSurfaces->back();
  251. LWO::Texture tex;
  252. // now get the exact type of the texture
  253. }
  254. // ------------------------------------------------------------------------------------------------
  255. void LWOImporter::LoadLWO2Surface(unsigned int size)
  256. {
  257. LE_NCONST uint8_t* const end = mFileBuffer + size;
  258. mSurfaces->push_back( LWO::Surface () );
  259. LWO::Surface& surf = mSurfaces->back();
  260. ParseString(surf.mName,size);
  261. mFileBuffer+=surf.mName.length()+1;
  262. // skip one byte if the length of the surface name is odd
  263. if (!(surf.mName.length() & 1))++mFileBuffer;
  264. while (true)
  265. {
  266. if (mFileBuffer + 6 > end)break;
  267. // no proper IFF header here - the chunk length is specified as int16
  268. uint32_t head_type = *((LE_NCONST uint32_t*)mFileBuffer);mFileBuffer+=4;
  269. uint16_t head_length = *((LE_NCONST uint16_t*)mFileBuffer);mFileBuffer+=2;
  270. AI_LSWAP4(head_type);
  271. AI_LSWAP2(head_length);
  272. if (mFileBuffer + head_length > end)
  273. {
  274. throw new ImportErrorException("LWO2: Invalid file, the size attribute of "
  275. "a surface sub chunk points behind the end of the file");
  276. }
  277. LE_NCONST uint8_t* const next = mFileBuffer+head_length;
  278. switch (head_type)
  279. {
  280. // diffuse color
  281. case AI_LWO_COLR:
  282. {
  283. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,COLR,12);
  284. surf.mColor.r = ((float*)mFileBuffer)[0];
  285. surf.mColor.g = ((float*)mFileBuffer)[1];
  286. surf.mColor.b = ((float*)mFileBuffer)[2];
  287. AI_LSWAP4(surf.mColor.r);
  288. AI_LSWAP4(surf.mColor.g);
  289. AI_LSWAP4(surf.mColor.b);
  290. break;
  291. }
  292. // diffuse strength ... hopefully
  293. case AI_LWO_DIFF:
  294. {
  295. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,DIFF,4);
  296. surf.mDiffuseValue = *((float*)mFileBuffer);
  297. AI_LSWAP4(surf.mDiffuseValue);
  298. break;
  299. }
  300. // specular strength ... hopefully
  301. case AI_LWO_SPEC:
  302. {
  303. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,SPEC,4);
  304. surf.mSpecularValue = *((float*)mFileBuffer);
  305. AI_LSWAP4(surf.mSpecularValue);
  306. break;
  307. }
  308. // transparency
  309. case AI_LWO_TRAN:
  310. {
  311. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,TRAN,4);
  312. surf.mTransparency = *((float*)mFileBuffer);
  313. AI_LSWAP4(surf.mTransparency);
  314. break;
  315. }
  316. // glossiness
  317. case AI_LWO_GLOS:
  318. {
  319. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,GLOS,4);
  320. surf.mGlossiness = *((float*)mFileBuffer);
  321. AI_LSWAP4(surf.mGlossiness);
  322. break;
  323. }
  324. // surface bock entry
  325. case AI_LWO_BLOK:
  326. {
  327. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,BLOK,4);
  328. uint32_t type = *((uint32_t*)mFileBuffer);
  329. AI_LSWAP4(type);
  330. switch (type)
  331. {
  332. case AI_LWO_IMAP:
  333. case AI_LWO_PROC:
  334. case AI_LWO_GRAD:
  335. mFileBuffer+=4;
  336. LoadLWO2TextureBlock(type,head_length-4);
  337. break;
  338. };
  339. break;
  340. }
  341. }
  342. mFileBuffer = next;
  343. }
  344. }
  345. // ------------------------------------------------------------------------------------------------
  346. bool LWOImporter::ComputeGradientTexture(LWO::GradientInfo& grad,
  347. std::vector<aiTexture*>& out)
  348. {
  349. aiTexture* tex = new aiTexture();
  350. tex->mHeight = configGradientResY;
  351. tex->mWidth = configGradientResX;
  352. unsigned int numPixels;
  353. tex->pcData = new aiTexel[numPixels = tex->mHeight * tex->mWidth];
  354. // to be implemented ...
  355. out.push_back(tex);
  356. return true;
  357. }