Light.h 10 KB

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