Material.cpp 12 KB

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