SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
DirectionalLight.hpp
Go to the documentation of this file.
1#ifndef _DIRECTIONAL_LIGHT_HPP_
2#define _DIRECTIONAL_LIGHT_HPP_
3
4#include <Lighting/Light.hpp>
5
6namespace Sleak {
7
8 /// Parallel-ray light with a directional shadow frustum; typically the sun/moon.
9 ///
10 /// A directional light has a direction but no position: every ray is
11 /// parallel, so it lights the whole scene evenly. It derives from
12 /// Light, which derives from GameObject, so you add it to a scene with
13 /// SceneBase::AddObject() and the scene registers it with the
14 /// LightManager for you.
15 ///
16 /// A three-light setup covers most scenes: a bright warm key that casts
17 /// shadows, a dim cool fill from the opposite side that does not, and a
18 /// rim light from behind to separate silhouettes from the sky. Only the
19 /// key light needs SetCastShadows(true).
20 ///
21 /// Shadow quality lives or dies on the frustum size. It is the world
22 /// extent the shadow map covers, so shrinking it to fit the area the
23 /// player can actually see multiplies your effective texel density.
24 /// The default covers a very large area, which is right for open
25 /// terrain and far too coarse for a single figure on a small floor.
26 ///
27 /// @code{.cpp}
28 /// auto* key = new Sleak::DirectionalLight("KeyLight");
29 /// key->SetDirection(Sleak::Math::Vector3D(-0.55f, -0.80f, -0.25f));
30 /// key->SetColor(1.0f, 0.93f, 0.80f);
31 /// key->SetIntensity(3.0f);
32 /// key->SetCastShadows(true);
33 /// key->SetShadowFrustumSize(28.0f); // tight coverage, crisp shadows
34 /// key->SetShadowDistance(80.0f);
35 /// key->SetShadowNearPlane(0.1f);
36 /// key->SetShadowFarPlane(200.0f);
37 /// key->SetShadowBias(0.0003f);
38 /// key->SetShadowNormalBias(0.012f);
39 /// AddObject(key);
40 ///
41 /// auto* fill = new Sleak::DirectionalLight("FillLight");
42 /// fill->SetDirection(Sleak::Math::Vector3D(0.70f, -0.35f, 0.25f));
43 /// fill->SetColor(0.72f, 0.82f, 1.00f);
44 /// fill->SetIntensity(0.9f);
45 /// fill->SetCastShadows(false);
46 /// AddObject(fill);
47 /// @endcode
48 ///
49 /// @see Light, LightManager, PointLight, SpotLight, AreaLight
50 /// @ingroup lighting
51 class ENGINE_API DirectionalLight : public Light {
52 public:
53 explicit DirectionalLight(
54 const std::string& name = "DirectionalLight");
55 ~DirectionalLight() override = default;
56
58
59 void SetDirection(const Math::Vector3D& dir) {
60 m_direction = dir;
61 m_direction.Normalize();
62 }
63 Math::Vector3D GetDirection() const { return m_direction; }
64
65 // Shadow frustum configuration
66 void SetShadowFrustumSize(float size) { m_shadowFrustumSize = size; }
67 float GetShadowFrustumSize() const { return m_shadowFrustumSize; }
68
69 void SetShadowDistance(float dist) { m_shadowDistance = dist; }
70 float GetShadowDistance() const { return m_shadowDistance; }
71
72 void SetShadowNearPlane(float nearPlane) { m_shadowNear = nearPlane; }
73 float GetShadowNearPlane() const { return m_shadowNear; }
74
75 void SetShadowFarPlane(float farPlane) { m_shadowFar = farPlane; }
76 float GetShadowFarPlane() const { return m_shadowFar; }
77
78 private:
79 Math::Vector3D m_direction{0.0f, -1.0f, 0.0f};
80 float m_shadowFrustumSize = 160.0f;
81 float m_shadowDistance = 160.0f;
82 float m_shadowNear = 0.1f;
83 float m_shadowFar = 500.0f;
84 };
85
86} // namespace Sleak
87
88#endif // _DIRECTIONAL_LIGHT_HPP_
void SetShadowFrustumSize(float size)
void SetDirection(const Math::Vector3D &dir)
RenderEngine::LightGPUEntry BuildGPUData() const override
Packs this light's parameters into the GPU-side entry used by the lighting constant buffer.
void SetShadowFarPlane(float farPlane)
void SetShadowDistance(float dist)
Math::Vector3D GetDirection() const
DirectionalLight(const std::string &name="DirectionalLight")
~DirectionalLight() override=default
void SetShadowNearPlane(float nearPlane)
Light(const std::string &name="Light")
Definition Light.cpp:5
Vector3D & Normalize()
Definition Vector.hpp:435
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
One light's packed GPU representation, laid out for std140/cbuffer alignment.