OgreImporterMaterial.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, ASSIMP Development 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 Development 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.h"
  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. MaterialHelper *NewMaterial=new MaterialHelper();
  58. aiString ts(MaterialName.c_str());
  59. NewMaterial->AddProperty(&ts, AI_MATKEY_NAME);
  60. /*For bettetr understanding of the material parser, here is a material example file:
  61. material Sarg
  62. {
  63. receive_shadows on
  64. technique
  65. {
  66. pass
  67. {
  68. ambient 0.500000 0.500000 0.500000 1.000000
  69. diffuse 0.640000 0.640000 0.640000 1.000000
  70. specular 0.500000 0.500000 0.500000 1.000000 12.500000
  71. emissive 0.000000 0.000000 0.000000 1.000000
  72. texture_unit
  73. {
  74. texture SargTextur.tga
  75. tex_address_mode wrap
  76. filtering linear linear none
  77. }
  78. }
  79. }
  80. }
  81. */
  82. const string MaterialFileName=m_CurrentFilename.substr(0, m_CurrentFilename.find('.'))+".material";
  83. DefaultLogger::get()->info("Trying to load " +MaterialFileName);
  84. //Read the file into memory and put it in a stringstream
  85. stringstream ss;
  86. {// after this block, the temporarly loaded data will be released
  87. IOStream* MatFilePtr=m_CurrentIOHandler->Open(MaterialFileName);
  88. if(NULL==MatFilePtr)
  89. {
  90. MatFilePtr=m_CurrentIOHandler->Open(m_MaterialLibFilename);
  91. if(NULL==MatFilePtr)
  92. {
  93. DefaultLogger::get()->error(m_MaterialLibFilename+" and "+MaterialFileName + " could not be opened, Material will not be loaded!");
  94. return NewMaterial;
  95. }
  96. }
  97. boost::scoped_ptr<IOStream> MaterialFile(MatFilePtr);
  98. vector<char> FileData(MaterialFile->FileSize());
  99. MaterialFile->Read(&FileData[0], MaterialFile->FileSize(), 1);
  100. BaseImporter::ConvertToUTF8(FileData);
  101. ss << &FileData[0];
  102. }
  103. string Line;
  104. ss >> Line;
  105. // unsigned int Level=0;//Hierarchielevels in the material file, like { } blocks into another
  106. while(!ss.eof())
  107. {
  108. if(Line=="material")
  109. {
  110. ss >> Line;
  111. if(Line==MaterialName)//Load the next material
  112. {
  113. ss >> Line;
  114. if(Line!="{")
  115. throw DeadlyImportError("empty material!");
  116. while(Line!="}")//read until the end of the material
  117. {
  118. //Proceed to the first technique
  119. ss >> Line;
  120. if(Line=="technique")
  121. {
  122. ss >> Line;
  123. if(Line!="{")
  124. throw DeadlyImportError("empty technique!");
  125. while(Line!="}")//read until the end of the technique
  126. {
  127. ss >> Line;
  128. if(Line=="pass")
  129. {
  130. ss >> Line;
  131. if(Line!="{")
  132. throw DeadlyImportError("empty pass!");
  133. while(Line!="}")//read until the end of the pass
  134. {
  135. ss >> Line;
  136. if(Line=="ambient")
  137. {
  138. float r,g,b;
  139. ss >> r >> g >> b;
  140. const aiColor3D Color(r,g,b);
  141. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_AMBIENT);
  142. }
  143. else if(Line=="diffuse")
  144. {
  145. float r,g,b;
  146. ss >> r >> g >> b;
  147. const aiColor3D Color(r,g,b);
  148. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_DIFFUSE);
  149. }
  150. else if(Line=="specular")
  151. {
  152. float r,g,b;
  153. ss >> r >> g >> b;
  154. const aiColor3D Color(r,g,b);
  155. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_SPECULAR);
  156. }
  157. else if(Line=="emmisive")
  158. {
  159. float r,g,b;
  160. ss >> r >> g >> b;
  161. const aiColor3D Color(r,g,b);
  162. NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_EMISSIVE);
  163. }
  164. else if(Line=="texture_unit")
  165. {
  166. ss >> Line;
  167. if(Line!="{")
  168. throw DeadlyImportError("empty texture unit!");
  169. while(Line!="}")//read until the end of the texture_unit
  170. {
  171. ss >> Line;
  172. if(Line=="texture")
  173. {
  174. ss >> Line;
  175. aiString ts(Line.c_str());
  176. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  177. }
  178. }//end of texture unit
  179. }
  180. }
  181. }
  182. }//end of technique
  183. }
  184. DefaultLogger::get()->info(Line);
  185. //read informations from a custom material:
  186. if(Line=="set")
  187. {
  188. ss >> Line;
  189. if(Line=="$specular")//todo load this values:
  190. {
  191. }
  192. if(Line=="$diffuse")
  193. {
  194. }
  195. if(Line=="$ambient")
  196. {
  197. }
  198. if(Line=="$colormap")
  199. {
  200. ss >> Line;
  201. aiString ts(Line.c_str());
  202. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  203. }
  204. if(Line=="$normalmap")
  205. {
  206. ss >> Line;
  207. aiString ts(Line.c_str());
  208. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
  209. }
  210. }
  211. }//end of material
  212. }
  213. else {} //this is the wrong material, proceed the file until we reach the next material
  214. }
  215. ss >> Line;
  216. }
  217. return NewMaterial;
  218. }
  219. }//namespace Ogre
  220. }//namespace Assimp
  221. #endif // !! ASSIMP_BUILD_NO_OGRE_IMPORTER