ObjFileMtlImporter.cpp 14 KB

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