RavEngine
Loading...
Searching...
No Matches
Light.hpp
1#pragma once
2#include "Queryable.hpp"
3#include "Common3D.hpp"
4#include "IDebugRenderable.hpp"
5#include "Layer.hpp"
6#include "Types.hpp"
7#if !RVE_SERVER
8#include "DepthPyramid.hpp"
9#endif
10
11namespace RavEngine{
12class MeshAsset;
13class CameraComponent;
14struct Skybox;
15struct CubemapTexture;
16struct Texture;
21class Light : public Queryable<Light,IDebugRenderable>, public IDebugRenderable {
22 ColorRGBA color{1,1,1,1};
23 float Intensity = 1.0;
24 renderlayer_t illuminationLayers = ALL_LAYERS;
25protected:
26 bool tickInvalidated = true; // trigger the world to update its parallel datastructure
27 constexpr inline void invalidate(){tickInvalidated = true;}
28public:
29 //MOVE_NO_COPY(Light);
30
31 constexpr void SetColorRGBA(const decltype(color)& inColor) {
32 invalidate();
33 color = inColor;
34 }
35 constexpr const decltype(color)& GetColorRGBA() const {
36 return color;
37 }
38 constexpr void SetIntensity(decltype(Intensity) inIntensity){
39 invalidate();
40 Intensity = inIntensity;
41 }
42 constexpr decltype(Intensity) GetIntensity() const{
43 return Intensity;
44 }
45 constexpr bool isInvalidated() const {
46 return tickInvalidated;
47 }
48 constexpr void clearInvalidate(){
49 tickInvalidated = false;
50 }
51
56 constexpr void SetIlluminationLayers(renderlayer_t layers){
57 invalidate();
58 illuminationLayers = layers;
59 }
60 constexpr auto GetIlluminationLayers(){
61 return illuminationLayers;
62 }
63};
64
69struct ShadowLightBase : public Light {
70private:
71 bool doesCastShadow = false;
72 renderlayer_t shadowLayers = ALL_LAYERS;
73
74public:
75 bool CastsShadows() const { return doesCastShadow; }
76 void SetCastsShadows(bool casts) {
77 invalidate();
78 doesCastShadow = casts;
79 }
84 constexpr void SetShadowLayers(renderlayer_t layers){
85 invalidate();
86 shadowLayers = layers;
87 }
88
89 constexpr auto GetShadowLayers() const{
90 return shadowLayers;
91 }
92};
93
99 MOVE_NO_COPY(EnvironmentLight);
100private:
101 friend class RenderEngine;
102 friend class World;
103 Ref<Skybox> sky;
104 Ref<CubemapTexture> outputTexture;
105 Ref<CubemapTexture> irradianceTexture;
106#if !RVE_SERVER
107 Ref<Texture> stagingTexture;
108 RGLTexturePtr stagingDepthTexture;
109#endif
110 bool environmentNeedsUpdate = true;
111public:
112 EnvironmentLight(decltype(sky) sky, decltype(outputTexture) ot, decltype(irradianceTexture) it);
114 void SetNeedsUpdate() {
115 environmentNeedsUpdate = true;
116 }
117};
118
119struct AmbientLight : public Light, public QueryableDelta<Light,AmbientLight>{
120 using light_t = AmbientLight;
121 using QueryableDelta<Light,AmbientLight>::GetQueryTypes;
122
123 // does not cast shadows
124 constexpr bool CastsShadows() const { return false; }
125
126 void DebugDraw(RavEngine::DebugDrawer&, const Transform&) const override;
127
128 std::optional<EnvironmentLight> environment; // optional
129};
130
134struct DirectionalLight : public ShadowLightBase, public QueryableDelta<QueryableDelta<Light,ShadowLightBase>,DirectionalLight>{
137
139
140#if !RVE_SERVER
141 struct ShadowMap {
143 Array<RGLTexturePtr,4> shadowMap;
144 } shadowData;
145 Array<float, MAX_CASCADES> shadowCascades{0.1, 0.2, 0.3, 1};
146 uint8_t numCascades = shadowCascades.size();
147#endif
148
149 void DebugDraw(RavEngine::DebugDrawer&, const Transform&) const override;
150
151};
152
156struct PointLight : public ShadowLightBase, public QueryableDelta<QueryableDelta<Light, ShadowLightBase>,PointLight>{
157 using light_t = PointLight;
159
160 void DebugDraw(RavEngine::DebugDrawer&, const Transform&) const override;
161
162 PointLight();
163
164#if !RVE_SERVER
165 struct ShadowData {
166 Array<DepthPyramid, 6> cubePyramids;
167 Array<RGLTexturePtr, 6> cubeShadowmaps;
168 RGLTexturePtr mapCube;
169 } shadowData;
170#endif
171
172 matrix4 CalcProjectionMatrix() const;
173 static matrix4 CalcViewMatrix(const vector3& lightPos, uint8_t index);
174
175private:
180 float CalculateRadius() const{
181 return sqrt( GetIntensity() / LIGHT_MIN_INFLUENCE);
182 }
183};
184
188class SpotLight : public ShadowLightBase, public QueryableDelta<QueryableDelta<Light,ShadowLightBase>,SpotLight>{
189 //light properties
190 float coneAngle = 45.0; // in degrees
191 float penumbraAngle = 10;
192public:
193 using light_t = SpotLight;
195
196 void DebugDraw(RavEngine::DebugDrawer&, const Transform&) const override;
197
198 SpotLight();
199
200 matrix4 CalcProjectionMatrix() const;
201 matrix4 CalcViewMatrix(const matrix4& worldTransform) const;
202
203#if !RVE_SERVER
204 struct ShadowMap {
205 DepthPyramid pyramid;
206 RGLTexturePtr shadowMap;
207 } shadowData;
208#endif
209
210#if !RVE_SERVER
211 const ShadowMap& GetShadowMap() const {
212 return shadowData;
213 }
214#endif
215
216 constexpr void SetConeAngle(decltype(coneAngle) inAngle){
217 invalidate();
218 coneAngle = inAngle;
219 }
220 constexpr decltype(coneAngle) GetConeAngle() const {return coneAngle;}
221 constexpr void SetPenumbraAngle(decltype(penumbraAngle) inAngle){
222 invalidate();
223 penumbraAngle = inAngle;
224 }
225 constexpr decltype(penumbraAngle) GetPenumbraAngle() const {return penumbraAngle;}
226
227};
228
229}
230
Definition DebugDrawer.hpp:11
Definition Light.hpp:21
constexpr void SetIlluminationLayers(renderlayer_t layers)
Definition Light.hpp:56
Definition RenderEngine.hpp:64
Definition Light.hpp:188
void DebugDraw(RavEngine::DebugDrawer &, const Transform &) const override
Definition Light.cpp:80
Definition World.hpp:181
Definition concurrentqueue.h:747
Definition Animation.hpp:6
Definition Light.hpp:119
void DebugDraw(RavEngine::DebugDrawer &, const Transform &) const override
Definition Light.cpp:69
Definition Common3D.hpp:55
Definition DepthPyramid.hpp:8
Definition Light.hpp:134
void DebugDraw(RavEngine::DebugDrawer &, const Transform &) const override
Definition Light.cpp:65
Definition Light.hpp:98
Definition IDebugRenderable.hpp:8
Definition Light.hpp:165
Definition Light.hpp:156
void DebugDraw(RavEngine::DebugDrawer &, const Transform &) const override
Definition Light.cpp:74
Definition Queryable.hpp:38
Definition Queryable.hpp:21
Definition Light.hpp:69
constexpr void SetShadowLayers(renderlayer_t layers)
Definition Light.hpp:84
Definition Light.hpp:204
Definition Transform.hpp:20
Definition qualifier.hpp:36
Definition qualifier.hpp:35