Material.cpp 12 KB

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