BsLight.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. float pi2 = Math::PI * Math::PI;
  59. switch (mType)
  60. {
  61. case LightType::Radial:
  62. if (mSourceRadius > 0.0f)
  63. return mIntensity / (4 * radius2 * pi2); // Luminous flux -> luminance
  64. else
  65. return mIntensity / (4 * Math::PI); // Luminous flux -> luminous intensity
  66. case LightType::Spot:
  67. {
  68. if (mSourceRadius > 0.0f)
  69. return mIntensity / (radius2 * pi2); // Luminous flux -> luminance
  70. else
  71. {
  72. // Note: Consider using the simpler conversion I / PI to match with the area-light conversion
  73. float cosTotalAngle = Math::cos(mSpotAngle);
  74. float cosFalloffAngle = Math::cos(mSpotFalloffAngle);
  75. // Luminous flux -> luminous intensity
  76. return mIntensity / (Math::TWO_PI * (1.0f - (cosFalloffAngle + cosTotalAngle) * 0.5f));
  77. }
  78. }
  79. case LightType::Directional:
  80. if (mSourceRadius > 0.0f)
  81. {
  82. float angularDiameter = mSourceRadius * 2; // Assumed in degrees
  83. // Use cone solid angle formulae to calculate disc solid angle
  84. float solidAngle = Math::TWO_PI * (1 - cos(0.5f * angularDiameter * Math::DEG2RAD));
  85. return mIntensity / solidAngle; // Illuminance -> luminance
  86. }
  87. else
  88. return mIntensity; // In luminance units by default
  89. default:
  90. return 0.0f;
  91. }
  92. }
  93. void LightBase::updateAttenuationRange()
  94. {
  95. // Value to which intensity needs to drop in order for the light contribution to fade out to zero
  96. const float minAttenuation = 0.2f;
  97. if(mSourceRadius > 0.0f)
  98. {
  99. // Inverse of the attenuation formula for area lights:
  100. // a = I / (1 + (2/r) * d + (1/r^2) * d^2
  101. // Where r is the source radius, and d is the distance from the light. As derived here:
  102. // https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
  103. float luminousFlux = getIntensity();
  104. float a = sqrt(minAttenuation);
  105. mAttRadius = (mSourceRadius * (sqrt(luminousFlux - a))) / a;
  106. }
  107. else // Based on the basic inverse square distance formula
  108. {
  109. float luminousIntensity = getLuminance();
  110. float a = minAttenuation;
  111. mAttRadius = sqrt(std::max(0.0f, luminousIntensity / a));
  112. }
  113. updateBounds();
  114. }
  115. void LightBase::updateBounds()
  116. {
  117. switch (mType)
  118. {
  119. case LightType::Directional:
  120. mBounds = Sphere(mPosition, std::numeric_limits<float>::infinity());
  121. break;
  122. case LightType::Radial:
  123. mBounds = Sphere(mPosition, mAttRadius);
  124. break;
  125. case LightType::Spot:
  126. {
  127. // Note: We could use the formula for calculating the circumcircle of a triangle (after projecting the cone),
  128. // but the radius of the sphere is the same as in the formula we use here, yet it is much simpler with no need
  129. // to calculate multiple determinants. Neither are good approximations when cone angle is wide.
  130. Vector3 offset(0, 0, mAttRadius * 0.5f);
  131. // Direction along the edge of the cone, on the YZ plane (doesn't matter if we used XZ instead)
  132. Degree angle = Math::clamp(mSpotAngle, Degree(-89), Degree(89));
  133. Vector3 coneDir(0, Math::tan(angle)*mAttRadius, mAttRadius);
  134. // Distance between the "corner" of the cone and our center, must be the radius (provided the center is at
  135. // the middle of the range)
  136. float radius = (offset - coneDir).length();
  137. Vector3 center = mPosition + mRotation.rotate(offset);
  138. mBounds = Sphere(center, radius);
  139. }
  140. break;
  141. }
  142. }
  143. Light::Light()
  144. :mLastUpdateHash(0)
  145. {
  146. }
  147. Light::Light(LightType type, Color color, float intensity, float attRadius, float srcRadius, bool castsShadows,
  148. Degree spotAngle, Degree spotFalloffAngle)
  149. : LightBase(type, color, intensity, attRadius, srcRadius, castsShadows, spotAngle, spotFalloffAngle),
  150. mLastUpdateHash(0)
  151. {
  152. // Calling virtual method is okay here because this is the most derived type
  153. updateBounds();
  154. }
  155. SPtr<ct::Light> Light::getCore() const
  156. {
  157. return std::static_pointer_cast<ct::Light>(mCoreSpecific);
  158. }
  159. SPtr<Light> Light::create(LightType type, Color color,
  160. float intensity, float attRadius, bool castsShadows, Degree spotAngle, Degree spotFalloffAngle)
  161. {
  162. Light* handler = new (bs_alloc<Light>())
  163. Light(type, color, intensity, attRadius, 0.0f, castsShadows, spotAngle, spotFalloffAngle);
  164. SPtr<Light> handlerPtr = bs_core_ptr<Light>(handler);
  165. handlerPtr->_setThisPtr(handlerPtr);
  166. handlerPtr->initialize();
  167. return handlerPtr;
  168. }
  169. SPtr<Light> Light::createEmpty()
  170. {
  171. Light* handler = new (bs_alloc<Light>()) Light();
  172. SPtr<Light> handlerPtr = bs_core_ptr<Light>(handler);
  173. handlerPtr->_setThisPtr(handlerPtr);
  174. return handlerPtr;
  175. }
  176. SPtr<ct::CoreObject> Light::createCore() const
  177. {
  178. ct::Light* handler = new (bs_alloc<ct::Light>())
  179. ct::Light(mType, mColor, mIntensity, mAttRadius, mSourceRadius, mCastsShadows, mSpotAngle, mSpotFalloffAngle);
  180. SPtr<ct::Light> handlerPtr = bs_shared_ptr<ct::Light>(handler);
  181. handlerPtr->_setThisPtr(handlerPtr);
  182. return handlerPtr;
  183. }
  184. CoreSyncData Light::syncToCore(FrameAlloc* allocator)
  185. {
  186. UINT32 size = 0;
  187. size += rttiGetElemSize(mPosition);
  188. size += rttiGetElemSize(mRotation);
  189. size += rttiGetElemSize(mType);
  190. size += rttiGetElemSize(mCastsShadows);
  191. size += rttiGetElemSize(mColor);
  192. size += rttiGetElemSize(mAttRadius);
  193. size += rttiGetElemSize(mSourceRadius);
  194. size += rttiGetElemSize(mIntensity);
  195. size += rttiGetElemSize(mSpotAngle);
  196. size += rttiGetElemSize(mSpotFalloffAngle);
  197. size += rttiGetElemSize(mAutoAttenuation);
  198. size += rttiGetElemSize(mIsActive);
  199. size += rttiGetElemSize(getCoreDirtyFlags());
  200. size += rttiGetElemSize(mBounds);
  201. UINT8* buffer = allocator->alloc(size);
  202. char* dataPtr = (char*)buffer;
  203. dataPtr = rttiWriteElem(mPosition, dataPtr);
  204. dataPtr = rttiWriteElem(mRotation, dataPtr);
  205. dataPtr = rttiWriteElem(mType, dataPtr);
  206. dataPtr = rttiWriteElem(mCastsShadows, dataPtr);
  207. dataPtr = rttiWriteElem(mColor, dataPtr);
  208. dataPtr = rttiWriteElem(mAttRadius, dataPtr);
  209. dataPtr = rttiWriteElem(mSourceRadius, dataPtr);
  210. dataPtr = rttiWriteElem(mIntensity, dataPtr);
  211. dataPtr = rttiWriteElem(mSpotAngle, dataPtr);
  212. dataPtr = rttiWriteElem(mSpotFalloffAngle, dataPtr);
  213. dataPtr = rttiWriteElem(mAutoAttenuation, dataPtr);
  214. dataPtr = rttiWriteElem(mIsActive, dataPtr);
  215. dataPtr = rttiWriteElem(getCoreDirtyFlags(), dataPtr);
  216. dataPtr = rttiWriteElem(mBounds, dataPtr);
  217. return CoreSyncData(buffer, size);
  218. }
  219. void Light::_updateTransform(const HSceneObject& parent)
  220. {
  221. UINT32 curHash = parent->getTransformHash();
  222. if (curHash != _getLastModifiedHash())
  223. {
  224. setPosition(parent->getWorldPosition());
  225. setRotation(parent->getWorldRotation());
  226. _setLastModifiedHash(curHash);
  227. }
  228. }
  229. void Light::_markCoreDirty(LightDirtyFlag flag)
  230. {
  231. markCoreDirty((UINT32)flag);
  232. }
  233. RTTITypeBase* Light::getRTTIStatic()
  234. {
  235. return LightRTTI::instance();
  236. }
  237. RTTITypeBase* Light::getRTTI() const
  238. {
  239. return Light::getRTTIStatic();
  240. }
  241. namespace ct
  242. {
  243. const UINT32 Light::LIGHT_CONE_NUM_SIDES = 20;
  244. const UINT32 Light::LIGHT_CONE_NUM_SLICES = 10;
  245. Light::Light(LightType type, Color color,
  246. float intensity, float attRadius, float srcRadius, bool castsShadows, Degree spotAngle, Degree spotFalloffAngle)
  247. :LightBase(type, color, intensity, attRadius, srcRadius, castsShadows, spotAngle, spotFalloffAngle), mRendererId(0)
  248. {
  249. }
  250. Light::~Light()
  251. {
  252. gRenderer()->notifyLightRemoved(this);
  253. }
  254. void Light::initialize()
  255. {
  256. updateBounds();
  257. gRenderer()->notifyLightAdded(this);
  258. CoreObject::initialize();
  259. }
  260. void Light::syncToCore(const CoreSyncData& data)
  261. {
  262. char* dataPtr = (char*)data.getBuffer();
  263. UINT32 dirtyFlags = 0;
  264. bool oldIsActive = mIsActive;
  265. LightType oldType = mType;
  266. dataPtr = rttiReadElem(mPosition, dataPtr);
  267. dataPtr = rttiReadElem(mRotation, dataPtr);
  268. dataPtr = rttiReadElem(mType, dataPtr);
  269. dataPtr = rttiReadElem(mCastsShadows, dataPtr);
  270. dataPtr = rttiReadElem(mColor, dataPtr);
  271. dataPtr = rttiReadElem(mAttRadius, dataPtr);
  272. dataPtr = rttiReadElem(mSourceRadius, dataPtr);
  273. dataPtr = rttiReadElem(mIntensity, dataPtr);
  274. dataPtr = rttiReadElem(mSpotAngle, dataPtr);
  275. dataPtr = rttiReadElem(mSpotFalloffAngle, dataPtr);
  276. dataPtr = rttiReadElem(mAutoAttenuation, dataPtr);
  277. dataPtr = rttiReadElem(mIsActive, dataPtr);
  278. dataPtr = rttiReadElem(dirtyFlags, dataPtr);
  279. dataPtr = rttiReadElem(mBounds, dataPtr);
  280. updateBounds();
  281. if (dirtyFlags == (UINT32)LightDirtyFlag::Transform)
  282. {
  283. if (mIsActive)
  284. gRenderer()->notifyLightUpdated(this);
  285. }
  286. else
  287. {
  288. if (oldIsActive != mIsActive)
  289. {
  290. if (mIsActive)
  291. gRenderer()->notifyLightAdded(this);
  292. else
  293. {
  294. LightType newType = mType;
  295. mType = oldType;
  296. gRenderer()->notifyLightRemoved(this);
  297. mType = newType;
  298. }
  299. }
  300. else
  301. {
  302. LightType newType = mType;
  303. mType = oldType;
  304. gRenderer()->notifyLightRemoved(this);
  305. mType = newType;
  306. gRenderer()->notifyLightAdded(this);
  307. }
  308. }
  309. }
  310. }}