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 );
87
89 static void SetColor(const Graphics::GraphicsEntityId id, const Math::vec3& color);
93 static void SetRange(const Graphics::GraphicsEntityId id, const float range);
95 static void SetIntensity(const Graphics::GraphicsEntityId id, const float intensity);
97 static float GetIntensity(const Graphics::GraphicsEntityId id);
99 static void SetTransform(const Graphics::GraphicsEntityId id, const float azimuth, const float zenith);
104
106 static const void SetPosition(const Graphics::GraphicsEntityId id, const Math::point& position);
110 static const void SetRotation(const Graphics::GraphicsEntityId id, const Math::quat& rotation);
114 static const void SetScale(const Graphics::GraphicsEntityId id, const Math::vec3& scale);
116 static const Math::vec3 GetScale(const Graphics::GraphicsEntityId id);
117
121 static void SetAmbient(const Graphics::GraphicsEntityId id, Math::vec3& ambient);
122
125
127 static void GetInnerOuterAngle(const Graphics::GraphicsEntityId id, float& inner, float& outer);
129 static void SetInnerOuterAngle(const Graphics::GraphicsEntityId id, float inner, float outer);
130
132 static void OnPrepareView(const Ptr<Graphics::View>& view, const Graphics::FrameContext& ctx);
133
139 static void WindowResized(const CoreGraphics::WindowId windowId, SizeT width, SizeT height);
140#ifndef PUBLIC_BUILD
142 static void OnRenderDebug(uint32_t flags);
143#endif
144
146 static void SetupTerrainShadows(const CoreGraphics::TextureId terrainShadowMap, const uint worldSize);
147
152private:
153
155 static void SetGlobalLightTransform(const Graphics::ContextEntityId id, const Math::mat4& transform, const Math::vector& direction);
157 static void SetGlobalLightViewProjTransform(const Graphics::ContextEntityId id, const Math::mat4& transform);
158
159 enum
160 {
167 };
168
169 typedef Ids::IdAllocator<
170 LightType, // type
171 Math::vec3, // color
172 float, // intensity
173 bool, // shadow caster
174 float,
175 Ids::Id32 // typed light id (index into pointlights, spotlights and globallights)
178
180 {
182 };
183
184 enum
185 {
192 };
193
194 typedef Ids::IdAllocator<
195 Math::transform44, // transform
196 ConstantBufferSet, // constant buffer binding for light
197 ConstantBufferSet, // constant buffer binding for shadows
198 Util::FixedArray<uint>, // dynamic offsets
199 CoreGraphics::TextureId, // projection (if invalid, don't use)
200 Graphics::GraphicsEntityId // graphics entity used for observer stuff
203
204 enum
205 {
214 };
215
216 typedef Ids::IdAllocator<
217 Math::transform44, // transform
218 ConstantBufferSet, // constant buffer binding for light
219 ConstantBufferSet, // constant buffer binding for shadows
220 Util::FixedArray<uint>, // dynamic offsets
221 std::array<float, 2>, // cone angle
222 CoreGraphics::TextureId, // projection (if invalid, don't use)
223 Math::mat4, // projection matrix
224 Graphics::GraphicsEntityId // graphics entity used for observer stuff
227
228 enum
229 {
237 };
238
239 typedef Ids::IdAllocator<
240 Math::transform44, // transform
241 AreaLightShape, // shape of area light
242 ConstantBufferSet, // constant buffer binding for light
243 ConstantBufferSet, // constant buffer binding for shadows
244 Util::FixedArray<uint>, // dynamic offsets
245 bool, // two sides
246 Graphics::GraphicsEntityId // graphics entity used for observer stuff
249
250 enum
251 {
259 };
260 typedef Ids::IdAllocator<
261 Math::vector, // direction
262 Math::vec3, // backlight color
263 float, // backlight offset
264 Math::vec3, // ambient
265 Math::mat4, // transform (basically just a rotation in the direction)
266 Math::mat4, // transform for visibility and such
267 Util::Array<Graphics::GraphicsEntityId> // view ids for cascades
270
271
272 enum
273 {
275 };
276 typedef Ids::IdAllocator<
281
285 static void Dealloc(Graphics::ContextEntityId id);
286};
287} // 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:571
static void WindowResized(const CoreGraphics::WindowId windowId, SizeT width, SizeT height)
React to window resize event.
Definition lightcontext.cc:1277
static const void SetScale(const Graphics::GraphicsEntityId id, const Math::vec3 &scale)
Set light scale.
Definition lightcontext.cc:764
static ShadowCasterAllocator shadowCasterAllocator
Definition lightcontext.h:279
static const CoreGraphics::BufferId GetLightIndexBuffer()
get light index lists buffer
Definition lightcontext.cc:975
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:274
static const Math::mat4 GetTransform(const Graphics::GraphicsEntityId id)
get transform
Definition lightcontext.cc:633
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:601
static const Math::point GetPosition(const Graphics::GraphicsEntityId id)
Get light position.
Definition lightcontext.cc:694
static void SetRange(const Graphics::GraphicsEntityId id, const float range)
set range of light
Definition lightcontext.cc:581
static const Math::quat GetRotation(const Graphics::GraphicsEntityId id)
Get light rotation.
Definition lightcontext.cc:742
static void GetInnerOuterAngle(const Graphics::GraphicsEntityId id, float &inner, float &outer)
get inner and outer angle for spotlights
Definition lightcontext.cc:830
@ SpotLight_DynamicOffsets
Definition lightcontext.h:209
@ SpotLight_ShadowConstantBufferSet
Definition lightcontext.h:208
@ SpotLight_Transform
Definition lightcontext.h:206
@ SpotLight_ProjectionTexture
Definition lightcontext.h:211
@ SpotLight_ConstantBufferSet
Definition lightcontext.h:207
@ SpotLight_ProjectionTransform
Definition lightcontext.h:212
@ SpotLight_Observer
Definition lightcontext.h:213
@ SpotLight_ConeAngles
Definition lightcontext.h:210
static const CoreGraphics::BufferId GetLightsBuffer()
get light lists buffer
Definition lightcontext.cc:984
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:716
static void SetGlobalLightViewProjTransform(const Graphics::ContextEntityId id, const Math::mat4 &transform)
Set global light shadow transform.
Definition lightcontext.cc:1004
LightType
Definition lightcontext.h:29
Ids::IdAllocator< LightType, Math::vec3, float, bool, float, Ids::Id32 > GenericLightAllocator
Definition lightcontext.h:176
Ids::IdAllocator< Math::vector, Math::vec3, float, Math::vec3, Math::mat4, Math::mat4, Util::Array< Graphics::GraphicsEntityId > > DirectionalLightAllocator
Definition lightcontext.h:268
static void SetColor(const Graphics::GraphicsEntityId id, const Math::vec3 &color)
set color of light
Definition lightcontext.cc:561
static Graphics::ContextEntityId Alloc()
allocate a new slice for this context
Definition lightcontext.cc:1301
static const Math::vec3 GetScale(const Graphics::GraphicsEntityId id)
Get light scale.
Definition lightcontext.cc:798
static void SetIntensity(const Graphics::GraphicsEntityId id, const float intensity)
set intensity of light
Definition lightcontext.cc:591
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:844
static const void SetPosition(const Graphics::GraphicsEntityId id, const Math::point &position)
Set light position.
Definition lightcontext.cc:668
static AreaLightAllocator areaLightAllocator
Definition lightcontext.h:248
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:417
static void SetGlobalLightTransform(const Graphics::ContextEntityId id, const Math::mat4 &transform, const Math::vector &direction)
Set global light transform.
Definition lightcontext.cc:993
static DirectionalLightAllocator directionalLightAllocator
Definition lightcontext.h:269
static PointLightAllocator pointLightAllocator
Definition lightcontext.h:202
static void SetTransform(const Graphics::GraphicsEntityId id, const float azimuth, const float zenith)
Set transform as angles.
Definition lightcontext.cc:611
static void OnRenderDebug(uint32_t flags)
render debug
Definition lightcontext.cc:1348
AreaLightShape
Definition lightcontext.h:37
virtual ~LightContext()
destructor
Definition lightcontext.cc:114
@ Range
Definition lightcontext.h:165
@ TypedLightId
Definition lightcontext.h:166
@ Intensity
Definition lightcontext.h:163
@ Type
Definition lightcontext.h:161
@ Color
Definition lightcontext.h:162
@ ShadowCaster
Definition lightcontext.h:164
static Util::HashTable< Graphics::GraphicsEntityId, uint, 16, 1 > shadowCasterSliceMap
Definition lightcontext.h:280
static const Math::mat4 GetObserverTransform(const Graphics::GraphicsEntityId id)
get the view transform including projections
Definition lightcontext.cc:658
static void SetupTerrainShadows(const CoreGraphics::TextureId terrainShadowMap, const uint worldSize)
Setup terrain shadows.
Definition lightcontext.cc:964
Ids::IdAllocator< Math::mat4 > ShadowCasterAllocator
Definition lightcontext.h:278
static SpotLightAllocator spotLightAllocator
Definition lightcontext.h:226
Ids::IdAllocator< Math::transform44, AreaLightShape, ConstantBufferSet, ConstantBufferSet, Util::FixedArray< uint >, bool, Graphics::GraphicsEntityId > AreaLightAllocator
Definition lightcontext.h:247
static GenericLightAllocator genericLightAllocator
Definition lightcontext.h:177
Ids::IdAllocator< Math::transform44, ConstantBufferSet, ConstantBufferSet, Util::FixedArray< uint >, CoreGraphics::TextureId, Graphics::GraphicsEntityId > PointLightAllocator
Definition lightcontext.h:201
static LightType GetType(const Graphics::GraphicsEntityId id)
get the light type
Definition lightcontext.cc:820
static void Create()
setup light context
Definition lightcontext.cc:123
static void Dealloc(Graphics::ContextEntityId id)
deallocate a slice
Definition lightcontext.cc:1310
@ PointLight_Observer
Definition lightcontext.h:191
@ PointLight_Transform
Definition lightcontext.h:186
@ PointLight_ProjectionTexture
Definition lightcontext.h:190
@ PointLight_DynamicOffsets
Definition lightcontext.h:189
@ PointLight_ConstantBufferSet
Definition lightcontext.h:187
@ PointLight_ShadowConstantBufferSet
Definition lightcontext.h:188
@ DirectionalLight_ViewProjTransform
Definition lightcontext.h:257
@ DirectionalLight_Ambient
Definition lightcontext.h:255
@ DirectionalLight_Direction
Definition lightcontext.h:252
@ DirectionalLight_CascadeObservers
Definition lightcontext.h:258
@ DirectionalLight_Transform
Definition lightcontext.h:256
@ DirectionalLight_Backlight
Definition lightcontext.h:253
@ DirectionalLight_BacklightOffset
Definition lightcontext.h:254
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)
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:860
Ids::IdAllocator< Math::transform44, ConstantBufferSet, ConstantBufferSet, Util::FixedArray< uint >, std::array< float, 2 >, CoreGraphics::TextureId, Math::mat4, Graphics::GraphicsEntityId > SpotLightAllocator
Definition lightcontext.h:225
static void UpdateViewDependentResources(const Ptr< Graphics::View > &view, const Graphics::FrameContext &ctx)
prepare light lists
Definition lightcontext.cc:1014
static void RunFrameScriptJobs(const Graphics::FrameContext &ctx)
run framescript when visibility is done
@ AreaLight_DynamicOffsets
Definition lightcontext.h:234
@ AreaLight_TwoSided
Definition lightcontext.h:235
@ AreaLight_ConstantBufferSet
Definition lightcontext.h:232
@ AreaLight_Shape
Definition lightcontext.h:231
@ AreaLight_ShadowConstantBufferSet
Definition lightcontext.h:233
@ AreaLight_Transform
Definition lightcontext.h:230
@ AreaLight_Observer
Definition lightcontext.h:236
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:180
uint offset
Definition lightcontext.h:181
uint slice
Definition lightcontext.h:181
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