ObjFileMtlImporter.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "ObjFileMtlImporter.h"
  2. #include "../include/aiTypes.h"
  3. #include "../include/aiAssert.h"
  4. #include "ObjTools.h"
  5. #include "ObjFileData.h"
  6. #include "fast_atof.h"
  7. namespace Assimp
  8. {
  9. // -------------------------------------------------------------------
  10. ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
  11. const std::string &strAbsPath,
  12. ObjFile::Model *pModel ) :
  13. m_DataIt( buffer.begin() ),
  14. m_DataItEnd( buffer.end() ),
  15. m_uiLine( 0 ),
  16. m_pModel( NULL )
  17. {
  18. ai_assert ( NULL != m_pModel );
  19. if ( NULL == m_pModel->m_pDefaultMaterial )
  20. {
  21. m_pModel->m_pDefaultMaterial = new ObjFile::Material();
  22. //m_pModel->m_pDefaultMaterial->
  23. }
  24. load();
  25. }
  26. // -------------------------------------------------------------------
  27. ObjFileMtlImporter::~ObjFileMtlImporter()
  28. {
  29. // empty
  30. }
  31. // -------------------------------------------------------------------
  32. ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter &rOther)
  33. {
  34. // empty
  35. }
  36. // -------------------------------------------------------------------
  37. ObjFileMtlImporter &ObjFileMtlImporter::operator = (
  38. const ObjFileMtlImporter &rOther)
  39. {
  40. return *this;
  41. }
  42. // -------------------------------------------------------------------
  43. void ObjFileMtlImporter::load()
  44. {
  45. if ( m_DataIt == m_DataItEnd )
  46. return;
  47. while ( m_DataIt != m_DataItEnd )
  48. {
  49. switch (*m_DataIt)
  50. {
  51. case 'K':
  52. {
  53. ++m_DataIt;
  54. if (*m_DataIt == 'a') // Ambient color
  55. {
  56. getColorRGBA( &m_pModel->m_pCurrentMaterial->ambient );
  57. }
  58. else if (*m_DataIt == 'd') // Diffuse color
  59. {
  60. getColorRGBA( &m_pModel->m_pCurrentMaterial->diffuse );
  61. }
  62. else if (*m_DataIt == 's')
  63. {
  64. getColorRGBA( &m_pModel->m_pCurrentMaterial->specular );
  65. }
  66. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  67. }
  68. break;
  69. case 'd': // Alpha value
  70. {
  71. getFloatValue( m_pModel->m_pCurrentMaterial->alpha );
  72. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  73. }
  74. break;
  75. case 'N': // Shineness
  76. {
  77. getIlluminationModel( m_pModel->m_pCurrentMaterial->illumination_model );
  78. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  79. }
  80. break;
  81. case 'm': // Texture
  82. {
  83. getTexture();
  84. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  85. }
  86. break;
  87. case 'n': // New material name
  88. {
  89. createMaterial();
  90. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  91. }
  92. break;
  93. case 'i': // Illumination model
  94. {
  95. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  96. }
  97. break;
  98. default:
  99. {
  100. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  101. }
  102. break;
  103. }
  104. }
  105. }
  106. // -------------------------------------------------------------------
  107. void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
  108. {
  109. ai_assert( NULL != pColor );
  110. float r, g, b;
  111. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  112. r = (float) fast_atof(m_buffer);
  113. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  114. g = (float) fast_atof(m_buffer);
  115. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  116. b = (float) fast_atof(m_buffer);
  117. pColor->r = r;
  118. pColor->g = g;
  119. pColor->b = b;
  120. }
  121. // -------------------------------------------------------------------
  122. void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
  123. {
  124. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  125. illum_model = atoi(m_buffer);
  126. }
  127. // -------------------------------------------------------------------
  128. void ObjFileMtlImporter::getFloatValue( float &value )
  129. {
  130. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  131. value = (float) fast_atof(m_buffer);
  132. }
  133. // -------------------------------------------------------------------
  134. void ObjFileMtlImporter::createMaterial()
  135. {
  136. m_pModel->m_pCurrentMaterial = new ObjFile::Material();
  137. /*m_DataIt = getNextToken<DataArrayIt>( m_DataIt, m_DataItEnd );
  138. if (m_DataIt == m_DataItEnd)
  139. return;
  140. char *pStart = &(*m_DataIt);
  141. while ( !isSpace(*m_DataIt) && m_DataIt != m_DataItEnd )
  142. ++m_DataIt;
  143. // Get name
  144. std::string strName(pStart, &(*m_DataIt));
  145. if ( strName.empty() )
  146. return;*/
  147. std::string strName;
  148. m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strName );
  149. if ( m_DataItEnd == m_DataIt )
  150. return;
  151. m_pModel->m_pCurrentMaterial->MaterialName.Set( strName );
  152. m_pModel->m_MaterialLib.push_back( strName );
  153. m_pModel->m_MaterialMap[ strName ] = m_pModel->m_pCurrentMaterial;
  154. }
  155. // -------------------------------------------------------------------
  156. void ObjFileMtlImporter::getTexture()
  157. {
  158. std::string strTexture;
  159. m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strTexture );
  160. if ( m_DataItEnd == m_DataIt )
  161. return;
  162. m_pModel->m_pCurrentMaterial->texture.Set( strTexture );
  163. }
  164. // -------------------------------------------------------------------
  165. } // Namespace Assimp