ObjFileMtlImporter.cpp 9.0 KB

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