OgreMaterial.cpp 13 KB

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