ObjFileMtlImporter.cpp 14 KB

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