Light.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Color.h"
  25. #include "Drawable.h"
  26. #include "Frustum.h"
  27. class Camera;
  28. class Texture;
  29. class Texture2D;
  30. class TextureCube;
  31. /// Light types
  32. enum LightType
  33. {
  34. LIGHT_DIRECTIONAL = 0,
  35. LIGHT_SPOT,
  36. LIGHT_POINT,
  37. LIGHT_SPLITPOINT
  38. };
  39. /// Shadow depth bias parameters
  40. struct BiasParameters
  41. {
  42. /// Construct as undefined
  43. BiasParameters()
  44. {
  45. }
  46. /// Construct with initial values
  47. BiasParameters(float constantBias, float slopeScaledBias) :
  48. constantBias_(constantBias),
  49. slopeScaledBias_(slopeScaledBias)
  50. {
  51. }
  52. /// Validate parameters
  53. void Validate();
  54. /// Constant bias
  55. float constantBias_;
  56. /// Slope scaled bias
  57. float slopeScaledBias_;
  58. };
  59. /// Cascaded shadow map parameters
  60. struct CascadeParameters
  61. {
  62. /// Construct as undefined
  63. CascadeParameters()
  64. {
  65. }
  66. /// Construct with initial values
  67. CascadeParameters(unsigned splits, float lambda, float splitFadeRange, float shadowRange) :
  68. splits_(splits),
  69. lambda_(lambda),
  70. splitFadeRange_(splitFadeRange),
  71. shadowRange_(shadowRange)
  72. {
  73. }
  74. /// Validate parameters
  75. void Validate();
  76. /// Number of splits
  77. unsigned splits_;
  78. /// Split lambda
  79. float lambda_;
  80. /// Fade range between splits
  81. float splitFadeRange_;
  82. /// Maximum shadow distance
  83. float shadowRange_;
  84. };
  85. /// Shadow map focusing parameters
  86. struct FocusParameters
  87. {
  88. /// Construct as undefined
  89. FocusParameters()
  90. {
  91. }
  92. /// Construct with initial values
  93. FocusParameters(bool focus, bool nonUniform, bool zoomOut, float quantize, float minView) :
  94. focus_(focus),
  95. nonUniform_(nonUniform),
  96. zoomOut_(zoomOut),
  97. quantize_(quantize),
  98. minView_(minView)
  99. {
  100. }
  101. /// Validate parameters
  102. void Validate();
  103. /// Focus flag
  104. bool focus_;
  105. /// Non-uniform focusing flag
  106. bool nonUniform_;
  107. /// Zoom out flag
  108. bool zoomOut_;
  109. /// Focus quantization
  110. float quantize_;
  111. /// Minimum view size
  112. float minView_;
  113. };
  114. static const float SHADOW_MIN_QUANTIZE = 0.1f;
  115. static const float SHADOW_MIN_VIEW = 1.0f;
  116. static const float SHADOW_DEFAULT_NEARCLIP = 0.1f;
  117. /// Light component
  118. class Light : public Drawable
  119. {
  120. OBJECT(Light);
  121. public:
  122. /// Construct
  123. Light(Context* context);
  124. /// Destruct
  125. virtual ~Light();
  126. /// Register object factory
  127. static void RegisterObject(Context* context);
  128. /// Handle attribute write access
  129. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& value);
  130. /// Handle attribute read access
  131. virtual Variant OnGetAttribute(const AttributeInfo& attr);
  132. /// Calculate distance for rendering
  133. virtual void UpdateDistance(const FrameInfo& frame);
  134. /// Add debug geometry to the debug graphics
  135. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  136. /// Set light type
  137. void SetLightType(LightType type);
  138. /// Set color
  139. void SetColor(const Color& color);
  140. /// Set specular intensity
  141. void SetSpecularIntensity(float intensity);
  142. /// Set range
  143. void SetRange(float range);
  144. /// Set spotlight field of view
  145. void SetFov(float fov);
  146. /// Set spotlight aspect ratio
  147. void SetAspectRatio(float aspectRatio);
  148. /// Set fade out start distance
  149. void SetFadeDistance(float distance);
  150. /// Set shadow fade out start distance. Only has effect if shadow distance is also non-zero
  151. void SetShadowFadeDistance(float distance);
  152. /// Set shadow depth bias parameters
  153. void SetShadowBias(const BiasParameters& parameters);
  154. /// Set directional light cascaded shadow parameters
  155. void SetShadowCascade(const CascadeParameters& parameters);
  156. /// Set shadow map focusing parameters
  157. void SetShadowFocus(const FocusParameters& parameters);
  158. /// Set shadow intensity between 0.0 - 1.0. 0.0 (the default) gives fully dark shadows.
  159. void SetShadowIntensity(float intensity);
  160. /// Set shadow resolution between 0.25 - 1.0. Determines the shadow map to use
  161. void SetShadowResolution(float resolution);
  162. /// Set shadow camera near/far clip distance ratio
  163. void SetShadowNearFarRatio(float nearFarRatio);
  164. /// Set range attenuation texture
  165. void SetRampTexture(Texture* texture);
  166. /// Set spotlight attenuation texture
  167. void SetShapeTexture(Texture* texture);
  168. /// Return light type
  169. LightType GetLightType() const { return lightType_; }
  170. /// Return color
  171. const Color& GetColor() const { return color_; }
  172. /// Return specular intensity
  173. float GetSpecularIntensity() const { return specularIntensity_; }
  174. /// Return range
  175. float GetRange() const { return range_; }
  176. /// Return spotlight field of view
  177. float GetFov() const { return fov_; }
  178. /// Return spotlight aspect ratio
  179. float GetAspectRatio() const { return aspectRatio_; }
  180. /// Return fade start distance
  181. float GetFadeDistance() const { return fadeDistance_; }
  182. /// Return shadow fade start distance
  183. float GetShadowFadeDistance() const { return shadowFadeDistance_; }
  184. /// Return shadow depth bias parameters
  185. const BiasParameters& GetShadowBias() const { return shadowBias_; }
  186. /// Return directional light cascaded shadow parameters
  187. const CascadeParameters& GetShadowCascade() const { return shadowCascade_; }
  188. /// Return shadow map focus parameters
  189. const FocusParameters& GetShadowFocus() const { return shadowFocus_; }
  190. /// Return shadow intensity
  191. float GetShadowIntensity() const { return shadowIntensity_; }
  192. /// Return shadow resolution
  193. float GetShadowResolution() const { return shadowResolution_; }
  194. /// Return shadow camera near/far clip distance ratio
  195. float GetShadowNearFarRatio() const { return shadowNearFarRatio_; }
  196. /// Return range attenuation texture
  197. Texture* GetRampTexture() const { return rampTexture_; }
  198. /// Return spotlight attenuation texture
  199. Texture* GetShapeTexture() const { return shapeTexture_; }
  200. /// Return spotlight frustum
  201. Frustum GetFrustum() const;
  202. /// Set near split distance for directional light
  203. void SetNearSplit(float near);
  204. /// Set far split distance for directional light
  205. void SetFarSplit(float far);
  206. /// Set near fade range for directional light
  207. void SetNearFadeRange(float range);
  208. /// Set far fade range for directional light
  209. void SetFarFadeRange(float range);
  210. /// Set shadow camera
  211. void SetShadowCamera(Camera* camera);
  212. /// Set shadow map depth texture
  213. void SetShadowMap(Texture2D* shadowMap);
  214. /// Set sort value based on intensity at given world position
  215. void SetIntensitySortValue(const Vector3& position);
  216. /// Copy values from another light
  217. void copyFrom(Light* original);
  218. /// Return near split distance
  219. float GetNearSplit() const { return nearSplit_; }
  220. /// Return far split distance
  221. float GetFarSplit() const { return farSplit_; }
  222. /// Return near fade range
  223. float GetNearFadeRange() const { return nearFadeRange_; }
  224. /// Return far fade range
  225. float GetFarFadeRange() const { return farFadeRange_; }
  226. /// Return shadow camera. Not safe to dereference outside rendering
  227. Camera* GetShadowCamera() { return shadowCamera_; }
  228. /// Return shadow map
  229. Texture2D* GetShadowMap() const { return shadowMap_; }
  230. /// Return original light (split lights only)
  231. Light* GetOriginalLight() const { return originalLight_; }
  232. /// Return volume safety extent of spot or point light
  233. float GetVolumeExtent() const;
  234. /// Return directional light quad transform for either near or far split
  235. Matrix4x3 GetDirLightTransform(Camera& camera, bool getNearQuad = false);
  236. /// Return light volume model transform. For directional lights, the view transform must be overridden
  237. const Matrix4x3& GetVolumeTransform(Camera& camera);
  238. protected:
  239. /// Update world-space bounding box
  240. virtual void OnWorldBoundingBoxUpdate();
  241. private:
  242. /// Light type
  243. LightType lightType_;
  244. /// Color
  245. Color color_;
  246. /// Specular intensity
  247. float specularIntensity_;
  248. /// Range
  249. float range_;
  250. /// Spotlight field of view
  251. float fov_;
  252. /// Spotlight aspect ratio
  253. float aspectRatio_;
  254. /// Fade start distance
  255. float fadeDistance_;
  256. /// Shadow fade start distance
  257. float shadowFadeDistance_;
  258. /// Shadow depth bias parameters
  259. BiasParameters shadowBias_;
  260. /// Directional light cascaded shadow parameters
  261. CascadeParameters shadowCascade_;
  262. /// Shadow map focus parameters
  263. FocusParameters shadowFocus_;
  264. /// Custom world transform for the light volume
  265. Matrix4x3 volumeTransform_;
  266. /// Shadow intensity
  267. float shadowIntensity_;
  268. /// Shadow resolution
  269. float shadowResolution_;
  270. /// Shadow camera near/far clip distance ratio
  271. float shadowNearFarRatio_;
  272. /// Directional light near split distance
  273. float nearSplit_;
  274. /// Directional light far split distance
  275. float farSplit_;
  276. /// Directional light near fade range
  277. float nearFadeRange_;
  278. /// Directional light far fade range
  279. float farFadeRange_;
  280. /// Range attenuation texture
  281. SharedPtr<Texture> rampTexture_;
  282. /// Spotlight attenuation texture
  283. SharedPtr<Texture> shapeTexture_;
  284. /// Shadow camera
  285. Camera* shadowCamera_;
  286. /// Shadow map
  287. Texture2D* shadowMap_;
  288. /// Original light for splitting
  289. Light* originalLight_;
  290. };