ObjFileMtlImporter.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 "ObjFileMtlImporter.h"
  35. #include "../include/aiTypes.h"
  36. #include "../include/aiAssert.h"
  37. #include "ObjTools.h"
  38. #include "ObjFileData.h"
  39. #include "fast_atof.h"
  40. namespace Assimp
  41. {
  42. // -------------------------------------------------------------------
  43. // Constructor
  44. ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
  45. const std::string &strAbsPath,
  46. ObjFile::Model *pModel ) :
  47. m_DataIt( buffer.begin() ),
  48. m_DataItEnd( buffer.end() ),
  49. m_pModel( pModel ),
  50. m_uiLine( 0 )
  51. {
  52. ai_assert ( NULL != m_pModel );
  53. if ( NULL == m_pModel->m_pDefaultMaterial )
  54. {
  55. m_pModel->m_pDefaultMaterial = new ObjFile::Material();
  56. m_pModel->m_pDefaultMaterial->MaterialName.Set( "default" );
  57. }
  58. load();
  59. }
  60. // -------------------------------------------------------------------
  61. // Destructor
  62. ObjFileMtlImporter::~ObjFileMtlImporter()
  63. {
  64. // empty
  65. }
  66. // -------------------------------------------------------------------
  67. // Private copy constructor
  68. ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter &rOther)
  69. {
  70. // empty
  71. }
  72. // -------------------------------------------------------------------
  73. // Private copy constructor
  74. ObjFileMtlImporter &ObjFileMtlImporter::operator = (
  75. const ObjFileMtlImporter &rOther)
  76. {
  77. return *this;
  78. }
  79. // -------------------------------------------------------------------
  80. // Loads the material description
  81. void ObjFileMtlImporter::load()
  82. {
  83. if ( m_DataIt == m_DataItEnd )
  84. return;
  85. while ( m_DataIt != m_DataItEnd )
  86. {
  87. switch (*m_DataIt)
  88. {
  89. case 'K':
  90. {
  91. ++m_DataIt;
  92. if (*m_DataIt == 'a') // Ambient color
  93. {
  94. getColorRGBA( &m_pModel->m_pCurrentMaterial->ambient );
  95. }
  96. else if (*m_DataIt == 'd') // Diffuse color
  97. {
  98. getColorRGBA( &m_pModel->m_pCurrentMaterial->diffuse );
  99. }
  100. else if (*m_DataIt == 's')
  101. {
  102. getColorRGBA( &m_pModel->m_pCurrentMaterial->specular );
  103. }
  104. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  105. }
  106. break;
  107. case 'd': // Alpha value
  108. {
  109. getFloatValue( m_pModel->m_pCurrentMaterial->alpha );
  110. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  111. }
  112. break;
  113. case 'N': // Shineness
  114. {
  115. getIlluminationModel( m_pModel->m_pCurrentMaterial->illumination_model );
  116. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  117. }
  118. break;
  119. case 'm': // Texture
  120. {
  121. getTexture();
  122. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  123. }
  124. break;
  125. case 'n': // New material name
  126. {
  127. createMaterial();
  128. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  129. }
  130. break;
  131. case 'i': // Illumination model
  132. {
  133. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  134. }
  135. break;
  136. default:
  137. {
  138. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  139. }
  140. break;
  141. }
  142. }
  143. }
  144. // -------------------------------------------------------------------
  145. // Loads a color definition
  146. void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
  147. {
  148. ai_assert( NULL != pColor );
  149. float r, g, b;
  150. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  151. r = (float) fast_atof(m_buffer);
  152. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  153. g = (float) fast_atof(m_buffer);
  154. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  155. b = (float) fast_atof(m_buffer);
  156. pColor->r = r;
  157. pColor->g = g;
  158. pColor->b = b;
  159. }
  160. // -------------------------------------------------------------------
  161. // Loads the kind of illumination model.
  162. void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
  163. {
  164. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  165. illum_model = atoi(m_buffer);
  166. }
  167. // -------------------------------------------------------------------
  168. // Loads a single float value.
  169. void ObjFileMtlImporter::getFloatValue( float &value )
  170. {
  171. m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
  172. value = (float) fast_atof(m_buffer);
  173. }
  174. // -------------------------------------------------------------------
  175. // Creates a material from loaded data.
  176. void ObjFileMtlImporter::createMaterial()
  177. {
  178. std::string strName;
  179. m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strName );
  180. if ( m_DataItEnd == m_DataIt )
  181. return;
  182. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strName );
  183. if ( m_pModel->m_MaterialMap.end() == it)
  184. {
  185. // New Material created
  186. m_pModel->m_pCurrentMaterial = new ObjFile::Material();
  187. m_pModel->m_pCurrentMaterial->MaterialName.Set( strName );
  188. m_pModel->m_MaterialLib.push_back( strName );
  189. m_pModel->m_MaterialMap[ strName ] = m_pModel->m_pCurrentMaterial;
  190. }
  191. else
  192. {
  193. // Use older material
  194. m_pModel->m_pCurrentMaterial = (*it).second;
  195. }
  196. }
  197. // -------------------------------------------------------------------
  198. // Gets a texture name from data.
  199. void ObjFileMtlImporter::getTexture()
  200. {
  201. std::string strTexture;
  202. m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strTexture );
  203. if ( m_DataItEnd == m_DataIt )
  204. return;
  205. m_pModel->m_pCurrentMaterial->texture.Set( strTexture );
  206. }
  207. // -------------------------------------------------------------------
  208. } // Namespace Assimp