-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAreaLight.cpp
More file actions
205 lines (179 loc) · 6.39 KB
/
Copy pathAreaLight.cpp
File metadata and controls
205 lines (179 loc) · 6.39 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "AreaLight.h"
#include "Manager.h"
#include <OgreSceneManager.h>
#include <OgreSceneNode.h>
#include <QtMath>
#include <vector>
namespace
{
struct SamplePoint
{
Ogre::Vector3 localPos;
};
std::vector<SamplePoint> sampleRectangle(float width, float height, int count)
{
std::vector<SamplePoint> points;
const int cols = qMax(1, static_cast<int>(std::ceil(std::sqrt(count))));
const int rows = qMax(1, (count + cols - 1) / cols);
for (int r = 0; r < rows; ++r)
{
for (int c = 0; c < cols; ++c)
{
if (static_cast<int>(points.size()) >= count)
break;
const float u = (cols == 1) ? 0.5f : static_cast<float>(c) / static_cast<float>(cols - 1);
const float v = (rows == 1) ? 0.5f : static_cast<float>(r) / static_cast<float>(rows - 1);
SamplePoint p;
p.localPos = Ogre::Vector3((u - 0.5f) * width, (v - 0.5f) * height, 0.0f);
points.push_back(p);
}
}
return points;
}
std::vector<SamplePoint> sampleDisk(float radius, int count)
{
std::vector<SamplePoint> points;
const int n = qMax(1, count);
for (int i = 0; i < n; ++i)
{
const float t = static_cast<float>(i) / static_cast<float>(n);
const float angle = t * 2.0f * Ogre::Math::PI;
const float ring = (i % 2 == 0) ? 1.0f : 0.55f;
SamplePoint p;
p.localPos = Ogre::Vector3(std::cos(angle) * radius * ring,
std::sin(angle) * radius * ring,
0.0f);
points.push_back(p);
}
return points;
}
std::vector<SamplePoint> sampleLine(float length, int count)
{
std::vector<SamplePoint> points;
const int n = qMax(2, count);
for (int i = 0; i < n; ++i)
{
const float t = static_cast<float>(i) / static_cast<float>(n - 1);
SamplePoint p;
p.localPos = Ogre::Vector3((t - 0.5f) * length, 0.0f, 0.0f);
points.push_back(p);
}
return points;
}
} // namespace
namespace AreaLight
{
bool isProxyLight(const Ogre::Light* light)
{
if (!light)
return false;
const auto any = light->getUserObjectBindings().getUserAny(kProxyLightTag);
return any.has_value() && Ogre::any_cast<bool>(any);
}
QString ownerLightName(const Ogre::Light* light)
{
if (!light)
return {};
const auto any = light->getUserObjectBindings().getUserAny(kProxyOwnerKey);
if (!any.has_value())
return {};
try
{
return QString::fromStdString(Ogre::any_cast<std::string>(any));
}
catch (...)
{
return {};
}
}
void removeProxies(const QString& ownerLightName)
{
auto* mgr = Manager::getSingletonPtr();
if (!mgr || ownerLightName.isEmpty())
return;
Ogre::SceneManager* sceneMgr = mgr->getSceneMgr();
if (!sceneMgr)
return;
const LightHandle* owner = LightManager::getSingletonPtr()
? LightManager::getSingleton()->findLight(ownerLightName)
: nullptr;
if (!owner || !owner->sceneNode)
return;
std::vector<Ogre::SceneNode*> toDestroy;
for (const auto& child : owner->sceneNode->getChildren())
{
auto* childNode = static_cast<Ogre::SceneNode*>(child);
for (unsigned short i = 0; i < childNode->numAttachedObjects(); ++i)
{
Ogre::MovableObject* obj = childNode->getAttachedObject(i);
if (!obj || obj->getMovableType() != QStringLiteral("Light"))
continue;
auto* light = static_cast<Ogre::Light*>(obj);
if (isProxyLight(light) && AreaLight::ownerLightName(light) == ownerLightName)
toDestroy.push_back(childNode);
}
}
for (Ogre::SceneNode* node : toDestroy)
{
node->removeAndDestroyAllChildren();
node->getCreator()->destroySceneNode(node);
}
}
void syncProxies(const LightHandle& owner, const LightSnapshot& snapshot)
{
#ifndef ENABLE_AREA_LIGHTS
Q_UNUSED(owner);
Q_UNUSED(snapshot);
return;
#else
if (!owner.isValid())
return;
removeProxies(owner.name);
const QString shape = snapshot.areaShape.trimmed().toLower();
if (shape.isEmpty())
{
owner.light->setVisible(snapshot.enabled);
return;
}
std::vector<SamplePoint> samples;
const int count = qBound(2, snapshot.areaSampleCount, 16);
if (shape == QStringLiteral("rectangle"))
samples = sampleRectangle(snapshot.areaWidth, snapshot.areaHeight, count);
else if (shape == QStringLiteral("disk"))
samples = sampleDisk(qMax(0.05f, snapshot.areaWidth * 0.5f), count);
else if (shape == QStringLiteral("line"))
samples = sampleLine(snapshot.areaWidth, count);
else
return;
owner.light->setVisible(false);
auto* lights = LightManager::getSingletonPtr();
auto* mgr = Manager::getSingletonPtr();
if (!lights || !mgr || !mgr->getSceneMgr())
return;
Ogre::SceneManager* sceneMgr = mgr->getSceneMgr();
const float weight = 1.0f / static_cast<float>(samples.size());
int index = 0;
for (const SamplePoint& sample : samples)
{
const QString proxyName = owner.name + QStringLiteral("_area%1").arg(index++);
Ogre::SceneNode* node = owner.sceneNode->createChildSceneNode(proxyName.toStdString());
Ogre::Light* proxyLight = sceneMgr->createLight(proxyName.toStdString());
proxyLight->setType(Ogre::Light::LT_POINT);
proxyLight->setDiffuseColour(owner.light->getDiffuseColour());
proxyLight->setSpecularColour(owner.light->getSpecularColour());
proxyLight->setPowerScale(owner.light->getPowerScale() * weight);
proxyLight->setAttenuation(owner.light->getAttenuationRange(),
owner.light->getAttenuationConstant(),
owner.light->getAttenuationLinear(),
owner.light->getAttenuationQuadric());
proxyLight->setVisible(snapshot.enabled);
proxyLight->setCastShadows(false);
proxyLight->getUserObjectBindings().setUserAny(kProxyLightTag, Ogre::Any(true));
proxyLight->getUserObjectBindings().setUserAny(kProxyOwnerKey,
Ogre::Any(owner.name.toStdString()));
node->attachObject(proxyLight);
node->setPosition(sample.localPos);
}
#endif
}
} // namespace AreaLight