TDME2  1.9.200
Light.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 #include <tdme/tdme.h>
7 #include <tdme/engine/fwd-tdme.h>
9 #include <tdme/engine/Color4.h>
11 #include <tdme/math/Math.h>
12 #include <tdme/math/Vector3.h>
13 #include <tdme/math/Vector4.h>
14 #include <tdme/math/Quaternion.h>
15 #include <tdme/utilities/Console.h>
16 
17 using std::to_string;
18 
22 using tdme::math::Math;
27 
28 /**
29  * Light representation
30  * @author Andreas Drewke
31  */
33 {
34 private:
35  int32_t id;
36  bool enabled;
42  float spotExponent;
43  float spotCutOff;
48  float sourceSize;
51  Renderer* renderer { nullptr };
52 
53 public:
54  // forbid class copy
56 
57  /**
58  * Public default constructor
59  */
60  Light();
61 
62  /**
63  * Public default constructor
64  * @param renderer renderer
65  * @param id id
66  */
67  Light(Renderer* renderer, int32_t id);
68 
69  /**
70  * @return light id
71  */
72  inline int32_t getId() const {
73  return id;
74  }
75 
76  /**
77  * @return enabled
78  */
79  inline bool isEnabled() const {
80  return enabled;
81  }
82 
83  /**
84  * Set enabled
85  * @param enabled enabled
86  */
87  inline void setEnabled(bool enabled) {
88  this->enabled = enabled;
89  }
90 
91  /**
92  * @return ambient light component
93  */
94  inline const Color4& getAmbient() const {
95  return ambient;
96  }
97 
98  /**
99  * Set ambient light component
100  * @param ambient ambient light component
101  */
102  inline void setAmbient(const Color4& ambient) {
103  this->ambient = ambient;
104  }
105 
106  /**
107  * @return diffuse light component
108  */
109  inline const Color4& getDiffuse() const {
110  return diffuse;
111  }
112 
113  /**
114  * Set diffuse light component
115  * @param diffuse diffuse light
116  */
117  inline void setDiffuse(const Color4& diffuse) {
118  this->diffuse = diffuse;
119  }
120 
121  /**
122  * @return specular light component
123  */
124  inline const Color4& getSpecular() const {
125  return specular;
126  }
127 
128  /**
129  * Set specular light component
130  * @param specular specular light
131  */
132  inline void setSpecular(const Color4& specular) {
133  this->specular = specular;
134  }
135 
136  /**
137  * @return position of light
138  */
139  inline const Vector4& getPosition() const {
140  return position;
141  }
142 
143  /**
144  * Set light position
145  * @param position position of light
146  */
147  inline void setPosition(const Vector4& position) {
148  this->position = position;
149  }
150 
151  /**
152  * @return spot direction
153  */
154  inline const Vector3& getSpotDirection() const {
155  return spotDirection;
156  }
157 
158  /**
159  * Set spot direction
160  * @param spotDirection spot direction
161  */
162  inline void setSpotDirection(const Vector3& spotDirection) {
163  this->spotDirection = spotDirection;
164  }
165 
166  /**
167  * @return spot exponent
168  */
169  inline float getSpotExponent() const {
170  return spotExponent;
171  }
172 
173  /**
174  * Set up spot exponent
175  * @param spotExponent spot exponent
176  */
177  inline void setSpotExponent(float spotExponent) {
178  this->spotExponent = spotExponent;
179  }
180 
181  /**
182  * @return spot cutoff
183  */
184  inline float getSpotCutOff() const {
185  return spotCutOff;
186  }
187 
188  /**
189  * Set spot cut off
190  * @param spotCutOff spot cut off
191  */
192  inline void setSpotCutOff(float spotCutOff) {
193  this->spotCutOff = spotCutOff;
194  }
195 
196  /**
197  * @return constant attenuation
198  */
199  inline float getConstantAttenuation() const {
200  return constantAttenuation;
201  }
202 
203  /**
204  * Set up constant attenuation
205  * @param constantAttenuation constant attenuation
206  */
208  this->constantAttenuation = constantAttenuation;
209  }
210 
211  /**
212  * @return linear attenuation
213  */
214  inline float getLinearAttenuation() const {
215  return linearAttenuation;
216  }
217 
218  /**
219  * Set up linear attenuation
220  * @param linearAttenuation linear attenuation
221  */
223  this->linearAttenuation = linearAttenuation;
224  }
225 
226  /**
227  * @return quadratic attenuation
228  */
229  inline float getQuadraticAttenuation() const {
230  return quadraticAttenuation;
231  }
232 
233  /**
234  * Set up quadratic attenuation
235  * @param quadraticAttenuation quadraticAttenuation
236  */
238  this->quadraticAttenuation = quadraticAttenuation;
239  }
240 
241  /**
242  * @return radius
243  */
244  inline float getRadius() {
245  // see: https://learnopengl.com/Advanced-Lighting/Deferred-Shading
246  float ambientLightMax = Math::max(Math::max(ambient.getRed(), ambient.getGreen()), ambient.getBlue());
247  if (ambientLightMax > Math::EPSILON) return 0.0f;
248  auto diffuseLightMax = Math::max(Math::max(diffuse.getRed(), diffuse.getGreen()), diffuse.getBlue());
249  if (diffuseLightMax < Math::EPSILON) return 0.0f;
250  if (linearAttenuation > Math::EPSILON || quadraticAttenuation > Math::EPSILON)
251  return (-linearAttenuation + Math::sqrt(linearAttenuation * linearAttenuation - 4.0f * quadraticAttenuation * (constantAttenuation - (256.0f / 5.0f) * diffuseLightMax))) / (quadraticAttenuation < Math::EPSILON?1.0f:2.0f * quadraticAttenuation);
252  return 0.0f;
253  }
254 
255  /**
256  * Returns if rendering light source is enabled
257  * @return rendering light source is enabled
258  */
259  inline bool isRenderSource() const {
260  return renderSource;
261  }
262 
263  /**
264  * Set rendering light source enabled/disabled
265  * @param renderLightSource render light source enabled
266  */
267  inline void setRenderSource(bool renderSource) {
268  this->renderSource = renderSource;
269  }
270 
271  /**
272  * Returns light source size
273  * @return light source size (moon, sun)
274  */
275  inline float getSourceSize() const {
276  return sourceSize;
277  }
278 
279  /**
280  * Set light source size (moon, sun)
281  * @param lightSourceSize light source size
282  */
283  inline void setLightSourceSize(float sourceSize) {
284  this->sourceSize = sourceSize;
285  }
286 
287  /**
288  * Returns light source texture
289  * @return light source texture
290  */
291  inline Texture* getSourceTexture() const {
292  return lightSourceTexture;
293  }
294 
295  /**
296  * Returns light source texture
297  * @return light source texture
298  */
299  void setSourceTexture(Texture* texture);
300 
301  /**
302  * Returns light source texture id
303  * @return light source texture id
304  */
305  inline int32_t getSourceTextureId() const {
306  return lightSourceTextureId;
307  }
308 
309  /**
310  * Returns if light is directional light like sun, moon lights
311  * @return directional light like sun, moon lights
312  */
313  inline bool isDirectional() const {
314  return position.getW() < Math::EPSILON;
315  }
316 
317  /**
318  * Dispose
319  */
320  void dispose();
321 
322  /**
323  * Update light
324  * @param contextIdx context index
325  */
326  void update(int contextIdx);
327 
328  /**
329  * Set up sun
330  * @param t time, while 0.0 <= t <= 1.0
331  */
332  inline void setupSun(float t) {
333  Quaternion lightRotationXQuaternion;
334  lightRotationXQuaternion.rotate(Vector3(0.0f, 0.0f, 1.0f), (1.0f - t) * 360.0f);
335  auto lightPosition = lightRotationXQuaternion * Vector3(1000000.0f, 0.0f, 1.0f);
336  setPosition(Vector4(lightPosition.getX(), lightPosition.getY(), lightPosition.getZ(), 0.0f));
337  setSpotDirection(Vector3(getPosition().getX(), getPosition().getY(), getPosition().getZ()).scale(-1.0f).normalize());
338  }
339 
340  /**
341  * Set up moon
342  * @param t time, while 0.0 <= t <= 1.0
343  */
344  inline void setupMoon(float t) {
345  Quaternion lightRotationXQuaternion;
346  lightRotationXQuaternion.rotate(Vector3(0.0f, 0.0f, 1.0f), (1.0f - t) * 360.0f);
347  auto lightPosition = lightRotationXQuaternion * Vector3(-1000000.0f, 0.0f, 1.0f);
348  setPosition(Vector4(lightPosition.getX(), lightPosition.getY(), lightPosition.getZ(), 0.0f));
349  setSpotDirection(Vector3(getPosition().getX(), getPosition().getY(), getPosition().getZ()).scale(-1.0f).normalize());
350  }
351 
352 };
Color 4 definition class.
Definition: Color4.h:18
float getRed() const
Definition: Color4.h:92
float getGreen() const
Definition: Color4.h:107
float getBlue() const
Definition: Color4.h:122
Light representation.
Definition: Light.h:33
float getQuadraticAttenuation() const
Definition: Light.h:229
bool renderSource
Definition: Light.h:47
int32_t id
Definition: Light.h:35
float getSpotExponent() const
Definition: Light.h:169
void setEnabled(bool enabled)
Set enabled.
Definition: Light.h:87
void setSpecular(const Color4 &specular)
Set specular light component.
Definition: Light.h:132
void setConstantAttenuation(float constantAttenuation)
Set up constant attenuation.
Definition: Light.h:207
void setLightSourceSize(float sourceSize)
Set light source size (moon, sun)
Definition: Light.h:283
void setSpotDirection(const Vector3 &spotDirection)
Set spot direction.
Definition: Light.h:162
const Color4 & getSpecular() const
Definition: Light.h:124
const Vector3 & getSpotDirection() const
Definition: Light.h:154
float getRadius()
Definition: Light.h:244
float getSpotCutOff() const
Definition: Light.h:184
float sourceSize
Definition: Light.h:48
Color4 diffuse
Definition: Light.h:38
Vector4 position
Definition: Light.h:40
void setAmbient(const Color4 &ambient)
Set ambient light component.
Definition: Light.h:102
Light()
Public default constructor.
Definition: Light.cpp:23
float getLinearAttenuation() const
Definition: Light.h:214
bool isDirectional() const
Returns if light is directional light like sun, moon lights.
Definition: Light.h:313
Texture * getSourceTexture() const
Returns light source texture.
Definition: Light.h:291
void setQuadraticAttenuation(float quadraticAttenuation)
Set up quadratic attenuation.
Definition: Light.h:237
void setSpotCutOff(float spotCutOff)
Set spot cut off.
Definition: Light.h:192
float constantAttenuation
Definition: Light.h:44
void update(int contextIdx)
Update light.
Definition: Light.cpp:80
bool isEnabled() const
Definition: Light.h:79
float quadraticAttenuation
Definition: Light.h:46
const Color4 & getAmbient() const
Definition: Light.h:94
void setPosition(const Vector4 &position)
Set light position.
Definition: Light.h:147
void setRenderSource(bool renderSource)
Set rendering light source enabled/disabled.
Definition: Light.h:267
const Vector4 & getPosition() const
Definition: Light.h:139
void dispose()
Dispose.
Definition: Light.cpp:74
void setSourceTexture(Texture *texture)
Returns light source texture.
Definition: Light.cpp:63
int32_t getSourceTextureId() const
Returns light source texture id.
Definition: Light.h:305
float spotCutOff
Definition: Light.h:43
void setDiffuse(const Color4 &diffuse)
Set diffuse light component.
Definition: Light.h:117
Color4 ambient
Definition: Light.h:37
float getSourceSize() const
Returns light source size.
Definition: Light.h:275
int32_t getId() const
Definition: Light.h:72
void setupSun(float t)
Set up sun.
Definition: Light.h:332
int32_t lightSourceTextureId
Definition: Light.h:50
void setLinearAttenuation(float linearAttenuation)
Set up linear attenuation.
Definition: Light.h:222
bool isRenderSource() const
Returns if rendering light source is enabled.
Definition: Light.h:259
void setSpotExponent(float spotExponent)
Set up spot exponent.
Definition: Light.h:177
Color4 specular
Definition: Light.h:39
float getConstantAttenuation() const
Definition: Light.h:199
Renderer * renderer
Definition: Light.h:51
float spotExponent
Definition: Light.h:42
void setupMoon(float t)
Set up moon.
Definition: Light.h:344
float linearAttenuation
Definition: Light.h:45
Texture * lightSourceTexture
Definition: Light.h:49
const Color4 & getDiffuse() const
Definition: Light.h:109
Vector3 spotDirection
Definition: Light.h:41
Texture entity.
Definition: Texture.h:24
Standard math functions.
Definition: Math.h:19
Quaternion class representing quaternion mathematical structure and operations with x,...
Definition: Quaternion.h:24
Quaternion & rotate(const Vector3 &axis, float angle)
Creates rotation quaternion.
Definition: Quaternion.h:361
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector4 class representing vector4 mathematical structure and operations with x, y,...
Definition: Vector4.h:22
float getW() const
Definition: Vector4.h:193
Console class.
Definition: Console.h:29
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6