選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

593 行
17 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "AssimpPCH.h"
  34. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  35. #include "OgreImporter.h"
  36. #include "TinyFormatter.h"
  37. #include "fast_atof.h"
  38. #include <vector>
  39. #include <sstream>
  40. using namespace std;
  41. namespace Assimp
  42. {
  43. namespace Ogre
  44. {
  45. static const string partComment = "//";
  46. static const string partBlockStart = "{";
  47. static const string partBlockEnd = "}";
  48. void OgreImporter::ReadMaterials(const std::string &pFile, Assimp::IOSystem *pIOHandler, aiScene *pScene, Mesh *mesh)
  49. {
  50. std::vector<aiMaterial*> materials;
  51. // Create materials that can be found and parsed via the IOSystem.
  52. for (size_t i=0, len=mesh->NumSubMeshes(); i<len; ++i)
  53. {
  54. SubMesh *submesh = mesh->GetSubMesh(i);
  55. if (submesh && !submesh->materialRef.empty())
  56. {
  57. aiMaterial *material = ReadMaterial(pFile, pIOHandler, submesh->materialRef);
  58. if (material)
  59. {
  60. submesh->materialIndex = materials.size();
  61. materials.push_back(material);
  62. }
  63. }
  64. }
  65. AssignMaterials(pScene, materials);
  66. }
  67. void OgreImporter::ReadMaterials(const std::string &pFile, Assimp::IOSystem *pIOHandler, aiScene *pScene, MeshXml *mesh)
  68. {
  69. std::vector<aiMaterial*> materials;
  70. // Create materials that can be found and parsed via the IOSystem.
  71. for (size_t i=0, len=mesh->NumSubMeshes(); i<len; ++i)
  72. {
  73. SubMeshXml *submesh = mesh->GetSubMesh(i);
  74. if (submesh && !submesh->materialRef.empty())
  75. {
  76. aiMaterial *material = ReadMaterial(pFile, pIOHandler, submesh->materialRef);
  77. if (material)
  78. {
  79. submesh->materialIndex = materials.size();
  80. materials.push_back(material);
  81. }
  82. }
  83. }
  84. AssignMaterials(pScene, materials);
  85. }
  86. void OgreImporter::AssignMaterials(aiScene *pScene, std::vector<aiMaterial*> &materials)
  87. {
  88. pScene->mNumMaterials = materials.size();
  89. if (pScene->mNumMaterials > 0)
  90. {
  91. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  92. for(size_t i=0;i<pScene->mNumMaterials; ++i) {
  93. pScene->mMaterials[i] = materials[i];
  94. }
  95. }
  96. }
  97. aiMaterial* OgreImporter::ReadMaterial(const std::string &pFile, Assimp::IOSystem *pIOHandler, const std::string materialName)
  98. {
  99. if (materialName.empty()) {
  100. return 0;
  101. }
  102. // Full reference and examples of Ogre Material Script
  103. // can be found from http://www.ogre3d.org/docs/manual/manual_14.html
  104. /*and here is another one:
  105. import * from abstract_base_passes_depth.material
  106. import * from abstract_base.material
  107. import * from mat_shadow_caster.material
  108. import * from mat_character_singlepass.material
  109. material hero/hair/caster : mat_shadow_caster_skin_areject
  110. {
  111. set $diffuse_map "hero_hair_alpha_c.dds"
  112. }
  113. material hero/hair_alpha : mat_char_cns_singlepass_areject_4weights
  114. {
  115. set $diffuse_map "hero_hair_alpha_c.dds"
  116. set $specular_map "hero_hair_alpha_s.dds"
  117. set $normal_map "hero_hair_alpha_n.dds"
  118. set $light_map "black_lightmap.dds"
  119. set $shadow_caster_material "hero/hair/caster"
  120. }
  121. */
  122. stringstream ss;
  123. // Scope for scopre_ptr auto release
  124. {
  125. /* There are three .material options in priority order:
  126. 1) File with the material name (materialName)
  127. 2) File with the mesh files base name (pFile)
  128. 3) Optional user defined material library file (m_userDefinedMaterialLibFile) */
  129. std::vector<string> potentialFiles;
  130. potentialFiles.push_back(materialName + ".material");
  131. potentialFiles.push_back(pFile.substr(0, pFile.rfind(".mesh")) + ".material");
  132. if (!m_userDefinedMaterialLibFile.empty())
  133. potentialFiles.push_back(m_userDefinedMaterialLibFile);
  134. IOStream *materialFile = 0;
  135. for(size_t i=0; i<potentialFiles.size(); ++i)
  136. {
  137. materialFile = pIOHandler->Open(potentialFiles[i]);
  138. if (materialFile) {
  139. break;
  140. }
  141. DefaultLogger::get()->debug(Formatter::format() << "Source file for material '" << materialName << "' " << potentialFiles[i] << " does not exist");
  142. }
  143. if (!materialFile)
  144. {
  145. DefaultLogger::get()->error(Formatter::format() << "Failed to find source file for material '" << materialName << "'");
  146. return 0;
  147. }
  148. boost::scoped_ptr<IOStream> stream(materialFile);
  149. if (stream->FileSize() == 0)
  150. {
  151. DefaultLogger::get()->warn(Formatter::format() << "Source file for material '" << materialName << "' is empty (size is 0 bytes)");
  152. return 0;
  153. }
  154. // Read bytes
  155. vector<char> data(stream->FileSize());
  156. stream->Read(&data[0], stream->FileSize(), 1);
  157. // Convert to UTF-8 and terminate the string for ss
  158. BaseImporter::ConvertToUTF8(data);
  159. data.push_back('\0');
  160. ss << &data[0];
  161. }
  162. DefaultLogger::get()->debug("Reading material '" + materialName + "'");
  163. aiMaterial *material = new aiMaterial();
  164. m_textures.clear();
  165. aiString ts(materialName);
  166. material->AddProperty(&ts, AI_MATKEY_NAME);
  167. // The stringstream will push words from a line until newline.
  168. // It will also trim whitespace from line start and between words.
  169. string linePart;
  170. ss >> linePart;
  171. const string partMaterial = "material";
  172. const string partTechnique = "technique";
  173. while(!ss.eof())
  174. {
  175. // Skip commented lines
  176. if (linePart == partComment)
  177. {
  178. NextAfterNewLine(ss, linePart);
  179. continue;
  180. }
  181. if (linePart != partMaterial)
  182. {
  183. ss >> linePart;
  184. continue;
  185. }
  186. ss >> linePart;
  187. if (linePart != materialName)
  188. {
  189. //DefaultLogger::get()->debug(Formatter::format() << "Found material '" << linePart << "' that does not match at index " << ss.tellg());
  190. ss >> linePart;
  191. continue;
  192. }
  193. NextAfterNewLine(ss, linePart);
  194. if (linePart != partBlockStart)
  195. {
  196. DefaultLogger::get()->error(Formatter::format() << "Invalid material: block start missing near index " << ss.tellg());
  197. return material;
  198. }
  199. DefaultLogger::get()->debug("material '" + materialName + "'");
  200. while(linePart != partBlockEnd)
  201. {
  202. // Proceed to the first technique
  203. ss >> linePart;
  204. if (linePart == partTechnique)
  205. {
  206. string techniqueName = SkipLine(ss);
  207. ReadTechnique(Trim(techniqueName), ss, material);
  208. }
  209. // Read informations from a custom material
  210. /** @todo This "set $x y" does not seem to be a official Ogre material system feature.
  211. Materials can inherit other materials and override texture units by using the (unique)
  212. parent texture unit name in your cloned material.
  213. This is not yet supported and below code is probably some hack from the original
  214. author of this Ogre importer. Should be removed? */
  215. if (linePart=="set")
  216. {
  217. ss >> linePart;
  218. if (linePart=="$specular")//todo load this values:
  219. {
  220. }
  221. else if (linePart=="$diffuse")
  222. {
  223. }
  224. else if (linePart=="$ambient")
  225. {
  226. }
  227. else if (linePart=="$colormap")
  228. {
  229. ss >> linePart;
  230. aiString ts(linePart);
  231. material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  232. }
  233. else if (linePart=="$normalmap")
  234. {
  235. ss >> linePart;
  236. aiString ts(linePart);
  237. material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
  238. }
  239. else if (linePart=="$shininess_strength")
  240. {
  241. ss >> linePart;
  242. float Shininess = fast_atof(linePart.c_str());
  243. material->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS_STRENGTH);
  244. }
  245. else if (linePart=="$shininess_exponent")
  246. {
  247. ss >> linePart;
  248. float Shininess = fast_atof(linePart.c_str());
  249. material->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS);
  250. }
  251. //Properties from Venetica:
  252. else if (linePart=="$diffuse_map")
  253. {
  254. ss >> linePart;
  255. if (linePart[0] == '"')// "file" -> file
  256. linePart = linePart.substr(1, linePart.size()-2);
  257. aiString ts(linePart);
  258. material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  259. }
  260. else if (linePart=="$specular_map")
  261. {
  262. ss >> linePart;
  263. if (linePart[0] == '"')// "file" -> file
  264. linePart = linePart.substr(1, linePart.size()-2);
  265. aiString ts(linePart);
  266. material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_SHININESS, 0));
  267. }
  268. else if (linePart=="$normal_map")
  269. {
  270. ss >> linePart;
  271. if (linePart[0]=='"')// "file" -> file
  272. linePart = linePart.substr(1, linePart.size()-2);
  273. aiString ts(linePart);
  274. material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
  275. }
  276. else if (linePart=="$light_map")
  277. {
  278. ss >> linePart;
  279. if (linePart[0]=='"') {
  280. linePart = linePart.substr(1, linePart.size() - 2);
  281. }
  282. aiString ts(linePart);
  283. material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, 0));
  284. }
  285. }
  286. }
  287. ss >> linePart;
  288. }
  289. return material;
  290. }
  291. bool OgreImporter::ReadTechnique(const std::string &techniqueName, stringstream &ss, aiMaterial *material)
  292. {
  293. string linePart;
  294. ss >> linePart;
  295. if (linePart != partBlockStart)
  296. {
  297. DefaultLogger::get()->error(Formatter::format() << "Invalid material: Technique block start missing near index " << ss.tellg());
  298. return false;
  299. }
  300. DefaultLogger::get()->debug(" technique '" + techniqueName + "'");
  301. const string partPass = "pass";
  302. while(linePart != partBlockEnd)
  303. {
  304. ss >> linePart;
  305. // Skip commented lines
  306. if (linePart == partComment)
  307. {
  308. SkipLine(ss);
  309. continue;
  310. }
  311. /// @todo Techniques have other attributes than just passes.
  312. if (linePart == partPass)
  313. {
  314. string passName = SkipLine(ss);
  315. ReadPass(Trim(passName), ss, material);
  316. }
  317. }
  318. return true;
  319. }
  320. bool OgreImporter::ReadPass(const std::string &passName, stringstream &ss, aiMaterial *material)
  321. {
  322. string linePart;
  323. ss >> linePart;
  324. if (linePart != partBlockStart)
  325. {
  326. DefaultLogger::get()->error(Formatter::format() << "Invalid material: Pass block start missing near index " << ss.tellg());
  327. return false;
  328. }
  329. DefaultLogger::get()->debug(" pass '" + passName + "'");
  330. const string partAmbient = "ambient";
  331. const string partDiffuse = "diffuse";
  332. const string partSpecular = "specular";
  333. const string partEmissive = "emissive";
  334. const string partTextureUnit = "texture_unit";
  335. while(linePart != partBlockEnd)
  336. {
  337. ss >> linePart;
  338. // Skip commented lines
  339. if (linePart == partComment)
  340. {
  341. SkipLine(ss);
  342. continue;
  343. }
  344. // Colors
  345. /// @todo Support alpha via aiColor4D.
  346. if (linePart == partAmbient || linePart == partDiffuse || linePart == partSpecular || linePart == partEmissive)
  347. {
  348. float r, g, b;
  349. ss >> r >> g >> b;
  350. const aiColor3D color(r, g, b);
  351. DefaultLogger::get()->debug(Formatter::format() << " " << linePart << " " << r << " " << g << " " << b);
  352. if (linePart == partAmbient)
  353. {
  354. material->AddProperty(&color, 1, AI_MATKEY_COLOR_AMBIENT);
  355. }
  356. else if (linePart == partDiffuse)
  357. {
  358. material->AddProperty(&color, 1, AI_MATKEY_COLOR_DIFFUSE);
  359. }
  360. else if (linePart == partSpecular)
  361. {
  362. material->AddProperty(&color, 1, AI_MATKEY_COLOR_SPECULAR);
  363. }
  364. else if (linePart == partEmissive)
  365. {
  366. material->AddProperty(&color, 1, AI_MATKEY_COLOR_EMISSIVE);
  367. }
  368. }
  369. else if (linePart == partTextureUnit)
  370. {
  371. string textureUnitName = SkipLine(ss);
  372. ReadTextureUnit(Trim(textureUnitName), ss, material);
  373. }
  374. }
  375. return true;
  376. }
  377. bool OgreImporter::ReadTextureUnit(const std::string &textureUnitName, stringstream &ss, aiMaterial *material)
  378. {
  379. string linePart;
  380. ss >> linePart;
  381. if (linePart != partBlockStart)
  382. {
  383. DefaultLogger::get()->error(Formatter::format() << "Invalid material: Texture unit block start missing near index " << ss.tellg());
  384. return false;
  385. }
  386. DefaultLogger::get()->debug(" texture_unit '" + textureUnitName + "'");
  387. const string partTexture = "texture";
  388. const string partTextCoordSet = "tex_coord_set";
  389. const string partColorOp = "colour_op";
  390. aiTextureType textureType = aiTextureType_NONE;
  391. std::string textureRef;
  392. int uvCoord = 0;
  393. while(linePart != partBlockEnd)
  394. {
  395. ss >> linePart;
  396. // Skip commented lines
  397. if (linePart == partComment)
  398. {
  399. SkipLine(ss);
  400. continue;
  401. }
  402. if (linePart == partTexture)
  403. {
  404. ss >> linePart;
  405. textureRef = linePart;
  406. // User defined Assimp config property to detect texture type from filename.
  407. if (m_detectTextureTypeFromFilename)
  408. {
  409. size_t posSuffix = textureRef.find_last_of(".");
  410. size_t posUnderscore = textureRef.find_last_of("_");
  411. if (posSuffix != string::npos && posUnderscore != string::npos && posSuffix > posUnderscore)
  412. {
  413. string identifier = Ogre::ToLower(textureRef.substr(posUnderscore, posSuffix - posUnderscore));
  414. DefaultLogger::get()->debug(Formatter::format() << "Detecting texture type from filename postfix '" << identifier << "'");
  415. if (identifier == "_n" || identifier == "_nrm" || identifier == "_nrml" || identifier == "_normal" || identifier == "_normals" || identifier == "_normalmap")
  416. {
  417. textureType = aiTextureType_NORMALS;
  418. }
  419. else if (identifier == "_s" || identifier == "_spec" || identifier == "_specular" || identifier == "_specularmap")
  420. {
  421. textureType = aiTextureType_SPECULAR;
  422. }
  423. else if (identifier == "_l" || identifier == "_light" || identifier == "_lightmap" || identifier == "_occ" || identifier == "_occlusion")
  424. {
  425. textureType = aiTextureType_LIGHTMAP;
  426. }
  427. else if (identifier == "_disp" || identifier == "_displacement")
  428. {
  429. textureType = aiTextureType_DISPLACEMENT;
  430. }
  431. else
  432. {
  433. textureType = aiTextureType_DIFFUSE;
  434. }
  435. }
  436. else
  437. {
  438. textureType = aiTextureType_DIFFUSE;
  439. }
  440. }
  441. // Detect from texture unit name. This cannot be too broad as
  442. // authors might give names like "LightSaber" or "NormalNinja".
  443. else
  444. {
  445. string unitNameLower = Ogre::ToLower(textureUnitName);
  446. if (unitNameLower.find("normalmap") != string::npos)
  447. {
  448. textureType = aiTextureType_NORMALS;
  449. }
  450. else if (unitNameLower.find("specularmap") != string::npos)
  451. {
  452. textureType = aiTextureType_SPECULAR;
  453. }
  454. else if (unitNameLower.find("lightmap") != string::npos)
  455. {
  456. textureType = aiTextureType_LIGHTMAP;
  457. }
  458. else if (unitNameLower.find("displacementmap") != string::npos)
  459. {
  460. textureType = aiTextureType_DISPLACEMENT;
  461. }
  462. else
  463. {
  464. textureType = aiTextureType_DIFFUSE;
  465. }
  466. }
  467. }
  468. else if (linePart == partTextCoordSet)
  469. {
  470. ss >> uvCoord;
  471. }
  472. /// @todo Implement
  473. else if(linePart == partColorOp)
  474. {
  475. /*
  476. ss >> linePart;
  477. if("replace"==linePart)//I don't think, assimp has something for this...
  478. {
  479. }
  480. else if("modulate"==linePart)
  481. {
  482. //TODO: set value
  483. //material->AddProperty(aiTextureOp_Multiply)
  484. }
  485. */
  486. }
  487. }
  488. if (textureRef.empty())
  489. {
  490. DefaultLogger::get()->warn("Texture reference is empty, ignoring texture_unit.");
  491. return false;
  492. }
  493. if (textureType == aiTextureType_NONE)
  494. {
  495. DefaultLogger::get()->warn("Failed to detect texture type for '" + textureRef + "', ignoring texture_unit.");
  496. return false;
  497. }
  498. unsigned int textureTypeIndex = m_textures[textureType];
  499. m_textures[textureType]++;
  500. DefaultLogger::get()->debug(Formatter::format() << " texture '" << textureRef << "' type " << textureType
  501. << " index " << textureTypeIndex << " UV " << uvCoord);
  502. aiString assimpTextureRef(textureRef);
  503. material->AddProperty(&assimpTextureRef, AI_MATKEY_TEXTURE(textureType, textureTypeIndex));
  504. material->AddProperty(&uvCoord, 1, AI_MATKEY_UVWSRC(textureType, textureTypeIndex));
  505. return true;
  506. }
  507. } // Ogre
  508. } // Assimp
  509. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER