Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Light.cpp
Go to the documentation of this file.
1
2
3#include "Headers/Light.h"
4
7
14
15namespace Divide
16{
17
18
19 namespace TypeUtil
20 {
21 const char* LightTypeToString( const LightType lightType ) noexcept
22 {
23 return Names::lightType[to_base( lightType )];
24 }
25
26 LightType StringToLightType( const string& name )
27 {
28 for ( U8 i = 0; i < to_U8( LightType::COUNT ); ++i )
29 {
30 if ( strcmp( name.c_str(), Names::lightType[i] ) == 0 )
31 {
32 return static_cast<LightType>(i);
33 }
34 }
35
36 return LightType::COUNT;
37 }
38 }
39
40 Light::Light( SceneGraphNode* sgn, [[maybe_unused]] const F32 range, const LightType type, LightPool& parentPool )
41 : IEventListener( sgn->sceneGraph()->GetECSEngine() ),
42 _castsShadows( false ),
43 _sgn( sgn ),
44 _parentPool( parentPool ),
45 _type( type )
46 {
49
50 if ( !_parentPool.addLight( *this ) )
51 {
52 //assert?
53 }
54
55 for ( U8 i = 0u; i < 6u; ++i )
56 {
57 _shadowProperties._lightVP[i].identity();
59 }
60
63
64 const ECS::CustomEvent evt = {
66 ._sourceCmp = sgn->get<TransformComponent>(),
68 };
69
70 updateCache( evt );
71
72
73 _enabled = true;
74 }
75
77 {
79 if ( !_parentPool.removeLight( *this ) )
80 {
82 }
83 }
84
86 {
87 EditorComponentField rangeField = {};
88 rangeField._name = "Range";
89 rangeField._data = &_range;
91 rangeField._readOnly = false;
92 rangeField._range = { EPSILON_F32, 10000.f };
94 comp.registerField( MOV( rangeField ) );
95
96 EditorComponentField intensityField = {};
97 intensityField._name = "Intensity";
98 intensityField._data = &_intensity;
100 intensityField._readOnly = false;
101 intensityField._range = { EPSILON_F32, 25.f };
102 intensityField._basicType = PushConstantType::FLOAT;
103 comp.registerField( MOV( intensityField ) );
104
105 EditorComponentField colourField = {};
106 colourField._name = "Colour";
107 colourField._dataGetter = [this]( void* dataOut ) noexcept
108 {
109 static_cast<FColour3*>(dataOut)->set( getDiffuseColour() );
110 };
111 colourField._dataSetter = [this]( const void* data ) noexcept
112 {
113 setDiffuseColour( *static_cast<const FColour3*>(data) );
114 };
116 colourField._readOnly = false;
118 comp.registerField( MOV( colourField ) );
119
120 EditorComponentField castsShadowsField = {};
121 castsShadowsField._name = "Is Shadow Caster";
122 castsShadowsField._data = &_castsShadows;
123 castsShadowsField._type = EditorComponentFieldType::PUSH_TYPE;
124 castsShadowsField._readOnly = false;
125 castsShadowsField._basicType = PushConstantType::BOOL;
126 comp.registerField( MOV( castsShadowsField ) );
127
128 EditorComponentField shadowBiasField = {};
129 shadowBiasField._name = "Shadow Bias";
130 shadowBiasField._data = &_shadowProperties._lightDetails.z;
132 shadowBiasField._readOnly = false;
133 shadowBiasField._format = "%.5f";
134 shadowBiasField._range = { EPSILON_F32, 1.0f };
135 shadowBiasField._basicType = PushConstantType::FLOAT;
136 comp.registerField( MOV( shadowBiasField ) );
137
138 EditorComponentField shadowStrengthField = {};
139 shadowStrengthField._name = "Shadow Strength";
140 shadowStrengthField._data = &_shadowProperties._lightDetails.w;
141 shadowStrengthField._type = EditorComponentFieldType::SLIDER_TYPE;
142 shadowStrengthField._readOnly = false;
143 shadowStrengthField._range = { EPSILON_F32, 10.0f };
144 shadowStrengthField._basicType = PushConstantType::FLOAT;
145 comp.registerField( MOV( shadowStrengthField ) );
146
147 EditorComponentField lightTagField = {};
148 lightTagField._name = "Light Tag Value";
149 lightTagField._data = &_tag;
151 lightTagField._readOnly = false;
152 lightTagField._hexadecimal = true;
153 lightTagField._basicType = PushConstantType::UINT;
154
155 comp.registerField( MOV( lightTagField ) );
156 }
157
159 {
161
162 const TransformComponent* tComp = static_cast<TransformComponent*>(event._sourceCmp);
163 assert( tComp != nullptr );
164
165 bool transformChanged = false;
167 {
168 _positionCache = tComp->getWorldPosition();
169 transformChanged = true;
170 }
171
172 if ( _type != LightType::POINT && ( event._flag & to_U32( TransformType::ROTATION ) ) )
173 {
174 _directionCache = tComp->getWorldDirection();
175 transformChanged = true;
176 }
177
178 if ( transformChanged )
179 {
180 staticShadowsDirty( true );
181 dynamicShadowsDirty( true );
182 }
183 }
184
185 void Light::updateBoundingVolume( const Camera* playerCamera )
186 {
187 switch ( getLightType() )
188 {
190 _boundingVolume.setCenter( playerCamera->snapshot()._eye );
191 _boundingVolume.setRadius( range() );
192 break;
193 case LightType::POINT:
194 _boundingVolume.setCenter( positionCache() );
195 _boundingVolume.setRadius( range() );
196 break;
197 case LightType::SPOT:
198 {
199 const Angle::RADIANS<F32> angle = Angle::DegreesToRadians( static_cast<SpotLightComponent*>(this)->outerConeCutoffAngle() );
200 const F32 radius = angle > M_PI_4 ? range() * tan( angle ) : range() * 0.5f / pow( cos( angle ), 2.0f );
201 const vec3<F32> position = positionCache() + directionCache() * radius;
202
203 _boundingVolume.setCenter( position );
204 _boundingVolume.setRadius( radius );
205 break;
206 };
207
208 default: break;
209 }
210 }
211
212 void Light::setDiffuseColour( const UColour3& newDiffuseColour ) noexcept
213 {
214 _colour.rgb = newDiffuseColour;
215 }
216
217} //namespace Divide
#define MOV(...)
#define DIVIDE_UNEXPECTED_CALL()
#define PROFILE_SCOPE_AUTO(CATEGORY)
Definition: Profiler.h:87
const CameraSnapshot & snapshot() const noexcept
Returns the internal camera snapshot data (eye, orientation, etc)
Definition: Camera.inl:43
void registerField(EditorComponentField &&field)
ShadowProperties _shadowProperties
Definition: Light.h:141
virtual void updateBoundingVolume(const Camera *playerCamera)
Definition: Light.cpp:185
void setDiffuseColour(const UColour3 &newDiffuseColour) noexcept
Definition: Light.cpp:212
const LightType & getLightType() const noexcept
Get the light type. (see LightType enum)
Definition: Light.inl:46
LightPool & _parentPool
Definition: Light.h:140
LightType _type
Definition: Light.h:143
FColour3 getDiffuseColour() const noexcept
Definition: Light.inl:40
void updateCache(const ECS::CustomEvent &event)
Definition: Light.cpp:158
void registerFields(EditorComponent &comp)
Definition: Light.cpp:85
virtual ~Light() override
Definition: Light.cpp:76
Light(SceneGraphNode *sgn, F32 range, LightType type, LightPool &parentPool)
Create a new light assigned to the specified slot with the specified range.
Definition: Light.cpp:40
bool removeLight(const Light &light)
remove a light from the manager
Definition: LightPool.cpp:203
bool addLight(Light &light)
Add a new light to the manager.
Definition: LightPool.cpp:182
FORCE_INLINE T * get() const
Returns a pointer to a specific component. Returns null if the SGN does not have the component reques...
const vec3< F32 > getWorldDirection(const vec3< F32 > &worldForward=WORLD_Z_NEG_AXIS) const
vec3< F32 > getWorldPosition() const
Return the position.
constexpr T DegreesToRadians(T angleDegrees) noexcept
Return the radian equivalent of the given degree value.
Definition: MathHelper.inl:429
FColour4 WHITE
Random stuff added for convenience.
Definition: Colours.cpp:8
static constexpr const char * lightType[]
constexpr Optick::Category::Type Scene
Definition: Profiler.h:66
const char * LightTypeToString(LightType lightType) noexcept
Definition: Light.cpp:21
LightType StringToLightType(const string &name)
Definition: Light.cpp:26
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U32 to_U32(const T value)
uint8_t U8
constexpr F32 to_F32(const T value)
constexpr F32 EPSILON_F32
constexpr F32 F32_MAX
LightType
The different types of lights supported.
SceneGraphNode * _flag[2]
Definition: WarScene.h:96
constexpr U8 to_U8(const T value)
constexpr auto to_base(const Type value) -> Type
DELEGATE_STD< void, void * > _dataGetter
DELEGATE_STD< void, const void * > _dataSetter
vec2< F32 > _range
Used by slider_type as a min / max range or dropdown as selected_index / count.
EditorComponentFieldType _type
std::array< vec4< F32 >, 6 > _lightPosition
light's position in world space. w - csm split distances (or whatever else might be needed)
Definition: Light.h:67
std::array< mat4< F32 >, 6 > _lightVP
light viewProjection matrices
Definition: Light.h:69
Divide::U32 _flag
Definition: SGNComponent.h:67