Light2D.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #pragma once
  5. #include "../Graphics/Texture2D.h"
  6. #include "../Atomic2D/Drawable2D.h"
  7. #include "../Atomic2D/PhysicsWorld2D.h"
  8. namespace Atomic
  9. {
  10. class Light2DGroup;
  11. enum LightType2D
  12. {
  13. LIGHT2D_DIRECTIONAL,
  14. LIGHT2D_POINT,
  15. };
  16. struct Light2DRay
  17. {
  18. float cos_;
  19. float sin_;
  20. Vector2 start_;
  21. Vector2 end_;
  22. // where along ray there was a collision if any
  23. float fraction_;
  24. };
  25. class ATOMIC_API Light2D : public Component
  26. {
  27. OBJECT(Light2D);
  28. public:
  29. /// Construct.
  30. Light2D(Context* context);
  31. /// Destruct.
  32. virtual ~Light2D();
  33. /// Register object factory
  34. static void RegisterObject(Context* context);
  35. void SetLightGroup(Light2DGroup* group) { lightgroup_ = group; }
  36. Light2DGroup* GetLightGroup() { return lightgroup_; }
  37. const Color& GetColor() const { return color_; }
  38. void SetColor(const Color& color) { color_ = color; }
  39. void AddVertices(Vector<Vertex2D>& vertices);
  40. virtual void UpdateVertices() {}
  41. void SetNumRays(int numRays);
  42. unsigned GetNumRays() const { return rays_.Size(); }
  43. virtual void OnSetEnabled();
  44. LightType2D GetLightType() { return lightType_; }
  45. bool GetCastShadows() const { return castShadows_; }
  46. void SetCastShadows(bool castShadows) { castShadows_ = castShadows; }
  47. bool GetSoftShadows() const { return softShadows_; }
  48. void SetSoftShadows(bool softShadows) { softShadows_ = softShadows; }
  49. float GetSoftShadowLength() const { return softShadowLength_; }
  50. void SetSoftShadowLength(float softShadowLength) { softShadowLength_ = softShadowLength; }
  51. bool GetBacktrace() const { return backtrace_; }
  52. void SetBacktrace(bool backtrace) { backtrace_ = backtrace; }
  53. protected:
  54. void CastRays();
  55. WeakPtr<Light2DGroup> lightgroup_;
  56. Color color_;
  57. bool castShadows_;
  58. bool softShadows_;
  59. bool backtrace_;
  60. float softShadowLength_;
  61. PODVector<Light2DRay> rays_;
  62. Vector<Vertex2D> vertices_;
  63. LightType2D lightType_;
  64. };
  65. class ATOMIC_API DirectionalLight2D : public Light2D
  66. {
  67. OBJECT(DirectionalLight2D);
  68. public:
  69. /// Construct.
  70. DirectionalLight2D(Context* context);
  71. /// Destruct.
  72. virtual ~DirectionalLight2D();
  73. /// Register object factory
  74. static void RegisterObject(Context* context);
  75. virtual void UpdateVertices();
  76. float GetDirection() const { return direction_; }
  77. void SetDirection(float direction) { direction_ = direction; }
  78. protected:
  79. float direction_;
  80. };
  81. class ATOMIC_API PositionalLight2D : public Light2D
  82. {
  83. OBJECT(PositionalLight2D);
  84. public:
  85. /// Construct.
  86. PositionalLight2D(Context* context);
  87. /// Destruct.
  88. virtual ~PositionalLight2D();
  89. /// Register object factory
  90. static void RegisterObject(Context* context);
  91. virtual void UpdateVertices();
  92. protected:
  93. };
  94. class ATOMIC_API PointLight2D : public PositionalLight2D
  95. {
  96. OBJECT(PointLight2D);
  97. public:
  98. /// Construct.
  99. PointLight2D(Context* context);
  100. /// Destruct.
  101. virtual ~PointLight2D();
  102. /// Register object factory
  103. static void RegisterObject(Context* context);
  104. virtual void UpdateVertices();
  105. void SetRadius(float radius) { radius_ = radius; }
  106. float GetRadius() const { return radius_; }
  107. protected:
  108. float radius_;
  109. };
  110. class ATOMIC_API Light2DGroup : public Drawable2D
  111. {
  112. OBJECT(Light2DGroup);
  113. public:
  114. /// Construct.
  115. Light2DGroup(Context* context);
  116. /// Destruct.
  117. virtual ~Light2DGroup();
  118. /// Register object factory. drawable2d must be registered first.
  119. static void RegisterObject(Context* context);
  120. void SetPhysicsWorld(PhysicsWorld2D* physicsWorld);
  121. PhysicsWorld2D* GetPhysicsWorld() { return physicsWorld_; }
  122. void AddLight2D(Light2D* light);
  123. Vector<WeakPtr<Light2D> >& GetLights() { return lights_; }
  124. void SetDirty() { /*verticesDirty_ = true;*/ }
  125. void SetAmbientColor(const Color& color);
  126. const Color& GetAmbientColor() { return ambientColor_; }
  127. const BoundingBox& GetFrustumBox() const { return frustumBoundingBox_; }
  128. protected:
  129. /// Recalculate the world-space bounding box.
  130. void OnWorldBoundingBoxUpdate();
  131. void OnNodeSet(Node* node);
  132. /// Handle draw order changed.
  133. virtual void OnDrawOrderChanged();
  134. /// Update source batches.
  135. virtual void UpdateSourceBatches();
  136. private:
  137. Color ambientColor_;
  138. void HandleBeginRendering(StringHash eventType, VariantMap& eventData);
  139. void HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData);
  140. void CreateLight2DMaterial();
  141. Vector<WeakPtr<Light2D> > lights_;
  142. SharedPtr<Material> shadow2DMaterial_;
  143. SharedPtr<Material> light2DMaterial_;
  144. WeakPtr<PhysicsWorld2D> physicsWorld_;
  145. /// Frustum for current frame.
  146. const Frustum* frustum_;
  147. /// Frustum bounding box for current frame.
  148. BoundingBox frustumBoundingBox_;
  149. };
  150. }