Material.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "Material.h"
  2. #include "FileIO.h"
  3. #include "StringUtil.h"
  4. #include "edtaa3func.h"
  5. namespace gameplay
  6. {
  7. using std::string;
  8. using std::vector;
  9. using std::map;
  10. Material::Material(const std::string& id) :
  11. _parent(NULL), _id(id), _lit(false)
  12. {
  13. }
  14. Material::Material(const Material& c) :
  15. _parent(c._parent),
  16. _id(c._id), _lit(false),
  17. _vertexShader(c._vertexShader),
  18. _fragmentShader(c._fragmentShader),
  19. _defines(c._defines),
  20. _uniforms(c._uniforms),
  21. _renderStates(c._renderStates)
  22. {
  23. for (vector<Sampler*>::const_iterator it = c._samplers.begin(); it != c._samplers.end(); ++it)
  24. {
  25. _samplers.push_back(new Sampler(**it));
  26. }
  27. }
  28. Material::~Material(void)
  29. {
  30. }
  31. const string& Material::getId() const
  32. {
  33. return _id;
  34. }
  35. void Material::setId(const char* id)
  36. {
  37. if (id)
  38. _id.assign(id);
  39. }
  40. Material* Material::getParent() const
  41. {
  42. return _parent;
  43. }
  44. void Material::setParent(Material* material)
  45. {
  46. if (material)
  47. _parent = material;
  48. }
  49. void Material::addDefine(const string& name)
  50. {
  51. if (!name.empty())
  52. {
  53. _defines[name] = string();
  54. }
  55. }
  56. bool Material::isDefined(const string& name) const
  57. {
  58. if (!name.empty())
  59. {
  60. return _defines.find(name) != _defines.end();
  61. }
  62. return false;
  63. }
  64. const char* Material::getUniform(const char* name) const
  65. {
  66. map<string, string>::const_iterator it = _uniforms.find(string(name));
  67. if (it != _uniforms.end())
  68. {
  69. return it->second.c_str();
  70. }
  71. return NULL;
  72. }
  73. void Material::setUniform(const string& name, const string& value)
  74. {
  75. _uniforms[name] = value;
  76. }
  77. const char* Material::getRenderState(const char* name) const
  78. {
  79. map<string, string>::const_iterator it = _renderStates.find(string(name));
  80. if (it != _renderStates.end())
  81. {
  82. return it->second.c_str();
  83. }
  84. return NULL;
  85. }
  86. void Material::setRenderState(const string& name, const string& value)
  87. {
  88. if (!name.empty())
  89. _renderStates[name] = value;
  90. }
  91. void Material::setVertexShader(const char* path)
  92. {
  93. if (path)
  94. _vertexShader.assign(path);
  95. }
  96. void Material::setFragmentShader(const char* path)
  97. {
  98. if (path)
  99. _fragmentShader.assign(path);
  100. }
  101. Sampler* Material::createSampler(const string& id)
  102. {
  103. Sampler* sampler = new Sampler(id.c_str());
  104. sampler->set("mipmap", "true");
  105. sampler->set("wrapS", CLAMP);
  106. sampler->set("wrapT", CLAMP);
  107. sampler->set(MIN_FILTER, LINEAR_MIPMAP_LINEAR);
  108. sampler->set(MAG_FILTER, LINEAR);
  109. _samplers.push_back(sampler);
  110. return sampler;
  111. }
  112. Sampler* Material::getSampler(const string& id) const
  113. {
  114. for (vector<Sampler*>::const_iterator it = _samplers.begin(); it != _samplers.end(); ++it)
  115. {
  116. Sampler* sampler = *it;
  117. if (sampler->getId() == id)
  118. {
  119. return sampler;
  120. }
  121. }
  122. return NULL;
  123. }
  124. bool Material::isTextured() const
  125. {
  126. return !_samplers.empty();
  127. }
  128. bool Material::isLit() const
  129. {
  130. return _lit;
  131. }
  132. bool Material::isSpecular() const
  133. {
  134. return isDefined(SPECULAR);
  135. }
  136. bool Material::isTextureRepeat() const
  137. {
  138. return isDefined(TEXTURE_REPEAT);
  139. }
  140. bool Material::isVertexColor() const
  141. {
  142. return isDefined("VERTEX_COLOR");
  143. }
  144. bool Material::isSkinned() const
  145. {
  146. return isDefined("SKINNING");
  147. }
  148. bool Material::isModulateAlpha() const
  149. {
  150. return isDefined("MODULATE_ALPHA");
  151. }
  152. void Material::setLit(bool value)
  153. {
  154. _lit = value;
  155. }
  156. void Material::writeMaterial(FILE* file)
  157. {
  158. fprintf(file, "material");
  159. if (getId().length() > 0)
  160. {
  161. fprintf(file, " %s", getId().c_str());
  162. }
  163. if (_parent)
  164. {
  165. assert(_parent->getId().length() > 0);
  166. fprintf(file, " : %s", _parent->getId().c_str());
  167. }
  168. fprintf(file, "\n");
  169. fprintf(file, "{\n");
  170. unsigned int indent = 1;
  171. writeUniforms(file, indent);
  172. writeSamplers(file, indent);
  173. writeRenderStates(file, indent);
  174. writeTechniqueAndPass(file, indent);
  175. --indent;
  176. writeIndent(indent, file);
  177. fprintf(file, "}\n");
  178. }
  179. void Material::writeDefines(FILE* file, unsigned int& indent)
  180. {
  181. writeIndent(indent, file);
  182. fprintf(file, "defines = ");
  183. for (map<string, string>::const_iterator it = _defines.begin(); it != _defines.end(); ++it)
  184. {
  185. if (it != _defines.begin())
  186. {
  187. fprintf(file, ";");
  188. }
  189. if (it->second.empty())
  190. {
  191. fprintf(file, "%s", it->first.c_str());
  192. }
  193. else
  194. {
  195. fprintf(file, "%s %s", it->first.c_str(), it->second.c_str());
  196. }
  197. }
  198. fprintf(file, "\n");
  199. }
  200. void Material::writeUniforms(FILE* file, unsigned int& indent)
  201. {
  202. for (map<string, string>::const_iterator it = _uniforms.begin(); it != _uniforms.end(); ++it)
  203. {
  204. writeIndent(indent, file);
  205. fprintf(file, "%s = %s\n", it->first.c_str(), it->second.c_str());
  206. }
  207. if (!_uniforms.empty())
  208. {
  209. writeIndent(indent, file);
  210. fprintf(file, "\n");
  211. }
  212. }
  213. void Material::writeSamplers(FILE* file, unsigned int& indent)
  214. {
  215. for (vector<Sampler*>::iterator it = _samplers.begin(); it != _samplers.end(); ++it)
  216. {
  217. Sampler* sampler = *it;
  218. Sampler* parentSampler = NULL;
  219. if (_parent)
  220. {
  221. parentSampler = _parent->getSampler(sampler->getId().c_str());
  222. }
  223. sampler->writeMaterial(file, indent, parentSampler);
  224. fprintf(file, "\n");
  225. }
  226. }
  227. void Material::writeRenderStates(FILE* file, unsigned int& indent)
  228. {
  229. if (_renderStates.empty())
  230. return;
  231. writeIndent(indent, file);
  232. fprintf(file, "renderState\n");
  233. writeIndent(indent, file);
  234. fprintf(file, "{\n");
  235. ++indent;
  236. for (map<string, string>::const_iterator it = _renderStates.begin(); it != _renderStates.end(); ++it)
  237. {
  238. writeIndent(indent, file);
  239. fprintf(file, "%s = %s\n", it->first.c_str(), it->second.c_str());
  240. }
  241. --indent;
  242. writeIndent(indent, file);
  243. fprintf(file, "}\n");
  244. writeIndent(indent, file);
  245. fprintf(file, "\n");
  246. }
  247. void Material::writeTechniqueAndPass(FILE* file, unsigned int& indent)
  248. {
  249. if (!_vertexShader.empty() || !_fragmentShader.empty() || !_defines.empty())
  250. {
  251. bool techniqueWritten = false;
  252. if (!_vertexShader.empty() ||
  253. !_fragmentShader.empty() ||
  254. (!_defines.empty() && (!_parent || _parent->_defines != _defines)))
  255. {
  256. writeTechniqueOpening(file, indent);
  257. techniqueWritten = true;
  258. }
  259. if (!_vertexShader.empty())
  260. {
  261. writeIndent(indent, file);
  262. fprintf(file, "%s = %s\n", "vertexShader", _vertexShader.c_str());
  263. }
  264. if (!_fragmentShader.empty())
  265. {
  266. writeIndent(indent, file);
  267. fprintf(file, "%s = %s\n", "fragmentShader", _fragmentShader.c_str());
  268. }
  269. if (!_defines.empty())
  270. {
  271. if (!_parent || _parent->_defines != _defines)
  272. {
  273. writeDefines(file, indent);
  274. }
  275. }
  276. if (techniqueWritten)
  277. {
  278. --indent;
  279. writeIndent(indent, file);
  280. fprintf(file, "}\n");
  281. --indent;
  282. writeIndent(indent, file);
  283. fprintf(file, "}\n");
  284. }
  285. }
  286. }
  287. void Material::writeTechniqueOpening(FILE* file, unsigned int& indent)
  288. {
  289. // write the techniques
  290. writeIndent(indent, file);
  291. fprintf(file, "technique\n");
  292. writeIndent(indent, file);
  293. fprintf(file, "{\n");
  294. ++indent;
  295. // write the passes
  296. writeIndent(indent, file);
  297. fprintf(file, "pass \n");
  298. writeIndent(indent, file);
  299. fprintf(file, "{\n");
  300. ++indent;
  301. }
  302. }