MaterialParameter.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * MaterialParameter.cpp
  3. */
  4. #include "Base.h"
  5. #include "MaterialParameter.h"
  6. namespace gameplay
  7. {
  8. MaterialParameter::MaterialParameter(const char* name) :
  9. _type(MaterialParameter::NONE), _count(1), _dynamic(false), _name(name), _uniform(NULL)
  10. {
  11. clearValue();
  12. }
  13. MaterialParameter::~MaterialParameter()
  14. {
  15. clearValue();
  16. }
  17. void MaterialParameter::clearValue()
  18. {
  19. if (_dynamic)
  20. {
  21. switch (_type)
  22. {
  23. case MaterialParameter::FLOAT:
  24. case MaterialParameter::VECTOR2:
  25. case MaterialParameter::VECTOR3:
  26. case MaterialParameter::VECTOR4:
  27. case MaterialParameter::MATRIX:
  28. SAFE_DELETE_ARRAY(_value.floatPtrValue);
  29. break;
  30. case MaterialParameter::INT:
  31. SAFE_DELETE_ARRAY(_value.intPtrValue);
  32. break;
  33. case MaterialParameter::METHOD:
  34. SAFE_DELETE(_value.method);
  35. break;
  36. }
  37. _dynamic = false;
  38. _count = 1;
  39. }
  40. else
  41. {
  42. switch (_type)
  43. {
  44. case MaterialParameter::SAMPLER:
  45. if (_value.samplerValue)
  46. {
  47. const_cast<Texture::Sampler*>(_value.samplerValue)->release();
  48. }
  49. break;
  50. }
  51. }
  52. memset(&_value, 0, sizeof(_value));
  53. _type = MaterialParameter::NONE;
  54. }
  55. const char* MaterialParameter::getName() const
  56. {
  57. return _name.c_str();
  58. }
  59. void MaterialParameter::setValue(float value)
  60. {
  61. clearValue();
  62. _value.floatValue = value;
  63. _type = MaterialParameter::FLOAT;
  64. }
  65. void MaterialParameter::setValue(int value)
  66. {
  67. clearValue();
  68. _value.intValue = value;
  69. _type = MaterialParameter::INT;
  70. }
  71. void MaterialParameter::setValue(const float* values, unsigned int count)
  72. {
  73. clearValue();
  74. _value.floatPtrValue = const_cast<float*> (values);
  75. _count = count;
  76. _type = MaterialParameter::FLOAT;
  77. }
  78. void MaterialParameter::setValue(const int* values, unsigned int count)
  79. {
  80. clearValue();
  81. _value.intPtrValue = const_cast<int*> (values);
  82. _count = count;
  83. _type = MaterialParameter::INT;
  84. }
  85. void MaterialParameter::setValue(const Vector2& value)
  86. {
  87. clearValue();
  88. // Copy data by-value into a dynamic array.
  89. float* array = new float[2];
  90. memcpy(array, &value.x, sizeof(float) * 2);
  91. _value.floatPtrValue = array;
  92. _dynamic = true;
  93. _count = 1;
  94. _type = MaterialParameter::VECTOR2;
  95. }
  96. void MaterialParameter::setValue(const Vector2* values, unsigned int count)
  97. {
  98. clearValue();
  99. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  100. _count = count;
  101. _type = MaterialParameter::VECTOR2;
  102. }
  103. void MaterialParameter::setValue(const Vector3& value)
  104. {
  105. clearValue();
  106. // Copy data by-value into a dynamic array.
  107. float* array = new float[3];
  108. memcpy(array, &value.x, sizeof(float) * 3);
  109. _value.floatPtrValue = array;
  110. _dynamic = true;
  111. _count = 1;
  112. _type = MaterialParameter::VECTOR3;
  113. }
  114. void MaterialParameter::setValue(const Vector3* values, unsigned int count)
  115. {
  116. clearValue();
  117. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  118. _count = count;
  119. _type = MaterialParameter::VECTOR3;
  120. }
  121. void MaterialParameter::setValue(const Vector4& value)
  122. {
  123. clearValue();
  124. // Copy data by-value into a dynamic array.
  125. float* array = new float[4];
  126. memcpy(array, &value.x, sizeof(float) * 4);
  127. _value.floatPtrValue = array;
  128. _dynamic = true;
  129. _count = 1;
  130. _type = MaterialParameter::VECTOR4;
  131. }
  132. void MaterialParameter::setValue(const Vector4* values, unsigned int count)
  133. {
  134. clearValue();
  135. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  136. _count = count;
  137. _type = MaterialParameter::VECTOR4;
  138. }
  139. void MaterialParameter::setValue(const Matrix& value)
  140. {
  141. // If this parameter is already storing a single dynamic matrix, no need to clear it.
  142. if (!(_dynamic && _count == 1 && _type == MaterialParameter::MATRIX && _value.floatPtrValue != NULL))
  143. {
  144. clearValue();
  145. // Allocate a new dynamic matrix.
  146. _value.floatPtrValue = new float[16];
  147. }
  148. memcpy(_value.floatPtrValue, value.m, sizeof(float) * 16);
  149. _dynamic = true;
  150. _count = 1;
  151. _type = MaterialParameter::MATRIX;
  152. }
  153. void MaterialParameter::setValue(const Matrix* values, unsigned int count)
  154. {
  155. clearValue();
  156. _value.floatPtrValue = const_cast<Matrix&> (values[0]).m;
  157. _count = count;
  158. _type = MaterialParameter::MATRIX;
  159. }
  160. void MaterialParameter::setValue(const Texture::Sampler* sampler)
  161. {
  162. clearValue();
  163. if (sampler)
  164. {
  165. const_cast<Texture::Sampler*>(sampler)->addRef();
  166. _value.samplerValue = sampler;
  167. _type = MaterialParameter::SAMPLER;
  168. }
  169. }
  170. Texture::Sampler* MaterialParameter::setValue(const char* texturePath, bool generateMipmaps)
  171. {
  172. if (texturePath)
  173. {
  174. clearValue();
  175. Texture::Sampler* sampler = Texture::Sampler::create(texturePath, generateMipmaps);
  176. if (sampler)
  177. {
  178. _value.samplerValue = sampler;
  179. _type = MaterialParameter::SAMPLER;
  180. }
  181. return sampler;
  182. }
  183. return NULL;
  184. }
  185. void MaterialParameter::bind(Effect* effect)
  186. {
  187. // If we had a Uniform cached that is not from the passed in effect,
  188. // we need to update our uniform to point to the new effect's uniform.
  189. if (!_uniform || _uniform->getEffect() != effect)
  190. {
  191. _uniform = effect->getUniform(_name.c_str());
  192. if (!_uniform)
  193. {
  194. // This parameter was not found in the specified effect, so do nothing.
  195. WARN_VARG("Warning: Material parameter '%s' not found in effect '%s'.", _name.c_str(), effect->getId());
  196. return;
  197. }
  198. }
  199. switch (_type)
  200. {
  201. case MaterialParameter::FLOAT:
  202. if (_count == 1)
  203. {
  204. effect->setValue(_uniform, _value.floatValue);
  205. }
  206. else
  207. {
  208. assert(_value.floatPtrValue);
  209. effect->setValue(_uniform, _value.floatPtrValue, _count);
  210. }
  211. break;
  212. case MaterialParameter::INT:
  213. if (_count == 1)
  214. {
  215. effect->setValue(_uniform, _value.intValue);
  216. }
  217. else
  218. {
  219. assert(_value.intPtrValue);
  220. effect->setValue(_uniform, _value.intPtrValue, _count);
  221. }
  222. break;
  223. case MaterialParameter::VECTOR2:
  224. assert(_value.floatPtrValue);
  225. effect->setValue(_uniform, reinterpret_cast<Vector2*>(_value.floatPtrValue), _count);
  226. break;
  227. case MaterialParameter::VECTOR3:
  228. assert(_value.floatPtrValue);
  229. effect->setValue(_uniform, reinterpret_cast<Vector3*>(_value.floatPtrValue), _count);
  230. break;
  231. case MaterialParameter::VECTOR4:
  232. assert(_value.floatPtrValue);
  233. effect->setValue(_uniform, reinterpret_cast<Vector4*>(_value.floatPtrValue), _count);
  234. break;
  235. case MaterialParameter::MATRIX:
  236. assert(_value.floatPtrValue);
  237. effect->setValue(_uniform, reinterpret_cast<Matrix*>(_value.floatPtrValue), _count);
  238. break;
  239. case MaterialParameter::SAMPLER:
  240. assert(_value.samplerValue);
  241. effect->setValue(_uniform, _value.samplerValue);
  242. break;
  243. case MaterialParameter::METHOD:
  244. assert(_value.method);
  245. _value.method->setValue(effect);
  246. break;
  247. }
  248. }
  249. unsigned int MaterialParameter::getAnimationPropertyComponentCount(int propertyId) const
  250. {
  251. switch (propertyId)
  252. {
  253. case ANIMATE_UNIFORM:
  254. {
  255. switch(_type)
  256. {
  257. // These types don't support animation.
  258. case NONE:
  259. case MATRIX:
  260. case SAMPLER:
  261. case METHOD:
  262. return 0;
  263. default:
  264. return _count;
  265. }
  266. }
  267. }
  268. return 0;
  269. }
  270. void MaterialParameter::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  271. {
  272. switch (propertyId)
  273. {
  274. case ANIMATE_UNIFORM:
  275. {
  276. switch (_type)
  277. {
  278. case FLOAT:
  279. value->setFloat(0, _value.floatValue);
  280. break;
  281. case INT:
  282. value->setFloat(0, _value.intValue);
  283. break;
  284. case VECTOR2:
  285. case VECTOR3:
  286. case VECTOR4:
  287. for (unsigned int i = 0; i < _count; i++)
  288. {
  289. value->setFloat(i, _value.floatPtrValue[i]);
  290. }
  291. break;
  292. // UNSUPPORTED: NONE, MATRIX, METHOD, SAMPLER
  293. }
  294. }
  295. }
  296. }
  297. void MaterialParameter::setAnimationPropertyValue(int propertyId, AnimationValue* value)
  298. {
  299. switch (propertyId)
  300. {
  301. case ANIMATE_UNIFORM:
  302. {
  303. switch (_type)
  304. {
  305. case FLOAT:
  306. _value.floatValue = value->getFloat(0);
  307. break;
  308. case INT:
  309. _value.intValue = value->getFloat(0);
  310. break;
  311. case VECTOR2:
  312. case VECTOR3:
  313. case VECTOR4:
  314. case MATRIX:
  315. for (unsigned int i = 0; i < _count; i++)
  316. {
  317. _value.floatPtrValue[i] = value->getFloat(i);
  318. }
  319. break;
  320. // UNSUPPORTED: NONE, MATRIX, METHOD, SAMPLER
  321. }
  322. }
  323. }
  324. }
  325. }