ObjFileMtlImporter.cpp 18 KB

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