ObjFileMtlImporter.cpp 14 KB

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