Light.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "anki/scene/Light.h"
  2. namespace anki {
  3. //==============================================================================
  4. // LightComponent =
  5. //==============================================================================
  6. //==============================================================================
  7. LightComponent::LightComponent(Light* node)
  8. : SceneComponent(LIGHT_COMPONENT, node)
  9. {}
  10. //==============================================================================
  11. // Light =
  12. //==============================================================================
  13. //==============================================================================
  14. Light::Light(
  15. const char* name, SceneGraph* scene, // SceneNode
  16. LightType t) // Self
  17. : SceneNode(name, scene),
  18. LightComponent(this),
  19. MoveComponent(this),
  20. SpatialComponent(this),
  21. type(t)
  22. {
  23. // Init components
  24. addComponent(static_cast<LightComponent*>(this));
  25. addComponent(static_cast<MoveComponent*>(this));
  26. addComponent(static_cast<SpatialComponent*>(this));
  27. }
  28. //==============================================================================
  29. Light::~Light()
  30. {}
  31. //==============================================================================
  32. void Light::frustumUpdate()
  33. {
  34. // Update the frustums
  35. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr)
  36. {
  37. fr.setProjectionMatrix(fr.getFrustum().calculateProjectionMatrix());
  38. fr.setViewProjectionMatrix(
  39. fr.getProjectionMatrix() * fr.getViewMatrix());
  40. fr.markForUpdate();
  41. });
  42. // Mark the spatial for update
  43. SpatialComponent& sp = getComponent<SpatialComponent>();
  44. sp.markForUpdate();
  45. }
  46. //==============================================================================
  47. void Light::moveUpdate(MoveComponent& move)
  48. {
  49. // Update the frustums
  50. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr)
  51. {
  52. fr.setProjectionMatrix(fr.getFrustum().calculateProjectionMatrix());
  53. fr.setViewMatrix(Mat4(move.getWorldTransform().getInverse()));
  54. fr.setViewProjectionMatrix(
  55. fr.getProjectionMatrix() * fr.getViewMatrix());
  56. fr.getFrustum().resetTransform();
  57. fr.getFrustum().transform(move.getWorldTransform());
  58. fr.markForUpdate();
  59. });
  60. // Update the spatial
  61. SpatialComponent& sp = getComponent<SpatialComponent>();
  62. sp.markForUpdate();
  63. }
  64. //==============================================================================
  65. // PointLight =
  66. //==============================================================================
  67. //==============================================================================
  68. PointLight::PointLight(const char* name, SceneGraph* scene)
  69. : Light(name, scene, LT_POINT)
  70. {}
  71. //==============================================================================
  72. void PointLight::componentUpdated(SceneComponent& comp,
  73. SceneComponent::UpdateType)
  74. {
  75. if(comp.getType() == MoveComponent::getClassType())
  76. {
  77. MoveComponent& move = comp.downCast<MoveComponent>();
  78. sphereW.setCenter(move.getWorldTransform().getOrigin());
  79. moveUpdate(move);
  80. }
  81. }
  82. //==============================================================================
  83. // SpotLight =
  84. //==============================================================================
  85. //==============================================================================
  86. SpotLight::SpotLight(const char* name, SceneGraph* scene)
  87. : Light(name, scene, LT_SPOT),
  88. FrustumComponent(this, &m_frustum)
  89. {
  90. // Init components
  91. addComponent(static_cast<FrustumComponent*>(this));
  92. const F32 ang = toRad(45.0);
  93. const F32 dist = 1.0;
  94. m_frustum.setAll(ang, ang, 0.1, dist);
  95. }
  96. //==============================================================================
  97. void SpotLight::componentUpdated(SceneComponent& comp,
  98. SceneComponent::UpdateType)
  99. {
  100. if(comp.getType() == MoveComponent::getClassType())
  101. {
  102. MoveComponent& move = comp.downCast<MoveComponent>();
  103. m_frustum.resetTransform();
  104. m_frustum.transform(move.getWorldTransform());
  105. moveUpdate(move);
  106. }
  107. }
  108. } // end namespace anki