OgreImporterMaterial.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. //the filename typically ends with .mesh or .mesh.xml
  98. const string MaterialFileName=m_CurrentFilename.substr(0, m_CurrentFilename.rfind(".mesh"))+".material";
  99. DefaultLogger::get()->info("Trying to load " + MaterialFileName);
  100. aiMaterial *NewMaterial=new aiMaterial();
  101. aiString ts(MaterialName.c_str());
  102. NewMaterial->AddProperty(&ts, AI_MATKEY_NAME);
  103. //Read the file into memory and put it in a stringstream
  104. stringstream ss;
  105. {// after this block, the temporarly loaded data will be released
  106. IOStream* 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. delete NewMaterial;
  117. return NULL;
  118. }
  119. }
  120. }
  121. boost::scoped_ptr<IOStream> MaterialFile(MatFilePtr);
  122. vector<char> FileData(MaterialFile->FileSize());
  123. MaterialFile->Read(&FileData[0], MaterialFile->FileSize(), 1);
  124. BaseImporter::ConvertToUTF8(FileData);
  125. ss << &FileData[0];
  126. }
  127. string Line;
  128. ss >> Line;
  129. // unsigned int Level=0;//Hierarchielevels in the material file, like { } blocks into another
  130. while(!ss.eof())
  131. {
  132. if(Line=="material")
  133. {
  134. ss >> Line;
  135. if(Line==MaterialName)//Load the next material
  136. {
  137. string RestOfLine;
  138. getline(ss, RestOfLine);//ignore the rest of the line
  139. ss >> Line;
  140. if(Line!="{")
  141. throw DeadlyImportError("empty material!");
  142. while(Line!="}")//read until the end of the material
  143. {
  144. //Proceed to the first technique
  145. ss >> Line;
  146. if(Line=="technique")
  147. {
  148. ReadTechnique(ss, NewMaterial);
  149. }
  150. DefaultLogger::get()->info(Line);
  151. //read informations from a custom material:
  152. if(Line=="set")
  153. {
  154. ss >> Line;
  155. if(Line=="$specular")//todo load this values:
  156. {
  157. }
  158. if(Line=="$diffuse")
  159. {
  160. }
  161. if(Line=="$ambient")
  162. {
  163. }
  164. if(Line=="$colormap")
  165. {
  166. ss >> Line;
  167. aiString ts(Line.c_str());
  168. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  169. }
  170. if(Line=="$normalmap")
  171. {
  172. ss >> Line;
  173. aiString ts(Line.c_str());
  174. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
  175. }
  176. if(Line=="$shininess_strength")
  177. {
  178. ss >> Line;
  179. float Shininess=fast_atof(Line.c_str());
  180. NewMaterial->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS_STRENGTH);
  181. }
  182. if(Line=="$shininess_exponent")
  183. {
  184. ss >> Line;
  185. float Shininess=fast_atof(Line.c_str());
  186. NewMaterial->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS);
  187. }
  188. //Properties from Venetica:
  189. if(Line=="$diffuse_map")
  190. {
  191. ss >> Line;
  192. if(Line[0]=='"')// "file" -> file
  193. Line=Line.substr(1, Line.size()-2);
  194. aiString ts(Line.c_str());
  195. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  196. }
  197. if(Line=="$specular_map")
  198. {
  199. ss >> Line;
  200. if(Line[0]=='"')// "file" -> file
  201. Line=Line.substr(1, Line.size()-2);
  202. aiString ts(Line.c_str());
  203. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_SHININESS, 0));
  204. }
  205. if(Line=="$normal_map")
  206. {
  207. ss >> Line;
  208. if(Line[0]=='"')// "file" -> file
  209. Line=Line.substr(1, Line.size()-2);
  210. aiString ts(Line.c_str());
  211. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
  212. }
  213. if(Line=="$light_map")
  214. {
  215. ss >> Line;
  216. if(Line[0]=='"')// "file" -> file
  217. Line=Line.substr(1, Line.size()-2);
  218. aiString ts(Line.c_str());
  219. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, 0));
  220. }
  221. }
  222. }//end of material
  223. }
  224. else {} //this is the wrong material, proceed the file until we reach the next material
  225. }
  226. ss >> Line;
  227. }
  228. return NewMaterial;
  229. }
  230. void OgreImporter::ReadTechnique(stringstream &ss, aiMaterial* NewMaterial)
  231. {
  232. string Line;
  233. ss >> Line;
  234. if(Line!="{")
  235. throw DeadlyImportError("empty technique!");
  236. while(Line!="}")//read until the end of the technique
  237. {
  238. ss >> Line;
  239. if(Line=="pass")
  240. {
  241. ss >> Line;
  242. if(Line!="{")
  243. throw DeadlyImportError("empty pass!");
  244. while(Line!="}")//read until the end of the pass
  245. {
  246. ss >> Line;
  247. if(Line=="ambient")
  248. {
  249. float r,g,b;
  250. ss >> r >> g >> b;
  251. const aiColor3D Color(r,g,b);
  252. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_AMBIENT);
  253. }
  254. else if(Line=="diffuse")
  255. {
  256. float r,g,b;
  257. ss >> r >> g >> b;
  258. const aiColor3D Color(r,g,b);
  259. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_DIFFUSE);
  260. }
  261. else if(Line=="specular")
  262. {
  263. float r,g,b;
  264. ss >> r >> g >> b;
  265. const aiColor3D Color(r,g,b);
  266. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_SPECULAR);
  267. }
  268. else if(Line=="emmisive")
  269. {
  270. float r,g,b;
  271. ss >> r >> g >> b;
  272. const aiColor3D Color(r,g,b);
  273. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_EMISSIVE);
  274. }
  275. else if(Line=="texture_unit")
  276. {
  277. ss >> Line;
  278. if(Line!="{")
  279. throw DeadlyImportError("empty texture unit!");
  280. while(Line!="}")//read until the end of the texture_unit
  281. {
  282. ss >> Line;
  283. if(Line=="texture")
  284. {
  285. ss >> Line;
  286. aiString ts(Line.c_str());
  287. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  288. }
  289. }//end of texture unit
  290. }
  291. }
  292. }
  293. }//end of technique
  294. }
  295. }//namespace Ogre
  296. }//namespace Assimp
  297. #endif // !! ASSIMP_BUILD_NO_OGRE_IMPORTER