Light.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. };
  38. static const float SHADOW_MIN_QUANTIZE = 0.1f;
  39. static const float SHADOW_MIN_VIEW = 1.0f;
  40. static const int MAX_LIGHT_SPLITS = 6;
  41. static const int MAX_CASCADE_SPLITS = 4;
  42. /// Shadow depth bias parameters.
  43. struct BiasParameters
  44. {
  45. /// Construct undefined.
  46. BiasParameters()
  47. {
  48. }
  49. /// Construct with initial values.
  50. BiasParameters(float constantBias, float slopeScaledBias) :
  51. constantBias_(constantBias),
  52. slopeScaledBias_(slopeScaledBias)
  53. {
  54. }
  55. /// Validate parameters.
  56. void Validate();
  57. /// Constant bias.
  58. float constantBias_;
  59. /// Slope scaled bias.
  60. float slopeScaledBias_;
  61. };
  62. /// Cascaded shadow map parameters.
  63. struct CascadeParameters
  64. {
  65. /// Construct undefined.
  66. CascadeParameters()
  67. {
  68. }
  69. /// Construct with initial values.
  70. CascadeParameters(float split1, float split2, float split3, float split4, float fadeStart) :
  71. fadeStart_(fadeStart)
  72. {
  73. splits_[0] = split1;
  74. splits_[1] = split2;
  75. splits_[2] = split3;
  76. splits_[3] = split4;
  77. }
  78. /// Validate parameters.
  79. void Validate();
  80. /// Return shadow maximum range.
  81. float GetShadowRange() const
  82. {
  83. float ret = 0.0f;
  84. for (unsigned i = 0; i < MAX_CASCADE_SPLITS; ++i)
  85. ret = Max(ret, splits_[i]);
  86. return ret;
  87. }
  88. /// Near clip of first split.
  89. float start_;
  90. /// Far clip values of the splits.
  91. float splits_[MAX_CASCADE_SPLITS];
  92. /// The point relative to final split far clip where fade out begins. (0.0 - 1.0)
  93. float fadeStart_;
  94. };
  95. /// Shadow map focusing parameters.
  96. struct FocusParameters
  97. {
  98. /// Construct undefined.
  99. FocusParameters()
  100. {
  101. }
  102. /// Construct with initial values.
  103. FocusParameters(bool focus, bool nonUniform, bool autoSize, float quantize, float minView) :
  104. focus_(focus),
  105. nonUniform_(nonUniform),
  106. autoSize_(autoSize),
  107. quantize_(quantize),
  108. minView_(minView)
  109. {
  110. }
  111. /// Validate parameters.
  112. void Validate();
  113. /// Focus flag.
  114. bool focus_;
  115. /// Non-uniform focusing flag.
  116. bool nonUniform_;
  117. /// Auto-size (reduce resolution when far away) flag.
  118. bool autoSize_;
  119. /// Zoom out flag.
  120. bool zoomOut_;
  121. /// Focus quantization.
  122. float quantize_;
  123. /// Minimum view size.
  124. float minView_;
  125. };
  126. /// %Light component.
  127. class Light : public Drawable
  128. {
  129. OBJECT(Light);
  130. public:
  131. /// Construct.
  132. Light(Context* context);
  133. /// Destruct.
  134. virtual ~Light();
  135. /// Register object factory. Drawable must be registered first.
  136. static void RegisterObject(Context* context);
  137. /// Handle attribute change.
  138. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  139. /// Process octree raycast. May be called from a worker thread.
  140. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  141. /// Calculate distance for rendering. May be called from worker thread(s), possibly re-entrantly.
  142. virtual void UpdateDistance(const FrameInfo& frame);
  143. /// Add debug geometry to the debug renderer.
  144. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  145. /// %Set light type.
  146. void SetLightType(LightType type);
  147. /// %Set vertex lighting mode.
  148. void SetPerVertex(bool enable);
  149. /// %Set color.
  150. void SetColor(const Color& color);
  151. /// %Set specular intensity.
  152. void SetSpecularIntensity(float intensity);
  153. /// %Set range.
  154. void SetRange(float range);
  155. /// %Set spotlight field of view.
  156. void SetFov(float fov);
  157. /// %Set spotlight aspect ratio.
  158. void SetAspectRatio(float aspectRatio);
  159. /// %Set fade out start distance.
  160. void SetFadeDistance(float distance);
  161. /// %Set shadow fade out start distance. Only has effect if shadow distance is also non-zero.
  162. void SetShadowFadeDistance(float distance);
  163. /// %Set shadow depth bias parameters.
  164. void SetShadowBias(const BiasParameters& parameters);
  165. /// %Set directional light cascaded shadow parameters.
  166. void SetShadowCascade(const CascadeParameters& parameters);
  167. /// %Set shadow map focusing parameters.
  168. void SetShadowFocus(const FocusParameters& parameters);
  169. /// %Set shadow intensity between 0.0 - 1.0. 0.0 (the default) gives fully dark shadows.
  170. void SetShadowIntensity(float intensity);
  171. /// %Set shadow resolution between 0.25 - 1.0. Determines the shadow map to use.
  172. void SetShadowResolution(float resolution);
  173. /// %Set shadow camera near/far clip distance ratio.
  174. void SetShadowNearFarRatio(float nearFarRatio);
  175. /// %Set range attenuation texture.
  176. void SetRampTexture(Texture* texture);
  177. /// %Set spotlight attenuation texture.
  178. void SetShapeTexture(Texture* texture);
  179. /// Return light type.
  180. LightType GetLightType() const { return lightType_; }
  181. /// Return vertex lighting mode.
  182. bool GetPerVertex() const { return perVertex_; }
  183. /// Return color.
  184. const Color& GetColor() const { return color_; }
  185. /// Return specular intensity.
  186. float GetSpecularIntensity() const { return specularIntensity_; }
  187. /// Return range.
  188. float GetRange() const { return range_; }
  189. /// Return spotlight field of view.
  190. float GetFov() const { return fov_; }
  191. /// Return spotlight aspect ratio.
  192. float GetAspectRatio() const { return aspectRatio_; }
  193. /// Return fade start distance.
  194. float GetFadeDistance() const { return fadeDistance_; }
  195. /// Return shadow fade start distance.
  196. float GetShadowFadeDistance() const { return shadowFadeDistance_; }
  197. /// Return shadow depth bias parameters.
  198. const BiasParameters& GetShadowBias() const { return shadowBias_; }
  199. /// Return directional light cascaded shadow parameters.
  200. const CascadeParameters& GetShadowCascade() const { return shadowCascade_; }
  201. /// Return shadow map focus parameters.
  202. const FocusParameters& GetShadowFocus() const { return shadowFocus_; }
  203. /// Return shadow intensity.
  204. float GetShadowIntensity() const { return shadowIntensity_; }
  205. /// Return shadow resolution.
  206. float GetShadowResolution() const { return shadowResolution_; }
  207. /// Return shadow camera near/far clip distance ratio.
  208. float GetShadowNearFarRatio() const { return shadowNearFarRatio_; }
  209. /// Return range attenuation texture.
  210. Texture* GetRampTexture() const { return rampTexture_; }
  211. /// Return spotlight attenuation texture.
  212. Texture* GetShapeTexture() const { return shapeTexture_; }
  213. /// Return spotlight frustum.
  214. Frustum GetFrustum() const;
  215. /// %Set sort value based on intensity and view distance.
  216. void SetIntensitySortValue(float distance);
  217. /// %Set sort value based on overall intensity over a bounding box.
  218. void SetIntensitySortValue(const BoundingBox& box);
  219. /// Return directional light quad transform for either near or far split.
  220. Matrix3x4 GetDirLightTransform(const Camera& camera, bool getNearQuad = false);
  221. /// Return light volume model transform.
  222. const Matrix3x4& GetVolumeTransform(const Camera& camera);
  223. /// %Set ramp texture attribute.
  224. void SetRampTextureAttr(ResourceRef value);
  225. /// %Set shape texture attribute.
  226. void SetShapeTextureAttr(ResourceRef value);
  227. /// Return ramp texture attribute.
  228. ResourceRef GetRampTextureAttr() const;
  229. /// Return shape texture attribute.
  230. ResourceRef GetShapeTextureAttr() const;
  231. protected:
  232. /// Recalculate the world-space bounding box.
  233. virtual void OnWorldBoundingBoxUpdate();
  234. private:
  235. /// Light type.
  236. LightType lightType_;
  237. /// Per-vertex lighting flag.
  238. bool perVertex_;
  239. /// Color.
  240. Color color_;
  241. /// Specular intensity.
  242. float specularIntensity_;
  243. /// Range.
  244. float range_;
  245. /// Spotlight field of view.
  246. float fov_;
  247. /// Spotlight aspect ratio.
  248. float aspectRatio_;
  249. /// Fade start distance.
  250. float fadeDistance_;
  251. /// Shadow fade start distance.
  252. float shadowFadeDistance_;
  253. /// Shadow depth bias parameters.
  254. BiasParameters shadowBias_;
  255. /// Directional light cascaded shadow parameters.
  256. CascadeParameters shadowCascade_;
  257. /// Shadow map focus parameters.
  258. FocusParameters shadowFocus_;
  259. /// Custom world transform for the light volume.
  260. Matrix3x4 volumeTransform_;
  261. /// Shadow intensity.
  262. float shadowIntensity_;
  263. /// Shadow resolution.
  264. float shadowResolution_;
  265. /// Shadow camera near/far clip distance ratio.
  266. float shadowNearFarRatio_;
  267. /// Range attenuation texture.
  268. SharedPtr<Texture> rampTexture_;
  269. /// Spotlight attenuation texture.
  270. SharedPtr<Texture> shapeTexture_;
  271. };