MaterialParameter.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #ifndef MATERIALPARAMETER_H_
  2. #define MATERIALPARAMETER_H_
  3. #include "AnimationTarget.h"
  4. #include "Vector2.h"
  5. #include "Vector3.h"
  6. #include "Vector4.h"
  7. #include "Matrix.h"
  8. #include "Texture.h"
  9. #include "Effect.h"
  10. namespace gameplay
  11. {
  12. /**
  13. * Defines a material parameter.
  14. *
  15. * This class represents a parameter that can be set for a material.
  16. * The methods in this class provide a mechanism to set parameters
  17. * of all supported types. Some types support setting by value,
  18. * while others only support setting by reference/pointer.
  19. *
  20. * Setting a parameter by reference/pointer provides the ability to
  21. * pass an array of values as well as a convenient way to support
  22. * auto-binding of values to a material parameter. For example, by
  23. * setting the parameter value to a pointer to a Matrix, any changes
  24. * to the Matrix will automatically be reflected in the technique the
  25. * next time the parameter is applied to the render state.
  26. */
  27. class MaterialParameter : public AnimationTarget, public Ref
  28. {
  29. friend class RenderState;
  30. public:
  31. /**
  32. * Animates the uniform.
  33. */
  34. static const int ANIMATE_UNIFORM = 1;
  35. /**
  36. * Returns the name of this material parameter.
  37. */
  38. const char* getName() const;
  39. /**
  40. * Sets the value of this parameter to a float value.
  41. */
  42. void setValue(float value);
  43. /**
  44. * Sets the value of this parameter to an integer value.
  45. */
  46. void setValue(int value);
  47. /**
  48. * Stores a pointer/array of float values in this parameter.
  49. */
  50. void setValue(const float* values, unsigned int count = 1);
  51. /**
  52. * Stores a pointer/array of integer values in this parameter.
  53. */
  54. void setValue(const int* values, unsigned int count = 1);
  55. /**
  56. * Stores a copy of the specified Vector2 value in this parameter.
  57. */
  58. void setValue(const Vector2& value);
  59. /**
  60. * Stores a pointer/array of Vector2 values in this parameter.
  61. */
  62. void setValue(const Vector2* values, unsigned int count = 1);
  63. /**
  64. * Stores a copy of the specified Vector3 value in this parameter.
  65. */
  66. void setValue(const Vector3& value);
  67. /**
  68. * Stores a pointer/array of Vector3 values in this parameter.
  69. */
  70. void setValue(const Vector3* values, unsigned int count = 1);
  71. /**
  72. * Stores a copy of the specified Vector4 value in this parameter.
  73. */
  74. void setValue(const Vector4& value);
  75. /**
  76. * Stores a pointer/array of Vector4 values in this parameter.
  77. */
  78. void setValue(const Vector4* values, unsigned int count = 1);
  79. /**
  80. * Stores a copy of the specified Matrix value in this parameter.
  81. */
  82. void setValue(const Matrix& value);
  83. /**
  84. * Stores a pointer/array of Matrix values in this parameter.
  85. */
  86. void setValue(const Matrix* values, unsigned int count = 1);
  87. /**
  88. * Sets the value of this parameter to the specified texture sampler.
  89. */
  90. void setValue(const Texture::Sampler* sampler);
  91. /**
  92. * Loads a texture sampler from the specified path and sets it as the value of this parameter.
  93. *
  94. * @param texturePath The path to the texture to set.
  95. * @param generateMipmaps True to generate a full mipmap chain for the texture, false otherwise.
  96. *
  97. * @return The texture sampler that was set for this material parameter.
  98. */
  99. Texture::Sampler* setValue(const char* texturePath, bool generateMipmaps);
  100. /**
  101. * Binds the return value of a class method to this material parameter.
  102. *
  103. * This method enables binding of arbitrary class methods to a material
  104. * parameter. This is useful when you want to set a material parameter
  105. * to a variable that is frequently changing (such as a world matrix).
  106. *
  107. * By binding a method pointer, the method will be called automatically
  108. * to retrieve the updated parameter value each time the material is bound
  109. * for rendering.
  110. *
  111. * @param classInstance The instance of the class containing the member method to bind.
  112. * @param valueMethod A pointer to the class method to bind (in the format '&class::method').
  113. */
  114. template <class ClassType, class ParameterType>
  115. void bindValue(ClassType* classInstance, ParameterType (ClassType::*valueMethod)() const);
  116. /**
  117. * Binds the return value of a class method to this material parameter.
  118. *
  119. * This overloads the setBinding method to provide support for array parameters.
  120. * The valueMethod parameter should return an array (pointer) of a supported
  121. * material parameter type, such as Matirx* for an array of matrices. The
  122. * countMethod should point to a method that returns the number of entries in
  123. * the value returned from valueMethod.
  124. *
  125. * @param classInstance The instance of the class containing the member method to bind.
  126. * @param valueMethod A pointer to the class method to bind (in the format '&class::method').
  127. * @param countMethod A pointer to a method that returns the number of entries in the array returned by valueMethod.
  128. */
  129. template <class ClassType, class ParameterType>
  130. void bindValue(ClassType* classInstance, ParameterType (ClassType::*valueMethod)() const, unsigned int (ClassType::*countMethod)() const);
  131. /**
  132. * @see AnimationTarget#getAnimationPropertyComponentCount
  133. */
  134. unsigned int getAnimationPropertyComponentCount(int propertyId) const;
  135. /**
  136. * @see AnimationTarget#getAnimationProperty
  137. */
  138. void getAnimationPropertyValue(int propertyId, AnimationValue* value);
  139. /**
  140. * @see AnimationTarget#setAnimationProperty
  141. */
  142. void setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight = 1.0f);
  143. private:
  144. /**
  145. * Constructor.
  146. */
  147. MaterialParameter(const char* name);
  148. /**
  149. * Destructor.
  150. */
  151. ~MaterialParameter();
  152. /**
  153. * Interface implemented by templated method bindings for simple storage and iteration.
  154. */
  155. class MethodBinding : public Ref
  156. {
  157. public:
  158. virtual void setValue(Effect* effect) = 0;
  159. protected:
  160. /**
  161. * Destructor.
  162. */
  163. virtual ~MethodBinding() { }
  164. };
  165. /**
  166. * Defines a method parameter binding for a single value.
  167. */
  168. template <class ClassType, class ParameterType>
  169. class MethodValueBinding : public MethodBinding
  170. {
  171. typedef ParameterType (ClassType::*ValueMethod)() const;
  172. public:
  173. MethodValueBinding(MaterialParameter* param, ClassType* instance, ValueMethod valueMethod);
  174. void setValue(Effect* effect);
  175. private:
  176. MaterialParameter* _parameter;
  177. ClassType* _instance;
  178. ValueMethod _valueMethod;
  179. };
  180. /**
  181. * Defines a method parameter binding for an array of values.
  182. */
  183. template <class ClassType, class ParameterType>
  184. class MethodArrayBinding : public MethodBinding
  185. {
  186. typedef ParameterType (ClassType::*ValueMethod)() const;
  187. typedef unsigned int (ClassType::*CountMethod)() const;
  188. public:
  189. MethodArrayBinding(MaterialParameter* param, ClassType* instance, ValueMethod valueMethod, CountMethod countMethod);
  190. void setValue(Effect* effect);
  191. private:
  192. MaterialParameter* _parameter;
  193. ClassType* _instance;
  194. ValueMethod _valueMethod;
  195. CountMethod _countMethod;
  196. };
  197. void clearValue();
  198. void bind(Effect* effect);
  199. void applyAnimationValue(AnimationValue* value, float blendWeight, int components);
  200. void cloneInto(MaterialParameter* materialParameter) const;
  201. union
  202. {
  203. float floatValue;
  204. int intValue;
  205. float* floatPtrValue;
  206. int* intPtrValue;
  207. const Texture::Sampler* samplerValue;
  208. MethodBinding* method;
  209. } _value;
  210. enum
  211. {
  212. NONE,
  213. FLOAT,
  214. INT,
  215. VECTOR2,
  216. VECTOR3,
  217. VECTOR4,
  218. MATRIX,
  219. SAMPLER,
  220. METHOD
  221. } _type;
  222. unsigned int _count;
  223. bool _dynamic;
  224. std::string _name;
  225. Uniform* _uniform;
  226. };
  227. template <class ClassType, class ParameterType>
  228. void MaterialParameter::bindValue(ClassType* classInstance, ParameterType (ClassType::*valueMethod)() const)
  229. {
  230. clearValue();
  231. _value.method = new MethodValueBinding<ClassType, ParameterType>(this, classInstance, valueMethod);
  232. _dynamic = true;
  233. _type = MaterialParameter::METHOD;
  234. }
  235. template <class ClassType, class ParameterType>
  236. void MaterialParameter::bindValue(ClassType* classInstance, ParameterType (ClassType::*valueMethod)() const, unsigned int (ClassType::*countMethod)() const)
  237. {
  238. clearValue();
  239. _value.method = new MethodArrayBinding<ClassType, ParameterType>(this, classInstance, valueMethod, countMethod);
  240. _dynamic = true;
  241. _type = MaterialParameter::METHOD;
  242. }
  243. template <class ClassType, class ParameterType>
  244. MaterialParameter::MethodValueBinding<ClassType, ParameterType>::MethodValueBinding(MaterialParameter* param, ClassType* instance, ValueMethod valueMethod) :
  245. _parameter(param), _instance(instance), _valueMethod(valueMethod)
  246. {
  247. }
  248. template <class ClassType, class ParameterType>
  249. void MaterialParameter::MethodValueBinding<ClassType, ParameterType>::setValue(Effect* effect)
  250. {
  251. effect->setValue(_parameter->_uniform, (_instance->*_valueMethod)());
  252. }
  253. template <class ClassType, class ParameterType>
  254. MaterialParameter::MethodArrayBinding<ClassType, ParameterType>::MethodArrayBinding(MaterialParameter* param, ClassType* instance, ValueMethod valueMethod, CountMethod countMethod) :
  255. _parameter(param), _instance(instance), _valueMethod(valueMethod), _countMethod(countMethod)
  256. {
  257. }
  258. template <class ClassType, class ParameterType>
  259. void MaterialParameter::MethodArrayBinding<ClassType, ParameterType>::setValue(Effect* effect)
  260. {
  261. effect->setValue(_parameter->_uniform, (_instance->*_valueMethod)(), (_instance->*_countMethod)());
  262. }
  263. }
  264. #endif