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