-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathspotlight.cpp
More file actions
105 lines (91 loc) · 2.69 KB
/
spotlight.cpp
File metadata and controls
105 lines (91 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "components/spotlight.h"
#include "components/actor.h"
#include "components/transform.h"
#include "resources/material.h"
#include "gizmos.h"
namespace {
const char *uni_position = "position";
const char *uni_direction = "direction";
};
/*!
\class SpotLight
\brief A Spot Light emits light from a single point in a cone shape.
\inmodule Engine
To determine the emitter position and emit direction SpotLight uses Transform component of the own Actor.
*/
SpotLight::SpotLight() :
m_angle(0.0f) {
setOuterAngle(45.0f);
Material *material = Engine::loadResource<Material>(".embedded/SpotLight.shader");
if(material) {
MaterialInstance *instance = material->createInstance();
setMaterial(instance);
}
}
/*!
\internal
*/
int SpotLight::lightType() const {
return BaseLight::SpotLight;
}
/*!
\internal
*/
AABBox SpotLight::bound() const {
float distance = params().y;
float diameter = tan(DEG2RAD * m_angle) * distance;
AABBox aabb(Vector3(0.0f, 0.0f, 0.5f) * distance, Vector3(diameter, diameter, distance));
return aabb * transform()->worldTransform();
}
/*!
Returns the attenuation distance of the light cone.
*/
float SpotLight::attenuationDistance() const {
return params().y;
}
/*!
Changes the attenuation \a distance of the light cone.
*/
void SpotLight::setAttenuationDistance(float distance) {
Vector4 p = params();
p.y = distance;
setParams(p);
}
/*!
Returns the angle of the light cone in degrees.
*/
float SpotLight::outerAngle() const {
return m_angle;
}
/*!
Changes the \a angle of the light cone in degrees.
*/
void SpotLight::setOuterAngle(float angle) {
m_angle = angle;
Vector4 p = params();
p.w = cos(DEG2RAD * m_angle * 0.5f);
setParams(p);
}
/*!
\internal
*/
void SpotLight::drawGizmos() {
Transform *t = transform();
Gizmos::drawIcon(t->worldPosition(), Vector2(0.5f), ".embedded/spotlight.png", color());
}
/*!
\internal
*/
void SpotLight::drawGizmosSelected() {
Transform *t = transform();
float distance = attenuationDistance();
float radius = tan(DEG2RAD * m_angle * 0.5f) * distance;
Matrix4 m(t->worldPosition(), t->worldQuaternion() * Quaternion(Vector3(1, 0, 0), 90), Vector3(1.0f));
Gizmos::drawCircle(t->worldQuaternion() * Vector3(0.0f, 0.0f, -distance), radius, gizmoColor(), m);
Gizmos::drawLines({Vector3(),
Vector3( 0.0f, radius, -distance),
Vector3( 0.0f,-radius, -distance),
Vector3( radius, 0.0f, -distance),
Vector3(-radius, 0.0f, -distance)},
{0, 1, 0, 2, 0, 3, 0, 4}, gizmoColor(), t->worldTransform());
}