2
0

BsLightRendering.cpp 4.0 KB

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