ObjFileMtlImporter.cpp 16 KB

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