You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

418 lines
14 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp 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 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. #include "AssimpPCH.h"
  35. #ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
  36. #include "ObjFileMtlImporter.h"
  37. #include "ObjTools.h"
  38. #include "ObjFileData.h"
  39. #include "fast_atof.h"
  40. namespace Assimp {
  41. // Material specific token
  42. static const std::string DiffuseTexture = "map_kd";
  43. static const std::string AmbientTexture = "map_ka";
  44. static const std::string SpecularTexture = "map_ks";
  45. static const std::string OpacityTexture = "map_d";
  46. static const std::string BumpTexture1 = "map_bump";
  47. static const std::string BumpTexture2 = "map_Bump";
  48. static const std::string BumpTexture3 = "bump";
  49. static const std::string NormalTexture = "map_Kn";
  50. static const std::string DisplacementTexture = "disp";
  51. static const std::string SpecularityTexture = "map_ns";
  52. // texture option specific token
  53. static const std::string BlendUOption = "-blendu";
  54. static const std::string BlendVOption = "-blendv";
  55. static const std::string BoostOption = "-boost";
  56. static const std::string ModifyMapOption = "-mm";
  57. static const std::string OffsetOption = "-o";
  58. static const std::string ScaleOption = "-s";
  59. static const std::string TurbulenceOption = "-t";
  60. static const std::string ResolutionOption = "-texres";
  61. static const std::string ClampOption = "-clamp";
  62. static const std::string BumpOption = "-bm";
  63. static const std::string ChannelOption = "-imfchan";
  64. static const std::string TypeOption = "-type";
  65. // -------------------------------------------------------------------
  66. // Constructor
  67. ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
  68. const std::string & /*strAbsPath*/,
  69. ObjFile::Model *pModel ) :
  70. m_DataIt( buffer.begin() ),
  71. m_DataItEnd( buffer.end() ),
  72. m_pModel( pModel ),
  73. m_uiLine( 0 )
  74. {
  75. ai_assert( NULL != m_pModel );
  76. if ( NULL == m_pModel->m_pDefaultMaterial )
  77. {
  78. m_pModel->m_pDefaultMaterial = new ObjFile::Material;
  79. m_pModel->m_pDefaultMaterial->MaterialName.Set( "default" );
  80. }
  81. load();
  82. }
  83. // -------------------------------------------------------------------
  84. // Destructor
  85. ObjFileMtlImporter::~ObjFileMtlImporter()
  86. {
  87. // empty
  88. }
  89. // -------------------------------------------------------------------
  90. // Private copy constructor
  91. ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter & /* rOther */ )
  92. {
  93. // empty
  94. }
  95. // -------------------------------------------------------------------
  96. // Private copy constructor
  97. ObjFileMtlImporter &ObjFileMtlImporter::operator = ( const ObjFileMtlImporter & /*rOther */ )
  98. {
  99. return *this;
  100. }
  101. // -------------------------------------------------------------------
  102. // Loads the material description
  103. void ObjFileMtlImporter::load()
  104. {
  105. if ( m_DataIt == m_DataItEnd )
  106. return;
  107. while ( m_DataIt != m_DataItEnd )
  108. {
  109. switch (*m_DataIt)
  110. {
  111. case 'K':
  112. {
  113. ++m_DataIt;
  114. if (*m_DataIt == 'a') // Ambient color
  115. {
  116. ++m_DataIt;
  117. getColorRGBA( &m_pModel->m_pCurrentMaterial->ambient );
  118. }
  119. else if (*m_DataIt == 'd') // Diffuse color
  120. {
  121. ++m_DataIt;
  122. getColorRGBA( &m_pModel->m_pCurrentMaterial->diffuse );
  123. }
  124. else if (*m_DataIt == 's')
  125. {
  126. ++m_DataIt;
  127. getColorRGBA( &m_pModel->m_pCurrentMaterial->specular );
  128. }
  129. else if (*m_DataIt == 'e')
  130. {
  131. ++m_DataIt;
  132. getColorRGBA( &m_pModel->m_pCurrentMaterial->emissive );
  133. }
  134. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  135. }
  136. break;
  137. case 'd': // Alpha value
  138. {
  139. ++m_DataIt;
  140. getFloatValue( m_pModel->m_pCurrentMaterial->alpha );
  141. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  142. }
  143. break;
  144. case 'N': // Shineness
  145. {
  146. ++m_DataIt;
  147. switch(*m_DataIt)
  148. {
  149. case 's':
  150. ++m_DataIt;
  151. getFloatValue(m_pModel->m_pCurrentMaterial->shineness);
  152. break;
  153. case 'i': //Index Of refraction
  154. ++m_DataIt;
  155. getFloatValue(m_pModel->m_pCurrentMaterial->ior);
  156. break;
  157. }
  158. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  159. break;
  160. }
  161. break;
  162. case 'm': // Texture
  163. case 'b': // quick'n'dirty - for 'bump' sections
  164. {
  165. getTexture();
  166. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  167. }
  168. break;
  169. case 'n': // New material name
  170. {
  171. createMaterial();
  172. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  173. }
  174. break;
  175. case 'i': // Illumination model
  176. {
  177. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  178. getIlluminationModel( m_pModel->m_pCurrentMaterial->illumination_model );
  179. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  180. }
  181. break;
  182. default:
  183. {
  184. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  185. }
  186. break;
  187. }
  188. }
  189. }
  190. // -------------------------------------------------------------------
  191. // Loads a color definition
  192. void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
  193. {
  194. ai_assert( NULL != pColor );
  195. float r, g, b;
  196. m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, r );
  197. pColor->r = r;
  198. m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, g );
  199. pColor->g = g;
  200. m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, b );
  201. pColor->b = b;
  202. }
  203. // -------------------------------------------------------------------
  204. // Loads the kind of illumination model.
  205. void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
  206. {
  207. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  208. illum_model = atoi(m_buffer);
  209. }
  210. // -------------------------------------------------------------------
  211. // Loads a single float value.
  212. void ObjFileMtlImporter::getFloatValue( float &value )
  213. {
  214. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  215. value = (float) fast_atof(m_buffer);
  216. }
  217. // -------------------------------------------------------------------
  218. // Creates a material from loaded data.
  219. void ObjFileMtlImporter::createMaterial()
  220. {
  221. std::string line( "" );
  222. while ( !isNewLine( *m_DataIt ) ) {
  223. line += *m_DataIt;
  224. ++m_DataIt;
  225. }
  226. std::vector<std::string> token;
  227. const unsigned int numToken = tokenize<std::string>( line, token, " " );
  228. std::string name( "" );
  229. if ( numToken == 1 ) {
  230. name = AI_DEFAULT_MATERIAL_NAME;
  231. } else {
  232. name = token[ 1 ];
  233. }
  234. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( name );
  235. if ( m_pModel->m_MaterialMap.end() == it) {
  236. // New Material created
  237. m_pModel->m_pCurrentMaterial = new ObjFile::Material();
  238. m_pModel->m_pCurrentMaterial->MaterialName.Set( name );
  239. m_pModel->m_MaterialLib.push_back( name );
  240. m_pModel->m_MaterialMap[ name ] = m_pModel->m_pCurrentMaterial;
  241. } else {
  242. // Use older material
  243. m_pModel->m_pCurrentMaterial = (*it).second;
  244. }
  245. }
  246. // -------------------------------------------------------------------
  247. // Gets a texture name from data.
  248. void ObjFileMtlImporter::getTexture() {
  249. aiString *out( NULL );
  250. int clampIndex = -1;
  251. const char *pPtr( &(*m_DataIt) );
  252. if ( !ASSIMP_strincmp( pPtr, DiffuseTexture.c_str(), DiffuseTexture.size() ) ) {
  253. // Diffuse texture
  254. out = & m_pModel->m_pCurrentMaterial->texture;
  255. clampIndex = ObjFile::Material::TextureDiffuseType;
  256. } else if ( !ASSIMP_strincmp( pPtr,AmbientTexture.c_str(),AmbientTexture.size() ) ) {
  257. // Ambient texture
  258. out = & m_pModel->m_pCurrentMaterial->textureAmbient;
  259. clampIndex = ObjFile::Material::TextureAmbientType;
  260. } else if (!ASSIMP_strincmp( pPtr, SpecularTexture.c_str(), SpecularTexture.size())) {
  261. // Specular texture
  262. out = & m_pModel->m_pCurrentMaterial->textureSpecular;
  263. clampIndex = ObjFile::Material::TextureSpecularType;
  264. } else if ( !ASSIMP_strincmp( pPtr, OpacityTexture.c_str(), OpacityTexture.size() ) ) {
  265. // Opacity texture
  266. out = & m_pModel->m_pCurrentMaterial->textureOpacity;
  267. clampIndex = ObjFile::Material::TextureOpacityType;
  268. } else if (!ASSIMP_strincmp( pPtr,"map_ka",6)) {
  269. // Ambient texture
  270. out = & m_pModel->m_pCurrentMaterial->textureAmbient;
  271. clampIndex = ObjFile::Material::TextureAmbientType;
  272. } else if (!ASSIMP_strincmp(&(*m_DataIt),"map_emissive",6)) {
  273. // Emissive texture
  274. out = & m_pModel->m_pCurrentMaterial->textureEmissive;
  275. clampIndex = ObjFile::Material::TextureEmissiveType;
  276. } else if ( !ASSIMP_strincmp( pPtr, BumpTexture1.c_str(), BumpTexture1.size() ) ||
  277. !ASSIMP_strincmp( pPtr, BumpTexture2.c_str(), BumpTexture2.size() ) ||
  278. !ASSIMP_strincmp( pPtr, BumpTexture3.c_str(), BumpTexture3.size() ) ) {
  279. // Bump texture
  280. out = & m_pModel->m_pCurrentMaterial->textureBump;
  281. clampIndex = ObjFile::Material::TextureBumpType;
  282. } else if (!ASSIMP_strincmp( pPtr,NormalTexture.c_str(), NormalTexture.size())) {
  283. // Normal map
  284. out = & m_pModel->m_pCurrentMaterial->textureNormal;
  285. clampIndex = ObjFile::Material::TextureNormalType;
  286. } else if (!ASSIMP_strincmp( pPtr, DisplacementTexture.c_str(), DisplacementTexture.size() ) ) {
  287. // Displacement texture
  288. out = &m_pModel->m_pCurrentMaterial->textureDisp;
  289. clampIndex = ObjFile::Material::TextureDispType;
  290. } else if (!ASSIMP_strincmp( pPtr, SpecularityTexture.c_str(),SpecularityTexture.size() ) ) {
  291. // Specularity scaling (glossiness)
  292. out = & m_pModel->m_pCurrentMaterial->textureSpecularity;
  293. clampIndex = ObjFile::Material::TextureSpecularityType;
  294. } else {
  295. DefaultLogger::get()->error("OBJ/MTL: Encountered unknown texture type");
  296. return;
  297. }
  298. bool clamp = false;
  299. getTextureOption(clamp);
  300. m_pModel->m_pCurrentMaterial->clamp[clampIndex] = clamp;
  301. std::string strTexture;
  302. m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strTexture );
  303. out->Set( strTexture );
  304. }
  305. /* /////////////////////////////////////////////////////////////////////////////
  306. * Texture Option
  307. * /////////////////////////////////////////////////////////////////////////////
  308. * According to http://en.wikipedia.org/wiki/Wavefront_.obj_file#Texture_options
  309. * Texture map statement can contains various texture option, for example:
  310. *
  311. * map_Ka -o 1 1 1 some.png
  312. * map_Kd -clamp on some.png
  313. *
  314. * So we need to parse and skip these options, and leave the last part which is
  315. * the url of image, otherwise we will get a wrong url like "-clamp on some.png".
  316. *
  317. * Because aiMaterial supports clamp option, so we also want to return it
  318. * /////////////////////////////////////////////////////////////////////////////
  319. */
  320. void ObjFileMtlImporter::getTextureOption(bool &clamp)
  321. {
  322. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  323. //If there is any more texture option
  324. while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-')
  325. {
  326. const char *pPtr( &(*m_DataIt) );
  327. //skip option key and value
  328. int skipToken = 1;
  329. if (!ASSIMP_strincmp(pPtr, ClampOption.c_str(), ClampOption.size()))
  330. {
  331. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  332. char value[3];
  333. CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
  334. if (!ASSIMP_strincmp(value, "on", 2))
  335. {
  336. clamp = true;
  337. }
  338. skipToken = 2;
  339. }
  340. else if ( !ASSIMP_strincmp(pPtr, BlendUOption.c_str(), BlendUOption.size())
  341. || !ASSIMP_strincmp(pPtr, BlendVOption.c_str(), BlendVOption.size())
  342. || !ASSIMP_strincmp(pPtr, BoostOption.c_str(), BoostOption.size())
  343. || !ASSIMP_strincmp(pPtr, ResolutionOption.c_str(), ResolutionOption.size())
  344. || !ASSIMP_strincmp(pPtr, BumpOption.c_str(), BumpOption.size())
  345. || !ASSIMP_strincmp(pPtr, ChannelOption.c_str(), ChannelOption.size())
  346. || !ASSIMP_strincmp(pPtr, TypeOption.c_str(), TypeOption.size()) )
  347. {
  348. skipToken = 2;
  349. }
  350. else if (!ASSIMP_strincmp(pPtr, ModifyMapOption.c_str(), ModifyMapOption.size()))
  351. {
  352. skipToken = 3;
  353. }
  354. else if ( !ASSIMP_strincmp(pPtr, OffsetOption.c_str(), OffsetOption.size())
  355. || !ASSIMP_strincmp(pPtr, ScaleOption.c_str(), ScaleOption.size())
  356. || !ASSIMP_strincmp(pPtr, TurbulenceOption.c_str(), TurbulenceOption.size())
  357. )
  358. {
  359. skipToken = 4;
  360. }
  361. for (int i = 0; i < skipToken; ++i)
  362. {
  363. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  364. }
  365. }
  366. }
  367. // -------------------------------------------------------------------
  368. } // Namespace Assimp
  369. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER