ObjFileMtlImporter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 BumpTexture1 = "map_bump";
  47. static const std::string BumpTexture2 = "map_Bump";
  48. static const std::string BumpTexture3 = "bump";
  49. static const std::string NormalTexture = "map_Kn";
  50. static const std::string DisplacementTexture = "disp";
  51. static const std::string SpecularityTexture = "map_ns";
  52. // -------------------------------------------------------------------
  53. // Constructor
  54. ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
  55. const std::string & /*strAbsPath*/,
  56. ObjFile::Model *pModel ) :
  57. m_DataIt( buffer.begin() ),
  58. m_DataItEnd( buffer.end() ),
  59. m_pModel( pModel ),
  60. m_uiLine( 0 )
  61. {
  62. ai_assert( NULL != m_pModel );
  63. if ( NULL == m_pModel->m_pDefaultMaterial )
  64. {
  65. m_pModel->m_pDefaultMaterial = new ObjFile::Material;
  66. m_pModel->m_pDefaultMaterial->MaterialName.Set( "default" );
  67. }
  68. load();
  69. }
  70. // -------------------------------------------------------------------
  71. // Destructor
  72. ObjFileMtlImporter::~ObjFileMtlImporter()
  73. {
  74. // empty
  75. }
  76. // -------------------------------------------------------------------
  77. // Private copy constructor
  78. ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter & /* rOther */ )
  79. {
  80. // empty
  81. }
  82. // -------------------------------------------------------------------
  83. // Private copy constructor
  84. ObjFileMtlImporter &ObjFileMtlImporter::operator = ( const ObjFileMtlImporter & /*rOther */ )
  85. {
  86. return *this;
  87. }
  88. // -------------------------------------------------------------------
  89. // Loads the material description
  90. void ObjFileMtlImporter::load()
  91. {
  92. if ( m_DataIt == m_DataItEnd )
  93. return;
  94. while ( m_DataIt != m_DataItEnd )
  95. {
  96. switch (*m_DataIt)
  97. {
  98. case 'K':
  99. {
  100. ++m_DataIt;
  101. if (*m_DataIt == 'a') // Ambient color
  102. {
  103. ++m_DataIt;
  104. getColorRGBA( &m_pModel->m_pCurrentMaterial->ambient );
  105. }
  106. else if (*m_DataIt == 'd') // Diffuse color
  107. {
  108. ++m_DataIt;
  109. getColorRGBA( &m_pModel->m_pCurrentMaterial->diffuse );
  110. }
  111. else if (*m_DataIt == 's')
  112. {
  113. ++m_DataIt;
  114. getColorRGBA( &m_pModel->m_pCurrentMaterial->specular );
  115. }
  116. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  117. }
  118. break;
  119. case 'd': // Alpha value
  120. {
  121. ++m_DataIt;
  122. getFloatValue( m_pModel->m_pCurrentMaterial->alpha );
  123. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  124. }
  125. break;
  126. case 'N': // Shineness
  127. {
  128. ++m_DataIt;
  129. switch(*m_DataIt)
  130. {
  131. case 's':
  132. ++m_DataIt;
  133. getFloatValue(m_pModel->m_pCurrentMaterial->shineness);
  134. break;
  135. case 'i': //Index Of refraction
  136. ++m_DataIt;
  137. getFloatValue(m_pModel->m_pCurrentMaterial->ior);
  138. break;
  139. }
  140. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  141. break;
  142. }
  143. break;
  144. case 'm': // Texture
  145. case 'b': // quick'n'dirty - for 'bump' sections
  146. {
  147. getTexture();
  148. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  149. }
  150. break;
  151. case 'n': // New material name
  152. {
  153. createMaterial();
  154. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  155. }
  156. break;
  157. case 'i': // Illumination model
  158. {
  159. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  160. getIlluminationModel( m_pModel->m_pCurrentMaterial->illumination_model );
  161. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  162. }
  163. break;
  164. default:
  165. {
  166. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  167. }
  168. break;
  169. }
  170. }
  171. }
  172. // -------------------------------------------------------------------
  173. // Loads a color definition
  174. void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
  175. {
  176. ai_assert( NULL != pColor );
  177. float r, g, b;
  178. m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, r );
  179. pColor->r = r;
  180. m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, g );
  181. pColor->g = g;
  182. m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, b );
  183. pColor->b = b;
  184. }
  185. // -------------------------------------------------------------------
  186. // Loads the kind of illumination model.
  187. void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
  188. {
  189. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  190. illum_model = atoi(m_buffer);
  191. }
  192. // -------------------------------------------------------------------
  193. // Loads a single float value.
  194. void ObjFileMtlImporter::getFloatValue( float &value )
  195. {
  196. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  197. value = (float) fast_atof(m_buffer);
  198. }
  199. // -------------------------------------------------------------------
  200. // Creates a material from loaded data.
  201. void ObjFileMtlImporter::createMaterial()
  202. {
  203. std::string line( "" );
  204. while ( !isNewLine( *m_DataIt ) ) {
  205. line += *m_DataIt;
  206. ++m_DataIt;
  207. }
  208. std::vector<std::string> token;
  209. const unsigned int numToken = tokenize<std::string>( line, token, " " );
  210. std::string name( "" );
  211. if ( numToken == 1 ) {
  212. name = AI_DEFAULT_MATERIAL_NAME;
  213. } else {
  214. name = token[ 1 ];
  215. }
  216. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( name );
  217. if ( m_pModel->m_MaterialMap.end() == it) {
  218. // New Material created
  219. m_pModel->m_pCurrentMaterial = new ObjFile::Material();
  220. m_pModel->m_pCurrentMaterial->MaterialName.Set( name );
  221. m_pModel->m_MaterialLib.push_back( name );
  222. m_pModel->m_MaterialMap[ name ] = m_pModel->m_pCurrentMaterial;
  223. } else {
  224. // Use older material
  225. m_pModel->m_pCurrentMaterial = (*it).second;
  226. }
  227. }
  228. // -------------------------------------------------------------------
  229. // Gets a texture name from data.
  230. void ObjFileMtlImporter::getTexture() {
  231. aiString *out( NULL );
  232. const char *pPtr( &(*m_DataIt) );
  233. if ( !ASSIMP_strincmp( pPtr, DiffuseTexture.c_str(), DiffuseTexture.size() ) ) {
  234. // Diffuse texture
  235. out = & m_pModel->m_pCurrentMaterial->texture;
  236. } else if ( !ASSIMP_strincmp( pPtr,AmbientTexture.c_str(),AmbientTexture.size() ) ) {
  237. // Ambient texture
  238. out = & m_pModel->m_pCurrentMaterial->textureAmbient;
  239. } else if (!ASSIMP_strincmp( pPtr, SpecularTexture.c_str(), SpecularTexture.size())) {
  240. // Specular texture
  241. out = & m_pModel->m_pCurrentMaterial->textureSpecular;
  242. } else if ( !ASSIMP_strincmp( pPtr, OpacityTexture.c_str(), OpacityTexture.size() ) ) {
  243. // Opacity texture
  244. out = & m_pModel->m_pCurrentMaterial->textureOpacity;
  245. } else if (!ASSIMP_strincmp( pPtr,"map_ka",6)) {
  246. // Ambient texture
  247. out = & m_pModel->m_pCurrentMaterial->textureAmbient;
  248. } else if ( !ASSIMP_strincmp( pPtr, BumpTexture1.c_str(), BumpTexture1.size() ) ||
  249. !ASSIMP_strincmp( pPtr, BumpTexture2.c_str(), BumpTexture2.size() ) ||
  250. !ASSIMP_strincmp( pPtr, BumpTexture3.c_str(), BumpTexture3.size() ) ) {
  251. // Bump texture
  252. out = & m_pModel->m_pCurrentMaterial->textureBump;
  253. } else if (!ASSIMP_strincmp( pPtr,NormalTexture.c_str(), NormalTexture.size())) {
  254. // Normal map
  255. out = & m_pModel->m_pCurrentMaterial->textureNormal;
  256. } else if (!ASSIMP_strincmp( pPtr, DisplacementTexture.c_str(), DisplacementTexture.size() ) ) {
  257. // Displacement texture
  258. out = &m_pModel->m_pCurrentMaterial->textureDisp;
  259. } else if (!ASSIMP_strincmp( pPtr, SpecularityTexture.c_str(),SpecularityTexture.size() ) ) {
  260. // Specularity scaling (glossiness)
  261. out = & m_pModel->m_pCurrentMaterial->textureSpecularity;
  262. } else {
  263. DefaultLogger::get()->error("OBJ/MTL: Encountered unknown texture type");
  264. return;
  265. }
  266. std::string strTexture;
  267. m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strTexture );
  268. out->Set( strTexture );
  269. }
  270. // -------------------------------------------------------------------
  271. } // Namespace Assimp
  272. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER