BsLightRendering.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "BsLightRendering.h"
  2. #include "BsMaterial.h"
  3. #include "BsShader.h"
  4. #include "BsRenderBeast.h"
  5. #include "BsRenderTargets.h"
  6. #include "BsGpuParams.h"
  7. #include "BsLight.h"
  8. namespace BansheeEngine
  9. {
  10. void PerLightParams::setParameters(const LightCore* light)
  11. {
  12. // Note: I could just copy the data directly to the parameter buffer if I ensured the parameter
  13. // layout matches
  14. Vector4 positionAndType = (Vector4)light->getPosition();
  15. switch (light->getType())
  16. {
  17. case LightType::Directional:
  18. positionAndType.w = 0;
  19. break;
  20. case LightType::Point:
  21. positionAndType.w = 0.3f;
  22. break;
  23. case LightType::Spot:
  24. positionAndType.w = 0.8f;
  25. break;
  26. }
  27. mBuffer.gLightPositionAndType.set(positionAndType);
  28. Vector4 colorAndIntensity;
  29. colorAndIntensity.x = light->getColor().r;
  30. colorAndIntensity.y = light->getColor().g;
  31. colorAndIntensity.z = light->getColor().b;
  32. colorAndIntensity.w = light->getIntensity();
  33. mBuffer.gLightColorAndIntensity.set(colorAndIntensity);
  34. Vector2 spotAngles;
  35. spotAngles.x = light->getSpotFalloffAngle().valueDegrees();
  36. spotAngles.y = light->getSpotAngle().valueDegrees();
  37. mBuffer.gLightSpotAngles.set(spotAngles);
  38. mBuffer.gLightDirection.set(light->getRotation().zAxis());
  39. Vector4 lightGeometry;
  40. lightGeometry.x = 20; // Cone geometry sides
  41. lightGeometry.y = 10; // Cone geometry slices
  42. lightGeometry.z = light->getBounds().getRadius();
  43. lightGeometry.w = light->getSpotAngle().valueDegrees();
  44. mBuffer.gLightGeometry.set(lightGeometry);
  45. }
  46. const SPtr<GpuParamBlockBufferCore>& PerLightParams::getBuffer() const
  47. {
  48. return mBuffer.getBuffer();
  49. }
  50. DirectionalLightMat::DirectionalLightMat()
  51. {
  52. mMaterial->setParamBlockBuffer("PerLight", mParams.getBuffer());
  53. auto& texParams = mMaterial->getShader()->getTextureParams();
  54. for (auto& entry : texParams)
  55. {
  56. if (entry.second.rendererSemantic == RPS_GBufferA)
  57. mGBufferA = mMaterial->getParamTexture(entry.second.name);
  58. else if (entry.second.rendererSemantic == RPS_GBufferB)
  59. mGBufferB = mMaterial->getParamTexture(entry.second.name);
  60. else if (entry.second.rendererSemantic == RPS_GBufferDepth)
  61. mGBufferDepth = mMaterial->getParamTexture(entry.second.name);
  62. }
  63. }
  64. void DirectionalLightMat::setGBuffer(const SPtr<RenderTargets>& gbuffer)
  65. {
  66. mGBufferA.set(gbuffer->getTextureA());
  67. mGBufferB.set(gbuffer->getTextureB());
  68. mGBufferDepth.set(gbuffer->getTextureDepth());
  69. }
  70. void DirectionalLightMat::setParameters(const LightCore* light)
  71. {
  72. mParams.setParameters(light);
  73. }
  74. PointLightMat::PointLightMat()
  75. {
  76. mMaterial->setParamBlockBuffer("PerLight", mParams.getBuffer());
  77. auto& texParams = mMaterial->getShader()->getTextureParams();
  78. for (auto& entry : texParams)
  79. {
  80. if (entry.second.rendererSemantic == RPS_GBufferA)
  81. mGBufferA = mMaterial->getParamTexture(entry.second.name);
  82. else if (entry.second.rendererSemantic == RPS_GBufferB)
  83. mGBufferB = mMaterial->getParamTexture(entry.second.name);
  84. else if (entry.second.rendererSemantic == RPS_GBufferDepth)
  85. mGBufferDepth = mMaterial->getParamTexture(entry.second.name);
  86. }
  87. }
  88. void PointLightMat::setGBuffer(const SPtr<RenderTargets>& gbuffer)
  89. {
  90. mGBufferA.set(gbuffer->getTextureA());
  91. mGBufferB.set(gbuffer->getTextureB());
  92. mGBufferDepth.set(gbuffer->getTextureDepth());
  93. }
  94. void PointLightMat::setParameters(const LightCore* light)
  95. {
  96. mParams.setParameters(light);
  97. }
  98. }