BsLight.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsLight.h"
  4. #include "BsLightRTTI.h"
  5. #include "BsRenderer.h"
  6. #include "BsFrameAlloc.h"
  7. #include "BsSceneObject.h"
  8. #include "BsMesh.h"
  9. namespace bs
  10. {
  11. LightBase::LightBase()
  12. : mPosition(BsZero), mRotation(BsIdentity), mType(LightType::Radial), mCastsShadows(false), mColor(Color::White)
  13. , mAttRadius(10.0f), mSourceRadius(0.0f), mIntensity(5.0f), mSpotAngle(45), mSpotFalloffAngle(35.0f)
  14. , mIsActive(true), mAutoAttenuation(true)
  15. {
  16. updateAttenuationRange();
  17. }
  18. LightBase::LightBase(LightType type, Color color, float intensity, float attRadius, float srcRadius, bool castsShadows,
  19. Degree spotAngle, Degree spotFalloffAngle)
  20. : mPosition(BsZero), mRotation(BsIdentity), mType(type), mCastsShadows(castsShadows), mColor(color)
  21. , mAttRadius(attRadius), mSourceRadius(srcRadius), mIntensity(intensity), mSpotAngle(spotAngle)
  22. , mSpotFalloffAngle(spotFalloffAngle), mIsActive(true), mAutoAttenuation(true)
  23. {
  24. updateAttenuationRange();
  25. }
  26. void LightBase::setUseAutoAttenuation(bool enabled)
  27. {
  28. mAutoAttenuation = enabled;
  29. if(enabled)
  30. updateAttenuationRange();
  31. _markCoreDirty();
  32. }
  33. void LightBase::setAttenuationRadius(float radius)
  34. {
  35. if (mAutoAttenuation)
  36. return;
  37. mAttRadius = radius;
  38. _markCoreDirty();
  39. updateBounds();
  40. }
  41. void LightBase::setSourceRadius(float radius)
  42. {
  43. mSourceRadius = radius;
  44. if (mAutoAttenuation)
  45. updateAttenuationRange();
  46. _markCoreDirty();
  47. }
  48. void LightBase::setIntensity(float intensity)
  49. {
  50. mIntensity = intensity;
  51. if (mAutoAttenuation)
  52. updateAttenuationRange();
  53. _markCoreDirty();
  54. }
  55. float LightBase::getLuminance() const
  56. {
  57. float radius2 = mSourceRadius * mSourceRadius;
  58. switch (mType)
  59. {
  60. case LightType::Radial:
  61. if (mSourceRadius > 0.0f)
  62. return mIntensity / (4 * radius2 * Math::PI); // Luminous flux -> luminance
  63. else
  64. return mIntensity / (4 * Math::PI); // Luminous flux -> luminous intensity
  65. case LightType::Spot:
  66. {
  67. if (mSourceRadius > 0.0f)
  68. return mIntensity / (radius2 * Math::PI); // Luminous flux -> luminance
  69. else
  70. {
  71. // Note: Consider using the simpler conversion I / PI to match with the area-light conversion
  72. float cosTotalAngle = Math::cos(mSpotAngle);
  73. float cosFalloffAngle = Math::cos(mSpotFalloffAngle);
  74. // Luminous flux -> luminous intensity
  75. return mIntensity / (Math::TWO_PI * (1.0f - (cosFalloffAngle + cosTotalAngle) * 0.5f));
  76. }
  77. }
  78. case LightType::Directional:
  79. if (mSourceRadius > 0.0f)
  80. {
  81. // Use cone solid angle formulae to calculate disc solid angle
  82. float solidAngle = Math::TWO_PI * (1 - cos(mSourceRadius * Math::DEG2RAD));
  83. return mIntensity / solidAngle; // Illuminance -> luminance
  84. }
  85. else
  86. return mIntensity; // In luminance units by default
  87. default:
  88. return 0.0f;
  89. }
  90. }
  91. void LightBase::updateAttenuationRange()
  92. {
  93. // Value to which intensity needs to drop in order for the light contribution to fade out to zero
  94. const float minAttenuation = 0.2f;
  95. if(mSourceRadius > 0.0f)
  96. {
  97. // Inverse of the attenuation formula for area lights:
  98. // a = I / (1 + (2/r) * d + (1/r^2) * d^2
  99. // Where r is the source radius, and d is the distance from the light. As derived here:
  100. // https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
  101. float luminousFlux = getIntensity();
  102. float a = sqrt(minAttenuation);
  103. mAttRadius = (mSourceRadius * (sqrt(luminousFlux - a))) / a;
  104. }
  105. else // Based on the basic inverse square distance formula
  106. {
  107. float luminousIntensity = getIntensity();
  108. float a = minAttenuation;
  109. mAttRadius = sqrt(std::max(0.0f, luminousIntensity / a));
  110. }
  111. updateBounds();
  112. }
  113. void LightBase::updateBounds()
  114. {
  115. switch (mType)
  116. {
  117. case LightType::Directional:
  118. mBounds = Sphere(mPosition, std::numeric_limits<float>::infinity());
  119. break;
  120. case LightType::Radial:
  121. mBounds = Sphere(mPosition, 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 = mPosition + mRotation.rotate(offset);
  136. mBounds = Sphere(center, radius);
  137. }
  138. break;
  139. }
  140. }
  141. Light::Light()
  142. :mLastUpdateHash(0)
  143. {
  144. }
  145. Light::Light(LightType type, Color color, float intensity, float attRadius, float srcRadius, bool castsShadows,
  146. Degree spotAngle, Degree spotFalloffAngle)
  147. : LightBase(type, color, intensity, attRadius, srcRadius, castsShadows, spotAngle, spotFalloffAngle),
  148. mLastUpdateHash(0)
  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 = 0;
  185. size += rttiGetElemSize(mPosition);
  186. size += rttiGetElemSize(mRotation);
  187. size += rttiGetElemSize(mType);
  188. size += rttiGetElemSize(mCastsShadows);
  189. size += rttiGetElemSize(mColor);
  190. size += rttiGetElemSize(mAttRadius);
  191. size += rttiGetElemSize(mSourceRadius);
  192. size += rttiGetElemSize(mIntensity);
  193. size += rttiGetElemSize(mSpotAngle);
  194. size += rttiGetElemSize(mSpotFalloffAngle);
  195. size += rttiGetElemSize(mAutoAttenuation);
  196. size += rttiGetElemSize(mIsActive);
  197. size += rttiGetElemSize(getCoreDirtyFlags());
  198. size += rttiGetElemSize(mBounds);
  199. UINT8* buffer = allocator->alloc(size);
  200. char* dataPtr = (char*)buffer;
  201. dataPtr = rttiWriteElem(mPosition, dataPtr);
  202. dataPtr = rttiWriteElem(mRotation, dataPtr);
  203. dataPtr = rttiWriteElem(mType, dataPtr);
  204. dataPtr = rttiWriteElem(mCastsShadows, dataPtr);
  205. dataPtr = rttiWriteElem(mColor, dataPtr);
  206. dataPtr = rttiWriteElem(mAttRadius, dataPtr);
  207. dataPtr = rttiWriteElem(mSourceRadius, dataPtr);
  208. dataPtr = rttiWriteElem(mIntensity, dataPtr);
  209. dataPtr = rttiWriteElem(mSpotAngle, dataPtr);
  210. dataPtr = rttiWriteElem(mSpotFalloffAngle, dataPtr);
  211. dataPtr = rttiWriteElem(mAutoAttenuation, dataPtr);
  212. dataPtr = rttiWriteElem(mIsActive, dataPtr);
  213. dataPtr = rttiWriteElem(getCoreDirtyFlags(), dataPtr);
  214. dataPtr = rttiWriteElem(mBounds, dataPtr);
  215. return CoreSyncData(buffer, size);
  216. }
  217. void Light::_updateTransform(const HSceneObject& parent)
  218. {
  219. UINT32 curHash = parent->getTransformHash();
  220. if (curHash != _getLastModifiedHash())
  221. {
  222. setPosition(parent->getWorldPosition());
  223. setRotation(parent->getWorldRotation());
  224. _setLastModifiedHash(curHash);
  225. }
  226. }
  227. void Light::_markCoreDirty(LightDirtyFlag flag)
  228. {
  229. markCoreDirty((UINT32)flag);
  230. }
  231. RTTITypeBase* Light::getRTTIStatic()
  232. {
  233. return LightRTTI::instance();
  234. }
  235. RTTITypeBase* Light::getRTTI() const
  236. {
  237. return Light::getRTTIStatic();
  238. }
  239. namespace ct
  240. {
  241. const UINT32 Light::LIGHT_CONE_NUM_SIDES = 20;
  242. const UINT32 Light::LIGHT_CONE_NUM_SLICES = 10;
  243. Light::Light(LightType type, Color color,
  244. float intensity, float attRadius, float srcRadius, bool castsShadows, Degree spotAngle, Degree spotFalloffAngle)
  245. :LightBase(type, color, intensity, attRadius, srcRadius, castsShadows, spotAngle, spotFalloffAngle), mRendererId(0)
  246. {
  247. }
  248. Light::~Light()
  249. {
  250. gRenderer()->notifyLightRemoved(this);
  251. }
  252. void Light::initialize()
  253. {
  254. updateBounds();
  255. gRenderer()->notifyLightAdded(this);
  256. CoreObject::initialize();
  257. }
  258. void Light::syncToCore(const CoreSyncData& data)
  259. {
  260. char* dataPtr = (char*)data.getBuffer();
  261. UINT32 dirtyFlags = 0;
  262. bool oldIsActive = mIsActive;
  263. LightType oldType = mType;
  264. dataPtr = rttiReadElem(mPosition, dataPtr);
  265. dataPtr = rttiReadElem(mRotation, dataPtr);
  266. dataPtr = rttiReadElem(mType, dataPtr);
  267. dataPtr = rttiReadElem(mCastsShadows, dataPtr);
  268. dataPtr = rttiReadElem(mColor, dataPtr);
  269. dataPtr = rttiReadElem(mAttRadius, dataPtr);
  270. dataPtr = rttiReadElem(mSourceRadius, dataPtr);
  271. dataPtr = rttiReadElem(mIntensity, dataPtr);
  272. dataPtr = rttiReadElem(mSpotAngle, dataPtr);
  273. dataPtr = rttiReadElem(mSpotFalloffAngle, dataPtr);
  274. dataPtr = rttiReadElem(mAutoAttenuation, dataPtr);
  275. dataPtr = rttiReadElem(mIsActive, dataPtr);
  276. dataPtr = rttiReadElem(dirtyFlags, dataPtr);
  277. dataPtr = rttiReadElem(mBounds, dataPtr);
  278. updateBounds();
  279. if (dirtyFlags == (UINT32)LightDirtyFlag::Transform)
  280. {
  281. if (mIsActive)
  282. gRenderer()->notifyLightUpdated(this);
  283. }
  284. else
  285. {
  286. if (oldIsActive != mIsActive)
  287. {
  288. if (mIsActive)
  289. gRenderer()->notifyLightAdded(this);
  290. else
  291. {
  292. LightType newType = mType;
  293. mType = oldType;
  294. gRenderer()->notifyLightRemoved(this);
  295. mType = newType;
  296. }
  297. }
  298. else
  299. {
  300. LightType newType = mType;
  301. mType = oldType;
  302. gRenderer()->notifyLightRemoved(this);
  303. mType = newType;
  304. gRenderer()->notifyLightAdded(this);
  305. }
  306. }
  307. }
  308. }}