ObjFileMtlImporter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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/ParsingUtils.h>
  39. #include <assimp/fast_atof.h>
  40. #include <assimp/material.h>
  41. #include <stdlib.h>
  42. #include <assimp/DefaultLogger.hpp>
  43. namespace Assimp {
  44. // Material specific token (case insensitive compare)
  45. static const std::string DiffuseTexture = "map_Kd";
  46. static const std::string AmbientTexture = "map_Ka";
  47. static const std::string SpecularTexture = "map_Ks";
  48. static const std::string OpacityTexture = "map_d";
  49. static const std::string EmissiveTexture1 = "map_emissive";
  50. static const std::string EmissiveTexture2 = "map_Ke";
  51. static const std::string BumpTexture1 = "map_bump";
  52. static const std::string BumpTexture2 = "bump";
  53. static const std::string NormalTextureV1 = "map_Kn";
  54. static const std::string NormalTextureV2 = "norm";
  55. static const std::string ReflectionTexture = "refl";
  56. static const std::string DisplacementTexture1 = "map_disp";
  57. static const std::string DisplacementTexture2 = "disp";
  58. static const std::string SpecularityTexture = "map_ns";
  59. // texture option specific token
  60. static const std::string BlendUOption = "-blendu";
  61. static const std::string BlendVOption = "-blendv";
  62. static const std::string BoostOption = "-boost";
  63. static const std::string ModifyMapOption = "-mm";
  64. static const std::string OffsetOption = "-o";
  65. static const std::string ScaleOption = "-s";
  66. static const std::string TurbulenceOption = "-t";
  67. static const std::string ResolutionOption = "-texres";
  68. static const std::string ClampOption = "-clamp";
  69. static const std::string BumpOption = "-bm";
  70. static const std::string ChannelOption = "-imfchan";
  71. static const std::string TypeOption = "-type";
  72. // -------------------------------------------------------------------
  73. // Constructor
  74. ObjFileMtlImporter::ObjFileMtlImporter(std::vector<char> &buffer,
  75. const std::string &,
  76. ObjFile::Model *pModel) :
  77. m_DataIt(buffer.begin()),
  78. m_DataItEnd(buffer.end()),
  79. m_pModel(pModel),
  80. m_uiLine(0),
  81. m_buffer() {
  82. ai_assert(nullptr != m_pModel);
  83. m_buffer.resize(BUFFERSIZE);
  84. std::fill(m_buffer.begin(), m_buffer.end(), '\0');
  85. if (nullptr == m_pModel->m_pDefaultMaterial) {
  86. m_pModel->m_pDefaultMaterial = new ObjFile::Material;
  87. m_pModel->m_pDefaultMaterial->MaterialName.Set("default");
  88. }
  89. load();
  90. }
  91. // -------------------------------------------------------------------
  92. // Destructor
  93. ObjFileMtlImporter::~ObjFileMtlImporter() {
  94. // empty
  95. }
  96. // -------------------------------------------------------------------
  97. // Loads the material description
  98. void ObjFileMtlImporter::load() {
  99. if (m_DataIt == m_DataItEnd)
  100. return;
  101. while (m_DataIt != m_DataItEnd) {
  102. switch (*m_DataIt) {
  103. case 'k':
  104. case 'K': {
  105. ++m_DataIt;
  106. if (*m_DataIt == 'a') // Ambient color
  107. {
  108. ++m_DataIt;
  109. getColorRGBA(&m_pModel->m_pCurrentMaterial->ambient);
  110. } else if (*m_DataIt == 'd') {
  111. // Diffuse color
  112. ++m_DataIt;
  113. getColorRGBA(&m_pModel->m_pCurrentMaterial->diffuse);
  114. } else if (*m_DataIt == 's') {
  115. ++m_DataIt;
  116. getColorRGBA(&m_pModel->m_pCurrentMaterial->specular);
  117. } else if (*m_DataIt == 'e') {
  118. ++m_DataIt;
  119. getColorRGBA(&m_pModel->m_pCurrentMaterial->emissive);
  120. }
  121. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  122. } break;
  123. case 'T': {
  124. ++m_DataIt;
  125. // Material transmission color
  126. if (*m_DataIt == 'f') {
  127. ++m_DataIt;
  128. getColorRGBA(&m_pModel->m_pCurrentMaterial->transparent);
  129. } else if (*m_DataIt == 'r') {
  130. // Material transmission alpha value
  131. ++m_DataIt;
  132. ai_real d;
  133. getFloatValue(d);
  134. m_pModel->m_pCurrentMaterial->alpha = static_cast<ai_real>(1.0) - d;
  135. }
  136. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  137. } break;
  138. case 'd': {
  139. if (*(m_DataIt + 1) == 'i' && *(m_DataIt + 2) == 's' && *(m_DataIt + 3) == 'p') {
  140. // A displacement map
  141. getTexture();
  142. } else {
  143. // Alpha value
  144. ++m_DataIt;
  145. getFloatValue(m_pModel->m_pCurrentMaterial->alpha);
  146. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  147. }
  148. } break;
  149. case 'N':
  150. case 'n': {
  151. ++m_DataIt;
  152. switch (*m_DataIt) {
  153. case 's': // Specular exponent
  154. ++m_DataIt;
  155. getFloatValue(m_pModel->m_pCurrentMaterial->shineness);
  156. break;
  157. case 'i': // Index Of refraction
  158. ++m_DataIt;
  159. getFloatValue(m_pModel->m_pCurrentMaterial->ior);
  160. break;
  161. case 'e': // New material
  162. createMaterial();
  163. break;
  164. }
  165. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  166. } break;
  167. case 'm': // Texture
  168. case 'b': // quick'n'dirty - for 'bump' sections
  169. case 'r': // quick'n'dirty - for 'refl' sections
  170. {
  171. getTexture();
  172. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  173. } break;
  174. case 'i': // Illumination model
  175. {
  176. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  177. getIlluminationModel(m_pModel->m_pCurrentMaterial->illumination_model);
  178. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  179. } break;
  180. default: {
  181. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  182. } break;
  183. }
  184. }
  185. }
  186. // -------------------------------------------------------------------
  187. // Loads a color definition
  188. void ObjFileMtlImporter::getColorRGBA(aiColor3D *pColor) {
  189. ai_assert(nullptr != pColor);
  190. ai_real r(0.0), g(0.0), b(0.0);
  191. m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, r);
  192. pColor->r = r;
  193. // we have to check if color is default 0 with only one token
  194. if (!IsLineEnd(*m_DataIt)) {
  195. m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, g);
  196. m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, b);
  197. }
  198. pColor->g = g;
  199. pColor->b = b;
  200. }
  201. // -------------------------------------------------------------------
  202. // Loads the kind of illumination model.
  203. void ObjFileMtlImporter::getIlluminationModel(int &illum_model) {
  204. m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, &m_buffer[0], BUFFERSIZE);
  205. illum_model = atoi(&m_buffer[0]);
  206. }
  207. // -------------------------------------------------------------------
  208. // Loads a single float value.
  209. void ObjFileMtlImporter::getFloatValue(ai_real &value) {
  210. m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, &m_buffer[0], BUFFERSIZE);
  211. size_t len = std::strlen(&m_buffer[0]);
  212. if (0 == len) {
  213. value = 0.0f;
  214. return;
  215. }
  216. value = (ai_real)fast_atof(&m_buffer[0]);
  217. }
  218. // -------------------------------------------------------------------
  219. // Creates a material from loaded data.
  220. void ObjFileMtlImporter::createMaterial() {
  221. std::string line;
  222. while (!IsLineEnd(*m_DataIt)) {
  223. line += *m_DataIt;
  224. ++m_DataIt;
  225. }
  226. std::vector<std::string> token;
  227. const unsigned int numToken = tokenize<std::string>(line, token, " \t");
  228. std::string name;
  229. if (numToken == 1) {
  230. name = AI_DEFAULT_MATERIAL_NAME;
  231. } else {
  232. // skip newmtl and all following white spaces
  233. std::size_t first_ws_pos = line.find_first_of(" \t");
  234. std::size_t first_non_ws_pos = line.find_first_not_of(" \t", first_ws_pos);
  235. if (first_non_ws_pos != std::string::npos) {
  236. name = line.substr(first_non_ws_pos);
  237. }
  238. }
  239. name = trim_whitespaces(name);
  240. std::map<std::string, ObjFile::Material *>::iterator it = m_pModel->m_MaterialMap.find(name);
  241. if (m_pModel->m_MaterialMap.end() == it) {
  242. // New Material created
  243. m_pModel->m_pCurrentMaterial = new ObjFile::Material();
  244. m_pModel->m_pCurrentMaterial->MaterialName.Set(name);
  245. m_pModel->m_MaterialLib.push_back(name);
  246. m_pModel->m_MaterialMap[name] = m_pModel->m_pCurrentMaterial;
  247. if (m_pModel->m_pCurrentMesh) {
  248. m_pModel->m_pCurrentMesh->m_uiMaterialIndex = static_cast<unsigned int>(m_pModel->m_MaterialLib.size() - 1);
  249. }
  250. } else {
  251. // Use older material
  252. m_pModel->m_pCurrentMaterial = (*it).second;
  253. }
  254. }
  255. // -------------------------------------------------------------------
  256. // Gets a texture name from data.
  257. void ObjFileMtlImporter::getTexture() {
  258. aiString *out(nullptr);
  259. int clampIndex = -1;
  260. const char *pPtr(&(*m_DataIt));
  261. if (!ASSIMP_strincmp(pPtr, DiffuseTexture.c_str(), static_cast<unsigned int>(DiffuseTexture.size()))) {
  262. // Diffuse texture
  263. out = &m_pModel->m_pCurrentMaterial->texture;
  264. clampIndex = ObjFile::Material::TextureDiffuseType;
  265. } else if (!ASSIMP_strincmp(pPtr, AmbientTexture.c_str(), static_cast<unsigned int>(AmbientTexture.size()))) {
  266. // Ambient texture
  267. out = &m_pModel->m_pCurrentMaterial->textureAmbient;
  268. clampIndex = ObjFile::Material::TextureAmbientType;
  269. } else if (!ASSIMP_strincmp(pPtr, SpecularTexture.c_str(), static_cast<unsigned int>(SpecularTexture.size()))) {
  270. // Specular texture
  271. out = &m_pModel->m_pCurrentMaterial->textureSpecular;
  272. clampIndex = ObjFile::Material::TextureSpecularType;
  273. } else if (!ASSIMP_strincmp(pPtr, DisplacementTexture1.c_str(), static_cast<unsigned int>(DisplacementTexture1.size())) ||
  274. !ASSIMP_strincmp(pPtr, DisplacementTexture2.c_str(), static_cast<unsigned int>(DisplacementTexture2.size()))) {
  275. // Displacement texture
  276. out = &m_pModel->m_pCurrentMaterial->textureDisp;
  277. clampIndex = ObjFile::Material::TextureDispType;
  278. } else if (!ASSIMP_strincmp(pPtr, OpacityTexture.c_str(), static_cast<unsigned int>(OpacityTexture.size()))) {
  279. // Opacity texture
  280. out = &m_pModel->m_pCurrentMaterial->textureOpacity;
  281. clampIndex = ObjFile::Material::TextureOpacityType;
  282. } else if (!ASSIMP_strincmp(pPtr, EmissiveTexture1.c_str(), static_cast<unsigned int>(EmissiveTexture1.size())) ||
  283. !ASSIMP_strincmp(pPtr, EmissiveTexture2.c_str(), static_cast<unsigned int>(EmissiveTexture2.size()))) {
  284. // Emissive texture
  285. out = &m_pModel->m_pCurrentMaterial->textureEmissive;
  286. clampIndex = ObjFile::Material::TextureEmissiveType;
  287. } else if (!ASSIMP_strincmp(pPtr, BumpTexture1.c_str(), static_cast<unsigned int>(BumpTexture1.size())) ||
  288. !ASSIMP_strincmp(pPtr, BumpTexture2.c_str(), static_cast<unsigned int>(BumpTexture2.size()))) {
  289. // Bump texture
  290. out = &m_pModel->m_pCurrentMaterial->textureBump;
  291. clampIndex = ObjFile::Material::TextureBumpType;
  292. } else if (!ASSIMP_strincmp(pPtr, NormalTextureV1.c_str(), static_cast<unsigned int>(NormalTextureV1.size())) || !ASSIMP_strincmp(pPtr, NormalTextureV2.c_str(), static_cast<unsigned int>(NormalTextureV2.size()))) {
  293. // Normal map
  294. out = &m_pModel->m_pCurrentMaterial->textureNormal;
  295. clampIndex = ObjFile::Material::TextureNormalType;
  296. } else if (!ASSIMP_strincmp(pPtr, ReflectionTexture.c_str(), static_cast<unsigned int>(ReflectionTexture.size()))) {
  297. // Reflection texture(s)
  298. //Do nothing here
  299. return;
  300. } else if (!ASSIMP_strincmp(pPtr, SpecularityTexture.c_str(), static_cast<unsigned int>(SpecularityTexture.size()))) {
  301. // Specularity scaling (glossiness)
  302. out = &m_pModel->m_pCurrentMaterial->textureSpecularity;
  303. clampIndex = ObjFile::Material::TextureSpecularityType;
  304. } else {
  305. ASSIMP_LOG_ERROR("OBJ/MTL: Encountered unknown texture type");
  306. return;
  307. }
  308. bool clamp = false;
  309. getTextureOption(clamp, clampIndex, out);
  310. m_pModel->m_pCurrentMaterial->clamp[clampIndex] = clamp;
  311. std::string texture;
  312. m_DataIt = getName<DataArrayIt>(m_DataIt, m_DataItEnd, texture);
  313. if (nullptr != out) {
  314. out->Set(texture);
  315. }
  316. }
  317. /* /////////////////////////////////////////////////////////////////////////////
  318. * Texture Option
  319. * /////////////////////////////////////////////////////////////////////////////
  320. * According to http://en.wikipedia.org/wiki/Wavefront_.obj_file#Texture_options
  321. * Texture map statement can contains various texture option, for example:
  322. *
  323. * map_Ka -o 1 1 1 some.png
  324. * map_Kd -clamp on some.png
  325. *
  326. * So we need to parse and skip these options, and leave the last part which is
  327. * the url of image, otherwise we will get a wrong url like "-clamp on some.png".
  328. *
  329. * Because aiMaterial supports clamp option, so we also want to return it
  330. * /////////////////////////////////////////////////////////////////////////////
  331. */
  332. void ObjFileMtlImporter::getTextureOption(bool &clamp, int &clampIndex, aiString *&out) {
  333. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  334. // If there is any more texture option
  335. while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-') {
  336. const char *pPtr(&(*m_DataIt));
  337. //skip option key and value
  338. int skipToken = 1;
  339. if (!ASSIMP_strincmp(pPtr, ClampOption.c_str(), static_cast<unsigned int>(ClampOption.size()))) {
  340. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  341. char value[3];
  342. CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
  343. if (!ASSIMP_strincmp(value, "on", 2)) {
  344. clamp = true;
  345. }
  346. skipToken = 2;
  347. } else if (!ASSIMP_strincmp(pPtr, TypeOption.c_str(), static_cast<unsigned int>(TypeOption.size()))) {
  348. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  349. char value[12];
  350. CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
  351. if (!ASSIMP_strincmp(value, "cube_top", 8)) {
  352. clampIndex = ObjFile::Material::TextureReflectionCubeTopType;
  353. out = &m_pModel->m_pCurrentMaterial->textureReflection[0];
  354. } else if (!ASSIMP_strincmp(value, "cube_bottom", 11)) {
  355. clampIndex = ObjFile::Material::TextureReflectionCubeBottomType;
  356. out = &m_pModel->m_pCurrentMaterial->textureReflection[1];
  357. } else if (!ASSIMP_strincmp(value, "cube_front", 10)) {
  358. clampIndex = ObjFile::Material::TextureReflectionCubeFrontType;
  359. out = &m_pModel->m_pCurrentMaterial->textureReflection[2];
  360. } else if (!ASSIMP_strincmp(value, "cube_back", 9)) {
  361. clampIndex = ObjFile::Material::TextureReflectionCubeBackType;
  362. out = &m_pModel->m_pCurrentMaterial->textureReflection[3];
  363. } else if (!ASSIMP_strincmp(value, "cube_left", 9)) {
  364. clampIndex = ObjFile::Material::TextureReflectionCubeLeftType;
  365. out = &m_pModel->m_pCurrentMaterial->textureReflection[4];
  366. } else if (!ASSIMP_strincmp(value, "cube_right", 10)) {
  367. clampIndex = ObjFile::Material::TextureReflectionCubeRightType;
  368. out = &m_pModel->m_pCurrentMaterial->textureReflection[5];
  369. } else if (!ASSIMP_strincmp(value, "sphere", 6)) {
  370. clampIndex = ObjFile::Material::TextureReflectionSphereType;
  371. out = &m_pModel->m_pCurrentMaterial->textureReflection[0];
  372. }
  373. skipToken = 2;
  374. } else if (!ASSIMP_strincmp(pPtr, BlendUOption.c_str(), static_cast<unsigned int>(BlendUOption.size())) || !ASSIMP_strincmp(pPtr, BlendVOption.c_str(), static_cast<unsigned int>(BlendVOption.size())) || !ASSIMP_strincmp(pPtr, BoostOption.c_str(), static_cast<unsigned int>(BoostOption.size())) || !ASSIMP_strincmp(pPtr, ResolutionOption.c_str(), static_cast<unsigned int>(ResolutionOption.size())) || !ASSIMP_strincmp(pPtr, BumpOption.c_str(), static_cast<unsigned int>(BumpOption.size())) || !ASSIMP_strincmp(pPtr, ChannelOption.c_str(), static_cast<unsigned int>(ChannelOption.size()))) {
  375. skipToken = 2;
  376. } else if (!ASSIMP_strincmp(pPtr, ModifyMapOption.c_str(), static_cast<unsigned int>(ModifyMapOption.size()))) {
  377. skipToken = 3;
  378. } else if (!ASSIMP_strincmp(pPtr, OffsetOption.c_str(), static_cast<unsigned int>(OffsetOption.size())) || !ASSIMP_strincmp(pPtr, ScaleOption.c_str(), static_cast<unsigned int>(ScaleOption.size())) || !ASSIMP_strincmp(pPtr, TurbulenceOption.c_str(), static_cast<unsigned int>(TurbulenceOption.size()))) {
  379. skipToken = 4;
  380. }
  381. for (int i = 0; i < skipToken; ++i) {
  382. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  383. }
  384. }
  385. }
  386. // -------------------------------------------------------------------
  387. } // Namespace Assimp
  388. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER