ObjFileMtlImporter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2018, 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. #ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
  35. #include <stdlib.h>
  36. #include "ObjFileMtlImporter.h"
  37. #include "ObjTools.h"
  38. #include "ObjFileData.h"
  39. #include <assimp/fast_atof.h>
  40. #include <assimp/ParsingUtils.h>
  41. #include <assimp/material.h>
  42. #include <assimp/DefaultLogger.hpp>
  43. namespace Assimp {
  44. // Material specific token (case insensitive compare)
  45. static const std::string DiffuseTexture = "map_Kd";
  46. static const std::string AmbientTexture = "map_Ka";
  47. static const std::string SpecularTexture = "map_Ks";
  48. static const std::string OpacityTexture = "map_d";
  49. static const std::string EmissiveTexture1 = "map_emissive";
  50. static const std::string EmissiveTexture2 = "map_Ke";
  51. static const std::string BumpTexture1 = "map_bump";
  52. static const std::string BumpTexture2 = "bump";
  53. static const std::string NormalTexture = "map_Kn";
  54. static const std::string ReflectionTexture = "refl";
  55. static const std::string DisplacementTexture1 = "map_disp";
  56. static const std::string DisplacementTexture2 = "disp";
  57. static const std::string SpecularityTexture = "map_ns";
  58. // texture option specific token
  59. static const std::string BlendUOption = "-blendu";
  60. static const std::string BlendVOption = "-blendv";
  61. static const std::string BoostOption = "-boost";
  62. static const std::string ModifyMapOption = "-mm";
  63. static const std::string OffsetOption = "-o";
  64. static const std::string ScaleOption = "-s";
  65. static const std::string TurbulenceOption = "-t";
  66. static const std::string ResolutionOption = "-texres";
  67. static const std::string ClampOption = "-clamp";
  68. static const std::string BumpOption = "-bm";
  69. static const std::string ChannelOption = "-imfchan";
  70. static const std::string TypeOption = "-type";
  71. // -------------------------------------------------------------------
  72. // Constructor
  73. ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
  74. const std::string &,
  75. ObjFile::Model *pModel ) :
  76. m_DataIt( buffer.begin() ),
  77. m_DataItEnd( buffer.end() ),
  78. m_pModel( pModel ),
  79. m_uiLine( 0 )
  80. {
  81. ai_assert( NULL != m_pModel );
  82. if ( NULL == m_pModel->m_pDefaultMaterial )
  83. {
  84. m_pModel->m_pDefaultMaterial = new ObjFile::Material;
  85. m_pModel->m_pDefaultMaterial->MaterialName.Set( "default" );
  86. }
  87. load();
  88. }
  89. // -------------------------------------------------------------------
  90. // Destructor
  91. ObjFileMtlImporter::~ObjFileMtlImporter()
  92. {
  93. // empty
  94. }
  95. // -------------------------------------------------------------------
  96. // Private copy constructor
  97. ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter & )
  98. {
  99. // empty
  100. }
  101. // -------------------------------------------------------------------
  102. // Private copy constructor
  103. ObjFileMtlImporter &ObjFileMtlImporter::operator = ( const ObjFileMtlImporter & )
  104. {
  105. return *this;
  106. }
  107. // -------------------------------------------------------------------
  108. // Loads the material description
  109. void ObjFileMtlImporter::load()
  110. {
  111. if ( m_DataIt == m_DataItEnd )
  112. return;
  113. while ( m_DataIt != m_DataItEnd )
  114. {
  115. switch (*m_DataIt)
  116. {
  117. case 'k':
  118. case 'K':
  119. {
  120. ++m_DataIt;
  121. if (*m_DataIt == 'a') // Ambient color
  122. {
  123. ++m_DataIt;
  124. getColorRGBA( &m_pModel->m_pCurrentMaterial->ambient );
  125. }
  126. else if (*m_DataIt == 'd') // Diffuse color
  127. {
  128. ++m_DataIt;
  129. getColorRGBA( &m_pModel->m_pCurrentMaterial->diffuse );
  130. }
  131. else if (*m_DataIt == 's')
  132. {
  133. ++m_DataIt;
  134. getColorRGBA( &m_pModel->m_pCurrentMaterial->specular );
  135. }
  136. else if (*m_DataIt == 'e')
  137. {
  138. ++m_DataIt;
  139. getColorRGBA( &m_pModel->m_pCurrentMaterial->emissive );
  140. }
  141. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  142. }
  143. break;
  144. case 'T':
  145. {
  146. ++m_DataIt;
  147. if (*m_DataIt == 'f') // Material transmission
  148. {
  149. ++m_DataIt;
  150. getColorRGBA( &m_pModel->m_pCurrentMaterial->transparent);
  151. }
  152. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  153. }
  154. break;
  155. case 'd':
  156. {
  157. if( *(m_DataIt+1) == 'i' && *( m_DataIt + 2 ) == 's' && *( m_DataIt + 3 ) == 'p' ) {
  158. // A displacement map
  159. getTexture();
  160. } else {
  161. // Alpha value
  162. ++m_DataIt;
  163. getFloatValue( m_pModel->m_pCurrentMaterial->alpha );
  164. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  165. }
  166. }
  167. break;
  168. case 'N':
  169. case 'n':
  170. {
  171. ++m_DataIt;
  172. switch(*m_DataIt)
  173. {
  174. case 's': // Specular exponent
  175. ++m_DataIt;
  176. getFloatValue(m_pModel->m_pCurrentMaterial->shineness);
  177. break;
  178. case 'i': // Index Of refraction
  179. ++m_DataIt;
  180. getFloatValue(m_pModel->m_pCurrentMaterial->ior);
  181. break;
  182. case 'e': // New material
  183. createMaterial();
  184. break;
  185. }
  186. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  187. }
  188. break;
  189. case 'm': // Texture
  190. case 'b': // quick'n'dirty - for 'bump' sections
  191. case 'r': // quick'n'dirty - for 'refl' sections
  192. {
  193. getTexture();
  194. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  195. }
  196. break;
  197. case 'i': // Illumination model
  198. {
  199. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  200. getIlluminationModel( m_pModel->m_pCurrentMaterial->illumination_model );
  201. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  202. }
  203. break;
  204. default:
  205. {
  206. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  207. }
  208. break;
  209. }
  210. }
  211. }
  212. // -------------------------------------------------------------------
  213. // Loads a color definition
  214. void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
  215. {
  216. ai_assert( NULL != pColor );
  217. ai_real r( 0.0 ), g( 0.0 ), b( 0.0 );
  218. m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, r );
  219. pColor->r = r;
  220. // we have to check if color is default 0 with only one token
  221. if( !IsLineEnd( *m_DataIt ) ) {
  222. m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, g );
  223. m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, b );
  224. }
  225. pColor->g = g;
  226. pColor->b = b;
  227. }
  228. // -------------------------------------------------------------------
  229. // Loads the kind of illumination model.
  230. void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
  231. {
  232. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  233. illum_model = atoi(m_buffer);
  234. }
  235. // -------------------------------------------------------------------
  236. // Loads a single float value.
  237. void ObjFileMtlImporter::getFloatValue( ai_real &value )
  238. {
  239. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  240. value = (ai_real) fast_atof(m_buffer);
  241. }
  242. // -------------------------------------------------------------------
  243. // Creates a material from loaded data.
  244. void ObjFileMtlImporter::createMaterial()
  245. {
  246. std::string line( "" );
  247. while( !IsLineEnd( *m_DataIt ) ) {
  248. line += *m_DataIt;
  249. ++m_DataIt;
  250. }
  251. std::vector<std::string> token;
  252. const unsigned int numToken = tokenize<std::string>( line, token, " \t" );
  253. std::string name( "" );
  254. if ( numToken == 1 ) {
  255. name = AI_DEFAULT_MATERIAL_NAME;
  256. } else {
  257. // skip newmtl and all following white spaces
  258. std::size_t first_ws_pos = line.find_first_of(" \t");
  259. std::size_t first_non_ws_pos = line.find_first_not_of(" \t", first_ws_pos);
  260. if (first_non_ws_pos != std::string::npos) {
  261. name = line.substr(first_non_ws_pos);
  262. }
  263. }
  264. name = trim_whitespaces(name);
  265. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( name );
  266. if ( m_pModel->m_MaterialMap.end() == it) {
  267. // New Material created
  268. m_pModel->m_pCurrentMaterial = new ObjFile::Material();
  269. m_pModel->m_pCurrentMaterial->MaterialName.Set( name );
  270. m_pModel->m_MaterialLib.push_back( name );
  271. m_pModel->m_MaterialMap[ name ] = m_pModel->m_pCurrentMaterial;
  272. if (m_pModel->m_pCurrentMesh) {
  273. m_pModel->m_pCurrentMesh->m_uiMaterialIndex = static_cast<unsigned int>(m_pModel->m_MaterialLib.size() - 1);
  274. }
  275. } else {
  276. // Use older material
  277. m_pModel->m_pCurrentMaterial = (*it).second;
  278. }
  279. }
  280. // -------------------------------------------------------------------
  281. // Gets a texture name from data.
  282. void ObjFileMtlImporter::getTexture() {
  283. aiString *out( NULL );
  284. int clampIndex = -1;
  285. const char *pPtr( &(*m_DataIt) );
  286. if ( !ASSIMP_strincmp( pPtr, DiffuseTexture.c_str(), static_cast<unsigned int>(DiffuseTexture.size()) ) ) {
  287. // Diffuse texture
  288. out = & m_pModel->m_pCurrentMaterial->texture;
  289. clampIndex = ObjFile::Material::TextureDiffuseType;
  290. } else if ( !ASSIMP_strincmp( pPtr,AmbientTexture.c_str(), static_cast<unsigned int>(AmbientTexture.size()) ) ) {
  291. // Ambient texture
  292. out = & m_pModel->m_pCurrentMaterial->textureAmbient;
  293. clampIndex = ObjFile::Material::TextureAmbientType;
  294. } else if ( !ASSIMP_strincmp( pPtr, SpecularTexture.c_str(), static_cast<unsigned int>(SpecularTexture.size()) ) ) {
  295. // Specular texture
  296. out = & m_pModel->m_pCurrentMaterial->textureSpecular;
  297. clampIndex = ObjFile::Material::TextureSpecularType;
  298. } else if ( !ASSIMP_strincmp( pPtr, OpacityTexture.c_str(), static_cast<unsigned int>(OpacityTexture.size()) ) ) {
  299. // Opacity texture
  300. out = & m_pModel->m_pCurrentMaterial->textureOpacity;
  301. clampIndex = ObjFile::Material::TextureOpacityType;
  302. } else if ( !ASSIMP_strincmp( pPtr, EmissiveTexture1.c_str(), static_cast<unsigned int>(EmissiveTexture1.size()) ) ||
  303. !ASSIMP_strincmp( pPtr, EmissiveTexture2.c_str(), static_cast<unsigned int>(EmissiveTexture2.size()) ) ) {
  304. // Emissive texture
  305. out = & m_pModel->m_pCurrentMaterial->textureEmissive;
  306. clampIndex = ObjFile::Material::TextureEmissiveType;
  307. } else if ( !ASSIMP_strincmp( pPtr, BumpTexture1.c_str(), static_cast<unsigned int>(BumpTexture1.size()) ) ||
  308. !ASSIMP_strincmp( pPtr, BumpTexture2.c_str(), static_cast<unsigned int>(BumpTexture2.size()) ) ) {
  309. // Bump texture
  310. out = & m_pModel->m_pCurrentMaterial->textureBump;
  311. clampIndex = ObjFile::Material::TextureBumpType;
  312. } else if ( !ASSIMP_strincmp( pPtr,NormalTexture.c_str(), static_cast<unsigned int>(NormalTexture.size()) ) ) {
  313. // Normal map
  314. out = & m_pModel->m_pCurrentMaterial->textureNormal;
  315. clampIndex = ObjFile::Material::TextureNormalType;
  316. } else if( !ASSIMP_strincmp( pPtr, ReflectionTexture.c_str(), static_cast<unsigned int>(ReflectionTexture.size()) ) ) {
  317. // Reflection texture(s)
  318. //Do nothing here
  319. return;
  320. } else if ( !ASSIMP_strincmp( pPtr, DisplacementTexture1.c_str(), static_cast<unsigned int>(DisplacementTexture1.size()) ) ||
  321. !ASSIMP_strincmp( pPtr, DisplacementTexture2.c_str(), static_cast<unsigned int>(DisplacementTexture2.size()) ) ) {
  322. // Displacement texture
  323. out = &m_pModel->m_pCurrentMaterial->textureDisp;
  324. clampIndex = ObjFile::Material::TextureDispType;
  325. } else if ( !ASSIMP_strincmp( pPtr, SpecularityTexture.c_str(), static_cast<unsigned int>(SpecularityTexture.size()) ) ) {
  326. // Specularity scaling (glossiness)
  327. out = & m_pModel->m_pCurrentMaterial->textureSpecularity;
  328. clampIndex = ObjFile::Material::TextureSpecularityType;
  329. } else {
  330. ASSIMP_LOG_ERROR("OBJ/MTL: Encountered unknown texture type");
  331. return;
  332. }
  333. bool clamp = false;
  334. getTextureOption(clamp, clampIndex, out);
  335. m_pModel->m_pCurrentMaterial->clamp[clampIndex] = clamp;
  336. std::string texture;
  337. m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, texture );
  338. if ( NULL!=out ) {
  339. out->Set( texture );
  340. }
  341. }
  342. /* /////////////////////////////////////////////////////////////////////////////
  343. * Texture Option
  344. * /////////////////////////////////////////////////////////////////////////////
  345. * According to http://en.wikipedia.org/wiki/Wavefront_.obj_file#Texture_options
  346. * Texture map statement can contains various texture option, for example:
  347. *
  348. * map_Ka -o 1 1 1 some.png
  349. * map_Kd -clamp on some.png
  350. *
  351. * So we need to parse and skip these options, and leave the last part which is
  352. * the url of image, otherwise we will get a wrong url like "-clamp on some.png".
  353. *
  354. * Because aiMaterial supports clamp option, so we also want to return it
  355. * /////////////////////////////////////////////////////////////////////////////
  356. */
  357. void ObjFileMtlImporter::getTextureOption(bool &clamp, int &clampIndex, aiString *&out) {
  358. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  359. // If there is any more texture option
  360. while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-')
  361. {
  362. const char *pPtr( &(*m_DataIt) );
  363. //skip option key and value
  364. int skipToken = 1;
  365. if (!ASSIMP_strincmp(pPtr, ClampOption.c_str(), static_cast<unsigned int>(ClampOption.size())))
  366. {
  367. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  368. char value[3];
  369. CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
  370. if (!ASSIMP_strincmp(value, "on", 2))
  371. {
  372. clamp = true;
  373. }
  374. skipToken = 2;
  375. }
  376. else if( !ASSIMP_strincmp( pPtr, TypeOption.c_str(), static_cast<unsigned int>(TypeOption.size()) ) )
  377. {
  378. DataArrayIt it = getNextToken<DataArrayIt>( m_DataIt, m_DataItEnd );
  379. char value[ 12 ];
  380. CopyNextWord( it, m_DataItEnd, value, sizeof( value ) / sizeof( *value ) );
  381. if( !ASSIMP_strincmp( value, "cube_top", 8 ) )
  382. {
  383. clampIndex = ObjFile::Material::TextureReflectionCubeTopType;
  384. out = &m_pModel->m_pCurrentMaterial->textureReflection[0];
  385. }
  386. else if( !ASSIMP_strincmp( value, "cube_bottom", 11 ) )
  387. {
  388. clampIndex = ObjFile::Material::TextureReflectionCubeBottomType;
  389. out = &m_pModel->m_pCurrentMaterial->textureReflection[1];
  390. }
  391. else if( !ASSIMP_strincmp( value, "cube_front", 10 ) )
  392. {
  393. clampIndex = ObjFile::Material::TextureReflectionCubeFrontType;
  394. out = &m_pModel->m_pCurrentMaterial->textureReflection[2];
  395. }
  396. else if( !ASSIMP_strincmp( value, "cube_back", 9 ) )
  397. {
  398. clampIndex = ObjFile::Material::TextureReflectionCubeBackType;
  399. out = &m_pModel->m_pCurrentMaterial->textureReflection[3];
  400. }
  401. else if( !ASSIMP_strincmp( value, "cube_left", 9 ) )
  402. {
  403. clampIndex = ObjFile::Material::TextureReflectionCubeLeftType;
  404. out = &m_pModel->m_pCurrentMaterial->textureReflection[4];
  405. }
  406. else if( !ASSIMP_strincmp( value, "cube_right", 10 ) )
  407. {
  408. clampIndex = ObjFile::Material::TextureReflectionCubeRightType;
  409. out = &m_pModel->m_pCurrentMaterial->textureReflection[5];
  410. }
  411. else if( !ASSIMP_strincmp( value, "sphere", 6 ) )
  412. {
  413. clampIndex = ObjFile::Material::TextureReflectionSphereType;
  414. out = &m_pModel->m_pCurrentMaterial->textureReflection[0];
  415. }
  416. skipToken = 2;
  417. }
  418. else if (!ASSIMP_strincmp(pPtr, BlendUOption.c_str(), static_cast<unsigned int>(BlendUOption.size()))
  419. || !ASSIMP_strincmp(pPtr, BlendVOption.c_str(), static_cast<unsigned int>(BlendVOption.size()))
  420. || !ASSIMP_strincmp(pPtr, BoostOption.c_str(), static_cast<unsigned int>(BoostOption.size()))
  421. || !ASSIMP_strincmp(pPtr, ResolutionOption.c_str(), static_cast<unsigned int>(ResolutionOption.size()))
  422. || !ASSIMP_strincmp(pPtr, BumpOption.c_str(), static_cast<unsigned int>(BumpOption.size()))
  423. || !ASSIMP_strincmp(pPtr, ChannelOption.c_str(), static_cast<unsigned int>(ChannelOption.size())))
  424. {
  425. skipToken = 2;
  426. }
  427. else if (!ASSIMP_strincmp(pPtr, ModifyMapOption.c_str(), static_cast<unsigned int>(ModifyMapOption.size())))
  428. {
  429. skipToken = 3;
  430. }
  431. else if ( !ASSIMP_strincmp(pPtr, OffsetOption.c_str(), static_cast<unsigned int>(OffsetOption.size()))
  432. || !ASSIMP_strincmp(pPtr, ScaleOption.c_str(), static_cast<unsigned int>(ScaleOption.size()))
  433. || !ASSIMP_strincmp(pPtr, TurbulenceOption.c_str(), static_cast<unsigned int>(TurbulenceOption.size()))
  434. )
  435. {
  436. skipToken = 4;
  437. }
  438. for (int i = 0; i < skipToken; ++i)
  439. {
  440. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  441. }
  442. }
  443. }
  444. // -------------------------------------------------------------------
  445. } // Namespace Assimp
  446. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER