BsLight.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Renderer/BsLight.h"
  4. #include "RTTI/BsLightRTTI.h"
  5. #include "Renderer/BsRenderer.h"
  6. #include "Allocators/BsFrameAlloc.h"
  7. #include "Scene/BsSceneObject.h"
  8. #include "Mesh/BsMesh.h"
  9. namespace bs
  10. {
  11. LightBase::LightBase()
  12. : mType(LightType::Radial), mCastsShadows(false), mColor(Color::White), mAttRadius(10.0f), mSourceRadius(0.0f)
  13. , mIntensity(5.0f), mSpotAngle(45), mSpotFalloffAngle(35.0f), mAutoAttenuation(true), mShadowBias(0.5f)
  14. {
  15. updateAttenuationRange();
  16. }
  17. LightBase::LightBase(LightType type, Color color, float intensity, float attRadius, float srcRadius, bool castsShadows,
  18. Degree spotAngle, Degree spotFalloffAngle)
  19. : mType(type), mCastsShadows(castsShadows), mColor(color), mAttRadius(attRadius), mSourceRadius(srcRadius)
  20. , mIntensity(intensity), mSpotAngle(spotAngle), mSpotFalloffAngle(spotFalloffAngle), mAutoAttenuation(true)
  21. , mShadowBias(0.5f)
  22. {
  23. updateAttenuationRange();
  24. }
  25. void LightBase::setUseAutoAttenuation(bool enabled)
  26. {
  27. mAutoAttenuation = enabled;
  28. if(enabled)
  29. updateAttenuationRange();
  30. _markCoreDirty();
  31. }
  32. void LightBase::setAttenuationRadius(float radius)
  33. {
  34. if (mAutoAttenuation)
  35. return;
  36. mAttRadius = radius;
  37. _markCoreDirty();
  38. updateBounds();
  39. }
  40. void LightBase::setSourceRadius(float radius)
  41. {
  42. mSourceRadius = radius;
  43. if (mAutoAttenuation)
  44. updateAttenuationRange();
  45. _markCoreDirty();
  46. }
  47. void LightBase::setIntensity(float intensity)
  48. {
  49. mIntensity = intensity;
  50. if (mAutoAttenuation)
  51. updateAttenuationRange();
  52. _markCoreDirty();
  53. }
  54. float LightBase::getLuminance() const
  55. {
  56. float radius2 = mSourceRadius * mSourceRadius;
  57. switch (mType)
  58. {
  59. case LightType::Radial:
  60. if (mSourceRadius > 0.0f)
  61. return mIntensity / (4 * radius2 * Math::PI); // Luminous flux -> luminance
  62. else
  63. return mIntensity / (4 * Math::PI); // Luminous flux -> luminous intensity
  64. case LightType::Spot:
  65. {
  66. if (mSourceRadius > 0.0f)
  67. return mIntensity / (radius2 * Math::PI); // Luminous flux -> luminance
  68. else
  69. {
  70. // Note: Consider using the simpler conversion I / PI to match with the area-light conversion
  71. float cosTotalAngle = Math::cos(mSpotAngle);
  72. float cosFalloffAngle = Math::cos(mSpotFalloffAngle);
  73. // Luminous flux -> luminous intensity
  74. return mIntensity / (Math::TWO_PI * (1.0f - (cosFalloffAngle + cosTotalAngle) * 0.5f));
  75. }
  76. }
  77. case LightType::Directional:
  78. if (mSourceRadius > 0.0f)
  79. {
  80. // Use cone solid angle formulae to calculate disc solid angle
  81. float solidAngle = Math::TWO_PI * (1 - cos(mSourceRadius * Math::DEG2RAD));
  82. return mIntensity / solidAngle; // Illuminance -> luminance
  83. }
  84. else
  85. return mIntensity; // In luminance units by default
  86. default:
  87. return 0.0f;
  88. }
  89. }
  90. void LightBase::updateAttenuationRange()
  91. {
  92. // Value to which intensity needs to drop in order for the light contribution to fade out to zero
  93. const float minAttenuation = 0.2f;
  94. if(mSourceRadius > 0.0f)
  95. {
  96. // Inverse of the attenuation formula for area lights:
  97. // a = I / (1 + (2/r) * d + (1/r^2) * d^2
  98. // Where r is the source radius, and d is the distance from the light. As derived here:
  99. // https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
  100. float luminousFlux = getIntensity();
  101. float a = sqrt(minAttenuation);
  102. mAttRadius = (mSourceRadius * (sqrt(luminousFlux - a))) / a;
  103. }
  104. else // Based on the basic inverse square distance formula
  105. {
  106. float luminousIntensity = getIntensity();
  107. float a = minAttenuation;
  108. mAttRadius = sqrt(std::max(0.0f, luminousIntensity / a));
  109. }
  110. updateBounds();
  111. }
  112. void LightBase::updateBounds()
  113. {
  114. const Transform& tfrm = getTransform();
  115. switch (mType)
  116. {
  117. case LightType::Directional:
  118. mBounds = Sphere(tfrm.getPosition(), std::numeric_limits<float>::infinity());
  119. break;
  120. case LightType::Radial:
  121. mBounds = Sphere(tfrm.getPosition(), mAttRadius);
  122. break;
  123. case LightType::Spot:
  124. {
  125. // Note: We could use the formula for calculating the circumcircle of a triangle (after projecting the cone),
  126. // but the radius of the sphere is the same as in the formula we use here, yet it is much simpler with no need
  127. // to calculate multiple determinants. Neither are good approximations when cone angle is wide.
  128. Vector3 offset(0, 0, mAttRadius * 0.5f);
  129. // Direction along the edge of the cone, on the YZ plane (doesn't matter if we used XZ instead)
  130. Degree angle = Math::clamp(mSpotAngle, Degree(-89), Degree(89));
  131. Vector3 coneDir(0, Math::tan(angle)*mAttRadius, mAttRadius);
  132. // Distance between the "corner" of the cone and our center, must be the radius (provided the center is at
  133. // the middle of the range)
  134. float radius = (offset - coneDir).length();
  135. Vector3 center = tfrm.getPosition() + tfrm.getRotation().rotate(offset);
  136. mBounds = Sphere(center, radius);
  137. }
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. Light::Light()
  144. {
  145. }
  146. Light::Light(LightType type, Color color, float intensity, float attRadius, float srcRadius, bool castsShadows,
  147. Degree spotAngle, Degree spotFalloffAngle)
  148. : LightBase(type, color, intensity, attRadius, srcRadius, castsShadows, spotAngle, spotFalloffAngle)
  149. {
  150. // Calling virtual method is okay here because this is the most derived type
  151. updateBounds();
  152. }
  153. SPtr<ct::Light> Light::getCore() const
  154. {
  155. return std::static_pointer_cast<ct::Light>(mCoreSpecific);
  156. }
  157. SPtr<Light> Light::create(LightType type, Color color,
  158. float intensity, float attRadius, bool castsShadows, Degree spotAngle, Degree spotFalloffAngle)
  159. {
  160. Light* handler = new (bs_alloc<Light>())
  161. Light(type, color, intensity, attRadius, 0.0f, castsShadows, spotAngle, spotFalloffAngle);
  162. SPtr<Light> handlerPtr = bs_core_ptr<Light>(handler);
  163. handlerPtr->_setThisPtr(handlerPtr);
  164. handlerPtr->initialize();
  165. return handlerPtr;
  166. }
  167. SPtr<Light> Light::createEmpty()
  168. {
  169. Light* handler = new (bs_alloc<Light>()) Light();
  170. SPtr<Light> handlerPtr = bs_core_ptr<Light>(handler);
  171. handlerPtr->_setThisPtr(handlerPtr);
  172. return handlerPtr;
  173. }
  174. SPtr<ct::CoreObject> Light::createCore() const
  175. {
  176. ct::Light* handler = new (bs_alloc<ct::Light>())
  177. ct::Light(mType, mColor, mIntensity, mAttRadius, mSourceRadius, mCastsShadows, mSpotAngle, mSpotFalloffAngle);
  178. SPtr<ct::Light> handlerPtr = bs_shared_ptr<ct::Light>(handler);
  179. handlerPtr->_setThisPtr(handlerPtr);
  180. return handlerPtr;
  181. }
  182. CoreSyncData Light::syncToCore(FrameAlloc* allocator)
  183. {
  184. UINT32 size = getActorSyncDataSize();
  185. size += rttiGetElemSize(mType);
  186. size += rttiGetElemSize(mCastsShadows);
  187. size += rttiGetElemSize(mColor);
  188. size += rttiGetElemSize(mAttRadius);
  189. size += rttiGetElemSize(mSourceRadius);
  190. size += rttiGetElemSize(mIntensity);
  191. size += rttiGetElemSize(mSpotAngle);
  192. size += rttiGetElemSize(mSpotFalloffAngle);
  193. size += rttiGetElemSize(mAutoAttenuation);
  194. size += rttiGetElemSize(getCoreDirtyFlags());
  195. size += rttiGetElemSize(mBounds);
  196. size += rttiGetElemSize(mShadowBias);
  197. UINT8* buffer = allocator->alloc(size);
  198. char* dataPtr = (char*)buffer;
  199. dataPtr = syncActorTo(dataPtr);
  200. dataPtr = rttiWriteElem(mType, dataPtr);
  201. dataPtr = rttiWriteElem(mCastsShadows, dataPtr);
  202. dataPtr = rttiWriteElem(mColor, dataPtr);
  203. dataPtr = rttiWriteElem(mAttRadius, dataPtr);
  204. dataPtr = rttiWriteElem(mSourceRadius, dataPtr);
  205. dataPtr = rttiWriteElem(mIntensity, dataPtr);
  206. dataPtr = rttiWriteElem(mSpotAngle, dataPtr);
  207. dataPtr = rttiWriteElem(mSpotFalloffAngle, dataPtr);
  208. dataPtr = rttiWriteElem(mAutoAttenuation, dataPtr);
  209. dataPtr = rttiWriteElem(getCoreDirtyFlags(), dataPtr);
  210. dataPtr = rttiWriteElem(mBounds, dataPtr);
  211. dataPtr = rttiWriteElem(mShadowBias, dataPtr);
  212. return CoreSyncData(buffer, size);
  213. }
  214. void Light::_markCoreDirty(ActorDirtyFlag flag)
  215. {
  216. markCoreDirty((UINT32)flag);
  217. }
  218. RTTITypeBase* Light::getRTTIStatic()
  219. {
  220. return LightRTTI::instance();
  221. }
  222. RTTITypeBase* Light::getRTTI() const
  223. {
  224. return Light::getRTTIStatic();
  225. }
  226. namespace ct
  227. {
  228. const UINT32 Light::LIGHT_CONE_NUM_SIDES = 20;
  229. const UINT32 Light::LIGHT_CONE_NUM_SLICES = 10;
  230. Light::Light(LightType type, Color color,
  231. float intensity, float attRadius, float srcRadius, bool castsShadows, Degree spotAngle, Degree spotFalloffAngle)
  232. :LightBase(type, color, intensity, attRadius, srcRadius, castsShadows, spotAngle, spotFalloffAngle), mRendererId(0)
  233. {
  234. }
  235. Light::~Light()
  236. {
  237. gRenderer()->notifyLightRemoved(this);
  238. }
  239. void Light::initialize()
  240. {
  241. updateBounds();
  242. gRenderer()->notifyLightAdded(this);
  243. CoreObject::initialize();
  244. }
  245. void Light::syncToCore(const CoreSyncData& data)
  246. {
  247. char* dataPtr = (char*)data.getBuffer();
  248. UINT32 dirtyFlags = 0;
  249. bool oldIsActive = mActive;
  250. LightType oldType = mType;
  251. dataPtr = syncActorFrom(dataPtr);
  252. dataPtr = rttiReadElem(mType, dataPtr);
  253. dataPtr = rttiReadElem(mCastsShadows, dataPtr);
  254. dataPtr = rttiReadElem(mColor, dataPtr);
  255. dataPtr = rttiReadElem(mAttRadius, dataPtr);
  256. dataPtr = rttiReadElem(mSourceRadius, dataPtr);
  257. dataPtr = rttiReadElem(mIntensity, dataPtr);
  258. dataPtr = rttiReadElem(mSpotAngle, dataPtr);
  259. dataPtr = rttiReadElem(mSpotFalloffAngle, dataPtr);
  260. dataPtr = rttiReadElem(mAutoAttenuation, dataPtr);
  261. dataPtr = rttiReadElem(dirtyFlags, dataPtr);
  262. dataPtr = rttiReadElem(mBounds, dataPtr);
  263. dataPtr = rttiReadElem(mShadowBias, dataPtr);
  264. updateBounds();
  265. if((dirtyFlags & ((UINT32)ActorDirtyFlag::Everything | (UINT32)ActorDirtyFlag::Active)) != 0)
  266. {
  267. if (oldIsActive != mActive)
  268. {
  269. if (mActive)
  270. gRenderer()->notifyLightAdded(this);
  271. else
  272. {
  273. LightType newType = mType;
  274. mType = oldType;
  275. gRenderer()->notifyLightRemoved(this);
  276. mType = newType;
  277. }
  278. }
  279. else
  280. {
  281. LightType newType = mType;
  282. mType = oldType;
  283. gRenderer()->notifyLightRemoved(this);
  284. mType = newType;
  285. gRenderer()->notifyLightAdded(this);
  286. }
  287. }
  288. else if((dirtyFlags & (UINT32)ActorDirtyFlag::Mobility) != 0)
  289. {
  290. gRenderer()->notifyLightRemoved(this);
  291. gRenderer()->notifyLightAdded(this);
  292. }
  293. else if ((dirtyFlags & (UINT32)ActorDirtyFlag::Transform) != 0)
  294. {
  295. if (mActive)
  296. gRenderer()->notifyLightUpdated(this);
  297. }
  298. }
  299. }}