Material.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include "Base.h"
  2. #include "Material.h"
  3. #include "FileSystem.h"
  4. #include "Effect.h"
  5. #include "Technique.h"
  6. #include "Pass.h"
  7. #include "Properties.h"
  8. namespace gameplay
  9. {
  10. Material::Material() :
  11. _currentTechnique(NULL)
  12. {
  13. }
  14. Material::Material(const Material& m)
  15. {
  16. }
  17. Material::~Material()
  18. {
  19. // Destroy all the techniques.
  20. for (unsigned int i = 0, count = _techniques.size(); i < count; ++i)
  21. {
  22. Technique* technique = _techniques[i];
  23. if (technique)
  24. {
  25. SAFE_RELEASE(technique);
  26. }
  27. }
  28. }
  29. Material* Material::create(const char* materialPath)
  30. {
  31. assert(materialPath);
  32. // Load the material properties from file
  33. Properties* properties = Properties::create(materialPath);
  34. assert(properties);
  35. if (properties == NULL)
  36. {
  37. return NULL;
  38. }
  39. Material* material = create(properties->getNextNamespace());
  40. SAFE_DELETE(properties);
  41. return material;
  42. }
  43. Material* Material::create(Properties* materialProperties)
  44. {
  45. // Check if the Properties is valid and has a valid namespace.
  46. assert(materialProperties);
  47. if (!materialProperties || !(strcmp(materialProperties->getNamespace(), "material") == 0))
  48. {
  49. return NULL;
  50. }
  51. // Create new material from the file passed in.
  52. Material* material = new Material();
  53. // Go through all the material properties and create techniques under this material.
  54. Properties* techniqueProperties = NULL;
  55. while ((techniqueProperties = materialProperties->getNextNamespace()))
  56. {
  57. if (strcmp(techniqueProperties->getNamespace(), "technique") == 0)
  58. {
  59. if (!loadTechnique(material, techniqueProperties))
  60. {
  61. SAFE_RELEASE(material);
  62. return NULL;
  63. }
  64. }
  65. }
  66. // Load uniform value parameters for this material
  67. loadRenderState(material, materialProperties);
  68. // Set the current technique to the first found technique
  69. if (material->getTechniqueCount() > 0)
  70. {
  71. material->setTechnique((unsigned int)0);
  72. }
  73. return material;
  74. }
  75. Material* Material::create(Effect* effect)
  76. {
  77. // Create a new material with a single technique and pass for the given effect
  78. Material* material = new Material();
  79. Technique* technique = new Technique(NULL, material);
  80. material->_techniques.push_back(technique);
  81. Pass* pass = new Pass(NULL, technique, effect);
  82. technique->_passes.push_back(pass);
  83. effect->addRef();
  84. material->_currentTechnique = technique;
  85. return material;
  86. }
  87. Material* Material::create(const char* vshPath, const char* fshPath, const char* defines)
  88. {
  89. // Create a new material with a single technique and pass for the given effect
  90. Material* material = new Material();
  91. Technique* technique = new Technique(NULL, material);
  92. material->_techniques.push_back(technique);
  93. Pass* pass = Pass::create(NULL, technique, vshPath, fshPath, defines);
  94. if (!pass)
  95. {
  96. SAFE_RELEASE(material);
  97. return NULL;
  98. }
  99. technique->_passes.push_back(pass);
  100. material->_currentTechnique = technique;
  101. return material;
  102. }
  103. Material* Material::clone(CloneContext &context) const
  104. {
  105. Material* material = new Material();
  106. RenderState::cloneInto(material, context);
  107. for (std::vector<Technique*>::const_iterator it = _techniques.begin(); it != _techniques.end(); ++it)
  108. {
  109. const Technique* technique = *it;
  110. Technique* techniqueClone = technique->clone(material, context);
  111. material->_techniques.push_back(techniqueClone);
  112. if (_currentTechnique == technique)
  113. {
  114. material->_currentTechnique = techniqueClone;
  115. }
  116. }
  117. return material;
  118. }
  119. unsigned int Material::getTechniqueCount() const
  120. {
  121. return _techniques.size();
  122. }
  123. Technique* Material::getTechnique(unsigned int index) const
  124. {
  125. assert(index < _techniques.size());
  126. return _techniques[index];
  127. }
  128. Technique* Material::getTechnique(const char* id) const
  129. {
  130. for (unsigned int i = 0, count = _techniques.size(); i < count; ++i)
  131. {
  132. Technique* t = _techniques[i];
  133. if (strcmp(t->getId(), id) == 0)
  134. {
  135. return t;
  136. }
  137. }
  138. return NULL;
  139. }
  140. Technique* Material::getTechnique() const
  141. {
  142. return _currentTechnique;
  143. }
  144. void Material::setTechnique(const char* id)
  145. {
  146. Technique* t = getTechnique(id);
  147. if (t)
  148. {
  149. _currentTechnique = t;
  150. }
  151. }
  152. void Material::setTechnique(unsigned int index)
  153. {
  154. Technique* t = getTechnique(index);
  155. if (t)
  156. {
  157. _currentTechnique = t;
  158. }
  159. }
  160. bool Material::loadTechnique(Material* material, Properties* techniqueProperties)
  161. {
  162. // Create a new technique
  163. Technique* technique = new Technique(techniqueProperties->getId(), material);
  164. // Go through all the properties and create passes under this technique.
  165. techniqueProperties->rewind();
  166. Properties* passProperties = NULL;
  167. while ((passProperties = techniqueProperties->getNextNamespace()))
  168. {
  169. if (strcmp(passProperties->getNamespace(), "pass") == 0)
  170. {
  171. // Create and load passes.
  172. if (!loadPass(technique, passProperties))
  173. {
  174. SAFE_RELEASE(technique);
  175. return false;
  176. }
  177. }
  178. }
  179. // Load uniform value parameters for this technique
  180. loadRenderState(technique, techniqueProperties);
  181. // Add the new technique to the material
  182. material->_techniques.push_back(technique);
  183. return true;
  184. }
  185. bool Material::loadPass(Technique* technique, Properties* passProperties)
  186. {
  187. // Fetch shader info required to create the effect of this technique.
  188. const char* vertexShaderPath = passProperties->getString("vertexShader");
  189. assert(vertexShaderPath);
  190. const char* fragmentShaderPath = passProperties->getString("fragmentShader");
  191. assert(fragmentShaderPath);
  192. const char* defines = passProperties->getString("defines");
  193. std::string define;
  194. if (defines != NULL)
  195. {
  196. define = defines;
  197. define.insert(0, "#define ");
  198. unsigned int pos;
  199. while ((pos = define.find(';')) != std::string::npos)
  200. {
  201. define.replace(pos, 1, "\n#define ");
  202. }
  203. define += "\n";
  204. }
  205. // Create the pass
  206. Pass* pass = Pass::create(passProperties->getId(), technique, vertexShaderPath, fragmentShaderPath, define.c_str());
  207. if (!pass)
  208. {
  209. return false;
  210. }
  211. // Load render state
  212. loadRenderState(pass, passProperties);
  213. // Add the new pass to the technique
  214. technique->_passes.push_back(pass);
  215. return true;
  216. }
  217. bool isMaterialKeyword(const char* str)
  218. {
  219. #define MATERIAL_KEYWORD_COUNT 3
  220. static const char* reservedKeywords[MATERIAL_KEYWORD_COUNT] =
  221. {
  222. "vertexShader",
  223. "fragmentShader",
  224. "defines"
  225. };
  226. for (unsigned int i = 0; i < MATERIAL_KEYWORD_COUNT; ++i)
  227. {
  228. if (strcmp(reservedKeywords[i], str) == 0)
  229. {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. Texture::Filter parseTextureFilterMode(const char* str, Texture::Filter defaultValue)
  236. {
  237. if (str == NULL || strlen(str) == 0)
  238. {
  239. return defaultValue;
  240. }
  241. else if (strcmp(str, "NEAREST") == 0)
  242. {
  243. return Texture::NEAREST;
  244. }
  245. else if (strcmp(str, "LINEAR") == 0)
  246. {
  247. return Texture::LINEAR;
  248. }
  249. else if (strcmp(str, "NEAREST_MIPMAP_NEAREST") == 0)
  250. {
  251. return Texture::NEAREST_MIPMAP_NEAREST;
  252. }
  253. else if (strcmp(str, "LINEAR_MIPMAP_NEAREST") == 0)
  254. {
  255. return Texture::LINEAR_MIPMAP_NEAREST;
  256. }
  257. else if (strcmp(str, "NEAREST_MIPMAP_LINEAR") == 0)
  258. {
  259. return Texture::NEAREST_MIPMAP_LINEAR;
  260. }
  261. else if (strcmp(str, "LINEAR_MIPMAP_LINEAR") == 0)
  262. {
  263. return Texture::LINEAR_MIPMAP_LINEAR;
  264. }
  265. return defaultValue;
  266. }
  267. Texture::Wrap parseTextureWrapMode(const char* str, Texture::Wrap defaultValue)
  268. {
  269. if (str == NULL || strlen(str) == 0)
  270. {
  271. return defaultValue;
  272. }
  273. else if (strcmp(str, "REPEAT") == 0)
  274. {
  275. return Texture::REPEAT;
  276. }
  277. else if (strcmp(str, "CLAMP") == 0)
  278. {
  279. return Texture::CLAMP;
  280. }
  281. return defaultValue;
  282. }
  283. void Material::loadRenderState(RenderState* renderState, Properties* properties)
  284. {
  285. // Rewind the properties to start reading from the start
  286. properties->rewind();
  287. const char* name;
  288. while (name = properties->getNextProperty())
  289. {
  290. if (isMaterialKeyword(name))
  291. continue; // keyword - skip
  292. switch (properties->getType())
  293. {
  294. case Properties::NUMBER:
  295. renderState->getParameter(name)->setValue(properties->getFloat());
  296. break;
  297. case Properties::VECTOR2:
  298. {
  299. Vector2 vector2;
  300. if (properties->getVector2(NULL, &vector2))
  301. {
  302. renderState->getParameter(name)->setValue(vector2);
  303. }
  304. }
  305. break;
  306. case Properties::VECTOR3:
  307. {
  308. Vector3 vector3;
  309. if (properties->getVector3(NULL, &vector3))
  310. {
  311. renderState->getParameter(name)->setValue(vector3);
  312. }
  313. }
  314. break;
  315. case Properties::VECTOR4:
  316. {
  317. Vector4 vector4;
  318. if (properties->getVector4(NULL, &vector4))
  319. {
  320. renderState->getParameter(name)->setValue(vector4);
  321. }
  322. }
  323. break;
  324. case Properties::MATRIX:
  325. {
  326. Matrix matrix;
  327. if (properties->getMatrix(NULL, &matrix))
  328. {
  329. renderState->getParameter(name)->setValue(matrix);
  330. }
  331. }
  332. break;
  333. default:
  334. {
  335. // Assume this is a parameter auto-binding
  336. renderState->setParameterAutoBinding(name, properties->getString());
  337. }
  338. break;
  339. }
  340. }
  341. // Iterate through all child namespaces searching for samplers and render state blocks
  342. Properties* ns;
  343. while (ns = properties->getNextNamespace())
  344. {
  345. if (strcmp(ns->getNamespace(), "sampler") == 0)
  346. {
  347. // Read the texture uniform name
  348. name = ns->getId();
  349. if (strlen(name) == 0)
  350. continue; // missing texture uniform name
  351. // Get the texture path
  352. const char* path = ns->getString("path");
  353. if (path == NULL || strlen(path) == 0)
  354. continue; // missing texture path
  355. // Read texture state (booleans default to 'false' if not present)
  356. bool mipmap = ns->getBool("mipmap");
  357. Texture::Wrap wrapS = parseTextureWrapMode(ns->getString("wrapS"), Texture::REPEAT);
  358. Texture::Wrap wrapT = parseTextureWrapMode(ns->getString("wrapT"), Texture::REPEAT);
  359. Texture::Filter minFilter = parseTextureFilterMode(ns->getString("minFilter"), mipmap ? Texture::NEAREST_MIPMAP_LINEAR : Texture::LINEAR);
  360. Texture::Filter magFilter = parseTextureFilterMode(ns->getString("magFilter"), Texture::LINEAR);
  361. // Set the sampler parameter
  362. Texture::Sampler* sampler = renderState->getParameter(name)->setValue(path, mipmap);
  363. if (sampler)
  364. {
  365. sampler->setWrapMode(wrapS, wrapT);
  366. sampler->setFilterMode(minFilter, magFilter);
  367. }
  368. }
  369. else if (strcmp(ns->getNamespace(), "renderState") == 0)
  370. {
  371. while (name = ns->getNextProperty())
  372. {
  373. renderState->getStateBlock()->setState(name, ns->getString());
  374. }
  375. }
  376. }
  377. }
  378. }