BsLightRendering.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "BsLightRendering.h"
  2. #include "BsMaterial.h"
  3. #include "BsGpuParams.h"
  4. #include "BsLight.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * Manipulates PerLight parameter buffer used in various shaders.
  9. */
  10. class PerLightParamBuffer
  11. {
  12. public:
  13. // TODO - Doc
  14. void initialize()
  15. {
  16. // TODO - This is never called
  17. // TODO - Create the buffer (think of an automated way of doing it, creating the desc and retrieving the params.
  18. mParams->getParam("gLightPositionAndType", mParamLightPositionAndType);
  19. mParams->getParam("gLightColorAndIntensity", mParamLightColorAndIntensity);
  20. mParams->getParam("gLightSpotAngles", mParamLightSpotAngles);
  21. mParams->getParam("gLightDirection", mParamLightDirection);
  22. mParams->getParam("gLightGeometry", mParamLightGeometry);
  23. }
  24. // TODO - Doc
  25. void setParameters(const LightCore* light)
  26. {
  27. // Note: I could just copy the data directly to the parameter buffer if I ensured the parameter
  28. // layout matches
  29. Vector4 positionAndType = (Vector4)light->getPosition();
  30. switch (light->getType())
  31. {
  32. case LightType::Directional:
  33. positionAndType.w = 0;
  34. break;
  35. case LightType::Point:
  36. positionAndType.w = 0.3f;
  37. break;
  38. case LightType::Spot:
  39. positionAndType.w = 0.8f;
  40. break;
  41. }
  42. mParamLightPositionAndType.set(positionAndType);
  43. Vector4 colorAndIntensity;
  44. colorAndIntensity.x = light->getColor().r;
  45. colorAndIntensity.y = light->getColor().g;
  46. colorAndIntensity.z = light->getColor().b;
  47. colorAndIntensity.w = light->getIntensity();
  48. mParamLightColorAndIntensity.set(colorAndIntensity);
  49. Vector2 spotAngles;
  50. spotAngles.x = light->getSpotFalloffAngle().valueDegrees();
  51. spotAngles.y = light->getSpotAngle().valueDegrees();
  52. mParamLightSpotAngles.set(spotAngles);
  53. mParamLightDirection.set(light->getRotation().zAxis());
  54. Vector4 lightGeometry;
  55. lightGeometry.x = 20; // Cone geometry sides
  56. lightGeometry.y = 10; // Cone geometry slices
  57. lightGeometry.z = light->getBounds().getRadius();
  58. lightGeometry.w = light->getSpotAngle().valueDegrees();
  59. mParamLightGeometry.set(lightGeometry);
  60. }
  61. // TODO - Doc
  62. SPtr<GpuParamBlockBufferCore> getBuffer()
  63. {
  64. return mParams->getParamBlockBuffer(0);
  65. }
  66. static PerLightParamBuffer instance;
  67. private:
  68. SPtr<GpuParamsCore> mParams;
  69. GpuParamVec4Core mParamLightPositionAndType;
  70. GpuParamVec4Core mParamLightColorAndIntensity;
  71. GpuParamVec2Core mParamLightSpotAngles;
  72. GpuParamVec3Core mParamLightDirection;
  73. GpuParamVec4Core mParamLightGeometry;
  74. };
  75. PerLightParamBuffer PerLightParamBuffer::instance;
  76. void DirectionalLightMat::setParameters(const LightCore* light)
  77. {
  78. PerLightParamBuffer::instance.setParameters(light);
  79. }
  80. void DirectionalLightMat::initialize()
  81. {
  82. mMaterial->setParamBlockBuffer("PerLight", PerLightParamBuffer::instance.getBuffer());
  83. }
  84. void PointLightMat::setParameters(const LightCore* light)
  85. {
  86. PerLightParamBuffer::instance.setParameters(light);
  87. }
  88. void PointLightMat::initialize()
  89. {
  90. mMaterial->setParamBlockBuffer("PerLight", PerLightParamBuffer::instance.getBuffer());
  91. }
  92. }