ObjFileMtlImporter.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, 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. #ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
  35. #include "ObjFileMtlImporter.h"
  36. #include "ObjFileData.h"
  37. #include "ObjTools.h"
  38. #include <assimp/DefaultIOSystem.h>
  39. #include <assimp/ParsingUtils.h>
  40. #include <assimp/fast_atof.h>
  41. #include <assimp/material.h>
  42. #include <stdlib.h>
  43. #include <assimp/DefaultLogger.hpp>
  44. namespace Assimp {
  45. // Material specific token (case insensitive compare)
  46. static constexpr char DiffuseTexture[] = "map_Kd";
  47. static constexpr char AmbientTexture[] = "map_Ka";
  48. static constexpr char SpecularTexture[] = "map_Ks";
  49. static constexpr char OpacityTexture[] = "map_d";
  50. static constexpr char EmissiveTexture1[] = "map_emissive";
  51. static constexpr char EmissiveTexture2[] = "map_Ke";
  52. static constexpr char BumpTexture1[] = "map_bump";
  53. static constexpr char BumpTexture2[] = "bump";
  54. static constexpr char NormalTextureV1[] = "map_Kn";
  55. static constexpr char NormalTextureV2[] = "norm";
  56. static constexpr char ReflectionTexture[] = "refl";
  57. static constexpr char DisplacementTexture1[] = "map_disp";
  58. static constexpr char DisplacementTexture2[] = "disp";
  59. static constexpr char SpecularityTexture[] = "map_ns";
  60. static constexpr char RoughnessTexture[] = "map_Pr";
  61. static constexpr char MetallicTexture[] = "map_Pm";
  62. static constexpr char SheenTexture[] = "map_Ps";
  63. static constexpr char RMATexture[] = "map_Ps";
  64. // texture option specific token
  65. static constexpr char BlendUOption[] = "-blendu";
  66. static constexpr char BlendVOption[] = "-blendv";
  67. static constexpr char BoostOption[] = "-boost";
  68. static constexpr char ModifyMapOption[] = "-mm";
  69. static constexpr char OffsetOption[] = "-o";
  70. static constexpr char ScaleOption[] = "-s";
  71. static constexpr char TurbulenceOption[] = "-t";
  72. static constexpr char ResolutionOption[] = "-texres";
  73. static constexpr char ClampOption[] = "-clamp";
  74. static constexpr char BumpOption[] = "-bm";
  75. static constexpr char ChannelOption[] = "-imfchan";
  76. static constexpr char TypeOption[] = "-type";
  77. // -------------------------------------------------------------------
  78. // Constructor
  79. ObjFileMtlImporter::ObjFileMtlImporter(std::vector<char> &buffer,
  80. const std::string &strAbsPath,
  81. ObjFile::Model *pModel) :
  82. m_strAbsPath(strAbsPath),
  83. m_DataIt(buffer.begin()),
  84. m_DataItEnd(buffer.end()),
  85. m_pModel(pModel),
  86. m_uiLine(0),
  87. m_buffer() {
  88. ai_assert(nullptr != m_pModel);
  89. m_buffer.resize(BUFFERSIZE);
  90. std::fill(m_buffer.begin(), m_buffer.end(), '\0');
  91. if (nullptr == m_pModel->mDefaultMaterial) {
  92. m_pModel->mDefaultMaterial = new ObjFile::Material;
  93. m_pModel->mDefaultMaterial->MaterialName.Set("default");
  94. }
  95. // Try with OS folder separator first
  96. char folderSeparator = DefaultIOSystem().getOsSeparator();
  97. std::size_t found = m_strAbsPath.find_last_of(folderSeparator);
  98. if (found == std::string::npos) {
  99. // Not found, try alternative folder separator
  100. folderSeparator = (folderSeparator == '/' ? '\\' : '/');
  101. found = m_strAbsPath.find_last_of(folderSeparator);
  102. }
  103. if (found != std::string::npos) {
  104. m_strAbsPath = m_strAbsPath.substr(0, found + 1);
  105. } else {
  106. m_strAbsPath = "";
  107. }
  108. load();
  109. }
  110. // -------------------------------------------------------------------
  111. // Loads the material description
  112. void ObjFileMtlImporter::load() {
  113. if (m_DataIt == m_DataItEnd)
  114. return;
  115. while (m_DataIt != m_DataItEnd) {
  116. switch (*m_DataIt) {
  117. case 'k':
  118. case 'K': {
  119. ++m_DataIt;
  120. if (*m_DataIt == 'a') // Ambient color
  121. {
  122. ++m_DataIt;
  123. if (m_pModel->mCurrentMaterial != nullptr)
  124. getColorRGBA(&m_pModel->mCurrentMaterial->ambient);
  125. } else if (*m_DataIt == 'd') {
  126. // Diffuse color
  127. ++m_DataIt;
  128. if (m_pModel->mCurrentMaterial != nullptr)
  129. getColorRGBA(&m_pModel->mCurrentMaterial->diffuse);
  130. } else if (*m_DataIt == 's') {
  131. ++m_DataIt;
  132. if (m_pModel->mCurrentMaterial != nullptr)
  133. getColorRGBA(&m_pModel->mCurrentMaterial->specular);
  134. } else if (*m_DataIt == 'e') {
  135. ++m_DataIt;
  136. if (m_pModel->mCurrentMaterial != nullptr)
  137. getColorRGBA(&m_pModel->mCurrentMaterial->emissive);
  138. }
  139. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  140. } break;
  141. case 'T': {
  142. ++m_DataIt;
  143. // Material transmission color
  144. if (*m_DataIt == 'f') {
  145. ++m_DataIt;
  146. if (m_pModel->mCurrentMaterial != nullptr)
  147. getColorRGBA(&m_pModel->mCurrentMaterial->transparent);
  148. } else if (*m_DataIt == 'r') {
  149. // Material transmission alpha value
  150. ++m_DataIt;
  151. ai_real d;
  152. getFloatValue(d);
  153. if (m_pModel->mCurrentMaterial != nullptr)
  154. m_pModel->mCurrentMaterial->alpha = static_cast<ai_real>(1.0) - d;
  155. }
  156. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  157. } break;
  158. case 'd': {
  159. if (*(m_DataIt + 1) == 'i' && *(m_DataIt + 2) == 's' && *(m_DataIt + 3) == 'p') {
  160. // A displacement map
  161. getTexture();
  162. } else {
  163. // Alpha value
  164. ++m_DataIt;
  165. if (m_pModel->mCurrentMaterial != nullptr)
  166. getFloatValue(m_pModel->mCurrentMaterial->alpha);
  167. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  168. }
  169. } break;
  170. case 'N':
  171. case 'n': {
  172. ++m_DataIt;
  173. switch (*m_DataIt) {
  174. case 's': // Specular exponent
  175. ++m_DataIt;
  176. if (m_pModel->mCurrentMaterial != nullptr)
  177. getFloatValue(m_pModel->mCurrentMaterial->shineness);
  178. break;
  179. case 'i': // Index Of refraction
  180. ++m_DataIt;
  181. if (m_pModel->mCurrentMaterial != nullptr)
  182. getFloatValue(m_pModel->mCurrentMaterial->ior);
  183. break;
  184. case 'e': // New material
  185. createMaterial();
  186. break;
  187. case 'o': // Norm texture
  188. --m_DataIt;
  189. getTexture();
  190. break;
  191. }
  192. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  193. } break;
  194. case 'P':
  195. {
  196. ++m_DataIt;
  197. switch(*m_DataIt)
  198. {
  199. case 'r':
  200. ++m_DataIt;
  201. if (m_pModel->mCurrentMaterial != nullptr)
  202. getFloatValue(m_pModel->mCurrentMaterial->roughness);
  203. break;
  204. case 'm':
  205. ++m_DataIt;
  206. if (m_pModel->mCurrentMaterial != nullptr)
  207. getFloatValue(m_pModel->mCurrentMaterial->metallic);
  208. break;
  209. case 's':
  210. ++m_DataIt;
  211. if (m_pModel->mCurrentMaterial != nullptr)
  212. getColorRGBA(m_pModel->mCurrentMaterial->sheen);
  213. break;
  214. case 'c':
  215. ++m_DataIt;
  216. if (*m_DataIt == 'r') {
  217. ++m_DataIt;
  218. if (m_pModel->mCurrentMaterial != nullptr)
  219. getFloatValue(m_pModel->mCurrentMaterial->clearcoat_roughness);
  220. } else {
  221. if (m_pModel->mCurrentMaterial != nullptr)
  222. getFloatValue(m_pModel->mCurrentMaterial->clearcoat_thickness);
  223. }
  224. break;
  225. }
  226. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  227. }
  228. break;
  229. case 'm': // Texture
  230. case 'b': // quick'n'dirty - for 'bump' sections
  231. case 'r': // quick'n'dirty - for 'refl' sections
  232. {
  233. getTexture();
  234. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  235. } break;
  236. case 'i': // Illumination model
  237. {
  238. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  239. if (m_pModel->mCurrentMaterial != nullptr)
  240. getIlluminationModel(m_pModel->mCurrentMaterial->illumination_model);
  241. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  242. } break;
  243. case 'a': // Anisotropy
  244. {
  245. ++m_DataIt;
  246. if (m_pModel->mCurrentMaterial != nullptr)
  247. getFloatValue(m_pModel->mCurrentMaterial->anisotropy);
  248. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  249. } break;
  250. default: {
  251. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  252. } break;
  253. }
  254. }
  255. }
  256. // -------------------------------------------------------------------
  257. // Loads a color definition
  258. void ObjFileMtlImporter::getColorRGBA(aiColor3D *pColor) {
  259. ai_assert(nullptr != pColor);
  260. ai_real r(0.0), g(0.0), b(0.0);
  261. m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, r);
  262. pColor->r = r;
  263. // we have to check if color is default 0 with only one token
  264. if (!IsLineEnd(*m_DataIt)) {
  265. m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, g);
  266. m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, b);
  267. }
  268. pColor->g = g;
  269. pColor->b = b;
  270. }
  271. // -------------------------------------------------------------------
  272. void ObjFileMtlImporter::getColorRGBA(Maybe<aiColor3D> &value) {
  273. aiColor3D v;
  274. getColorRGBA(&v);
  275. value = Maybe<aiColor3D>(v);
  276. }
  277. // -------------------------------------------------------------------
  278. // Loads the kind of illumination model.
  279. void ObjFileMtlImporter::getIlluminationModel(int &illum_model) {
  280. m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, &m_buffer[0], BUFFERSIZE);
  281. illum_model = atoi(&m_buffer[0]);
  282. }
  283. // -------------------------------------------------------------------
  284. // Loads a single float value.
  285. void ObjFileMtlImporter::getFloatValue(ai_real &value) {
  286. m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, &m_buffer[0], BUFFERSIZE);
  287. size_t len = std::strlen(&m_buffer[0]);
  288. if (0 == len) {
  289. value = 0.0f;
  290. return;
  291. }
  292. value = (ai_real)fast_atof(&m_buffer[0]);
  293. }
  294. // -------------------------------------------------------------------
  295. void ObjFileMtlImporter::getFloatValue(Maybe<ai_real> &value) {
  296. m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, &m_buffer[0], BUFFERSIZE);
  297. size_t len = std::strlen(&m_buffer[0]);
  298. if (len)
  299. value = Maybe<ai_real>(fast_atof(&m_buffer[0]));
  300. else
  301. value = Maybe<ai_real>();
  302. }
  303. // -------------------------------------------------------------------
  304. // Creates a material from loaded data.
  305. void ObjFileMtlImporter::createMaterial() {
  306. std::string line;
  307. while (!IsLineEnd(*m_DataIt)) {
  308. line += *m_DataIt;
  309. ++m_DataIt;
  310. }
  311. std::vector<std::string> token;
  312. const unsigned int numToken = tokenize<std::string>(line, token, " \t");
  313. std::string name;
  314. if (numToken == 1) {
  315. name = AI_DEFAULT_MATERIAL_NAME;
  316. } else {
  317. // skip newmtl and all following white spaces
  318. std::size_t first_ws_pos = line.find_first_of(" \t");
  319. std::size_t first_non_ws_pos = line.find_first_not_of(" \t", first_ws_pos);
  320. if (first_non_ws_pos != std::string::npos) {
  321. name = line.substr(first_non_ws_pos);
  322. }
  323. }
  324. name = ai_trim(name);
  325. std::map<std::string, ObjFile::Material *>::iterator it = m_pModel->mMaterialMap.find(name);
  326. if (m_pModel->mMaterialMap.end() == it) {
  327. // New Material created
  328. m_pModel->mCurrentMaterial = new ObjFile::Material();
  329. m_pModel->mCurrentMaterial->MaterialName.Set(name);
  330. m_pModel->mMaterialLib.push_back(name);
  331. m_pModel->mMaterialMap[name] = m_pModel->mCurrentMaterial;
  332. if (m_pModel->mCurrentMesh) {
  333. m_pModel->mCurrentMesh->m_uiMaterialIndex = static_cast<unsigned int>(m_pModel->mMaterialLib.size() - 1);
  334. }
  335. } else {
  336. // Use older material
  337. m_pModel->mCurrentMaterial = it->second;
  338. }
  339. }
  340. // -------------------------------------------------------------------
  341. // Gets a texture name from data.
  342. void ObjFileMtlImporter::getTexture() {
  343. aiString *out = nullptr;
  344. int clampIndex = -1;
  345. if (m_pModel->mCurrentMaterial == nullptr) {
  346. m_pModel->mCurrentMaterial = new ObjFile::Material();
  347. m_pModel->mCurrentMaterial->MaterialName.Set("Empty_Material");
  348. m_pModel->mMaterialMap["Empty_Material"] = m_pModel->mCurrentMaterial;
  349. }
  350. const char *pPtr(&(*m_DataIt));
  351. if (!ASSIMP_strincmp(pPtr, DiffuseTexture, static_cast<unsigned int>(strlen(DiffuseTexture)))) {
  352. // Diffuse texture
  353. out = &m_pModel->mCurrentMaterial->texture;
  354. clampIndex = ObjFile::Material::TextureDiffuseType;
  355. } else if (!ASSIMP_strincmp(pPtr, AmbientTexture, static_cast<unsigned int>(strlen(AmbientTexture)))) {
  356. // Ambient texture
  357. out = &m_pModel->mCurrentMaterial->textureAmbient;
  358. clampIndex = ObjFile::Material::TextureAmbientType;
  359. } else if (!ASSIMP_strincmp(pPtr, SpecularTexture, static_cast<unsigned int>(strlen(SpecularTexture)))) {
  360. // Specular texture
  361. out = &m_pModel->mCurrentMaterial->textureSpecular;
  362. clampIndex = ObjFile::Material::TextureSpecularType;
  363. } else if (!ASSIMP_strincmp(pPtr, DisplacementTexture1, static_cast<unsigned int>(strlen(DisplacementTexture1))) ||
  364. !ASSIMP_strincmp(pPtr, DisplacementTexture2, static_cast<unsigned int>(strlen(DisplacementTexture2)))) {
  365. // Displacement texture
  366. out = &m_pModel->mCurrentMaterial->textureDisp;
  367. clampIndex = ObjFile::Material::TextureDispType;
  368. } else if (!ASSIMP_strincmp(pPtr, OpacityTexture, static_cast<unsigned int>(strlen(OpacityTexture)))) {
  369. // Opacity texture
  370. out = &m_pModel->mCurrentMaterial->textureOpacity;
  371. clampIndex = ObjFile::Material::TextureOpacityType;
  372. } else if (!ASSIMP_strincmp(pPtr, EmissiveTexture1, static_cast<unsigned int>(strlen(EmissiveTexture1))) ||
  373. !ASSIMP_strincmp(pPtr, EmissiveTexture2, static_cast<unsigned int>(strlen(EmissiveTexture2)))) {
  374. // Emissive texture
  375. out = &m_pModel->mCurrentMaterial->textureEmissive;
  376. clampIndex = ObjFile::Material::TextureEmissiveType;
  377. } else if (!ASSIMP_strincmp(pPtr, BumpTexture1, static_cast<unsigned int>(strlen(BumpTexture1))) ||
  378. !ASSIMP_strincmp(pPtr, BumpTexture2, static_cast<unsigned int>(strlen(BumpTexture2)))) {
  379. // Bump texture
  380. out = &m_pModel->mCurrentMaterial->textureBump;
  381. clampIndex = ObjFile::Material::TextureBumpType;
  382. } else if (!ASSIMP_strincmp(pPtr, NormalTextureV1, static_cast<unsigned int>(strlen(NormalTextureV1))) || !ASSIMP_strincmp(pPtr, NormalTextureV2, static_cast<unsigned int>(strlen(NormalTextureV2)))) {
  383. // Normal map
  384. out = &m_pModel->mCurrentMaterial->textureNormal;
  385. clampIndex = ObjFile::Material::TextureNormalType;
  386. } else if (!ASSIMP_strincmp(pPtr, ReflectionTexture, static_cast<unsigned int>(strlen(ReflectionTexture)))) {
  387. // Reflection texture(s)
  388. //Do nothing here
  389. return;
  390. } else if (!ASSIMP_strincmp(pPtr, SpecularityTexture, static_cast<unsigned int>(strlen(SpecularityTexture)))) {
  391. // Specularity scaling (glossiness)
  392. out = &m_pModel->mCurrentMaterial->textureSpecularity;
  393. clampIndex = ObjFile::Material::TextureSpecularityType;
  394. } else if ( !ASSIMP_strincmp( pPtr, RoughnessTexture, static_cast<unsigned int>(strlen(RoughnessTexture)))) {
  395. // PBR Roughness texture
  396. out = & m_pModel->mCurrentMaterial->textureRoughness;
  397. clampIndex = ObjFile::Material::TextureRoughnessType;
  398. } else if ( !ASSIMP_strincmp( pPtr, MetallicTexture, static_cast<unsigned int>(strlen(MetallicTexture)))) {
  399. // PBR Metallic texture
  400. out = & m_pModel->mCurrentMaterial->textureMetallic;
  401. clampIndex = ObjFile::Material::TextureMetallicType;
  402. } else if (!ASSIMP_strincmp( pPtr, SheenTexture, static_cast<unsigned int>(strlen(SheenTexture)))) {
  403. // PBR Sheen (reflectance) texture
  404. out = & m_pModel->mCurrentMaterial->textureSheen;
  405. clampIndex = ObjFile::Material::TextureSheenType;
  406. } else if (!ASSIMP_strincmp( pPtr, RMATexture, static_cast<unsigned int>(strlen(RMATexture)))) {
  407. // PBR Rough/Metal/AO texture
  408. out = & m_pModel->mCurrentMaterial->textureRMA;
  409. clampIndex = ObjFile::Material::TextureRMAType;
  410. } else {
  411. ASSIMP_LOG_ERROR("OBJ/MTL: Encountered unknown texture type");
  412. return;
  413. }
  414. bool clamp = false;
  415. getTextureOption(clamp, clampIndex, out);
  416. m_pModel->mCurrentMaterial->clamp[clampIndex] = clamp;
  417. std::string texture;
  418. m_DataIt = getName<DataArrayIt>(m_DataIt, m_DataItEnd, texture);
  419. if (nullptr != out) {
  420. out->Set(m_strAbsPath + texture);
  421. }
  422. }
  423. /* /////////////////////////////////////////////////////////////////////////////
  424. * Texture Option
  425. * /////////////////////////////////////////////////////////////////////////////
  426. * According to http://en.wikipedia.org/wiki/Wavefront_.obj_file#Texture_options
  427. * Texture map statement can contains various texture option, for example:
  428. *
  429. * map_Ka -o 1 1 1 some.png
  430. * map_Kd -clamp on some.png
  431. *
  432. * So we need to parse and skip these options, and leave the last part which is
  433. * the url of image, otherwise we will get a wrong url like "-clamp on some.png".
  434. *
  435. * Because aiMaterial supports clamp option, so we also want to return it
  436. * /////////////////////////////////////////////////////////////////////////////
  437. */
  438. void ObjFileMtlImporter::getTextureOption(bool &clamp, int &clampIndex, aiString *&out) {
  439. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  440. // If there is any more texture option
  441. while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-') {
  442. const char *pPtr(&(*m_DataIt));
  443. //skip option key and value
  444. int skipToken = 1;
  445. if (!ASSIMP_strincmp(pPtr, ClampOption, static_cast<unsigned int>(strlen(ClampOption)))) {
  446. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  447. char value[3];
  448. CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
  449. if (!ASSIMP_strincmp(value, "on", 2)) {
  450. clamp = true;
  451. }
  452. skipToken = 2;
  453. } else if (!ASSIMP_strincmp(pPtr, TypeOption, static_cast<unsigned int>(strlen(TypeOption)))) {
  454. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  455. char value[12];
  456. CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
  457. if (!ASSIMP_strincmp(value, "cube_top", 8)) {
  458. clampIndex = ObjFile::Material::TextureReflectionCubeTopType;
  459. out = &m_pModel->mCurrentMaterial->textureReflection[0];
  460. } else if (!ASSIMP_strincmp(value, "cube_bottom", 11)) {
  461. clampIndex = ObjFile::Material::TextureReflectionCubeBottomType;
  462. out = &m_pModel->mCurrentMaterial->textureReflection[1];
  463. } else if (!ASSIMP_strincmp(value, "cube_front", 10)) {
  464. clampIndex = ObjFile::Material::TextureReflectionCubeFrontType;
  465. out = &m_pModel->mCurrentMaterial->textureReflection[2];
  466. } else if (!ASSIMP_strincmp(value, "cube_back", 9)) {
  467. clampIndex = ObjFile::Material::TextureReflectionCubeBackType;
  468. out = &m_pModel->mCurrentMaterial->textureReflection[3];
  469. } else if (!ASSIMP_strincmp(value, "cube_left", 9)) {
  470. clampIndex = ObjFile::Material::TextureReflectionCubeLeftType;
  471. out = &m_pModel->mCurrentMaterial->textureReflection[4];
  472. } else if (!ASSIMP_strincmp(value, "cube_right", 10)) {
  473. clampIndex = ObjFile::Material::TextureReflectionCubeRightType;
  474. out = &m_pModel->mCurrentMaterial->textureReflection[5];
  475. } else if (!ASSIMP_strincmp(value, "sphere", 6)) {
  476. clampIndex = ObjFile::Material::TextureReflectionSphereType;
  477. out = &m_pModel->mCurrentMaterial->textureReflection[0];
  478. }
  479. skipToken = 2;
  480. } else if (!ASSIMP_strincmp(pPtr, BumpOption, static_cast<unsigned int>(strlen(BumpOption)))) {
  481. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  482. getFloat(it, m_DataItEnd, m_pModel->mCurrentMaterial->bump_multiplier);
  483. skipToken = 2;
  484. } else if (!ASSIMP_strincmp(pPtr, BlendUOption, static_cast<unsigned int>(strlen(BlendUOption))) ||
  485. !ASSIMP_strincmp(pPtr, BlendVOption, static_cast<unsigned int>(strlen(BlendVOption))) ||
  486. !ASSIMP_strincmp(pPtr, BoostOption, static_cast<unsigned int>(strlen(BoostOption))) ||
  487. !ASSIMP_strincmp(pPtr, ResolutionOption, static_cast<unsigned int>(strlen(ResolutionOption))) ||
  488. !ASSIMP_strincmp(pPtr, ChannelOption, static_cast<unsigned int>(strlen(ChannelOption)))) {
  489. skipToken = 2;
  490. } else if (!ASSIMP_strincmp(pPtr, ModifyMapOption, static_cast<unsigned int>(strlen(ModifyMapOption)))) {
  491. skipToken = 3;
  492. } else if (!ASSIMP_strincmp(pPtr, OffsetOption, static_cast<unsigned int>(strlen(OffsetOption))) ||
  493. !ASSIMP_strincmp(pPtr, ScaleOption, static_cast<unsigned int>(strlen(ScaleOption))) ||
  494. !ASSIMP_strincmp(pPtr, TurbulenceOption, static_cast<unsigned int>(strlen(TurbulenceOption)))) {
  495. skipToken = 4;
  496. }
  497. for (int i = 0; i < skipToken; ++i) {
  498. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  499. }
  500. }
  501. }
  502. // -------------------------------------------------------------------
  503. } // Namespace Assimp
  504. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER