BsLight.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. Radial,
  21. Spot
  22. };
  23. /** Signals which portion of a light is dirty. */
  24. enum class LightDirtyFlag
  25. {
  26. Transform = 0x01,
  27. Everything = 0x02
  28. };
  29. /** @} */
  30. /** @addtogroup Implementation
  31. * @{
  32. */
  33. /** Base class for both sim and core thread Light implementations. */
  34. class BS_CORE_EXPORT LightBase
  35. {
  36. public:
  37. LightBase();
  38. LightBase(LightType type, Color color, float intensity, float attRadius, float srcRadius,
  39. bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  40. virtual ~LightBase() { }
  41. /** Returns the position of the light, in world space. */
  42. Vector3 getPosition() const { return mPosition; }
  43. /** Sets the position of the light, in world space. */
  44. void setPosition(const Vector3& position)
  45. { mPosition = position; _markCoreDirty(LightDirtyFlag::Transform); updateBounds(); }
  46. /** Returns the rotation of the light, in world space. */
  47. Quaternion getRotation() const { return mRotation; }
  48. /** Sets the rotation of the light, in world space. */
  49. void setRotation(const Quaternion& rotation)
  50. { mRotation = rotation; _markCoreDirty(LightDirtyFlag::Transform); 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. /** @see setAttenuationRadius */
  64. float getAttenuationRadius() const { return mAttRadius; }
  65. /**
  66. * Range at which the light contribution fades out to zero. Use setUseAutoAttenuation to provide a radius
  67. * automatically dependant on light intensity. The radius will cut-off light contribution and therefore manually set
  68. * very small radius can end up being very physically incorrect.
  69. */
  70. void setAttenuationRadius(float radius);
  71. /** @see setSourceRadius */
  72. float getSourceRadius() const { return mSourceRadius; }
  73. /**
  74. * Radius of the light source. If non-zero then this light represents an area light, otherwise it is a punctual
  75. * light. Area lights have different attenuation then punctual lights, and their appearance in specular reflections
  76. * is realistic. Shape of the area light depends on light type:
  77. * - For directional light the shape is a disc projected on the hemisphere on the sky. This parameter
  78. * represents angular radius (in degrees) of the disk and should be very small (think of how much space the Sun
  79. * takes on the sky - roughly 0.25 degree radius).
  80. * - For radial light the shape is a sphere and the source radius is the radius of the sphere.
  81. * - For spot lights the shape is a disc oriented in the direction of the spot light and the source radius is the
  82. * radius of the disc.
  83. */
  84. void setSourceRadius(float radius);
  85. /** @see setUseAutoAttenuation */
  86. bool getUseAutoAttenuation() const { return mAutoAttenuation; }
  87. /**
  88. * If enabled the attenuation radius will automatically be controlled in order to provide reasonable light radius,
  89. * depending on its intensity.
  90. */
  91. void setUseAutoAttenuation(bool enabled);
  92. /** @see setIntensity */
  93. float getIntensity() const { return mIntensity; }
  94. /**
  95. * Determines the power of the light source. This will be luminous flux for radial & spot lights,
  96. * luminance for directional lights with no area, and illuminance for directional lights with area (non-zero source
  97. * radius).
  98. */
  99. void setIntensity(float intensity);
  100. /** Gets the total angle covered by a spot light. */
  101. Degree getSpotAngle() const { return mSpotAngle; }
  102. /** Sets the total angle covered by a spot light. */
  103. void setSpotAngle(const Degree& spotAngle) { mSpotAngle = spotAngle; _markCoreDirty(); updateBounds(); }
  104. /**
  105. * Gets the falloff angle covered by a spot light. Falloff angle determines at what point does light intensity
  106. * starts quadratically falling off as the angle approaches the total spot angle.
  107. */
  108. Degree getSpotFalloffAngle() const { return mSpotFalloffAngle; }
  109. /**
  110. * Sets the falloff angle covered by a spot light. Falloff angle determines at what point does light intensity
  111. * starts quadratically falling off as the angle approaches the total spot angle.
  112. */
  113. void setSpotFalloffAngle(const Degree& spotFallofAngle)
  114. { mSpotFalloffAngle = spotFallofAngle; _markCoreDirty(); updateBounds(); }
  115. /** Returns world space bounds that completely encompass the light's area of influence. */
  116. Sphere getBounds() const { return mBounds; }
  117. /**
  118. * Returns the luminance of the light source. This is the value that should be used in lighting equations.
  119. *
  120. * @note
  121. * For point light sources this method returns luminous intensity and not luminance. We use the same method for both
  122. * as a convenience since in either case its used as a measure of intensity in lighting equations.
  123. */
  124. float getLuminance() const;
  125. /** Checks whether the light should be rendered or not. */
  126. bool getIsActive() const { return mIsActive; }
  127. /** Sets whether the light should be rendered or not. */
  128. void setIsActive(bool active) { mIsActive = active; _markCoreDirty(); }
  129. /**
  130. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  131. * thread counterpart.
  132. */
  133. virtual void _markCoreDirty(LightDirtyFlag flag = LightDirtyFlag::Everything) { }
  134. protected:
  135. /** Updates the internal bounds for the light. Call this whenever a property affecting the bounds changes. */
  136. void updateBounds();
  137. /** Calculates maximum light range based on light intensity. */
  138. void updateAttenuationRange();
  139. Vector3 mPosition; /**< World space position. */
  140. Quaternion mRotation; /**< World space rotation. */
  141. LightType mType; /**< Type of light that determines how are the rest of the parameters interpreted. */
  142. bool mCastsShadows; /**< Determines whether the light casts shadows. */
  143. Color mColor; /**< Color of the light. */
  144. float mAttRadius; /**< Radius at which light intensity falls off to zero. */
  145. float mSourceRadius; /**< Radius of the light source. If > 0 the light is an area light. */
  146. float mIntensity; /**< Power of the light source. @see setIntensity. */
  147. Degree mSpotAngle; /**< Total angle covered by a spot light. */
  148. Degree mSpotFalloffAngle; /**< Spot light angle at which falloff starts. Must be smaller than total angle. */
  149. bool mIsActive; /**< Whether the light should be rendered or not. */
  150. Sphere mBounds; /**< Sphere that bounds the light area of influence. */
  151. bool mAutoAttenuation; /**< Determines is attenuation radius is automatically determined. */
  152. };
  153. /** @} */
  154. /** @addtogroup Renderer-Engine-Internal
  155. * @{
  156. */
  157. namespace ct { class Light; }
  158. /** Illuminates a portion of the scene covered by a light. */
  159. class BS_CORE_EXPORT Light : public IReflectable, public CoreObject, public LightBase
  160. {
  161. public:
  162. /** Retrieves an implementation of the light usable only from the core thread. */
  163. SPtr<ct::Light> getCore() const;
  164. /** Returns the hash value that can be used to identify if the internal data needs an update. */
  165. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  166. /** Sets the hash value that can be used to identify if the internal data needs an update. */
  167. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  168. /**
  169. * Updates internal transform values from the specified scene object, in case that scene object's transform changed
  170. * since the last call.
  171. *
  172. * @note Assumes the same scene object will be provided every time.
  173. */
  174. void _updateTransform(const HSceneObject& parent);
  175. /**
  176. * Creates a new light with provided settings.
  177. *
  178. * @param[in] type Type of light that determines how are the rest of the parameters interpreted.
  179. * @param[in] color Color of the light.
  180. * @param[in] intensity Power of the light source. This will be luminous flux for radial & spot lights,
  181. * luminance for directional lights with no area, and illuminance for directional
  182. * lights with area (non-zero source radius).
  183. * @param[in] attRadius Radius at which light's influence fades out to zero.
  184. * @param[in] castsShadows Determines whether the light casts shadows.
  185. * @param[in] spotAngle Total angle covered by a spot light.
  186. * @param[in] spotFalloffAngle Spot light angle at which falloff starts. Must be smaller than total angle.
  187. */
  188. static SPtr<Light> create(LightType type = LightType::Radial, Color color = Color::White,
  189. float intensity = 100.0f, float attRadius = 10.0f, bool castsShadows = false,
  190. Degree spotAngle = Degree(45), Degree spotFalloffAngle = Degree(40));
  191. protected:
  192. Light(LightType type, Color color, float intensity, float attRadius, float srcRadius,
  193. bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  194. /** @copydoc CoreObject::createCore */
  195. SPtr<ct::CoreObject> createCore() const override;
  196. /** @copydoc LightBase::_markCoreDirty */
  197. void _markCoreDirty(LightDirtyFlag flag = LightDirtyFlag::Everything) override;
  198. /** @copydoc CoreObject::syncToCore */
  199. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  200. /** Creates a light with without initializing it. Used for serialization. */
  201. static SPtr<Light> createEmpty();
  202. UINT32 mLastUpdateHash;
  203. /************************************************************************/
  204. /* RTTI */
  205. /************************************************************************/
  206. public:
  207. friend class LightRTTI;
  208. static RTTITypeBase* getRTTIStatic();
  209. RTTITypeBase* getRTTI() const override;
  210. protected:
  211. Light(); // Serialization only
  212. };
  213. namespace ct
  214. {
  215. /** Core thread usable version of bs::Light. */
  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, float attRadius, float srcRadius, bool castsShadows,
  229. 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. }