OgreMaterial.cpp 11 KB

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