OgreMaterial.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. /**
  34. This file contains material related code. This is
  35. spilitted up from the main file OgreImporter.cpp
  36. to make it shorter easier to maintain.
  37. */
  38. #include "AssimpPCH.h"
  39. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  40. #include <vector>
  41. #include <sstream>
  42. using namespace std;
  43. #include "OgreImporter.hpp"
  44. #include "irrXMLWrapper.h"
  45. #include "TinyFormatter.h"
  46. namespace Assimp
  47. {
  48. namespace Ogre
  49. {
  50. aiMaterial* OgreImporter::LoadMaterial(const std::string MaterialName) const
  51. {
  52. const aiScene* const m_CurrentScene=this->m_CurrentScene;//make sure, that we can access but not change the scene
  53. /*For better understanding of the material parser, here is a material example file:
  54. material Sarg
  55. {
  56. receive_shadows on
  57. technique
  58. {
  59. pass
  60. {
  61. ambient 0.500000 0.500000 0.500000 1.000000
  62. diffuse 0.640000 0.640000 0.640000 1.000000
  63. specular 0.500000 0.500000 0.500000 1.000000 12.500000
  64. emissive 0.000000 0.000000 0.000000 1.000000
  65. texture_unit
  66. {
  67. texture SargTextur.tga
  68. tex_address_mode wrap
  69. filtering linear linear none
  70. }
  71. }
  72. }
  73. }
  74. */
  75. /*and here is another one:
  76. import * from abstract_base_passes_depth.material
  77. import * from abstract_base.material
  78. import * from mat_shadow_caster.material
  79. import * from mat_character_singlepass.material
  80. material hero/hair/caster : mat_shadow_caster_skin_areject
  81. {
  82. set $diffuse_map "hero_hair_alpha_c.dds"
  83. }
  84. material hero/hair_alpha : mat_char_cns_singlepass_areject_4weights
  85. {
  86. set $diffuse_map "hero_hair_alpha_c.dds"
  87. set $specular_map "hero_hair_alpha_s.dds"
  88. set $normal_map "hero_hair_alpha_n.dds"
  89. set $light_map "black_lightmap.dds"
  90. set $shadow_caster_material "hero/hair/caster"
  91. }
  92. */
  93. //Read the file into memory and put it in a stringstream
  94. stringstream ss;
  95. {// after this block, the temporarly loaded data will be released
  96. /*
  97. We have 3 guesses for the Material filename:
  98. - the Material Name
  99. - the Name of the mesh file
  100. - the DefaultMaterialLib (which you can set before importing)
  101. */
  102. IOStream* MatFilePtr=m_CurrentIOHandler->Open(MaterialName+".material");
  103. if(NULL==MatFilePtr)
  104. {
  105. //the filename typically ends with .mesh or .mesh.xml
  106. const string MaterialFileName=m_CurrentFilename.substr(0, m_CurrentFilename.rfind(".mesh"))+".material";
  107. MatFilePtr=m_CurrentIOHandler->Open(MaterialFileName);
  108. if(NULL==MatFilePtr)
  109. {
  110. //try the default mat Library
  111. if(NULL==MatFilePtr)
  112. {
  113. MatFilePtr=m_CurrentIOHandler->Open(m_MaterialLibFilename);
  114. if(NULL==MatFilePtr)
  115. {
  116. DefaultLogger::get()->error(m_MaterialLibFilename+" and "+MaterialFileName + " could not be opened, Material will not be loaded!");
  117. return new aiMaterial();
  118. }
  119. }
  120. }
  121. }
  122. //Fill the stream
  123. boost::scoped_ptr<IOStream> MaterialFile(MatFilePtr);
  124. if(MaterialFile->FileSize()>0)
  125. {
  126. vector<char> FileData(MaterialFile->FileSize());
  127. MaterialFile->Read(&FileData[0], MaterialFile->FileSize(), 1);
  128. BaseImporter::ConvertToUTF8(FileData);
  129. FileData.push_back('\0');//terminate the string with zero, so that the ss can parse it correctly
  130. ss << &FileData[0];
  131. }
  132. else
  133. {
  134. DefaultLogger::get()->warn("Material " + MaterialName + " seams to be empty");
  135. return NULL;
  136. }
  137. }
  138. //create the material
  139. aiMaterial *NewMaterial=new aiMaterial();
  140. aiString ts(MaterialName.c_str());
  141. NewMaterial->AddProperty(&ts, AI_MATKEY_NAME);
  142. string Line;
  143. ss >> Line;
  144. // unsigned int Level=0;//Hierarchielevels in the material file, like { } blocks into another
  145. while(!ss.eof())
  146. {
  147. if(Line=="material")
  148. {
  149. ss >> Line;
  150. if(Line==MaterialName)//Load the next material
  151. {
  152. string RestOfLine;
  153. getline(ss, RestOfLine);//ignore the rest of the line
  154. ss >> Line;
  155. if(Line!="{")
  156. {
  157. DefaultLogger::get()->warn("empyt material!");
  158. return NULL;
  159. }
  160. while(Line!="}")//read until the end of the material
  161. {
  162. //Proceed to the first technique
  163. ss >> Line;
  164. if(Line=="technique")
  165. {
  166. ReadTechnique(ss, NewMaterial);
  167. }
  168. DefaultLogger::get()->info(Line);
  169. //read informations from a custom material:
  170. if(Line=="set")
  171. {
  172. ss >> Line;
  173. if(Line=="$specular")//todo load this values:
  174. {
  175. }
  176. if(Line=="$diffuse")
  177. {
  178. }
  179. if(Line=="$ambient")
  180. {
  181. }
  182. if(Line=="$colormap")
  183. {
  184. ss >> Line;
  185. aiString ts(Line.c_str());
  186. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  187. }
  188. if(Line=="$normalmap")
  189. {
  190. ss >> Line;
  191. aiString ts(Line.c_str());
  192. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
  193. }
  194. if(Line=="$shininess_strength")
  195. {
  196. ss >> Line;
  197. float Shininess=fast_atof(Line.c_str());
  198. NewMaterial->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS_STRENGTH);
  199. }
  200. if(Line=="$shininess_exponent")
  201. {
  202. ss >> Line;
  203. float Shininess=fast_atof(Line.c_str());
  204. NewMaterial->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS);
  205. }
  206. //Properties from Venetica:
  207. if(Line=="$diffuse_map")
  208. {
  209. ss >> Line;
  210. if(Line[0]=='"')// "file" -> file
  211. Line=Line.substr(1, Line.size()-2);
  212. aiString ts(Line.c_str());
  213. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  214. }
  215. if(Line=="$specular_map")
  216. {
  217. ss >> Line;
  218. if(Line[0]=='"')// "file" -> file
  219. Line=Line.substr(1, Line.size()-2);
  220. aiString ts(Line.c_str());
  221. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_SHININESS, 0));
  222. }
  223. if(Line=="$normal_map")
  224. {
  225. ss >> Line;
  226. if(Line[0]=='"')// "file" -> file
  227. Line=Line.substr(1, Line.size()-2);
  228. aiString ts(Line.c_str());
  229. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
  230. }
  231. if(Line=="$light_map")
  232. {
  233. ss >> Line;
  234. if(Line[0]=='"')// "file" -> file
  235. Line=Line.substr(1, Line.size()-2);
  236. aiString ts(Line.c_str());
  237. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, 0));
  238. }
  239. }
  240. }//end of material
  241. }
  242. else {} //this is the wrong material, proceed the file until we reach the next material
  243. }
  244. ss >> Line;
  245. }
  246. return NewMaterial;
  247. }
  248. void OgreImporter::ReadTechnique(stringstream &ss, aiMaterial* NewMaterial) const
  249. {
  250. unsigned int CurrentDiffuseTextureId=0;
  251. unsigned int CurrentSpecularTextureId=0;
  252. unsigned int CurrentNormalTextureId=0;
  253. unsigned int CurrentLightTextureId=0;
  254. string RestOfLine;
  255. getline(ss, RestOfLine);//ignore the rest of the line
  256. string Line;
  257. ss >> Line;
  258. if(Line!="{")
  259. {
  260. DefaultLogger::get()->warn("empty technique!");
  261. return;
  262. }
  263. while(Line!="}")//read until the end of the technique
  264. {
  265. ss >> Line;
  266. if(Line=="pass")
  267. {
  268. getline(ss, RestOfLine);//ignore the rest of the line
  269. ss >> Line;
  270. if(Line!="{")
  271. {
  272. DefaultLogger::get()->warn("empty pass!");
  273. return;
  274. }
  275. while(Line!="}")//read until the end of the pass
  276. {
  277. ss >> Line;
  278. if(Line=="ambient")
  279. {
  280. float r,g,b;
  281. ss >> r >> g >> b;
  282. const aiColor3D Color(r,g,b);
  283. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_AMBIENT);
  284. }
  285. else if(Line=="diffuse")
  286. {
  287. float r,g,b;
  288. ss >> r >> g >> b;
  289. const aiColor3D Color(r,g,b);
  290. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_DIFFUSE);
  291. }
  292. else if(Line=="specular")
  293. {
  294. float r,g,b;
  295. ss >> r >> g >> b;
  296. const aiColor3D Color(r,g,b);
  297. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_SPECULAR);
  298. }
  299. else if(Line=="emmisive")
  300. {
  301. float r,g,b;
  302. ss >> r >> g >> b;
  303. const aiColor3D Color(r,g,b);
  304. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_EMISSIVE);
  305. }
  306. else if(Line=="texture_unit")
  307. {
  308. getline(ss, RestOfLine);//ignore the rest of the line
  309. std::string TextureName;
  310. int TextureType=-1;
  311. int UvSet=0;
  312. ss >> Line;
  313. if(Line!="{")
  314. throw DeadlyImportError("empty texture unit!");
  315. while(Line!="}")//read until the end of the texture_unit
  316. {
  317. ss >> Line;
  318. if(Line=="texture")
  319. {
  320. ss >> Line;
  321. TextureName=Line;
  322. if(m_TextureTypeFromFilename)
  323. {
  324. if(Line.find("_n.")!=string::npos)// Normalmap
  325. {
  326. TextureType=aiTextureType_NORMALS;
  327. }
  328. else if(Line.find("_s.")!=string::npos)// Specularmap
  329. {
  330. TextureType=aiTextureType_SPECULAR;
  331. }
  332. else if(Line.find("_l.")!=string::npos)// Lightmap
  333. {
  334. TextureType=aiTextureType_LIGHTMAP;
  335. }
  336. else// colormap
  337. {
  338. TextureType=aiTextureType_DIFFUSE;
  339. }
  340. }
  341. else
  342. {
  343. TextureType=aiTextureType_DIFFUSE;
  344. }
  345. }
  346. else if(Line=="tex_coord_set")
  347. {
  348. ss >> UvSet;
  349. }
  350. else if(Line=="colour_op")//TODO implement this
  351. {
  352. /*
  353. ss >> Line;
  354. if("replace"==Line)//I don't think, assimp has something for this...
  355. {
  356. }
  357. else if("modulate"==Line)
  358. {
  359. //TODO: set value
  360. //NewMaterial->AddProperty(aiTextureOp_Multiply)
  361. }
  362. */
  363. }
  364. }//end of texture unit
  365. Line="";//clear the } that would end the outer loop
  366. //give the texture to assimp:
  367. aiString ts(TextureName.c_str());
  368. switch(TextureType)
  369. {
  370. case aiTextureType_DIFFUSE:
  371. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, CurrentDiffuseTextureId));
  372. NewMaterial->AddProperty(&UvSet, 1, AI_MATKEY_UVWSRC(0, CurrentDiffuseTextureId));
  373. CurrentDiffuseTextureId++;
  374. break;
  375. case aiTextureType_NORMALS:
  376. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, CurrentNormalTextureId));
  377. NewMaterial->AddProperty(&UvSet, 1, AI_MATKEY_UVWSRC(0, CurrentNormalTextureId));
  378. CurrentNormalTextureId++;
  379. break;
  380. case aiTextureType_SPECULAR:
  381. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_SPECULAR, CurrentSpecularTextureId));
  382. NewMaterial->AddProperty(&UvSet, 1, AI_MATKEY_UVWSRC(0, CurrentSpecularTextureId));
  383. CurrentSpecularTextureId++;
  384. break;
  385. case aiTextureType_LIGHTMAP:
  386. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, CurrentLightTextureId));
  387. NewMaterial->AddProperty(&UvSet, 1, AI_MATKEY_UVWSRC(0, CurrentLightTextureId));
  388. CurrentLightTextureId++;
  389. break;
  390. default:
  391. DefaultLogger::get()->warn("Invalid Texture Type!");
  392. break;
  393. }
  394. }
  395. }
  396. Line="";//clear the } that would end the outer loop
  397. }
  398. }//end of technique
  399. }
  400. }//namespace Ogre
  401. }//namespace Assimp
  402. #endif // !! ASSIMP_BUILD_NO_OGRE_IMPORTER