IShaderConstantSetCallBack.h 3.1 KB

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