OgreMaterial.cpp 13 KB

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