BsLightRendering.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 = light->getType() == LightType::Spot ? (float)LightCore::LIGHT_CONE_NUM_SIDES : 0;
  41. lightGeometry.y = (float)LightCore::LIGHT_CONE_NUM_SLICES;
  42. lightGeometry.z = light->getBounds().getRadius();
  43. float coneRadius = Math::sin(light->getSpotAngle()) * light->getRange();
  44. lightGeometry.w = coneRadius;
  45. mBuffer.gLightGeometry.set(lightGeometry);
  46. Matrix4 transform = Matrix4::TRS(light->getPosition(), light->getRotation(), Vector3::ONE);
  47. mBuffer.gMatConeTransform.set(transform);
  48. }
  49. const SPtr<GpuParamBlockBufferCore>& PerLightParams::getBuffer() const
  50. {
  51. return mBuffer.getBuffer();
  52. }
  53. DirectionalLightMat::DirectionalLightMat()
  54. {
  55. mMaterial->setParamBlockBuffer("PerLight", mParams.getBuffer());
  56. auto& texParams = mMaterial->getShader()->getTextureParams();
  57. for (auto& entry : texParams)
  58. {
  59. if (entry.second.rendererSemantic == RPS_GBufferA)
  60. mGBufferA = mMaterial->getParamTexture(entry.second.name);
  61. else if (entry.second.rendererSemantic == RPS_GBufferB)
  62. mGBufferB = mMaterial->getParamTexture(entry.second.name);
  63. else if (entry.second.rendererSemantic == RPS_GBufferDepth)
  64. mGBufferDepth = mMaterial->getParamTexture(entry.second.name);
  65. }
  66. }
  67. void DirectionalLightMat::setGBuffer(const SPtr<RenderTargets>& gbuffer)
  68. {
  69. mGBufferA.set(gbuffer->getTextureA());
  70. mGBufferB.set(gbuffer->getTextureB());
  71. mGBufferDepth.set(gbuffer->getTextureDepth());
  72. }
  73. void DirectionalLightMat::setParameters(const LightCore* light)
  74. {
  75. mParams.setParameters(light);
  76. }
  77. PointLightMat::PointLightMat()
  78. {
  79. mMaterial->setParamBlockBuffer("PerLight", mParams.getBuffer());
  80. auto& texParams = mMaterial->getShader()->getTextureParams();
  81. for (auto& entry : texParams)
  82. {
  83. if (entry.second.rendererSemantic == RPS_GBufferA)
  84. mGBufferA = mMaterial->getParamTexture(entry.second.name);
  85. else if (entry.second.rendererSemantic == RPS_GBufferB)
  86. mGBufferB = mMaterial->getParamTexture(entry.second.name);
  87. else if (entry.second.rendererSemantic == RPS_GBufferDepth)
  88. mGBufferDepth = mMaterial->getParamTexture(entry.second.name);
  89. }
  90. }
  91. void PointLightMat::setGBuffer(const SPtr<RenderTargets>& gbuffer)
  92. {
  93. mGBufferA.set(gbuffer->getTextureA());
  94. mGBufferB.set(gbuffer->getTextureB());
  95. mGBufferDepth.set(gbuffer->getTextureDepth());
  96. }
  97. void PointLightMat::setParameters(const LightCore* light)
  98. {
  99. mParams.setParameters(light);
  100. }
  101. }