BsLight.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsIReflectable.h"
  6. #include "BsVector3.h"
  7. #include "BsQuaternion.h"
  8. #include "BsColor.h"
  9. #include "BsSphere.h"
  10. #include "BsCoreObject.h"
  11. namespace bs
  12. {
  13. /** @addtogroup Renderer-Engine-Internal
  14. * @{
  15. */
  16. /** Light type that determines how is light information parsed by the renderer and other systems. */
  17. enum class LightType
  18. {
  19. Directional,
  20. Point,
  21. Spot
  22. };
  23. /** Signals which portion of a LightInternal is dirty. */
  24. enum class LightDirtyFlag
  25. {
  26. Transform = 0x01,
  27. Everything = 0x02
  28. };
  29. /** @} */
  30. /** @addtogroup Implementation
  31. * @{
  32. */
  33. /**
  34. * Illuminates a portion of the scene covered by a light. Base class for both sim and core thread Light implementations.
  35. */
  36. class BS_CORE_EXPORT LightBase
  37. {
  38. public:
  39. LightBase();
  40. LightBase(LightType type, Color color, float intensity, float range,
  41. bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  42. virtual ~LightBase() { }
  43. /** Returns the position of the light, in world space. */
  44. Vector3 getPosition() const { return mPosition; }
  45. /** Sets the position of the light, in world space. */
  46. void setPosition(const Vector3& position) { mPosition = position; _markCoreDirty(); updateBounds(); }
  47. /** Returns the rotation of the light, in world space. */
  48. Quaternion getRotation() const { return mRotation; }
  49. /** Sets the rotation of the light, in world space. */
  50. void setRotation(const Quaternion& rotation) { mRotation = rotation; _markCoreDirty(); updateBounds(); }
  51. /** Returns the type of the light. */
  52. LightType getType() const { return mType; }
  53. /** Changes the type of the light. */
  54. void setType(LightType type) { mType = type; _markCoreDirty(); updateBounds(); }
  55. /** Checks does this light cast shadows when rendered. */
  56. bool getCastsShadow() const { return mCastsShadows; }
  57. /** Sets whether this light will cast shadows when rendered. */
  58. void setCastsShadow(bool castsShadow) { mCastsShadows = castsShadow; _markCoreDirty(); }
  59. /** Returns the color emitted from the light. */
  60. Color getColor() const { return mColor; }
  61. /** Sets the color emitted from the light. */
  62. void setColor(const Color& color) { mColor = color; _markCoreDirty(); }
  63. /** Returns the maximum range of the light. Light will not affect any geometry past that point. */
  64. float getRange() const { return mRange; }
  65. /**
  66. * Sets the maximum range of the light. Light will not affect any geometry past that point. Value is ignored if
  67. * physically based attenuation is active.
  68. *
  69. * @note Normally you want to set this at a point where light intensity is too low due to attenuation.
  70. */
  71. void setRange(float range);
  72. /**
  73. * Checks is physically based attenuation active. If true the range and attenuation of the light are controlled
  74. * by inverse square of distance. If false then the user is allowed to set the range and attenuation is adjusted
  75. * accordingly.
  76. */
  77. bool getPhysicallyBasedAttenuation() const { return mPhysCorrectAtten; }
  78. /**
  79. * Activates or deactivates physically based attenuation. If true the range and attenuation of the light are
  80. * controlled by inverse square of distance. If false then the user is allowed to set the range and attenuation is
  81. * adjusted accordingly.
  82. */
  83. void setPhysicallyBasedAttenuation(bool enabled);
  84. /**
  85. * Gets the power of the light source. This is luminous flux for point & spot lights, and radiance for directional
  86. * lights.
  87. */
  88. float getIntensity() const { return mIntensity; }
  89. /**
  90. * Sets the power of the light source. This will be luminous flux for point & spot lights, and radiance for
  91. * directional lights.
  92. */
  93. void setIntensity(float intensity);
  94. /** Gets the total angle covered by a spot light. */
  95. Degree getSpotAngle() const { return mSpotAngle; }
  96. /** Sets the total angle covered by a spot light. */
  97. void setSpotAngle(const Degree& spotAngle) { mSpotAngle = spotAngle; _markCoreDirty(); updateBounds(); }
  98. /**
  99. * Gets the falloff angle covered by a spot light. Falloff angle determines at what point does light intensity
  100. * starts quadratically falling off as the angle approaches the total spot angle.
  101. */
  102. Degree getSpotFalloffAngle() const { return mSpotFalloffAngle; }
  103. /**
  104. * Sets the falloff angle covered by a spot light. Falloff angle determines at what point does light intensity
  105. * starts quadratically falling off as the angle approaches the total spot angle.
  106. */
  107. void setSpotFalloffAngle(const Degree& spotFallofAngle)
  108. { mSpotFalloffAngle = spotFallofAngle; _markCoreDirty(); updateBounds(); }
  109. /** Returns world space bounds that completely encompass the light's area of influence. */
  110. Sphere getBounds() const { return mBounds; }
  111. /**
  112. * Returns the radiance of the light source. This is the value that should be used in lighting equations.
  113. *
  114. * @note
  115. * Ignores the light direction, therefore caller must ensure to properly handle non-uniform emitters like spot
  116. * lights.
  117. */
  118. float getRadiance() const;
  119. /** Checks whether the light should be rendered or not. */
  120. bool getIsActive() const { return mIsActive; }
  121. /** Sets whether the light should be rendered or not. */
  122. void setIsActive(bool active) { mIsActive = active; _markCoreDirty(); }
  123. /**
  124. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  125. * thread counterpart.
  126. */
  127. virtual void _markCoreDirty(LightDirtyFlag flag = LightDirtyFlag::Everything) { }
  128. protected:
  129. /** Updates the internal bounds for the light. Call this whenever a property affecting the bounds changes. */
  130. void updateBounds();
  131. /** Calculates maximum light range based on light intensity. */
  132. void updatePhysicallyCorrectRange();
  133. Vector3 mPosition; /**< World space position. */
  134. Quaternion mRotation; /**< World space rotation. */
  135. LightType mType; /**< Type of light that determines how are the rest of the parameters interpreted. */
  136. bool mCastsShadows; /**< Determines whether the light casts shadows. */
  137. Color mColor; /**< Color of the light. */
  138. float mRange; /**< Cut off range for the light when rendering. */
  139. float mIntensity; /**< Power of the light source. This will be luminous flux for point & spot lights, and radiance for directional lights. */
  140. Degree mSpotAngle; /**< Total angle covered by a spot light. */
  141. Degree mSpotFalloffAngle; /**< Spot light angle at which falloff starts. Must be smaller than total angle. */
  142. bool mIsActive; /**< Whether the light should be rendered or not. */
  143. Sphere mBounds; /**< Sphere that bounds the light area of influence. */
  144. bool mPhysCorrectAtten; /**< Determines is physically based attentuation active. */
  145. };
  146. /** @} */
  147. /** @addtogroup Renderer-Engine-Internal
  148. * @{
  149. */
  150. namespace ct { class Light; }
  151. /**
  152. * Sim thread usable version of a light.
  153. *
  154. * @see LightBase
  155. */
  156. class BS_CORE_EXPORT Light : public IReflectable, public CoreObject, public LightBase
  157. {
  158. public:
  159. /** Retrieves an implementation of a light usable only from the core thread. */
  160. SPtr<ct::Light> getCore() const;
  161. /** Returns the hash value that can be used to identify if the internal data needs an update. */
  162. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  163. /** Sets the hash value that can be used to identify if the internal data needs an update. */
  164. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  165. /**
  166. * Updates internal transform values from the specified scene object, in case that scene object's transform changed
  167. * since the last call.
  168. *
  169. * @note Assumes the same scene object will be provided every time.
  170. */
  171. void _updateTransform(const HSceneObject& parent);
  172. /**
  173. * Creates a new light with provided settings.
  174. *
  175. * @param[in] type Type of light that determines how are the rest of the parameters interpreted.
  176. * @param[in] color Color of the light.
  177. * @param[in] intensity Power of the light source. This will be luminous flux for point & spot lights,
  178. * and radiance for directional lights.
  179. * @param[in] range Cut off range for the light when rendering.
  180. * @param[in] castsShadows Determines whether the light casts shadows.
  181. * @param[in] spotAngle Total angle covered by a spot light.
  182. * @param[in] spotFalloffAngle Spot light angle at which falloff starts. Must be smaller than total angle.
  183. */
  184. static SPtr<Light> create(LightType type = LightType::Point, Color color = Color::White,
  185. float intensity = 100.0f, float range = 10.0f, bool castsShadows = false,
  186. Degree spotAngle = Degree(45), Degree spotFalloffAngle = Degree(40));
  187. protected:
  188. Light(LightType type, Color color, float intensity, float range,
  189. bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  190. /** @copydoc CoreObject::createCore */
  191. SPtr<ct::CoreObject> createCore() const override;
  192. /** @copydoc LightBase::_markCoreDirty */
  193. void _markCoreDirty(LightDirtyFlag flag = LightDirtyFlag::Everything) override;
  194. /** @copydoc CoreObject::syncToCore */
  195. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  196. /** Creates a light with without initializing it. Used for serialization. */
  197. static SPtr<Light> createEmpty();
  198. UINT32 mLastUpdateHash;
  199. /************************************************************************/
  200. /* RTTI */
  201. /************************************************************************/
  202. public:
  203. friend class LightRTTI;
  204. static RTTITypeBase* getRTTIStatic();
  205. RTTITypeBase* getRTTI() const override;
  206. protected:
  207. Light(); // Serialization only
  208. };
  209. namespace ct
  210. {
  211. /**
  212. * Core thread usable version of a light.
  213. *
  214. * @see LightBase
  215. */
  216. class BS_CORE_EXPORT Light : public CoreObject, public LightBase
  217. {
  218. public:
  219. ~Light();
  220. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  221. void setRendererId(UINT32 id) { mRendererId = id; }
  222. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  223. UINT32 getRendererId() const { return mRendererId; }
  224. static const UINT32 LIGHT_CONE_NUM_SIDES;
  225. static const UINT32 LIGHT_CONE_NUM_SLICES;
  226. protected:
  227. friend class bs::Light;
  228. Light(LightType type, Color color, float intensity,
  229. float range, bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  230. /** @copydoc CoreObject::initialize */
  231. void initialize() override;
  232. /** @copydoc CoreObject::syncToCore */
  233. void syncToCore(const CoreSyncData& data) override;
  234. UINT32 mRendererId;
  235. SPtr<Mesh> mMesh;
  236. };
  237. }
  238. /** @} */
  239. }