OgreImporterMaterial.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. This file contains material related code. This is
  3. spilitted up from the main file OgreImporter.cpp
  4. to make it shorter and better amintainable.
  5. */
  6. #include "AssimpPCH.h"
  7. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  8. #include <vector>
  9. #include <sstream>
  10. using namespace std;
  11. //#include "boost/format.hpp"
  12. //#include "boost/foreach.hpp"
  13. using namespace boost;
  14. #include "OgreImporter.h"
  15. #include "irrXMLWrapper.h"
  16. namespace Assimp
  17. {
  18. namespace Ogre
  19. {
  20. aiMaterial* OgreImporter::LoadMaterial(const std::string MaterialName)
  21. {
  22. MaterialHelper *NewMaterial=new MaterialHelper();
  23. aiString ts(MaterialName.c_str());
  24. NewMaterial->AddProperty(&ts, AI_MATKEY_NAME);
  25. /*For bettetr understanding of the material parser, here is a material example file:
  26. material Sarg
  27. {
  28. receive_shadows on
  29. technique
  30. {
  31. pass
  32. {
  33. ambient 0.500000 0.500000 0.500000 1.000000
  34. diffuse 0.640000 0.640000 0.640000 1.000000
  35. specular 0.500000 0.500000 0.500000 1.000000 12.500000
  36. emissive 0.000000 0.000000 0.000000 1.000000
  37. texture_unit
  38. {
  39. texture SargTextur.tga
  40. tex_address_mode wrap
  41. filtering linear linear none
  42. }
  43. }
  44. }
  45. }
  46. */
  47. string MaterialFileName=m_CurrentFilename.substr(0, m_CurrentFilename.find('.'))+".material";
  48. DefaultLogger::get()->info(str(format("Trying to load %1%") % MaterialFileName));
  49. //Read the file into memory and put it in a stringstream
  50. stringstream ss;
  51. {// after this block, the temporarly loaded data will be released
  52. IOStream* MatFilePtr=m_CurrentIOHandler->Open(MaterialFileName);
  53. if(NULL==MatFilePtr)
  54. {
  55. MatFilePtr=m_CurrentIOHandler->Open(m_MaterialLibFilename);
  56. if(NULL==MatFilePtr)
  57. {
  58. DefaultLogger::get()->error(m_MaterialLibFilename+" and "+MaterialFileName + " could not be opned, Material will not be loaded!");
  59. return NewMaterial;
  60. }
  61. }
  62. scoped_ptr<IOStream> MaterialFile(MatFilePtr);
  63. vector<char> FileData(MaterialFile->FileSize());
  64. MaterialFile->Read(&FileData[0], MaterialFile->FileSize(), 1);
  65. BaseImporter::ConvertToUTF8(FileData);
  66. ss << &FileData[0];
  67. }
  68. string Line;
  69. ss >> Line;
  70. // unsigned int Level=0;//Hierarchielevels in the material file, like { } blocks into another
  71. while(!ss.eof())
  72. {
  73. if(Line=="material")
  74. {
  75. ss >> Line;
  76. if(Line==MaterialName)//Load the next material
  77. {
  78. ss >> Line;
  79. if(Line!="{")
  80. throw DeadlyImportError("empty material!");
  81. while(Line!="}")//read until the end of the material
  82. {
  83. //Proceed to the first technique
  84. ss >> Line;
  85. if(Line=="technique")
  86. {
  87. ss >> Line;
  88. if(Line!="{")
  89. throw DeadlyImportError("empty technique!");
  90. while(Line!="}")//read until the end of the technique
  91. {
  92. ss >> Line;
  93. if(Line=="pass")
  94. {
  95. ss >> Line;
  96. if(Line!="{")
  97. throw DeadlyImportError("empty pass!");
  98. while(Line!="}")//read until the end of the pass
  99. {
  100. ss >> Line;
  101. if(Line=="ambient")
  102. {
  103. //read the ambient light values:
  104. }
  105. else if(Line=="diffuse")
  106. {
  107. }
  108. else if(Line=="specular")
  109. {
  110. }
  111. else if(Line=="emmisive")
  112. {
  113. }
  114. else if(Line=="texture_unit")
  115. {
  116. ss >> Line;
  117. if(Line!="{")
  118. throw DeadlyImportError("empty texture unit!");
  119. while(Line!="}")//read until the end of the texture_unit
  120. {
  121. ss >> Line;
  122. if(Line=="texture")
  123. {
  124. ss >> Line;
  125. aiString ts(Line.c_str());
  126. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  127. }
  128. }//end of texture unit
  129. }
  130. }
  131. }
  132. }//end of technique
  133. }
  134. DefaultLogger::get()->info(Line);
  135. //read informations from a custom material:
  136. if(Line=="set")
  137. {
  138. ss >> Line;
  139. if(Line=="$specular")//todo load this values:
  140. {
  141. }
  142. if(Line=="$diffuse")
  143. {
  144. }
  145. if(Line=="$ambient")
  146. {
  147. }
  148. if(Line=="$colormap")
  149. {
  150. ss >> Line;
  151. aiString ts(Line.c_str());
  152. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
  153. }
  154. if(Line=="$normalmap")
  155. {
  156. ss >> Line;
  157. aiString ts(Line.c_str());
  158. NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
  159. }
  160. }
  161. }//end of material
  162. }
  163. else {} //this is the wrong material, proceed the file until we reach the next material
  164. }
  165. ss >> Line;
  166. }
  167. return NewMaterial;
  168. }
  169. }//namespace Ogre
  170. }//namespace Assimp
  171. #endif // !! ASSIMP_BUILD_NO_OGRE_IMPORTER