BsLightRendering.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "BsLightRendering.h"
  2. #include "BsMaterial.h"
  3. #include "BsGpuParams.h"
  4. #include "BsLight.h"
  5. namespace BansheeEngine
  6. {
  7. void PerLightParams::setParameters(const LightCore* light)
  8. {
  9. // Note: I could just copy the data directly to the parameter buffer if I ensured the parameter
  10. // layout matches
  11. Vector4 positionAndType = (Vector4)light->getPosition();
  12. switch (light->getType())
  13. {
  14. case LightType::Directional:
  15. positionAndType.w = 0;
  16. break;
  17. case LightType::Point:
  18. positionAndType.w = 0.3f;
  19. break;
  20. case LightType::Spot:
  21. positionAndType.w = 0.8f;
  22. break;
  23. }
  24. mBuffer.gLightPositionAndType.set(positionAndType);
  25. Vector4 colorAndIntensity;
  26. colorAndIntensity.x = light->getColor().r;
  27. colorAndIntensity.y = light->getColor().g;
  28. colorAndIntensity.z = light->getColor().b;
  29. colorAndIntensity.w = light->getIntensity();
  30. mBuffer.gLightColorAndIntensity.set(colorAndIntensity);
  31. Vector2 spotAngles;
  32. spotAngles.x = light->getSpotFalloffAngle().valueDegrees();
  33. spotAngles.y = light->getSpotAngle().valueDegrees();
  34. mBuffer.gLightSpotAngles.set(spotAngles);
  35. mBuffer.gLightDirection.set(light->getRotation().zAxis());
  36. Vector4 lightGeometry;
  37. lightGeometry.x = 20; // Cone geometry sides
  38. lightGeometry.y = 10; // Cone geometry slices
  39. lightGeometry.z = light->getBounds().getRadius();
  40. lightGeometry.w = light->getSpotAngle().valueDegrees();
  41. mBuffer.gLightGeometry.set(lightGeometry);
  42. }
  43. const SPtr<GpuParamBlockBufferCore>& PerLightParams::getBuffer() const
  44. {
  45. return mBuffer.getBuffer();
  46. }
  47. void DirectionalLightMat::setParameters(const LightCore* light)
  48. {
  49. mParams.setParameters(light);
  50. }
  51. void DirectionalLightMat::initialize()
  52. {
  53. mMaterial->setParamBlockBuffer("PerLight", mParams.getBuffer());
  54. }
  55. void PointLightMat::setParameters(const LightCore* light)
  56. {
  57. mParams.setParameters(light);
  58. }
  59. void PointLightMat::initialize()
  60. {
  61. mMaterial->setParamBlockBuffer("PerLight", mParams.getBuffer());
  62. }
  63. }