TDME2  1.9.200
Light.cpp
Go to the documentation of this file.
1 #include <tdme/engine/Light.h>
2 
3 #include <tdme/tdme.h>
4 #include <tdme/engine/Texture.h>
5 #include <tdme/engine/Color4.h>
8 #include <tdme/engine/Engine.h>
9 #include <tdme/math/Matrix4x4.h>
10 #include <tdme/math/Vector3.h>
11 #include <tdme/math/Vector4.h>
12 
22 
23 Light::Light()
24 {
25  this->renderer = nullptr;
26  this->id = -1;
27  enabled = false;
28  ambient.set(0.0f, 0.0f, 0.0f, 1.0f);
29  diffuse.set(1.0f, 1.0f, 1.0f, 1.0f);
30  specular.set(1.0f, 1.0f, 1.0f, 1.0f);
31  position.set(0.0f, 0.0f, 0.0f, 0.0f);
32  spotDirection.set(0.0f, 0.0f, -1.0f);
33  spotExponent = 0.0f;
34  spotCutOff = 180.0f;
35  constantAttenuation = 0.5f;
36  linearAttenuation = 0.0f;
37  quadraticAttenuation = 0.0f;
38  renderSource = false;
39  sourceSize = 0.25f;
41 }
42 
43 Light::Light(Renderer* renderer, int32_t id)
44 {
45  this->renderer = renderer;
46  this->id = id;
47  enabled = false;
48  ambient.set(0.0f, 0.0f, 0.0f, 1.0f);
49  diffuse.set(1.0f, 1.0f, 1.0f, 1.0f);
50  specular.set(1.0f, 1.0f, 1.0f, 1.0f);
51  position.set(0.0f, 0.0f, 0.0f, 0.0f);
52  spotDirection.set(0.0f, 0.0f, -1.0f);
53  spotExponent = 0.0f;
54  spotCutOff = 180.0f;
55  constantAttenuation = 0.5f;
56  linearAttenuation = 0.0f;
57  quadraticAttenuation = 0.0f;
58  renderSource = false;
59  sourceSize = 0.25f;
60  lightSourceTextureId = renderer->ID_NONE;
61 }
62 
64  if (lightSourceTexture == texture) return;
65  if (lightSourceTexture != nullptr) {
69  }
70  lightSourceTexture = texture;
72 }
73 
75  if (lightSourceTexture == nullptr) return;
78 }
79 
80 void Light::update(int contextIdx) {
81  if (enabled == true) {
82  auto& light = renderer->getLight(contextIdx, id);
83  light.enabled = 1;
84  light.ambient = ambient.getArray();
85  light.diffuse = diffuse.getArray();
86  light.position = position.getArray();
87  light.spotDirection = spotDirection.getArray();
88  light.spotExponent = spotExponent;
89  light.spotCosCutoff = static_cast<float>(Math::cos(Math::PI / 180.0f * spotCutOff));
90  light.constantAttenuation = constantAttenuation;
91  light.linearAttenuation = linearAttenuation;
92  light.quadraticAttenuation = quadraticAttenuation;
93  light.radius = getRadius();
94  renderer->onUpdateLight(contextIdx, id);
95  } else {
96  auto& light = renderer->getLight(contextIdx, id);
97  light.enabled = 0;
98  renderer->onUpdateLight(contextIdx, id);
99  }
100 }
Color 4 definition class.
Definition: Color4.h:18
const array< float, 4 > & getArray() const
Definition: Color4.h:262
void set(float r, float g, float b, float a)
Sets this color by its components.
Definition: Color4.h:66
Engine main class.
Definition: Engine.h:131
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:612
static TextureManager * getTextureManager()
Definition: Engine.h:627
Light representation.
Definition: Light.h:33
bool renderSource
Definition: Light.h:47
int32_t id
Definition: Light.h:35
float getRadius()
Definition: Light.h:244
float sourceSize
Definition: Light.h:48
Color4 diffuse
Definition: Light.h:38
Vector4 position
Definition: Light.h:40
Light()
Public default constructor.
Definition: Light.cpp:23
float constantAttenuation
Definition: Light.h:44
void update(int contextIdx)
Update light.
Definition: Light.cpp:80
float quadraticAttenuation
Definition: Light.h:46
void dispose()
Dispose.
Definition: Light.cpp:74
void setSourceTexture(Texture *texture)
Returns light source texture.
Definition: Light.cpp:63
float spotCutOff
Definition: Light.h:43
Color4 ambient
Definition: Light.h:37
int32_t lightSourceTextureId
Definition: Light.h:50
Color4 specular
Definition: Light.h:39
Renderer * renderer
Definition: Light.h:51
float spotExponent
Definition: Light.h:42
float linearAttenuation
Definition: Light.h:45
Texture * lightSourceTexture
Definition: Light.h:49
Vector3 spotDirection
Definition: Light.h:41
Texture entity.
Definition: Texture.h:24
void removeTexture(Texture *texture)
Removes a texture from manager.
TextureManager_TextureManaged * addTexture(const string &id, bool &created)
Adds a texture to manager.
virtual void onUpdateLight(int contextIdx, int32_t lightId)=0
Update light.
Renderer_Light & getLight(int contextIdx, int32_t lightIdx)
Get light.
Definition: Renderer.h:1114
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
const array< float, 3 > & getArray() const
Definition: Vector3.h:366
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
Vector4 class representing vector4 mathematical structure and operations with x, y,...
Definition: Vector4.h:22
Vector4 & set(float x, float y, float z, float w)
Sets this vector4 by its components.
Definition: Vector4.h:97
const array< float, 4 > & getArray() const
Definition: Vector4.h:357
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
Definition: Reference.h:38