LWOMaterial.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 * 50.0f;
  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::FindUVChannels(const LWO::Surface& surf, const LWO::Layer& layer,
  95. unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS])
  96. {
  97. out[0] = 0xffffffff;
  98. }
  99. // ------------------------------------------------------------------------------------------------
  100. void LWOImporter::FindVCChannels(const LWO::Surface& surf, const LWO::Layer& layer,
  101. unsigned int out[AI_MAX_NUMBER_OF_COLOR_SETS])
  102. {
  103. out[0] = 0xffffffff;
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. void LWOImporter::LoadLWO2ImageMap(unsigned int size, LWO::Texture& tex )
  107. {
  108. //LE_NCONST uint8_t* const end = mFileBuffer + size;
  109. }
  110. // ------------------------------------------------------------------------------------------------
  111. void LWOImporter::LoadLWO2Procedural(unsigned int size, LWO::Texture& tex )
  112. {
  113. // --- not supported at the moment
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. void LWOImporter::LoadLWO2Gradient(unsigned int size, LWO::Texture& tex )
  117. {
  118. // --- not supported at the moment
  119. }
  120. // ------------------------------------------------------------------------------------------------
  121. void LWOImporter::LoadLWO2TextureHeader(unsigned int size, LWO::Texture& tex )
  122. {
  123. //LE_NCONST uint8_t* const end = mFileBuffer + size;
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. void LWOImporter::LoadLWO2TextureBlock(uint32_t type, unsigned int size )
  127. {
  128. //LE_NCONST uint8_t* const end = mFileBuffer + size;
  129. //LWO::Surface& surf = mSurfaces->back();
  130. //LWO::Texture tex;
  131. // now get the exact type of the texture
  132. }
  133. // ------------------------------------------------------------------------------------------------
  134. void LWOImporter::LoadLWO2Surface(unsigned int size)
  135. {
  136. LE_NCONST uint8_t* const end = mFileBuffer + size;
  137. mSurfaces->push_back( LWO::Surface () );
  138. LWO::Surface& surf = mSurfaces->back();
  139. ParseString(surf.mName,size);
  140. mFileBuffer+=surf.mName.length()+1;
  141. // skip one byte if the length of the surface name is odd
  142. if (!(surf.mName.length() & 1))++mFileBuffer;
  143. mFileBuffer += 2;
  144. while (true)
  145. {
  146. if (mFileBuffer + 6 >= end)break;
  147. // no proper IFF header here - the chunk length is specified as int16
  148. uint32_t head_type = *((LE_NCONST uint32_t*)mFileBuffer);mFileBuffer+=4;
  149. uint16_t head_length = *((LE_NCONST uint16_t*)mFileBuffer);mFileBuffer+=2;
  150. AI_LSWAP4(head_type);
  151. AI_LSWAP2(head_length);
  152. if (mFileBuffer + head_length > end)
  153. {
  154. throw new ImportErrorException("LWO2: Invalid file, the size attribute of "
  155. "a surface sub chunk points behind the end of the file");
  156. }
  157. LE_NCONST uint8_t* const next = mFileBuffer+head_length;
  158. switch (head_type)
  159. {
  160. // diffuse color
  161. case AI_LWO_COLR:
  162. {
  163. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,COLR,12);
  164. surf.mColor.r = ((float*)mFileBuffer)[0];
  165. surf.mColor.g = ((float*)mFileBuffer)[1];
  166. surf.mColor.b = ((float*)mFileBuffer)[2];
  167. AI_LSWAP4(surf.mColor.r);
  168. AI_LSWAP4(surf.mColor.g);
  169. AI_LSWAP4(surf.mColor.b);
  170. break;
  171. }
  172. // diffuse strength ... hopefully
  173. case AI_LWO_DIFF:
  174. {
  175. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,DIFF,4);
  176. surf.mDiffuseValue = *((float*)mFileBuffer);
  177. AI_LSWAP4(surf.mDiffuseValue);
  178. break;
  179. }
  180. // specular strength ... hopefully
  181. case AI_LWO_SPEC:
  182. {
  183. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,SPEC,4);
  184. surf.mSpecularValue = *((float*)mFileBuffer);
  185. AI_LSWAP4(surf.mSpecularValue);
  186. break;
  187. }
  188. // transparency
  189. case AI_LWO_TRAN:
  190. {
  191. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,TRAN,4);
  192. surf.mTransparency = *((float*)mFileBuffer);
  193. AI_LSWAP4(surf.mTransparency);
  194. break;
  195. }
  196. // glossiness
  197. case AI_LWO_GLOS:
  198. {
  199. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,GLOS,4);
  200. surf.mGlossiness = *((float*)mFileBuffer);
  201. AI_LSWAP4(surf.mGlossiness);
  202. break;
  203. }
  204. // surface bock entry
  205. case AI_LWO_BLOK:
  206. {
  207. AI_LWO_VALIDATE_CHUNK_LENGTH(head_length,BLOK,4);
  208. uint32_t type = *((uint32_t*)mFileBuffer);
  209. AI_LSWAP4(type);
  210. switch (type)
  211. {
  212. case AI_LWO_IMAP:
  213. case AI_LWO_PROC:
  214. break;
  215. case AI_LWO_GRAD:
  216. mFileBuffer+=4;
  217. LoadLWO2TextureBlock(type,head_length-4);
  218. break;
  219. };
  220. break;
  221. }
  222. }
  223. mFileBuffer = next;
  224. }
  225. }