ObjFileMtlImporter.cpp 18 KB

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