Material.cpp 13 KB

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