|
| 1 | +#include "AreaLight.h" |
| 2 | + |
| 3 | +#include "Manager.h" |
| 4 | + |
| 5 | +#include <OgreSceneManager.h> |
| 6 | +#include <OgreSceneNode.h> |
| 7 | + |
| 8 | +#include <QtMath> |
| 9 | + |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +namespace |
| 13 | +{ |
| 14 | + |
| 15 | +struct SamplePoint |
| 16 | +{ |
| 17 | + Ogre::Vector3 localPos; |
| 18 | +}; |
| 19 | + |
| 20 | +std::vector<SamplePoint> sampleRectangle(float width, float height, int count) |
| 21 | +{ |
| 22 | + std::vector<SamplePoint> points; |
| 23 | + const int cols = qMax(1, static_cast<int>(std::ceil(std::sqrt(count)))); |
| 24 | + const int rows = qMax(1, (count + cols - 1) / cols); |
| 25 | + for (int r = 0; r < rows; ++r) |
| 26 | + { |
| 27 | + for (int c = 0; c < cols; ++c) |
| 28 | + { |
| 29 | + if (static_cast<int>(points.size()) >= count) |
| 30 | + break; |
| 31 | + const float u = (cols == 1) ? 0.5f : static_cast<float>(c) / static_cast<float>(cols - 1); |
| 32 | + const float v = (rows == 1) ? 0.5f : static_cast<float>(r) / static_cast<float>(rows - 1); |
| 33 | + SamplePoint p; |
| 34 | + p.localPos = Ogre::Vector3((u - 0.5f) * width, (v - 0.5f) * height, 0.0f); |
| 35 | + points.push_back(p); |
| 36 | + } |
| 37 | + } |
| 38 | + return points; |
| 39 | +} |
| 40 | + |
| 41 | +std::vector<SamplePoint> sampleDisk(float radius, int count) |
| 42 | +{ |
| 43 | + std::vector<SamplePoint> points; |
| 44 | + const int n = qMax(1, count); |
| 45 | + for (int i = 0; i < n; ++i) |
| 46 | + { |
| 47 | + const float t = static_cast<float>(i) / static_cast<float>(n); |
| 48 | + const float angle = t * 2.0f * Ogre::Math::PI; |
| 49 | + const float ring = (i % 2 == 0) ? 1.0f : 0.55f; |
| 50 | + SamplePoint p; |
| 51 | + p.localPos = Ogre::Vector3(std::cos(angle) * radius * ring, |
| 52 | + std::sin(angle) * radius * ring, |
| 53 | + 0.0f); |
| 54 | + points.push_back(p); |
| 55 | + } |
| 56 | + return points; |
| 57 | +} |
| 58 | + |
| 59 | +std::vector<SamplePoint> sampleLine(float length, int count) |
| 60 | +{ |
| 61 | + std::vector<SamplePoint> points; |
| 62 | + const int n = qMax(2, count); |
| 63 | + for (int i = 0; i < n; ++i) |
| 64 | + { |
| 65 | + const float t = static_cast<float>(i) / static_cast<float>(n - 1); |
| 66 | + SamplePoint p; |
| 67 | + p.localPos = Ogre::Vector3((t - 0.5f) * length, 0.0f, 0.0f); |
| 68 | + points.push_back(p); |
| 69 | + } |
| 70 | + return points; |
| 71 | +} |
| 72 | + |
| 73 | +} // namespace |
| 74 | + |
| 75 | +namespace AreaLight |
| 76 | +{ |
| 77 | + |
| 78 | +bool isProxyLight(const Ogre::Light* light) |
| 79 | +{ |
| 80 | + if (!light) |
| 81 | + return false; |
| 82 | + const auto any = light->getUserObjectBindings().getUserAny(kProxyLightTag); |
| 83 | + return any.has_value() && Ogre::any_cast<bool>(any); |
| 84 | +} |
| 85 | + |
| 86 | +QString ownerLightName(const Ogre::Light* light) |
| 87 | +{ |
| 88 | + if (!light) |
| 89 | + return {}; |
| 90 | + const auto any = light->getUserObjectBindings().getUserAny(kProxyOwnerKey); |
| 91 | + if (!any.has_value()) |
| 92 | + return {}; |
| 93 | + try |
| 94 | + { |
| 95 | + return QString::fromStdString(Ogre::any_cast<std::string>(any)); |
| 96 | + } |
| 97 | + catch (...) |
| 98 | + { |
| 99 | + return {}; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +void removeProxies(const QString& ownerLightName) |
| 104 | +{ |
| 105 | + auto* mgr = Manager::getSingletonPtr(); |
| 106 | + if (!mgr || ownerLightName.isEmpty()) |
| 107 | + return; |
| 108 | + |
| 109 | + Ogre::SceneManager* sceneMgr = mgr->getSceneMgr(); |
| 110 | + if (!sceneMgr) |
| 111 | + return; |
| 112 | + |
| 113 | + const LightHandle* owner = LightManager::getSingletonPtr() |
| 114 | + ? LightManager::getSingleton()->findLight(ownerLightName) |
| 115 | + : nullptr; |
| 116 | + if (!owner || !owner->sceneNode) |
| 117 | + return; |
| 118 | + |
| 119 | + std::vector<Ogre::SceneNode*> toDestroy; |
| 120 | + for (const auto& child : owner->sceneNode->getChildren()) |
| 121 | + { |
| 122 | + auto* childNode = static_cast<Ogre::SceneNode*>(child); |
| 123 | + for (unsigned short i = 0; i < childNode->numAttachedObjects(); ++i) |
| 124 | + { |
| 125 | + Ogre::MovableObject* obj = childNode->getAttachedObject(i); |
| 126 | + if (!obj || obj->getMovableType() != QStringLiteral("Light")) |
| 127 | + continue; |
| 128 | + auto* light = static_cast<Ogre::Light*>(obj); |
| 129 | + if (isProxyLight(light) && AreaLight::ownerLightName(light) == ownerLightName) |
| 130 | + toDestroy.push_back(childNode); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + for (Ogre::SceneNode* node : toDestroy) |
| 135 | + { |
| 136 | + node->removeAndDestroyAllChildren(); |
| 137 | + node->getCreator()->destroySceneNode(node); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +void syncProxies(const LightHandle& owner, const LightSnapshot& snapshot) |
| 142 | +{ |
| 143 | +#ifndef ENABLE_AREA_LIGHTS |
| 144 | + Q_UNUSED(owner); |
| 145 | + Q_UNUSED(snapshot); |
| 146 | + return; |
| 147 | +#else |
| 148 | + if (!owner.isValid()) |
| 149 | + return; |
| 150 | + |
| 151 | + removeProxies(owner.name); |
| 152 | + |
| 153 | + const QString shape = snapshot.areaShape.trimmed().toLower(); |
| 154 | + if (shape.isEmpty()) |
| 155 | + { |
| 156 | + owner.light->setVisible(snapshot.enabled); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + std::vector<SamplePoint> samples; |
| 161 | + const int count = qBound(2, snapshot.areaSampleCount, 16); |
| 162 | + if (shape == QStringLiteral("rectangle")) |
| 163 | + samples = sampleRectangle(snapshot.areaWidth, snapshot.areaHeight, count); |
| 164 | + else if (shape == QStringLiteral("disk")) |
| 165 | + samples = sampleDisk(qMax(0.05f, snapshot.areaWidth * 0.5f), count); |
| 166 | + else if (shape == QStringLiteral("line")) |
| 167 | + samples = sampleLine(snapshot.areaWidth, count); |
| 168 | + else |
| 169 | + return; |
| 170 | + |
| 171 | + owner.light->setVisible(false); |
| 172 | + |
| 173 | + auto* lights = LightManager::getSingletonPtr(); |
| 174 | + auto* mgr = Manager::getSingletonPtr(); |
| 175 | + if (!lights || !mgr || !mgr->getSceneMgr()) |
| 176 | + return; |
| 177 | + |
| 178 | + Ogre::SceneManager* sceneMgr = mgr->getSceneMgr(); |
| 179 | + const float weight = 1.0f / static_cast<float>(samples.size()); |
| 180 | + int index = 0; |
| 181 | + for (const SamplePoint& sample : samples) |
| 182 | + { |
| 183 | + const QString proxyName = owner.name + QStringLiteral("_area%1").arg(index++); |
| 184 | + Ogre::SceneNode* node = owner.sceneNode->createChildSceneNode(proxyName.toStdString()); |
| 185 | + Ogre::Light* proxyLight = sceneMgr->createLight(proxyName.toStdString()); |
| 186 | + proxyLight->setType(Ogre::Light::LT_POINT); |
| 187 | + proxyLight->setDiffuseColour(owner.light->getDiffuseColour()); |
| 188 | + proxyLight->setSpecularColour(owner.light->getSpecularColour()); |
| 189 | + proxyLight->setPowerScale(owner.light->getPowerScale() * weight); |
| 190 | + proxyLight->setAttenuation(owner.light->getAttenuationRange(), |
| 191 | + owner.light->getAttenuationConstant(), |
| 192 | + owner.light->getAttenuationLinear(), |
| 193 | + owner.light->getAttenuationQuadric()); |
| 194 | + proxyLight->setVisible(snapshot.enabled); |
| 195 | + proxyLight->setCastShadows(false); |
| 196 | + proxyLight->getUserObjectBindings().setUserAny(kProxyLightTag, Ogre::Any(true)); |
| 197 | + proxyLight->getUserObjectBindings().setUserAny(kProxyOwnerKey, |
| 198 | + Ogre::Any(owner.name.toStdString())); |
| 199 | + node->attachObject(proxyLight); |
| 200 | + node->setPosition(sample.localPos); |
| 201 | + } |
| 202 | +#endif |
| 203 | +} |
| 204 | + |
| 205 | +} // namespace AreaLight |
0 commit comments