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
16namespace Frame
17{
18class FrameScript;
19};
20
21namespace Lighting
22{
24{
26public:
27
35
36 enum class AreaLightShape
37 {
38 Disk,
40 Tube
41 };
42
46 virtual ~LightContext();
47
49 static void Create();
51 static void Discard();
52
54 static void SetupGlobalLight(const Graphics::GraphicsEntityId id, const Math::vec3& color, const float intensity, const Math::vec3& ambient, const Math::vec3& backlight, const float backlightFactor, const float zenith, const float azimuth, bool castShadows = false);
56 static void SetupPointLight(
58 , const Math::vec3& color
59 , const float intensity
60 , const float range
61 , bool castShadows = false
62 , const CoreGraphics::TextureId projection = CoreGraphics::InvalidTextureId
63 );
64
66 static void SetupSpotLight(
68 , const Math::vec3& color
69 , const float intensity
70 , const float innerConeAngle
71 , const float outerConeAngle
72 , const float range
73 , bool castShadows = false
74 , const CoreGraphics::TextureId projection = CoreGraphics::InvalidTextureId
75 );
76
78 static void SetupAreaLight(
80 , const AreaLightShape shape
81 , const Math::vec3& color
82 , const float intensity
83 , const float range
84 , bool twoSided = false
85 , bool castShadows = false
86 , bool renderMesh = false
87 );
88
90 static void SetColor(const Graphics::GraphicsEntityId id, const Math::vec3& color);
94 static void SetRange(const Graphics::GraphicsEntityId id, const float range);
96 static void SetIntensity(const Graphics::GraphicsEntityId id, const float intensity);
98 static float GetIntensity(const Graphics::GraphicsEntityId id);
100 static void SetTransform(const Graphics::GraphicsEntityId id, const float azimuth, const float zenith);
105
107 static const void SetPosition(const Graphics::GraphicsEntityId id, const Math::point& position);
111 static const void SetRotation(const Graphics::GraphicsEntityId id, const Math::quat& rotation);
115 static const void SetScale(const Graphics::GraphicsEntityId id, const Math::vec3& scale);
117 static const Math::vec3 GetScale(const Graphics::GraphicsEntityId id);
118
122 static void SetAmbient(const Graphics::GraphicsEntityId id, Math::vec3& ambient);
123
126
128 static void GetInnerOuterAngle(const Graphics::GraphicsEntityId id, float& inner, float& outer);
130 static void SetInnerOuterAngle(const Graphics::GraphicsEntityId id, float inner, float outer);
131
133 static void OnPrepareView(const Ptr<Graphics::View>& view, const Graphics::FrameContext& ctx);
134
140 static void WindowResized(const CoreGraphics::WindowId windowId, SizeT width, SizeT height);
141#ifndef PUBLIC_BUILD
143 static void OnRenderDebug(uint32_t flags);
144#endif
145
147 static void SetupTerrainShadows(const CoreGraphics::TextureId terrainShadowMap, const uint worldSize);
148
153private:
154
156 static void SetGlobalLightTransform(const Graphics::ContextEntityId id, const Math::mat4& transform, const Math::vector& direction);
158 static void SetGlobalLightViewProjTransform(const Graphics::ContextEntityId id, const Math::mat4& transform);
159
160 enum
161 {
168 };
169
170 typedef Ids::IdAllocator<
171 LightType, // type
172 Math::vec3, // color
173 float, // intensity
174 bool, // shadow caster
175 float,
176 Ids::Id32 // typed light id (index into pointlights, spotlights and globallights)
179
181 {
183 };
184
185 enum
186 {
193 };
194
195 typedef Ids::IdAllocator<
196 Math::transform44, // transform
197 ConstantBufferSet, // constant buffer binding for light
198 ConstantBufferSet, // constant buffer binding for shadows
199 Util::FixedArray<uint>, // dynamic offsets
200 CoreGraphics::TextureId, // projection (if invalid, don't use)
201 Graphics::GraphicsEntityId // graphics entity used for observer stuff
204
205 enum
206 {
215 };
216
217 typedef Ids::IdAllocator<
218 Math::transform44, // transform
219 ConstantBufferSet, // constant buffer binding for light
220 ConstantBufferSet, // constant buffer binding for shadows
221 Util::FixedArray<uint>, // dynamic offsets
222 std::array<float, 2>, // cone angle
223 CoreGraphics::TextureId, // projection (if invalid, don't use)
224 Math::mat4, // projection matrix
225 Graphics::GraphicsEntityId // graphics entity used for observer stuff
228
229 enum
230 {
239 };
240
241 typedef Ids::IdAllocator<
242 Math::transform44, // transform
243 AreaLightShape, // shape of area light
244 ConstantBufferSet, // constant buffer binding for light
245 ConstantBufferSet, // constant buffer binding for shadows
246 Util::FixedArray<uint>, // dynamic offsets
247 bool, // two sides
248 Graphics::GraphicsEntityId, // graphics entity used for observer stuff
249 bool // render mesh as well
252
253 enum
254 {
262 };
263
264 typedef Ids::IdAllocator<
265 Math::vector, // direction
266 Math::vec3, // backlight color
267 float, // backlight offset
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
Definition graphicscontext.h:165
An ID allocator associates an id with a slice in an N number of arrays.
Definition idallocator.h:39
Definition lightcontext.h:24
static Math::vec3 GetColor(const Graphics::GraphicsEntityId id)
Definition lightcontext.cc:579
static void WindowResized(const CoreGraphics::WindowId windowId, SizeT width, SizeT height)
React to window resize event.
Definition lightcontext.cc:1303
static const void SetScale(const Graphics::GraphicsEntityId id, const Math::vec3 &scale)
Set light scale.
Definition lightcontext.cc:783
static ShadowCasterAllocator shadowCasterAllocator
Definition lightcontext.h:285
static const CoreGraphics::BufferId GetLightIndexBuffer()
get light index lists buffer
Definition lightcontext.cc:1001
static Math::vec3 GetAmbient(const Graphics::GraphicsEntityId id)
Definition lightcontext.cc:355
static void SetAmbient(const Graphics::GraphicsEntityId id, Math::vec3 &ambient)
Definition lightcontext.cc:365
@ ShadowCaster_Transform
Definition lightcontext.h:278
static const Math::mat4 GetTransform(const Graphics::GraphicsEntityId id)
get transform
Definition lightcontext.cc:644
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:378
static float GetIntensity(const Graphics::GraphicsEntityId id)
Definition lightcontext.cc:609
static const Math::point GetPosition(const Graphics::GraphicsEntityId id)
Get light position.
Definition lightcontext.cc:709
static void SetRange(const Graphics::GraphicsEntityId id, const float range)
set range of light
Definition lightcontext.cc:589
static const Math::quat GetRotation(const Graphics::GraphicsEntityId id)
Get light rotation.
Definition lightcontext.cc:761
static void GetInnerOuterAngle(const Graphics::GraphicsEntityId id, float &inner, float &outer)
get inner and outer angle for spotlights
Definition lightcontext.cc:853
@ SpotLight_DynamicOffsets
Definition lightcontext.h:210
@ SpotLight_ShadowConstantBufferSet
Definition lightcontext.h:209
@ SpotLight_Transform
Definition lightcontext.h:207
@ SpotLight_ProjectionTexture
Definition lightcontext.h:212
@ SpotLight_ConstantBufferSet
Definition lightcontext.h:208
@ SpotLight_ProjectionTransform
Definition lightcontext.h:213
@ SpotLight_Observer
Definition lightcontext.h:214
@ SpotLight_ConeAngles
Definition lightcontext.h:211
static const CoreGraphics::BufferId GetLightsBuffer()
get light lists buffer
Definition lightcontext.cc:1010
static void SetupGlobalLight(const Graphics::GraphicsEntityId id, const Math::vec3 &color, const float intensity, const Math::vec3 &ambient, const Math::vec3 &backlight, const float backlightFactor, const float zenith, const float azimuth, bool castShadows=false)
setup entity as global light
Definition lightcontext.cc:295
static const void SetRotation(const Graphics::GraphicsEntityId id, const Math::quat &rotation)
Set light rotation.
Definition lightcontext.cc:731
static void SetGlobalLightViewProjTransform(const Graphics::ContextEntityId id, const Math::mat4 &transform)
Set global light shadow transform.
Definition lightcontext.cc:1030
LightType
Definition lightcontext.h:29
Ids::IdAllocator< LightType, Math::vec3, float, bool, float, Ids::Id32 > GenericLightAllocator
Definition lightcontext.h:177
Ids::IdAllocator< Math::vector, Math::vec3, float, Math::vec3, Math::mat4, Math::mat4, Util::Array< Graphics::GraphicsEntityId > > DirectionalLightAllocator
Definition lightcontext.h:272
static void SetColor(const Graphics::GraphicsEntityId id, const Math::vec3 &color)
set color of light
Definition lightcontext.cc:569
static Graphics::ContextEntityId Alloc()
allocate a new slice for this context
Definition lightcontext.cc:1327
static const Math::vec3 GetScale(const Graphics::GraphicsEntityId id)
Get light scale.
Definition lightcontext.cc:821
static void SetIntensity(const Graphics::GraphicsEntityId id, const float intensity)
set intensity of light
Definition lightcontext.cc:599
static void Discard()
discard light context
Definition lightcontext.cc:286
static void SetInnerOuterAngle(const Graphics::GraphicsEntityId id, float inner, float outer)
set inner and outer angle for spotlights
Definition lightcontext.cc:867
static const void SetPosition(const Graphics::GraphicsEntityId id, const Math::point &position)
Set light position.
Definition lightcontext.cc:679
static AreaLightAllocator areaLightAllocator
Definition lightcontext.h:251
LightContext()
constructor
Definition lightcontext.cc:106
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:420
static void SetGlobalLightTransform(const Graphics::ContextEntityId id, const Math::mat4 &transform, const Math::vector &direction)
Set global light transform.
Definition lightcontext.cc:1019
static DirectionalLightAllocator directionalLightAllocator
Definition lightcontext.h:273
static PointLightAllocator pointLightAllocator
Definition lightcontext.h:203
static void SetTransform(const Graphics::GraphicsEntityId id, const float azimuth, const float zenith)
Set transform as angles.
Definition lightcontext.cc:619
static void OnRenderDebug(uint32_t flags)
render debug
Definition lightcontext.cc:1374
AreaLightShape
Definition lightcontext.h:37
virtual ~LightContext()
destructor
Definition lightcontext.cc:114
@ Range
Definition lightcontext.h:166
@ TypedLightId
Definition lightcontext.h:167
@ Intensity
Definition lightcontext.h:164
@ Type
Definition lightcontext.h:162
@ Color
Definition lightcontext.h:163
@ ShadowCaster
Definition lightcontext.h:165
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:669
static void SetupTerrainShadows(const CoreGraphics::TextureId terrainShadowMap, const uint worldSize)
Setup terrain shadows.
Definition lightcontext.cc:990
Ids::IdAllocator< Math::mat4 > ShadowCasterAllocator
Definition lightcontext.h:283
static SpotLightAllocator spotLightAllocator
Definition lightcontext.h:227
static GenericLightAllocator genericLightAllocator
Definition lightcontext.h:178
Ids::IdAllocator< Math::transform44, ConstantBufferSet, ConstantBufferSet, Util::FixedArray< uint >, CoreGraphics::TextureId, Graphics::GraphicsEntityId > PointLightAllocator
Definition lightcontext.h:202
static LightType GetType(const Graphics::GraphicsEntityId id)
get the light type
Definition lightcontext.cc:843
Ids::IdAllocator< Math::transform44, AreaLightShape, ConstantBufferSet, ConstantBufferSet, Util::FixedArray< uint >, bool, Graphics::GraphicsEntityId, bool > AreaLightAllocator
Definition lightcontext.h:250
static void Create()
setup light context
Definition lightcontext.cc:123
static void Dealloc(Graphics::ContextEntityId id)
deallocate a slice
Definition lightcontext.cc:1336
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:477
@ PointLight_Observer
Definition lightcontext.h:192
@ PointLight_Transform
Definition lightcontext.h:187
@ PointLight_ProjectionTexture
Definition lightcontext.h:191
@ PointLight_DynamicOffsets
Definition lightcontext.h:190
@ PointLight_ConstantBufferSet
Definition lightcontext.h:188
@ PointLight_ShadowConstantBufferSet
Definition lightcontext.h:189
@ DirectionalLight_ViewProjTransform
Definition lightcontext.h:260
@ DirectionalLight_Ambient
Definition lightcontext.h:258
@ DirectionalLight_Direction
Definition lightcontext.h:255
@ DirectionalLight_CascadeObservers
Definition lightcontext.h:261
@ DirectionalLight_Transform
Definition lightcontext.h:259
@ DirectionalLight_Backlight
Definition lightcontext.h:256
@ DirectionalLight_BacklightOffset
Definition lightcontext.h:257
static void OnPrepareView(const Ptr< Graphics::View > &view, const Graphics::FrameContext &ctx)
prepare light visibility
Definition lightcontext.cc:886
Ids::IdAllocator< Math::transform44, ConstantBufferSet, ConstantBufferSet, Util::FixedArray< uint >, std::array< float, 2 >, CoreGraphics::TextureId, Math::mat4, Graphics::GraphicsEntityId > SpotLightAllocator
Definition lightcontext.h:226
static void UpdateViewDependentResources(const Ptr< Graphics::View > &view, const Graphics::FrameContext &ctx)
prepare light lists
Definition lightcontext.cc:1040
static void RunFrameScriptJobs(const Graphics::FrameContext &ctx)
run framescript when visibility is done
@ AreaLight_DynamicOffsets
Definition lightcontext.h:235
@ AreaLight_TwoSided
Definition lightcontext.h:236
@ AreaLight_ConstantBufferSet
Definition lightcontext.h:233
@ AreaLight_Shape
Definition lightcontext.h:232
@ AreaLight_ShadowConstantBufferSet
Definition lightcontext.h:234
@ AreaLight_RenderMesh
Definition lightcontext.h:238
@ AreaLight_Transform
Definition lightcontext.h:231
@ AreaLight_Observer
Definition lightcontext.h:237
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:181
uint offset
Definition lightcontext.h:182
uint slice
Definition lightcontext.h:182
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