Light.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #ifndef ANKI_SCENE_LIGHT_H
  2. #define ANKI_SCENE_LIGHT_H
  3. #include "anki/scene/SceneNode.h"
  4. #include "anki/scene/MoveComponent.h"
  5. #include "anki/scene/FrustumComponent.h"
  6. #include "anki/scene/SpatialComponent.h"
  7. #include "anki/resource/Resource.h"
  8. #include "anki/resource/TextureResource.h"
  9. namespace anki {
  10. /// XXX
  11. class FlareBatch
  12. {
  13. public:
  14. static constexpr U MAX_FLARES = 10;
  15. enum FlareFlag
  16. {
  17. POSITION_LIGHT = 1 << 0,
  18. POSITION_FLOATING = 1 << 1
  19. };
  20. private:
  21. /// A 2D array texture with the flare textures
  22. TextureResourcePointer flaresTex;
  23. /// The size of each flare
  24. Array<Vec2, MAX_FLARES> size;
  25. Array<Vec2, MAX_FLARES> stretchMultiplier;
  26. F32 flaresAlpha = 1.0;
  27. };
  28. /// Light scene node. It can be spot or point
  29. ///
  30. /// Explaining the lighting model:
  31. /// @code
  32. /// Final intensity: If = Ia + Id + Is
  33. /// Ambient intensity: Ia = Al * Am
  34. /// Ambient intensity of light: Al
  35. /// Ambient intensity of material: Am
  36. /// Diffuse intensity: Id = Dl * Dm * LambertTerm
  37. /// Diffuse intensity of light: Dl
  38. /// Diffuse intensity of material: Dm
  39. /// LambertTerm: max(Normal dot Light, 0.0)
  40. /// Specular intensity: Is = Sm * Sl * pow(max(R dot E, 0.0), f)
  41. /// Specular intensity of light: Sl
  42. /// Specular intensity of material: Sm
  43. /// @endcode
  44. class Light: public SceneNode, public MoveComponent, public SpatialComponent
  45. {
  46. public:
  47. enum LightType
  48. {
  49. LT_POINT,
  50. LT_SPOT,
  51. LT_NUM
  52. };
  53. /// @name Constructors
  54. /// @{
  55. Light(
  56. const char* name, SceneGraph* scene, // SceneNode
  57. CollisionShape* cs, // SpatialComponent
  58. LightType t); // Self
  59. /// @}
  60. virtual ~Light();
  61. /// @name Accessors
  62. /// @{
  63. LightType getLightType() const
  64. {
  65. return type;
  66. }
  67. const Vec4& getDiffuseColor() const
  68. {
  69. return color;
  70. }
  71. Vec4& getDiffuseColor()
  72. {
  73. return color;
  74. }
  75. void setDiffuseColor(const Vec4& x)
  76. {
  77. color = x;
  78. }
  79. const Vec4& getSpecularColor() const
  80. {
  81. return specColor;
  82. }
  83. Vec4& getSpecularColor()
  84. {
  85. return specColor;
  86. }
  87. void setSpecularColor(const Vec4& x)
  88. {
  89. specColor = x;
  90. }
  91. Bool getShadowEnabled() const
  92. {
  93. return shadow;
  94. }
  95. void setShadowEnabled(const Bool x)
  96. {
  97. shadow = x;
  98. }
  99. U getShadowMapIndex() const
  100. {
  101. return (U)shadowMapIndex;
  102. }
  103. void setShadowMapIndex(const U i)
  104. {
  105. ANKI_ASSERT(i < 0xFF);
  106. shadowMapIndex = (U8)i;
  107. }
  108. Bool hasLensFlare() const
  109. {
  110. return flaresTex.isLoaded();
  111. }
  112. const Texture& getLensFlareTexture() const
  113. {
  114. ANKI_ASSERT(hasLensFlare());
  115. return *flaresTex;
  116. }
  117. const Vec2& getLensFlaresSize() const
  118. {
  119. return flaresSize;
  120. }
  121. void setLensFlaresSize(const Vec2& val)
  122. {
  123. flaresSize = val;
  124. }
  125. const Vec2& getLensFlaresStretchMultiplier() const
  126. {
  127. return flaresStretchMultiplier;
  128. }
  129. void setLensFlaresStretchMultiplier(const Vec2& val)
  130. {
  131. flaresStretchMultiplier = val;
  132. }
  133. F32 getLensFlaresAlpha() const
  134. {
  135. return flaresAlpha;
  136. }
  137. void setLensFlaresAlpha(F32 val)
  138. {
  139. flaresAlpha = val;
  140. }
  141. /// @}
  142. /// @name SceneNode virtuals
  143. /// @{
  144. /// Override SceneNode::frameUpdate
  145. void frameUpdate(F32 prevUpdateTime, F32 crntTime, int frame)
  146. {
  147. SceneNode::frameUpdate(prevUpdateTime, crntTime, frame);
  148. }
  149. /// @}
  150. void loadLensFlare(const char* filename)
  151. {
  152. ANKI_ASSERT(!hasLensFlare());
  153. flaresTex.load(filename);
  154. }
  155. private:
  156. LightType type;
  157. Vec4 color = Vec4(1.0);
  158. Vec4 specColor = Vec4(1.0);
  159. /// @name Flare struff
  160. /// @{
  161. TextureResourcePointer flaresTex;
  162. Vec2 flaresSize = Vec2(0.2);
  163. Vec2 flaresStretchMultiplier = Vec2(1.0);
  164. F32 flaresAlpha = 1.0;
  165. /// @}
  166. Bool8 shadow = false;
  167. U8 shadowMapIndex = 0xFF; ///< Used by the renderer
  168. };
  169. /// Point light
  170. class PointLight: public Light
  171. {
  172. public:
  173. /// @name Constructors/Destructor
  174. /// @{
  175. PointLight(const char* name, SceneGraph* scene);
  176. /// @}
  177. /// @name Accessors
  178. /// @{
  179. F32 getRadius() const
  180. {
  181. return sphereW.getRadius();
  182. }
  183. void setRadius(const F32 x)
  184. {
  185. sphereW.setRadius(x);
  186. SpatialComponent::markForUpdate();
  187. }
  188. const Sphere& getSphere() const
  189. {
  190. return sphereW;
  191. }
  192. /// @}
  193. /// @name MoveComponent virtuals
  194. /// @{
  195. /// Overrides MoveComponent::moveUpdate(). This does:
  196. /// - Update the collision shape
  197. void moveUpdate()
  198. {
  199. sphereW.setCenter(getWorldTransform().getOrigin());
  200. SpatialComponent::markForUpdate();
  201. }
  202. /// @}
  203. public:
  204. Sphere sphereW = Sphere(Vec3(0.0), 1.0);
  205. };
  206. /// Spot light
  207. class SpotLight: public Light, public FrustumComponent
  208. {
  209. public:
  210. /// @name Constructors/Destructor
  211. /// @{
  212. SpotLight(const char* name, SceneGraph* scene);
  213. /// @}
  214. /// @name Accessors
  215. /// @{
  216. Texture& getTexture()
  217. {
  218. return *tex;
  219. }
  220. const Texture& getTexture() const
  221. {
  222. return *tex;
  223. }
  224. F32 getOuterAngle() const
  225. {
  226. return frustum.getFovX();
  227. }
  228. void setOuterAngle(F32 x)
  229. {
  230. frustum.setFovX(x);
  231. frustum.setFovY(x);
  232. cosOuterAngle = cos(x / 2.0);
  233. frustumUpdate();
  234. }
  235. F32 getOuterAngleCos() const
  236. {
  237. return cosOuterAngle;
  238. }
  239. void setInnerAngle(F32 ang)
  240. {
  241. cosInnerAngle = cos(ang / 2.0);
  242. }
  243. F32 getInnerAngleCos() const
  244. {
  245. return cosInnerAngle;
  246. }
  247. F32 getDistance() const
  248. {
  249. return frustum.getFar();
  250. }
  251. void setDistance(F32 f)
  252. {
  253. frustum.setFar(f);
  254. frustumUpdate();
  255. }
  256. const PerspectiveFrustum& getFrustum() const
  257. {
  258. return frustum;
  259. }
  260. /// @}
  261. /// @name MoveComponent virtuals
  262. /// @{
  263. /// Overrides MoveComponent::moveUpdate(). This does:
  264. /// - Update the collision shape
  265. void moveUpdate()
  266. {
  267. frustum.setTransform(getWorldTransform());
  268. viewMat = Mat4(getWorldTransform().getInverse());
  269. viewProjectionMat = projectionMat * viewMat;
  270. SpatialComponent::markForUpdate();
  271. }
  272. /// @}
  273. /// @name FrustumComponent virtuals
  274. /// @{
  275. /// Override FrustumComponent::getFrustumOrigin()
  276. const Vec3& getFrustumOrigin() const
  277. {
  278. return getWorldTransform().getOrigin();
  279. }
  280. /// @}
  281. void loadTexture(const char* filename)
  282. {
  283. tex.load(filename);
  284. }
  285. private:
  286. PerspectiveFrustum frustum;
  287. TextureResourcePointer tex;
  288. F32 cosOuterAngle;
  289. F32 cosInnerAngle;
  290. void frustumUpdate()
  291. {
  292. projectionMat = frustum.calculateProjectionMatrix();
  293. viewProjectionMat = projectionMat * viewMat;
  294. SpatialComponent::markForUpdate();
  295. FrustumComponent::markForUpdate();
  296. }
  297. };
  298. } // end namespace anki
  299. #endif