Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
LightComponent.cpp
Go to the documentation of this file.
1 #include "LightComponent.hpp"
3 #include "Engine/engine.hpp"
4 
6 
7 #ifdef USING_OPENGL
9 #endif
10 #ifdef USING_VULKAN
12 #endif
13 
14 namespace Engine {
15 
17  {
18 #ifdef USING_VULKAN
19  renderer.lock()->SetLightType(lightName, LightType::LIGHT_NONEXISTENT);
20 #endif
21  }
22 
23  void LightComponent::SetLightName(eastl::string name)
24  {
25  eastl::string oldName = lightName;
26  lightName = name;
27 #ifdef USING_VULKAN
28  renderer.lock()->SetLightType(oldName, LightType::LIGHT_NONEXISTENT);
29  renderer.lock()->CreateLight(lightName, lightType,
30  glm::vec3(lightInfo.position.x, lightInfo.position.y, lightInfo.position.z),
31  glm::vec3(lightInfo.direction.x, lightInfo.direction.y, lightInfo.direction.z),
32  glm::vec3(lightInfo.color.x, lightInfo.color.y, lightInfo.color.z),
33  lightInfo.radius,
34  lightInfo.attunuation,
35  lightInfo.coneInnerAngle,
36  lightInfo.coneOuterAngle);
37 #endif
38  }
39 
40  eastl::string LightComponent::GetLightName() const
41  {
42  return lightName;
43  }
44 
46  {
47 #ifdef USING_VULKAN
48  if (vulkanEnabled && type != lightType && isEnabled) {
49  renderer.lock()->SetLightType(lightName, type);
50  switch (type) {
52  renderer.lock()->SetLightDirection(lightName,
53  glm::vec3(lightInfo.direction.x, lightInfo.direction.y, lightInfo.direction.z));
54  break;
56  renderer.lock()->SetLightPosition(lightName,
57  glm::vec3(lightInfo.position.x, lightInfo.position.y, lightInfo.position.z));
58  break;
60  renderer.lock()->SetLightDirection(lightName,
61  glm::vec3(lightInfo.direction.x, lightInfo.direction.y, lightInfo.direction.z));
62  renderer.lock()->SetLightConeInnerAngle(lightName, lightInfo.coneInnerAngle);
63  renderer.lock()->SetLightConeOuterAngle(lightName, lightInfo.coneOuterAngle);
64  break;
65  }
66  lightType = type;
67  }
68 #endif
69  if (!isEnabled)
70  lightType = type;
71  }
72 
74  {
75  return lightType;
76  }
77 
78  void LightComponent::SetLightPosition(glm::vec3 position)
79  {
80 #ifdef USING_VULKAN
81  renderer.lock()->SetLightPosition(lightName, position);
82 #endif
83  lightInfo.position = glm::vec4(position.x, position.y, position.z, 1.f);
84  }
85 
87  {
88  return glm::vec3(lightInfo.position.x, lightInfo.position.y, lightInfo.position.z);
89  }
90 
91  void LightComponent::SetLightDirection(glm::vec3 direction)
92  {
93 #ifdef USING_VULKAN
94  renderer.lock()->SetLightDirection(lightName, direction);
95 #endif
96  lightInfo.direction = glm::vec4(direction.x, direction.y, direction.z, 0.f);
97  }
98 
100  {
101  return glm::vec3(lightInfo.direction.x, lightInfo.direction.y, lightInfo.direction.z);
102  }
103 
104  void LightComponent::SetLightColor(glm::vec3 color)
105  {
106 #ifdef USING_VULKAN
107  renderer.lock()->SetLightColor(lightName, color);
108 #endif
109  lightInfo.color = glm::vec4(color.x, color.y, color.z, 1.f);
110  }
111 
113  {
114  return glm::vec3(lightInfo.color.x, lightInfo.color.y, lightInfo.color.z);
115  }
116 
118  {
119 #ifdef USING_VULKAN
120  renderer.lock()->SetLightRadius(lightName, radius);
121 #endif
122  lightInfo.radius = radius;
123  }
124 
126  {
127  return lightInfo.radius;
128  }
129 
131  {
132 #ifdef USING_VULKAN
133  renderer.lock()->SetLightConeInnerAngle(lightName, angle);
134 #endif
135  lightInfo.coneInnerAngle = angle;
136  }
137 
139  {
140  return lightInfo.coneInnerAngle;
141  }
142 
144  {
145 #ifdef USING_VULKAN
146  renderer.lock()->SetLightConeOuterAngle(lightName, angle);
147 #endif
148  lightInfo.coneOuterAngle = angle;
149  }
150 
152  {
153  return lightInfo.coneOuterAngle;
154  }
155 
156  LightComponent::LightComponent(eastl::string name, LightType type, glm::vec3 position, glm::vec3 direction, glm::vec3 color, float radius, float attunuation, float coneInnerAngle, float coneOuterAngle) noexcept
157  {
158 #ifdef USING_VULKAN
159  vulkanEnabled = true;
160  renderer = Engine::GetEngine().lock()->GetRenderer<VulkanRenderer>().lock();
161  isEnabled = true;
162 #else
163  vulkanEnabled = false;
164  isEnabled = false;
165 #endif// USING_VULKAN
166 
167  lightName = name;
168 
169  if (name == "")
170  lightName = GetOwner().lock()->name;
171 
172 #ifdef USING_VULKAN
173  renderer.lock()->CreateLight(lightName,
174  type,
175  glm::vec3(position.x, position.y, position.z),
176  glm::vec3(direction.x, direction.y, direction.z),
177  glm::vec3(color.x, color.y, color.z),
178  radius, attunuation, coneInnerAngle, coneOuterAngle);
179 #endif
180  lightType = type;
181  lightInfo.position = glm::vec4(position.x, position.y, position.z, 1.f);
182  lightInfo.direction = glm::vec4(direction.x, direction.y, direction.z, 0.f);
183  lightInfo.color = glm::vec4(color.x, color.y, color.z, 1.f);
184  lightInfo.radius = radius;
185  lightInfo.attunuation = attunuation;
186  lightInfo.coneInnerAngle = coneInnerAngle;
187  lightInfo.coneOuterAngle = coneOuterAngle;
188 
189  created = true;
190  }
191 
192  LightComponent::LightComponent(eastl::string name) noexcept
193  {
194  lightName = name;
195 
196  if (name == "")
197  lightName = GetOwner().lock()->name;
198 
199  created = false;
200  }
201 
202  void LightComponent::InitializeComponent()
203  {
204  if (!created)
205  {
206 #ifdef USING_VULKAN
207  vulkanEnabled = true;
208  renderer = eastl::static_pointer_cast<VulkanRenderer, Renderer>(
209  Engine::GetEngine().lock()->GetRenderer().lock()
210  );
211  isEnabled = true;
212 #else
213  vulkanEnabled = false;
214  isEnabled = false;
215 #endif
216 
217  if (lightName == "")
218  lightName = GetOwner().lock()->name;
219 #ifdef USING_VULKAN
220  renderer.lock()->CreateLight(lightName,
221  LightType::LIGHT_NONEXISTENT,
222  glm::vec3(),
223  glm::vec3(),
224  glm::vec3(),
225  0.f, 0.f, 0.f, 0.f);
226 #endif
227  lightType = LightType::LIGHT_NONEXISTENT;
228  lightInfo.position = glm::vec4();
229  lightInfo.direction = glm::vec4();
230  lightInfo.color = glm::vec4();
231  lightInfo.radius = 0.f;
232  lightInfo.attunuation = 0.f;
233  lightInfo.coneInnerAngle = 0.f;
234  lightInfo.coneOuterAngle = 0.f;
235  }
236  }
237 
238  void LightComponent::Update()
239  {
240 #ifdef USING_VULKAN
241  if (isEnabled != active)
242  {
243  active = isEnabled;
244 
245  if (active == false)
246  renderer.lock()->SetLightType(lightName, LightType::LIGHT_NONEXISTENT);
247  else
248  renderer.lock()->SetLightType(lightName, lightType);
249  }
250 #endif
251  }
252 }
glm::vec3 GetLightDirection()
Returns the direction of the light.
float radius
Definition: Light.hpp:22
void SetLightConeOuterAngle(float angle)
Sets the outer angle of the light cone. NOTE: If the light isn&#39;t a spot light this value will take ef...
void SetLightPosition(glm::vec3 position)
Sets the position of the light. NOTE: In case of directional lights and ambient lights this value wil...
LightComponent(eastl::string name, LightType type, glm::vec3 position, glm::vec3 direction, glm::vec3 color, float radius, float attunuation, float coneInnerAngle, float coneOuterAngle) noexcept
Creates a new light component with the given name and type, and initializes it with the given variabl...
float GetLightConeInnerAngle()
Returns the inner angle of the light cone.
LIGHT_SPOT_LIGHT
Definition: Light.hpp:10
float GetLightConeOuterAngle()
Returns the outer angle of the light cone.
LightType GetLightType() const
Returns the type of the light.
void SetLightRadius(float radius)
Sets the new radius of the light. NOTE: In case of directional lights and ambient lights this value w...
This is the main renderer parent class which contains the base methods required for a renderer...
Definition: Renderer.hpp:15
float coneOuterAngle
Definition: Light.hpp:25
void SetLightColor(glm::vec3 color)
Sets the color of the light.
eastl::string GetLightName() const
Returns the name of the light. The default name is the entity name.
glm::vec3 GetLightPosition()
Returns the position of the light.
eastl::weak_ptr< Entity > GetOwner() const
This method allows you to get the entity holding this component.
Definition: Component.cpp:9
float coneInnerAngle
Definition: Light.hpp:24
LIGHT_POINT_LIGHT
Definition: Light.hpp:10
void SetLightName(eastl::string name)
Sets the name of the light. This is used internally by the engine to reference the light...
void SetLightConeInnerAngle(float angle)
Sets the inner angle of the light cone. NOTE: If the light isn&#39;t a spot light this value will take ef...
glm::vec4 color
Definition: Light.hpp:21
LIGHT_DIRECTIONAL_LIGHT
Definition: Light.hpp:10
float attunuation
Definition: Light.hpp:23
void SetLightDirection(glm::vec3 direction)
Sets the direction of the light. NOTE: In case of point and ambient lights this value will take effec...
enum ENGINE_API LightType
Enum containing the different types of lights that can be created.
Definition: Light.hpp:9
static eastl::weak_ptr< Engine > GetEngine() noexcept
This method allows you to get the instance of the Engine. This method will automatically initialize t...
Definition: engine.cpp:130
float GetLightRadius() const
Returns the radius of the light.
glm::vec4 position
Definition: Light.hpp:19
void SetLightType(LightType type)
Sets the type of the light. NOTE: the type non-existent (-1) disables the light.
glm::vec3 GetLightColor()
Returns the color of the light.
glm::vec4 direction
Definition: Light.hpp:20