IShaderConstantSetCallBack.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #pragma once
  5. #include "IReferenceCounted.h"
  6. namespace irr
  7. {
  8. namespace video
  9. {
  10. class IMaterialRendererServices;
  11. class SMaterial;
  12. //! Interface making it possible to set constants for gpu programs every frame.
  13. /** Implement this interface in an own class and pass a pointer to it to one of
  14. the methods in IGPUProgrammingServices when creating a shader. The
  15. OnSetConstants method will be called every frame now. */
  16. class IShaderConstantSetCallBack : public virtual IReferenceCounted
  17. {
  18. public:
  19. //! Called to let the callBack know the used material (optional method)
  20. /**
  21. \code
  22. class MyCallBack : public IShaderConstantSetCallBack
  23. {
  24. const video::SMaterial *UsedMaterial;
  25. OnSetMaterial(const video::SMaterial& material)
  26. {
  27. UsedMaterial=&material;
  28. }
  29. OnSetConstants(IMaterialRendererServices* services, s32 userData)
  30. {
  31. services->setVertexShaderConstant("myColor", reinterpret_cast<f32*>(&UsedMaterial->color), 4);
  32. }
  33. }
  34. \endcode
  35. */
  36. virtual void OnSetMaterial(const SMaterial &material) {}
  37. //! Called by the engine when the vertex and/or pixel shader constants for an material renderer should be set.
  38. /**
  39. Implement the IShaderConstantSetCallBack in an own class and implement your own
  40. OnSetConstants method using the given IMaterialRendererServices interface.
  41. Pass a pointer to this class to one of the methods in IGPUProgrammingServices
  42. when creating a shader. The OnSetConstants method will now be called every time
  43. before geometry is being drawn using your shader material. A sample implementation
  44. would look like this:
  45. \code
  46. virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
  47. {
  48. video::IVideoDriver* driver = services->getVideoDriver();
  49. // set clip matrix at register 4
  50. core::matrix4 worldViewProj(driver->getTransform(video::ETS_PROJECTION));
  51. worldViewProj *= driver->getTransform(video::ETS_VIEW);
  52. worldViewProj *= driver->getTransform(video::ETS_WORLD);
  53. services->setVertexShaderConstant(&worldViewProj.M[0], 4, 4);
  54. // for high level shading languages, this would be another solution:
  55. //services->setVertexShaderConstant("mWorldViewProj", worldViewProj.M, 16);
  56. // set some light color at register 9
  57. video::SColorf col(0.0f,1.0f,1.0f,0.0f);
  58. services->setVertexShaderConstant(reinterpret_cast<const f32*>(&col), 9, 1);
  59. // for high level shading languages, this would be another solution:
  60. //services->setVertexShaderConstant("myColor", reinterpret_cast<f32*>(&col), 4);
  61. }
  62. \endcode
  63. \param services: Pointer to an interface providing methods to set the constants for the shader.
  64. \param userData: Userdata int which can be specified when creating the shader.
  65. */
  66. virtual void OnSetConstants(IMaterialRendererServices *services, s32 userData) = 0;
  67. };
  68. } // end namespace video
  69. } // end namespace irr