Light2D.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 SetLightGroupID(int id) { lightgroupID_ = id; }
  36. int GetLightGroupID() const { return lightgroupID_; }
  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. int GetNumRays() const { return (int) 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 OnSceneSet(Scene* scene);
  55. void CastRays();
  56. int lightgroupID_;
  57. WeakPtr<Light2DGroup> lightgroup_;
  58. Color color_;
  59. bool castShadows_;
  60. bool softShadows_;
  61. bool backtrace_;
  62. float softShadowLength_;
  63. PODVector<Light2DRay> rays_;
  64. bool raysInitialized_;
  65. Vector<Vertex2D> vertices_;
  66. LightType2D lightType_;
  67. };
  68. class ATOMIC_API DirectionalLight2D : public Light2D
  69. {
  70. OBJECT(DirectionalLight2D);
  71. public:
  72. /// Construct.
  73. DirectionalLight2D(Context* context);
  74. /// Destruct.
  75. virtual ~DirectionalLight2D();
  76. /// Register object factory
  77. static void RegisterObject(Context* context);
  78. virtual void UpdateVertices();
  79. float GetDirection() const { return direction_; }
  80. void SetDirection(float direction) { direction_ = direction; }
  81. protected:
  82. float direction_;
  83. };
  84. class ATOMIC_API PositionalLight2D : public Light2D
  85. {
  86. OBJECT(PositionalLight2D);
  87. public:
  88. /// Construct.
  89. PositionalLight2D(Context* context);
  90. /// Destruct.
  91. virtual ~PositionalLight2D();
  92. /// Register object factory
  93. static void RegisterObject(Context* context);
  94. virtual void UpdateVertices();
  95. protected:
  96. };
  97. class ATOMIC_API PointLight2D : public PositionalLight2D
  98. {
  99. OBJECT(PointLight2D);
  100. public:
  101. /// Construct.
  102. PointLight2D(Context* context);
  103. /// Destruct.
  104. virtual ~PointLight2D();
  105. /// Register object factory
  106. static void RegisterObject(Context* context);
  107. virtual void UpdateVertices();
  108. void SetRadius(float radius) { radius_ = radius; }
  109. float GetRadius() const { return radius_; }
  110. protected:
  111. float radius_;
  112. };
  113. class ATOMIC_API Light2DGroup : public Drawable2D
  114. {
  115. OBJECT(Light2DGroup);
  116. public:
  117. /// Construct.
  118. Light2DGroup(Context* context);
  119. /// Destruct.
  120. virtual ~Light2DGroup();
  121. /// Register object factory. drawable2d must be registered first.
  122. static void RegisterObject(Context* context);
  123. PhysicsWorld2D* GetPhysicsWorld() { return physicsWorld_; }
  124. void AddLight2D(Light2D* light);
  125. void RemoveLight2D(Light2D* light);
  126. Vector<WeakPtr<Light2D> >& GetLights() { return lights_; }
  127. void SetDirty() { /*verticesDirty_ = true;*/ }
  128. void SetAmbientColor(const Color& color);
  129. const Color& GetAmbientColor() { return ambientColor_; }
  130. void SetLightGroupID(int id) { lightgroupID_ = id; }
  131. int GetLightGroupID() const { return lightgroupID_; }
  132. const BoundingBox& GetFrustumBox() const { return frustumBoundingBox_; }
  133. protected:
  134. /// Recalculate the world-space bounding box.
  135. void OnWorldBoundingBoxUpdate();
  136. void OnSceneSet(Scene* scene);
  137. /// Handle draw order changed.
  138. virtual void OnDrawOrderChanged();
  139. /// Update source batches.
  140. virtual void UpdateSourceBatches();
  141. private:
  142. void HandleBeginRendering(StringHash eventType, VariantMap& eventData);
  143. void HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData);
  144. void CreateLight2DMaterial();
  145. int lightgroupID_;
  146. Color ambientColor_;
  147. Vector<WeakPtr<Light2D> > lights_;
  148. SharedPtr<Material> shadow2DMaterial_;
  149. SharedPtr<Material> light2DMaterial_;
  150. WeakPtr<PhysicsWorld2D> physicsWorld_;
  151. /// Frustum for current frame.
  152. const Frustum* frustum_;
  153. /// Frustum bounding box for current frame.
  154. BoundingBox frustumBoundingBox_;
  155. };
  156. }