IShaderConstantSetCallBack.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef IRR_I_SHADER_CONSTANT_SET_CALLBACT_H_INCLUDED
  5. #define IRR_I_SHADER_CONSTANT_SET_CALLBACT_H_INCLUDED
  6. #include "IReferenceCounted.h"
  7. namespace irr
  8. {
  9. namespace video
  10. {
  11. class IMaterialRendererServices;
  12. class SMaterial;
  13. //! Interface making it possible to set constants for gpu programs every frame.
  14. /** Implement this interface in an own class and pass a pointer to it to one of
  15. the methods in IGPUProgrammingServices when creating a shader. The
  16. OnSetConstants method will be called every frame now. */
  17. class IShaderConstantSetCallBack : public virtual IReferenceCounted
  18. {
  19. public:
  20. //! Called by the engine after a shader material has been created successfully
  21. /** If you are using one callback instance per shader (much recommended)
  22. this is a good place to get shader constant id's for high level shaders.
  23. \param services: Pointer to an interface providing methods to set/get the constants for the shader.
  24. \param userData: Userdata int which can be specified when creating the shader. */
  25. virtual void OnCreate(IMaterialRendererServices* services, s32 userData) { }
  26. //! Called to let the callBack know the used material (optional method)
  27. /**
  28. \code
  29. class MyCallBack : public IShaderConstantSetCallBack
  30. {
  31. const video::SMaterial *UsedMaterial;
  32. OnSetMaterial(const video::SMaterial& material)
  33. {
  34. UsedMaterial=&material;
  35. }
  36. OnSetConstants(IMaterialRendererServices* services, s32 userData)
  37. {
  38. services->setVertexShaderConstant("myColor", reinterpret_cast<f32*>(&UsedMaterial->color), 4);
  39. }
  40. }
  41. \endcode
  42. */
  43. virtual void OnSetMaterial(const SMaterial& material) { }
  44. //! Called by the engine when the vertex and/or pixel shader constants for an material renderer should be set.
  45. /**
  46. Implement the IShaderConstantSetCallBack in an own class and implement your own
  47. OnSetConstants method using the given IMaterialRendererServices interface.
  48. Pass a pointer to this class to one of the methods in IGPUProgrammingServices
  49. when creating a shader. The OnSetConstants method will now be called every time
  50. before geometry is being drawn using your shader material. A sample implementation
  51. would look like this:
  52. \code
  53. virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
  54. {
  55. video::IVideoDriver* driver = services->getVideoDriver();
  56. // set clip matrix at register 4
  57. core::matrix4 worldViewProj(driver->getTransform(video::ETS_PROJECTION));
  58. worldViewProj *= driver->getTransform(video::ETS_VIEW);
  59. worldViewProj *= driver->getTransform(video::ETS_WORLD);
  60. services->setVertexShaderConstant(&worldViewProj.M[0], 4, 4);
  61. // for high level shading languages, this would be another solution:
  62. //services->setVertexShaderConstant("mWorldViewProj", worldViewProj.M, 16);
  63. // set some light color at register 9
  64. video::SColorf col(0.0f,1.0f,1.0f,0.0f);
  65. services->setVertexShaderConstant(reinterpret_cast<const f32*>(&col), 9, 1);
  66. // for high level shading languages, this would be another solution:
  67. //services->setVertexShaderConstant("myColor", reinterpret_cast<f32*>(&col), 4);
  68. }
  69. \endcode
  70. \param services: Pointer to an interface providing methods to set the constants for the shader.
  71. \param userData: Userdata int which can be specified when creating the shader.
  72. */
  73. virtual void OnSetConstants(IMaterialRendererServices* services, s32 userData) = 0;
  74. };
  75. } // end namespace video
  76. } // end namespace irr
  77. #endif