Material.cpp 11 KB

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