CmMaterial.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. namespace CamelotEngine
  4. {
  5. class Material
  6. {
  7. struct ParamsPerPass
  8. {
  9. GpuProgramParametersPtr mVertParams;
  10. GpuProgramParametersPtr mFragParams;
  11. GpuProgramParametersPtr mGeomParams;
  12. };
  13. public:
  14. /**
  15. * @brief Sets a shader that will be used by the material.
  16. * Shaders best technique will be retrieved and used in all subsequent Material
  17. * operations.
  18. * You need to call this method before doing any other operations with this class.
  19. * After setting the shader if change any systems that a shader technique is defendant upon (render system, renderer, ...)
  20. * you will need to call this method again on all your Materials to make sure technique used is updated.
  21. */
  22. void setShader(ShaderPtr shader);
  23. void setTexture(const String& name, TextureRef& value);
  24. void setFloat(const String& name, float value);
  25. void setColor(const String& name, const Color& value);
  26. void setVec2(const String& name, const Vector2& value);
  27. void setVec3(const String& name, const Vector3& value);
  28. void setVec4(const String& name, const Vector4& value);
  29. void setMat3(const String& name, const Matrix3& value);
  30. void setMat4(const String& name, const Matrix4& value);
  31. /**
  32. * @brief Use the provided pass for rendering of all following objects.
  33. */
  34. void applyPass(UINT32 passIdx);
  35. UINT32 getNumPasses() const;
  36. private:
  37. ShaderPtr mShader;
  38. TechniquePtr mBestTechnique;
  39. vector<ParamsPerPass>::type mParameters;
  40. void throwIfNotInitialized() const;
  41. template <typename T>
  42. void setParam(const String& name, T& value)
  43. {
  44. for(auto iter = mParameters.begin(); iter != mParameters.end(); ++iter)
  45. {
  46. ParamsPerPass params = *iter;
  47. if(params.mVertParams)
  48. {
  49. if(params.mVertParams->hasNamedConstant(name))
  50. params.mVertParams->setNamedConstant(name, value);
  51. }
  52. if(params.mFragParams)
  53. {
  54. if(params.mFragParams->hasNamedConstant(name))
  55. params.mFragParams->setNamedConstant(name, value);
  56. }
  57. if(params.mGeomParams)
  58. {
  59. if(params.mGeomParams->hasNamedConstant(name))
  60. params.mGeomParams->setNamedConstant(name, value);
  61. }
  62. }
  63. }
  64. };
  65. }