BsLight.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. Mobility = 0x04
  29. };
  30. /** @} */
  31. /** @addtogroup Implementation
  32. * @{
  33. */
  34. /** Base class for both sim and core thread Light implementations. */
  35. class BS_CORE_EXPORT LightBase
  36. {
  37. public:
  38. LightBase();
  39. LightBase(LightType type, Color color, float intensity, float attRadius, float srcRadius,
  40. bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  41. virtual ~LightBase() { }
  42. /** Returns the position of the light, in world space. */
  43. Vector3 getPosition() const { return mPosition; }
  44. /** Sets the position of the light, in world space. */
  45. void setPosition(const Vector3& position)
  46. { mPosition = position; _markCoreDirty(LightDirtyFlag::Transform); 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)
  51. { mRotation = rotation; _markCoreDirty(LightDirtyFlag::Transform); updateBounds(); }
  52. /** Returns the type of the light. */
  53. LightType getType() const { return mType; }
  54. /** Changes the type of the light. */
  55. void setType(LightType type) { mType = type; _markCoreDirty(); updateBounds(); }
  56. /** Checks does this light cast shadows when rendered. */
  57. bool getCastsShadow() const { return mCastsShadows; }
  58. /** Sets whether this light will cast shadows when rendered. */
  59. void setCastsShadow(bool castsShadow) { mCastsShadows = castsShadow; _markCoreDirty(); }
  60. /**
  61. * Shadow bias determines shadow accuracy. Low bias values mean that shadows start closer near their caster surface
  62. * but will result in more shadowing artifacts (shadow acne). Larger values reduce shadow acne but caster may appear
  63. * as floating on air as nearby part of the shadow is cut off (peter paning).
  64. *
  65. * Default value is 0.5. Must be in range [0, 1].
  66. */
  67. void setShadowBias(float bias) { mShadowBias = std::max(std::min(bias, 1.0f), 0.0f); _markCoreDirty(); }
  68. /** @copydoc setShadowBias() */
  69. float getShadowBias() const { return mShadowBias; }
  70. /** Returns the color emitted from the light. */
  71. Color getColor() const { return mColor; }
  72. /** Sets the color emitted from the light. */
  73. void setColor(const Color& color) { mColor = color; _markCoreDirty(); }
  74. /** @see setAttenuationRadius */
  75. float getAttenuationRadius() const { return mAttRadius; }
  76. /**
  77. * Range at which the light contribution fades out to zero. Use setUseAutoAttenuation to provide a radius
  78. * automatically dependant on light intensity. The radius will cut-off light contribution and therefore manually set
  79. * very small radius can end up being very physically incorrect.
  80. */
  81. void setAttenuationRadius(float radius);
  82. /** @see setSourceRadius */
  83. float getSourceRadius() const { return mSourceRadius; }
  84. /**
  85. * Radius of the light source. If non-zero then this light represents an area light, otherwise it is a punctual
  86. * light. Area lights have different attenuation then punctual lights, and their appearance in specular reflections
  87. * is realistic. Shape of the area light depends on light type:
  88. * - For directional light the shape is a disc projected on the hemisphere on the sky. This parameter
  89. * represents angular radius (in degrees) of the disk and should be very small (think of how much space the Sun
  90. * takes on the sky - roughly 0.25 degree radius).
  91. * - For radial light the shape is a sphere and the source radius is the radius of the sphere.
  92. * - For spot lights the shape is a disc oriented in the direction of the spot light and the source radius is the
  93. * radius of the disc.
  94. */
  95. void setSourceRadius(float radius);
  96. /** @see setUseAutoAttenuation */
  97. bool getUseAutoAttenuation() const { return mAutoAttenuation; }
  98. /**
  99. * If enabled the attenuation radius will automatically be controlled in order to provide reasonable light radius,
  100. * depending on its intensity.
  101. */
  102. void setUseAutoAttenuation(bool enabled);
  103. /** @see setIntensity */
  104. float getIntensity() const { return mIntensity; }
  105. /**
  106. * Determines the power of the light source. This will be luminous flux for radial & spot lights,
  107. * luminance for directional lights with no area, and illuminance for directional lights with area (non-zero source
  108. * radius).
  109. */
  110. void setIntensity(float intensity);
  111. /** Gets the total angle covered by a spot light. */
  112. Degree getSpotAngle() const { return mSpotAngle; }
  113. /** Sets the total angle covered by a spot light. */
  114. void setSpotAngle(const Degree& spotAngle) { mSpotAngle = spotAngle; _markCoreDirty(); updateBounds(); }
  115. /**
  116. * Gets the falloff angle covered by a spot light. Falloff angle determines at what point does light intensity
  117. * starts quadratically falling off as the angle approaches the total spot angle.
  118. */
  119. Degree getSpotFalloffAngle() const { return mSpotFalloffAngle; }
  120. /**
  121. * Sets the falloff angle covered by a spot light. Falloff angle determines at what point does light intensity
  122. * starts quadratically falling off as the angle approaches the total spot angle.
  123. */
  124. void setSpotFalloffAngle(const Degree& spotFallofAngle)
  125. { mSpotFalloffAngle = spotFallofAngle; _markCoreDirty(); updateBounds(); }
  126. /** Returns world space bounds that completely encompass the light's area of influence. */
  127. Sphere getBounds() const { return mBounds; }
  128. /**
  129. * Returns the luminance of the light source. This is the value that should be used in lighting equations.
  130. *
  131. * @note
  132. * For point light sources this method returns luminous intensity and not luminance. We use the same method for both
  133. * as a convenience since in either case its used as a measure of intensity in lighting equations.
  134. */
  135. float getLuminance() const;
  136. /** Checks whether the light should be rendered or not. */
  137. bool getIsActive() const { return mIsActive; }
  138. /** Sets whether the light should be rendered or not. */
  139. void setIsActive(bool active) { mIsActive = active; _markCoreDirty(); }
  140. /**
  141. * Sets the mobility of a scene object. This is used primarily as a performance hint to engine systems. Objects
  142. * with more restricted mobility will result in higher performance. Some mobility constraints will be enforced by
  143. * the engine itself, while for others the caller must be sure not to break the promise he made when mobility was
  144. * set. By default scene object's mobility is unrestricted.
  145. */
  146. void setMobility(ObjectMobility mobility);
  147. /**
  148. * Gets the mobility setting for this scene object. See setMobility();
  149. */
  150. ObjectMobility getMobility() const { return mMobility; }
  151. /**
  152. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  153. * thread counterpart.
  154. */
  155. virtual void _markCoreDirty(LightDirtyFlag flag = LightDirtyFlag::Everything) { }
  156. protected:
  157. /** Updates the internal bounds for the light. Call this whenever a property affecting the bounds changes. */
  158. void updateBounds();
  159. /** Calculates maximum light range based on light intensity. */
  160. void updateAttenuationRange();
  161. Vector3 mPosition; /**< World space position. */
  162. Quaternion mRotation; /**< World space rotation. */
  163. LightType mType; /**< Type of light that determines how are the rest of the parameters interpreted. */
  164. bool mCastsShadows; /**< Determines whether the light casts shadows. */
  165. Color mColor; /**< Color of the light. */
  166. float mAttRadius; /**< Radius at which light intensity falls off to zero. */
  167. float mSourceRadius; /**< Radius of the light source. If > 0 the light is an area light. */
  168. float mIntensity; /**< Power of the light source. @see setIntensity. */
  169. Degree mSpotAngle; /**< Total angle covered by a spot light. */
  170. Degree mSpotFalloffAngle; /**< Spot light angle at which falloff starts. Must be smaller than total angle. */
  171. bool mIsActive; /**< Whether the light should be rendered or not. */
  172. Sphere mBounds; /**< Sphere that bounds the light area of influence. */
  173. bool mAutoAttenuation; /**< Determines is attenuation radius is automatically determined. */
  174. ObjectMobility mMobility; /**< Determines if there are any restrictions placed on light movement. */
  175. float mShadowBias; /**< See setShadowBias() */
  176. };
  177. /** @} */
  178. /** @addtogroup Renderer-Engine-Internal
  179. * @{
  180. */
  181. namespace ct { class Light; }
  182. /** Illuminates a portion of the scene covered by a light. */
  183. class BS_CORE_EXPORT Light : public IReflectable, public CoreObject, public LightBase
  184. {
  185. public:
  186. /** Retrieves an implementation of the light usable only from the core thread. */
  187. SPtr<ct::Light> getCore() const;
  188. /** Returns the hash value that can be used to identify if the internal data needs an update. */
  189. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  190. /** Sets the hash value that can be used to identify if the internal data needs an update. */
  191. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  192. /**
  193. * Updates internal transform values from the specified scene object, in case that scene object's transform changed
  194. * since the last call.
  195. *
  196. * @note Assumes the same scene object will be provided every time.
  197. */
  198. void _updateTransform(const HSceneObject& parent);
  199. /**
  200. * Creates a new light with provided settings.
  201. *
  202. * @param[in] type Type of light that determines how are the rest of the parameters interpreted.
  203. * @param[in] color Color of the light.
  204. * @param[in] intensity Power of the light source. This will be luminous flux for radial & spot lights,
  205. * luminance for directional lights with no area, and illuminance for directional
  206. * lights with area (non-zero source radius).
  207. * @param[in] attRadius Radius at which light's influence fades out to zero.
  208. * @param[in] castsShadows Determines whether the light casts shadows.
  209. * @param[in] spotAngle Total angle covered by a spot light.
  210. * @param[in] spotFalloffAngle Spot light angle at which falloff starts. Must be smaller than total angle.
  211. */
  212. static SPtr<Light> create(LightType type = LightType::Radial, Color color = Color::White,
  213. float intensity = 100.0f, float attRadius = 10.0f, bool castsShadows = false,
  214. Degree spotAngle = Degree(45), Degree spotFalloffAngle = Degree(40));
  215. protected:
  216. Light(LightType type, Color color, float intensity, float attRadius, float srcRadius,
  217. bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  218. /** @copydoc CoreObject::createCore */
  219. SPtr<ct::CoreObject> createCore() const override;
  220. /** @copydoc LightBase::_markCoreDirty */
  221. void _markCoreDirty(LightDirtyFlag flag = LightDirtyFlag::Everything) override;
  222. /** @copydoc CoreObject::syncToCore */
  223. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  224. /** Creates a light with without initializing it. Used for serialization. */
  225. static SPtr<Light> createEmpty();
  226. UINT32 mLastUpdateHash;
  227. /************************************************************************/
  228. /* RTTI */
  229. /************************************************************************/
  230. public:
  231. friend class LightRTTI;
  232. static RTTITypeBase* getRTTIStatic();
  233. RTTITypeBase* getRTTI() const override;
  234. protected:
  235. Light(); // Serialization only
  236. };
  237. namespace ct
  238. {
  239. /** Core thread usable version of bs::Light. */
  240. class BS_CORE_EXPORT Light : public CoreObject, public LightBase
  241. {
  242. public:
  243. ~Light();
  244. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  245. void setRendererId(UINT32 id) { mRendererId = id; }
  246. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  247. UINT32 getRendererId() const { return mRendererId; }
  248. static const UINT32 LIGHT_CONE_NUM_SIDES;
  249. static const UINT32 LIGHT_CONE_NUM_SLICES;
  250. protected:
  251. friend class bs::Light;
  252. Light(LightType type, Color color, float intensity, float attRadius, float srcRadius, bool castsShadows,
  253. Degree spotAngle, Degree spotFalloffAngle);
  254. /** @copydoc CoreObject::initialize */
  255. void initialize() override;
  256. /** @copydoc CoreObject::syncToCore */
  257. void syncToCore(const CoreSyncData& data) override;
  258. UINT32 mRendererId;
  259. SPtr<Mesh> mMesh;
  260. };
  261. }
  262. /** @} */
  263. }