Nebula
Loading...
Searching...
No Matches
lightcontext.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
11#include "coregraphics/shader.h"
12#include "coregraphics/buffer.h"
14#include <array>
15#include <system_shaders/lights_cluster.h>
16
17namespace Frame
18{
19class FrameScript;
20};
21
22namespace Lighting
23{
25{
27public:
28
36
37 enum class AreaLightShape
38 {
42 };
43
47 virtual ~LightContext();
48
50 static void Create();
52 static void Discard();
53
55 static void SetupGlobalLight(const Graphics::GraphicsEntityId id, const Math::vec3& color, const float intensity, const Math::vec3& ambient, const float zenith, const float azimuth, bool castShadows = false);
57 static void SetupPointLight(
59 , const Math::vec3& color
60 , const float intensity
61 , const float range
62 , bool castShadows = false
63 , const CoreGraphics::TextureId projection = CoreGraphics::InvalidTextureId
64 );
65
67 static void SetupSpotLight(
69 , const Math::vec3& color
70 , const float intensity
71 , const float innerConeAngle
72 , const float outerConeAngle
73 , const float range
74 , bool castShadows = false
75 , const CoreGraphics::TextureId projection = CoreGraphics::InvalidTextureId
76 );
77
79 static void SetupAreaLight(
81 , const AreaLightShape shape
82 , const Math::vec3& color
83 , const float intensity
84 , const float range
85 , bool twoSided = false
86 , bool castShadows = false
87 , bool renderMesh = false
88 );
89
91 static void SetColor(const Graphics::GraphicsEntityId id, const Math::vec3& color);
95 static void SetRange(const Graphics::GraphicsEntityId id, const float range);
97 static void SetIntensity(const Graphics::GraphicsEntityId id, const float intensity);
99 static float GetIntensity(const Graphics::GraphicsEntityId id);
101 static void SetTransform(const Graphics::GraphicsEntityId id, const float azimuth, const float zenith);
106
108 static const void SetPosition(const Graphics::GraphicsEntityId id, const Math::point& position);
112 static const void SetRotation(const Graphics::GraphicsEntityId id, const Math::quat& rotation);
116 static const void SetScale(const Graphics::GraphicsEntityId id, const Math::vec3& scale);
118 static const Math::vec3 GetScale(const Graphics::GraphicsEntityId id);
119
123 static void SetAmbient(const Graphics::GraphicsEntityId id, Math::vec3& ambient);
124
127
129 static void GetInnerOuterAngle(const Graphics::GraphicsEntityId id, float& inner, float& outer);
131 static void SetInnerOuterAngle(const Graphics::GraphicsEntityId id, float inner, float outer);
132
134 static void OnPrepareView(const Ptr<Graphics::View>& view, const Graphics::FrameContext& ctx);
135
141 static void WindowResized(const CoreGraphics::WindowId windowId, SizeT width, SizeT height);
142#ifndef PUBLIC_BUILD
144 static void OnRenderDebug(uint32_t flags);
145#endif
146
148 static void SetupTerrainShadows(const CoreGraphics::TextureId terrainShadowMap, const uint worldSize);
149
155 static const LightsCluster::LightUniforms& GetLightUniforms();
156
157private:
158
160 static void SetGlobalLightTransform(const Graphics::ContextEntityId id, const Math::mat4& transform, const Math::vector& direction);
162 static void SetGlobalLightViewProjTransform(const Graphics::ContextEntityId id, const Math::mat4& transform);
163
164 enum
165 {
172 };
173
174 typedef Ids::IdAllocator<
175 LightType, // type
176 Math::vec3, // color
177 float, // intensity
178 bool, // shadow caster
179 float,
180 Ids::Id32 // typed light id (index into pointlights, spotlights and globallights)
183
185 {
187 };
188
189 enum
190 {
197 };
198
199 typedef Ids::IdAllocator<
200 Math::transform44, // transform
201 ConstantBufferSet, // constant buffer binding for light
202 ConstantBufferSet, // constant buffer binding for shadows
203 Util::FixedArray<uint>, // dynamic offsets
204 CoreGraphics::TextureId, // projection (if invalid, don't use)
205 Graphics::GraphicsEntityId // graphics entity used for observer stuff
208
209 enum
210 {
219 };
220
221 typedef Ids::IdAllocator<
222 Math::transform44, // transform
223 ConstantBufferSet, // constant buffer binding for light
224 ConstantBufferSet, // constant buffer binding for shadows
225 Util::FixedArray<uint>, // dynamic offsets
226 std::array<float, 2>, // cone angle
227 CoreGraphics::TextureId, // projection (if invalid, don't use)
228 Math::mat4, // projection matrix
229 Graphics::GraphicsEntityId // graphics entity used for observer stuff
232
233 enum
234 {
243 };
244
245 typedef Ids::IdAllocator<
246 Math::transform44, // transform
247 AreaLightShape, // shape of area light
248 ConstantBufferSet, // constant buffer binding for light
249 ConstantBufferSet, // constant buffer binding for shadows
250 Util::FixedArray<uint>, // dynamic offsets
251 bool, // two sides
252 Graphics::GraphicsEntityId, // graphics entity used for observer stuff
253 bool // render mesh as well
256
257 enum
258 {
264 };
265
266 typedef Ids::IdAllocator<
267 Math::vector, // direction
268 Math::vec3, // ambient
269 Math::mat4, // transform (basically just a rotation in the direction)
270 Math::mat4, // transform for visibility and such
271 Util::Array<Graphics::GraphicsEntityId> // view ids for cascades
274
275
276 enum
277 {
279 };
280
281 typedef Ids::IdAllocator<
284
287
291 static void Dealloc(Graphics::ContextEntityId id);
292};
293} // namespace Lighting
A FrameScript describes render operations being done to produce a single frame.
Definition framescript.h:41
Definition graphicscontext.h:165
An ID allocator associates an id with a slice in an N number of arrays.
Definition idallocator.h:39
static Math::vec3 GetColor(const Graphics::GraphicsEntityId id)
Definition lightcontext.cc:576
static void SetupGlobalLight(const Graphics::GraphicsEntityId id, const Math::vec3 &color, const float intensity, const Math::vec3 &ambient, const float zenith, const float azimuth, bool castShadows=false)
setup entity as global light
Definition lightcontext.cc:296
static void WindowResized(const CoreGraphics::WindowId windowId, SizeT width, SizeT height)
React to window resize event.
Definition lightcontext.cc:1308
static const void SetScale(const Graphics::GraphicsEntityId id, const Math::vec3 &scale)
Set light scale.
Definition lightcontext.cc:780
Ids::IdAllocator< Math::vector, Math::vec3, Math::mat4, Math::mat4, Util::Array< Graphics::GraphicsEntityId > > DirectionalLightAllocator
Definition lightcontext.h:272
@ PointLight_Observer
Definition lightcontext.h:196
@ PointLight_Transform
Definition lightcontext.h:191
@ PointLight_ProjectionTexture
Definition lightcontext.h:195
@ PointLight_DynamicOffsets
Definition lightcontext.h:194
@ PointLight_ConstantBufferSet
Definition lightcontext.h:192
@ PointLight_ShadowConstantBufferSet
Definition lightcontext.h:193
@ ShadowCaster_Transform
Definition lightcontext.h:278
static ShadowCasterAllocator shadowCasterAllocator
Definition lightcontext.h:285
static const CoreGraphics::BufferId GetLightIndexBuffer()
get light index lists buffer
Definition lightcontext.cc:998
static Math::vec3 GetAmbient(const Graphics::GraphicsEntityId id)
Definition lightcontext.cc:352
static void SetAmbient(const Graphics::GraphicsEntityId id, Math::vec3 &ambient)
Definition lightcontext.cc:362
@ DirectionalLight_ViewProjTransform
Definition lightcontext.h:262
@ DirectionalLight_Ambient
Definition lightcontext.h:260
@ DirectionalLight_Direction
Definition lightcontext.h:259
@ DirectionalLight_CascadeObservers
Definition lightcontext.h:263
@ DirectionalLight_Transform
Definition lightcontext.h:261
static const Math::mat4 GetTransform(const Graphics::GraphicsEntityId id)
get transform
Definition lightcontext.cc:641
static const LightsCluster::LightUniforms & GetLightUniforms()
get light uniforms
Definition lightcontext.cc:1016
Ids::IdAllocator< Math::transform44, AreaLightShape, ConstantBufferSet, ConstantBufferSet, Util::FixedArray< uint >, bool, Graphics::GraphicsEntityId, bool > AreaLightAllocator
Definition lightcontext.h:254
static void SetupPointLight(const Graphics::GraphicsEntityId id, const Math::vec3 &color, const float intensity, const float range, bool castShadows=false, const CoreGraphics::TextureId projection=CoreGraphics::InvalidTextureId)
Setup entity as point light source.
Definition lightcontext.cc:375
static float GetIntensity(const Graphics::GraphicsEntityId id)
Definition lightcontext.cc:606
static const Math::point GetPosition(const Graphics::GraphicsEntityId id)
Get light position.
Definition lightcontext.cc:706
static void SetRange(const Graphics::GraphicsEntityId id, const float range)
set range of light
Definition lightcontext.cc:586
static const Math::quat GetRotation(const Graphics::GraphicsEntityId id)
Get light rotation.
Definition lightcontext.cc:758
static void GetInnerOuterAngle(const Graphics::GraphicsEntityId id, float &inner, float &outer)
get inner and outer angle for spotlights
Definition lightcontext.cc:850
static const CoreGraphics::BufferId GetLightsBuffer()
get light lists buffer
Definition lightcontext.cc:1007
static const void SetRotation(const Graphics::GraphicsEntityId id, const Math::quat &rotation)
Set light rotation.
Definition lightcontext.cc:728
static void SetGlobalLightViewProjTransform(const Graphics::ContextEntityId id, const Math::mat4 &transform)
Set global light shadow transform.
Definition lightcontext.cc:1036
LightType
Definition lightcontext.h:30
@ PointLightType
Definition lightcontext.h:32
@ AreaLightType
Definition lightcontext.h:34
@ SpotLightType
Definition lightcontext.h:33
@ DirectionalLightType
Definition lightcontext.h:31
static void SetColor(const Graphics::GraphicsEntityId id, const Math::vec3 &color)
set color of light
Definition lightcontext.cc:566
static Graphics::ContextEntityId Alloc()
allocate a new slice for this context
Definition lightcontext.cc:1332
static const Math::vec3 GetScale(const Graphics::GraphicsEntityId id)
Get light scale.
Definition lightcontext.cc:818
static void SetIntensity(const Graphics::GraphicsEntityId id, const float intensity)
set intensity of light
Definition lightcontext.cc:596
static void Discard()
discard light context
Definition lightcontext.cc:287
static void SetInnerOuterAngle(const Graphics::GraphicsEntityId id, float inner, float outer)
set inner and outer angle for spotlights
Definition lightcontext.cc:864
static const void SetPosition(const Graphics::GraphicsEntityId id, const Math::point &position)
Set light position.
Definition lightcontext.cc:676
static AreaLightAllocator areaLightAllocator
Definition lightcontext.h:255
LightContext()
constructor
Definition lightcontext.cc:107
static void SetupSpotLight(const Graphics::GraphicsEntityId id, const Math::vec3 &color, const float intensity, const float innerConeAngle, const float outerConeAngle, const float range, bool castShadows=false, const CoreGraphics::TextureId projection=CoreGraphics::InvalidTextureId)
Setup entity as spot light.
Definition lightcontext.cc:417
static void SetGlobalLightTransform(const Graphics::ContextEntityId id, const Math::mat4 &transform, const Math::vector &direction)
Set global light transform.
Definition lightcontext.cc:1025
static DirectionalLightAllocator directionalLightAllocator
Definition lightcontext.h:273
@ SpotLight_DynamicOffsets
Definition lightcontext.h:214
@ SpotLight_ShadowConstantBufferSet
Definition lightcontext.h:213
@ SpotLight_Transform
Definition lightcontext.h:211
@ SpotLight_ProjectionTexture
Definition lightcontext.h:216
@ SpotLight_ConstantBufferSet
Definition lightcontext.h:212
@ SpotLight_ProjectionTransform
Definition lightcontext.h:217
@ SpotLight_Observer
Definition lightcontext.h:218
@ SpotLight_ConeAngles
Definition lightcontext.h:215
static PointLightAllocator pointLightAllocator
Definition lightcontext.h:207
@ Range
Definition lightcontext.h:170
@ TypedLightId
Definition lightcontext.h:171
@ Intensity
Definition lightcontext.h:168
@ Type
Definition lightcontext.h:166
@ Color
Definition lightcontext.h:167
@ ShadowCaster
Definition lightcontext.h:169
static void SetTransform(const Graphics::GraphicsEntityId id, const float azimuth, const float zenith)
Set transform as angles.
Definition lightcontext.cc:616
static void OnRenderDebug(uint32_t flags)
render debug
Definition lightcontext.cc:1379
AreaLightShape
Definition lightcontext.h:38
@ Disk
Definition lightcontext.h:39
@ Rectangle
Definition lightcontext.h:40
@ Tube
Definition lightcontext.h:41
virtual ~LightContext()
destructor
Definition lightcontext.cc:115
Ids::IdAllocator< Math::transform44, ConstantBufferSet, ConstantBufferSet, Util::FixedArray< uint >, std::array< float, 2 >, CoreGraphics::TextureId, Math::mat4, Graphics::GraphicsEntityId > SpotLightAllocator
Definition lightcontext.h:230
Ids::IdAllocator< Math::transform44, ConstantBufferSet, ConstantBufferSet, Util::FixedArray< uint >, CoreGraphics::TextureId, Graphics::GraphicsEntityId > PointLightAllocator
Definition lightcontext.h:206
static Util::HashTable< Graphics::GraphicsEntityId, uint, 16, 1 > shadowCasterSliceMap
Definition lightcontext.h:286
static const Math::mat4 GetObserverTransform(const Graphics::GraphicsEntityId id)
get the view transform including projections
Definition lightcontext.cc:666
static void SetupTerrainShadows(const CoreGraphics::TextureId terrainShadowMap, const uint worldSize)
Setup terrain shadows.
Definition lightcontext.cc:987
static SpotLightAllocator spotLightAllocator
Definition lightcontext.h:231
static GenericLightAllocator genericLightAllocator
Definition lightcontext.h:182
static LightType GetType(const Graphics::GraphicsEntityId id)
get the light type
Definition lightcontext.cc:840
Ids::IdAllocator< Math::mat4 > ShadowCasterAllocator
Definition lightcontext.h:283
Ids::IdAllocator< LightType, Math::vec3, float, bool, float, Ids::Id32 > GenericLightAllocator
Definition lightcontext.h:181
static void Create()
setup light context
Definition lightcontext.cc:124
static void Dealloc(Graphics::ContextEntityId id)
deallocate a slice
Definition lightcontext.cc:1341
static void SetupAreaLight(const Graphics::GraphicsEntityId id, const AreaLightShape shape, const Math::vec3 &color, const float intensity, const float range, bool twoSided=false, bool castShadows=false, bool renderMesh=false)
Setup entity as area light.
Definition lightcontext.cc:474
static void OnPrepareView(const Ptr< Graphics::View > &view, const Graphics::FrameContext &ctx)
prepare light visibility
Definition lightcontext.cc:883
static void UpdateViewDependentResources(const Ptr< Graphics::View > &view, const Graphics::FrameContext &ctx)
prepare light lists
Definition lightcontext.cc:1046
static void RunFrameScriptJobs(const Graphics::FrameContext &ctx)
run framescript when visibility is done
@ AreaLight_DynamicOffsets
Definition lightcontext.h:239
@ AreaLight_TwoSided
Definition lightcontext.h:240
@ AreaLight_ConstantBufferSet
Definition lightcontext.h:237
@ AreaLight_Shape
Definition lightcontext.h:236
@ AreaLight_ShadowConstantBufferSet
Definition lightcontext.h:238
@ AreaLight_RenderMesh
Definition lightcontext.h:242
@ AreaLight_Transform
Definition lightcontext.h:235
@ AreaLight_Observer
Definition lightcontext.h:241
A 4x4 matrix which is described by translation, rotation and scale.
Definition transform44.h:20
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
Nebula's dynamic array class.
Definition array.h:60
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
Organizes key/value pairs by a hash code.
Definition hashtable.h:42
ImGUI debug interface for inspecting frame scripts.
Definition shaderserverbase.h:52
uint32_t Id32
Definition id.h:138
The lighting subsystem allows for attaching light source functionality to a GraphicsEntityId.
Definition csmutil.cc:19
CoreGraphics::TextureId terrainShadowMap
Definition lightcontext.cc:57
A shader represents an entire shader resource, containing several stages and programs.
Definition buffer.h:23
texture type
Definition texture.h:25
Definition window.h:25
Definition graphicscontext.h:133
Definition graphicsserver.h:31
The graphics entity is only an Id, to which we can attach GraphicsContexts.
Definition graphicsentity.h:16
Definition lightcontext.h:185
uint offset
Definition lightcontext.h:186
uint slice
Definition lightcontext.h:186
A 4x4 single point precision float matrix.
Definition mat4.h:49
Represents a 3D point in space.
Definition point.h:22
A quaternion is usually used to represent an orientation in 3D space.
Definition quat.h:30
A 3D vector.
Definition vec3.h:40
A vector is a 3D direction in space.
Definition vector.h:22
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31