Light2D.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/Texture2D.h"
  24. #include "../Atomic2D/Drawable2D.h"
  25. #include "../Atomic2D/PhysicsWorld2D.h"
  26. namespace Atomic
  27. {
  28. class Light2DGroup;
  29. enum LightType2D
  30. {
  31. LIGHT2D_DIRECTIONAL,
  32. LIGHT2D_POINT,
  33. };
  34. struct Light2DRay
  35. {
  36. float cos_;
  37. float sin_;
  38. Vector2 start_;
  39. Vector2 end_;
  40. // where along ray there was a collision if any
  41. float fraction_;
  42. };
  43. class ATOMIC_API Light2D : public Component
  44. {
  45. ATOMIC_OBJECT(Light2D, Component);
  46. public:
  47. /// Construct.
  48. Light2D(Context* context);
  49. /// Destruct.
  50. virtual ~Light2D();
  51. /// Register object factory
  52. static void RegisterObject(Context* context);
  53. void SetLightGroupID(int id) { lightgroupID_ = id; }
  54. int GetLightGroupID() const { return lightgroupID_; }
  55. const Color& GetColor() const { return color_; }
  56. void SetColor(const Color& color) { color_ = color; }
  57. void AddVertices(Vector<Vertex2D> &vertices);
  58. virtual void UpdateVertices() {}
  59. void SetNumRays(int numRays);
  60. int GetNumRays() const { return (int) rays_.Size(); }
  61. virtual void OnSetEnabled();
  62. LightType2D GetLightType() { return lightType_; }
  63. bool GetCastShadows() const { return castShadows_; }
  64. void SetCastShadows(bool castShadows) { castShadows_ = castShadows; }
  65. bool GetSoftShadows() const { return softShadows_; }
  66. void SetSoftShadows(bool softShadows) { softShadows_ = softShadows; }
  67. float GetSoftShadowLength() const { return softShadowLength_; }
  68. void SetSoftShadowLength(float softShadowLength) { softShadowLength_ = softShadowLength; }
  69. bool GetBacktrace() const { return backtrace_; }
  70. void SetBacktrace(bool backtrace) { backtrace_ = backtrace; }
  71. protected:
  72. void OnSceneSet(Scene* scene);
  73. void CastRays();
  74. int lightgroupID_;
  75. WeakPtr<Light2DGroup> lightgroup_;
  76. Color color_;
  77. bool castShadows_;
  78. bool softShadows_;
  79. bool backtrace_;
  80. float softShadowLength_;
  81. PODVector<Light2DRay> rays_;
  82. bool raysInitialized_;
  83. Vector<Vertex2D> vertices_;
  84. LightType2D lightType_;
  85. };
  86. class ATOMIC_API DirectionalLight2D : public Light2D
  87. {
  88. ATOMIC_OBJECT(DirectionalLight2D, Light2D);
  89. public:
  90. /// Construct.
  91. DirectionalLight2D(Context* context);
  92. /// Destruct.
  93. virtual ~DirectionalLight2D();
  94. /// Register object factory
  95. static void RegisterObject(Context* context);
  96. virtual void UpdateVertices();
  97. float GetDirection() const { return direction_; }
  98. void SetDirection(float direction) { direction_ = direction; }
  99. protected:
  100. float direction_;
  101. };
  102. class ATOMIC_API PositionalLight2D : public Light2D
  103. {
  104. ATOMIC_OBJECT(PositionalLight2D, Light2D);
  105. public:
  106. /// Construct.
  107. PositionalLight2D(Context* context);
  108. /// Destruct.
  109. virtual ~PositionalLight2D();
  110. /// Register object factory
  111. static void RegisterObject(Context* context);
  112. virtual void UpdateVertices();
  113. protected:
  114. };
  115. class ATOMIC_API PointLight2D : public PositionalLight2D
  116. {
  117. ATOMIC_OBJECT(PointLight2D, PositionalLight2D);
  118. public:
  119. /// Construct.
  120. PointLight2D(Context* context);
  121. /// Destruct.
  122. virtual ~PointLight2D();
  123. /// Register object factory
  124. static void RegisterObject(Context* context);
  125. virtual void UpdateVertices();
  126. void SetRadius(float radius) { radius_ = radius; }
  127. float GetRadius() const { return radius_; }
  128. protected:
  129. float radius_;
  130. };
  131. class ATOMIC_API Light2DGroup : public Drawable2D
  132. {
  133. ATOMIC_OBJECT(Light2DGroup, Drawable2D);
  134. public:
  135. /// Construct.
  136. Light2DGroup(Context* context);
  137. /// Destruct.
  138. virtual ~Light2DGroup();
  139. /// Register object factory. drawable2d must be registered first.
  140. static void RegisterObject(Context* context);
  141. PhysicsWorld2D* GetPhysicsWorld() { return physicsWorld_; }
  142. void AddLight2D(Light2D* light);
  143. void RemoveLight2D(Light2D* light);
  144. Vector<WeakPtr<Light2D> >& GetLights() { return lights_; }
  145. void SetDirty() { /*verticesDirty_ = true;*/ }
  146. void SetAmbientColor(const Color& color);
  147. const Color& GetAmbientColor() { return ambientColor_; }
  148. void SetLightGroupID(int id) { lightgroupID_ = id; }
  149. int GetLightGroupID() const { return lightgroupID_; }
  150. const BoundingBox& GetFrustumBox() const { return frustumBoundingBox_; }
  151. protected:
  152. /// Recalculate the world-space bounding box.
  153. void OnWorldBoundingBoxUpdate();
  154. void OnSceneSet(Scene* scene);
  155. /// Handle draw order changed.
  156. virtual void OnDrawOrderChanged();
  157. /// Update source batches.
  158. virtual void UpdateSourceBatches();
  159. private:
  160. void HandleBeginRendering(StringHash eventType, VariantMap& eventData);
  161. void HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData);
  162. void CreateLight2DMaterial();
  163. int lightgroupID_;
  164. Color ambientColor_;
  165. Vector<WeakPtr<Light2D> > lights_;
  166. SharedPtr<Material> shadow2DMaterial_;
  167. SharedPtr<Material> light2DMaterial_;
  168. WeakPtr<PhysicsWorld2D> physicsWorld_;
  169. /// Frustum for current frame.
  170. const Frustum* frustum_;
  171. /// Frustum bounding box for current frame.
  172. BoundingBox frustumBoundingBox_;
  173. };
  174. }