Light.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. /// Calculate distance for rendering
  129. virtual void UpdateDistance(const FrameInfo& frame);
  130. /// Add debug geometry to the debug graphics
  131. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  132. /// Set light type
  133. void SetLightType(LightType type);
  134. /// Set color
  135. void SetColor(const Color& color);
  136. /// Set specular intensity
  137. void SetSpecularIntensity(float intensity);
  138. /// Set range
  139. void SetRange(float range);
  140. /// Set spotlight field of view
  141. void SetFov(float fov);
  142. /// Set spotlight aspect ratio
  143. void SetAspectRatio(float aspectRatio);
  144. /// Set fade out start distance
  145. void SetFadeDistance(float distance);
  146. /// Set shadow fade out start distance. Only has effect if shadow distance is also non-zero
  147. void SetShadowFadeDistance(float distance);
  148. /// Set shadow depth bias parameters
  149. void SetShadowBias(const BiasParameters& parameters);
  150. /// Set directional light cascaded shadow parameters
  151. void SetShadowCascade(const CascadeParameters& parameters);
  152. /// Set shadow map focusing parameters
  153. void SetShadowFocus(const FocusParameters& parameters);
  154. /// Set shadow intensity between 0.0 - 1.0. 0.0 (the default) gives fully dark shadows.
  155. void SetShadowIntensity(float intensity);
  156. /// Set shadow resolution between 0.25 - 1.0. Determines the shadow map to use
  157. void SetShadowResolution(float resolution);
  158. /// Set shadow camera near/far clip distance ratio
  159. void SetShadowNearFarRatio(float nearFarRatio);
  160. /// Set range attenuation texture
  161. void SetRampTexture(Texture* texture);
  162. /// Set spotlight attenuation texture
  163. void SetShapeTexture(Texture* texture);
  164. /// Return light type
  165. LightType GetLightType() const { return lightType_; }
  166. /// Return color
  167. const Color& GetColor() const { return color_; }
  168. /// Return specular intensity
  169. float GetSpecularIntensity() const { return specularIntensity_; }
  170. /// Return range
  171. float GetRange() const { return range_; }
  172. /// Return spotlight field of view
  173. float GetFov() const { return fov_; }
  174. /// Return spotlight aspect ratio
  175. float GetAspectRatio() const { return aspectRatio_; }
  176. /// Return fade start distance
  177. float GetFadeDistance() const { return fadeDistance_; }
  178. /// Return shadow fade start distance
  179. float GetShadowFadeDistance() const { return shadowFadeDistance_; }
  180. /// Return shadow depth bias parameters
  181. const BiasParameters& GetShadowBias() const { return shadowBias_; }
  182. /// Return directional light cascaded shadow parameters
  183. const CascadeParameters& GetShadowCascade() const { return shadowCascade_; }
  184. /// Return shadow map focus parameters
  185. const FocusParameters& GetShadowFocus() const { return shadowFocus_; }
  186. /// Return shadow intensity
  187. float GetShadowIntensity() const { return shadowIntensity_; }
  188. /// Return shadow resolution
  189. float GetShadowResolution() const { return shadowResolution_; }
  190. /// Return shadow camera near/far clip distance ratio
  191. float GetShadowNearFarRatio() const { return shadowNearFarRatio_; }
  192. /// Return range attenuation texture
  193. Texture* GetRampTexture() const { return rampTexture_; }
  194. /// Return spotlight attenuation texture
  195. Texture* GetShapeTexture() const { return shapeTexture_; }
  196. /// Return spotlight frustum
  197. Frustum GetFrustum() const;
  198. /// Set near split distance for directional light
  199. void SetNearSplit(float near);
  200. /// Set far split distance for directional light
  201. void SetFarSplit(float far);
  202. /// Set near fade range for directional light
  203. void SetNearFadeRange(float range);
  204. /// Set far fade range for directional light
  205. void SetFarFadeRange(float range);
  206. /// Set shadow camera
  207. void SetShadowCamera(Camera* camera);
  208. /// Set shadow map depth texture
  209. void SetShadowMap(Texture2D* shadowMap);
  210. /// Set sort value based on intensity at given world position
  211. void SetIntensitySortValue(const Vector3& position);
  212. /// Copy values from another light
  213. void CopyFrom(Light* original);
  214. /// Return near split distance
  215. float GetNearSplit() const { return nearSplit_; }
  216. /// Return far split distance
  217. float GetFarSplit() const { return farSplit_; }
  218. /// Return near fade range
  219. float GetNearFadeRange() const { return nearFadeRange_; }
  220. /// Return far fade range
  221. float GetFarFadeRange() const { return farFadeRange_; }
  222. /// Return shadow camera. Not safe to dereference outside rendering
  223. Camera* GetShadowCamera() { return shadowCamera_; }
  224. /// Return shadow map
  225. Texture2D* GetShadowMap() const { return shadowMap_; }
  226. /// Return original light (split lights only)
  227. Light* GetOriginalLight() const { return originalLight_; }
  228. /// Return volume safety extent of spot or point light
  229. float GetVolumeExtent() const;
  230. /// Return directional light quad transform for either near or far split
  231. Matrix3x4 GetDirLightTransform(Camera& camera, bool getNearQuad = false);
  232. /// Return light volume model transform. For directional lights, the view transform must be overridden
  233. const Matrix3x4& GetVolumeTransform(Camera& camera);
  234. /// Set ramp texture attribute
  235. void SetRampTextureAttr(ResourceRef value);
  236. /// Set shape texture attribute
  237. void SetShapeTextureAttr(ResourceRef value);
  238. /// Return ramp texture attribute
  239. ResourceRef GetRampTextureAttr() const;
  240. /// Return shape texture attribute
  241. ResourceRef GetShapeTextureAttr() const;
  242. protected:
  243. /// Update world-space bounding box
  244. virtual void OnWorldBoundingBoxUpdate();
  245. private:
  246. /// Light type
  247. LightType lightType_;
  248. /// Color
  249. Color color_;
  250. /// Specular intensity
  251. float specularIntensity_;
  252. /// Range
  253. float range_;
  254. /// Spotlight field of view
  255. float fov_;
  256. /// Spotlight aspect ratio
  257. float aspectRatio_;
  258. /// Fade start distance
  259. float fadeDistance_;
  260. /// Shadow fade start distance
  261. float shadowFadeDistance_;
  262. /// Shadow depth bias parameters
  263. BiasParameters shadowBias_;
  264. /// Directional light cascaded shadow parameters
  265. CascadeParameters shadowCascade_;
  266. /// Shadow map focus parameters
  267. FocusParameters shadowFocus_;
  268. /// Custom world transform for the light volume
  269. Matrix3x4 volumeTransform_;
  270. /// Shadow intensity
  271. float shadowIntensity_;
  272. /// Shadow resolution
  273. float shadowResolution_;
  274. /// Shadow camera near/far clip distance ratio
  275. float shadowNearFarRatio_;
  276. /// Directional light near split distance
  277. float nearSplit_;
  278. /// Directional light far split distance
  279. float farSplit_;
  280. /// Directional light near fade range
  281. float nearFadeRange_;
  282. /// Directional light far fade range
  283. float farFadeRange_;
  284. /// Range attenuation texture
  285. SharedPtr<Texture> rampTexture_;
  286. /// Spotlight attenuation texture
  287. SharedPtr<Texture> shapeTexture_;
  288. /// Shadow camera
  289. Camera* shadowCamera_;
  290. /// Shadow map
  291. Texture2D* shadowMap_;
  292. /// Original light for splitting
  293. Light* originalLight_;
  294. };