OgreImporterMaterial.cpp 7.3 KB

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