mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-06 00:40:04 +00:00
Merge branch 'dayofviewindependence' into 'master'
Make object light lists (mostly) view-independent See merge request OpenMW/openmw!4128
This commit is contained in:
commit
a2e83d9a21
@ -26,14 +26,6 @@ namespace
|
|||||||
{
|
{
|
||||||
constexpr int ffpMaxLights = 8;
|
constexpr int ffpMaxLights = 8;
|
||||||
|
|
||||||
bool sortLights(const SceneUtil::LightManager::LightSourceViewBound* left,
|
|
||||||
const SceneUtil::LightManager::LightSourceViewBound* right)
|
|
||||||
{
|
|
||||||
static auto constexpr illuminationBias = 81.f;
|
|
||||||
return left->mViewBound.center().length2() - left->mViewBound.radius2() * illuminationBias
|
|
||||||
< right->mViewBound.center().length2() - right->mViewBound.radius2() * illuminationBias;
|
|
||||||
}
|
|
||||||
|
|
||||||
void configurePosition(osg::Matrixf& mat, const osg::Vec4& pos)
|
void configurePosition(osg::Matrixf& mat, const osg::Vec4& pos)
|
||||||
{
|
{
|
||||||
mat(0, 0) = pos.x();
|
mat(0, 0) = pos.x();
|
||||||
@ -1130,10 +1122,8 @@ namespace SceneUtil
|
|||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<LightManager::LightSourceViewBound>& LightManager::getLightsInViewSpace(
|
const std::vector<LightManager::LightSourceViewBound>& LightManager::getLightsInViewSpace(
|
||||||
osgUtil::CullVisitor* cv, const osg::RefMatrix* viewMatrix, size_t frameNum)
|
osg::Camera* camera, const osg::RefMatrix* viewMatrix, size_t frameNum)
|
||||||
{
|
{
|
||||||
osg::Camera* camera = cv->getCurrentCamera();
|
|
||||||
|
|
||||||
osg::observer_ptr<osg::Camera> camPtr(camera);
|
osg::observer_ptr<osg::Camera> camPtr(camera);
|
||||||
auto it = mLightsInViewSpace.find(camPtr);
|
auto it = mLightsInViewSpace.find(camPtr);
|
||||||
|
|
||||||
@ -1147,7 +1137,7 @@ namespace SceneUtil
|
|||||||
|
|
||||||
float radius = transform.mLightSource->getRadius();
|
float radius = transform.mLightSource->getRadius();
|
||||||
|
|
||||||
osg::BoundingSphere viewBound = osg::BoundingSphere(osg::Vec3f(0, 0, 0), radius);
|
osg::BoundingSphere viewBound(osg::Vec3f(), radius * mPointLightRadiusMultiplier);
|
||||||
transformBoundingSphere(worldViewMat, viewBound);
|
transformBoundingSphere(worldViewMat, viewBound);
|
||||||
|
|
||||||
if (transform.mLightSource->getLastAppliedFrame() != frameNum && mPointLightFadeEnd != 0.f)
|
if (transform.mLightSource->getLastAppliedFrame() != frameNum && mPointLightFadeEnd != 0.f)
|
||||||
@ -1164,15 +1154,6 @@ namespace SceneUtil
|
|||||||
transform.mLightSource->setLastAppliedFrame(frameNum);
|
transform.mLightSource->setLastAppliedFrame(frameNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove lights culled by this camera
|
|
||||||
if (!usingFFP())
|
|
||||||
{
|
|
||||||
viewBound._radius *= 2.f;
|
|
||||||
if (cv->getModelViewCullingStack().front().isCulled(viewBound))
|
|
||||||
continue;
|
|
||||||
viewBound._radius /= 2.f;
|
|
||||||
}
|
|
||||||
viewBound._radius *= mPointLightRadiusMultiplier;
|
|
||||||
LightSourceViewBound l;
|
LightSourceViewBound l;
|
||||||
l.mLightSource = transform.mLightSource;
|
l.mLightSource = transform.mLightSource;
|
||||||
l.mViewBound = viewBound;
|
l.mViewBound = viewBound;
|
||||||
@ -1180,8 +1161,10 @@ namespace SceneUtil
|
|||||||
}
|
}
|
||||||
|
|
||||||
const bool fillPPLights = mPPLightBuffer && it->first->getName() == Constants::SceneCamera;
|
const bool fillPPLights = mPPLightBuffer && it->first->getName() == Constants::SceneCamera;
|
||||||
|
const bool sceneLimitReached = getLightingMethod() == LightingMethod::SingleUBO
|
||||||
|
&& it->second.size() > static_cast<size_t>(getMaxLightsInScene() - 1);
|
||||||
|
|
||||||
if (fillPPLights || getLightingMethod() == LightingMethod::SingleUBO)
|
if (fillPPLights || sceneLimitReached)
|
||||||
{
|
{
|
||||||
auto sorter = [](const LightSourceViewBound& left, const LightSourceViewBound& right) {
|
auto sorter = [](const LightSourceViewBound& left, const LightSourceViewBound& right) {
|
||||||
return left.mViewBound.center().length2() - left.mViewBound.radius2()
|
return left.mViewBound.center().length2() - left.mViewBound.radius2()
|
||||||
@ -1202,7 +1185,7 @@ namespace SceneUtil
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (it->second.size() > static_cast<size_t>(getMaxLightsInScene() - 1))
|
if (sceneLimitReached)
|
||||||
it->second.resize(getMaxLightsInScene() - 1);
|
it->second.resize(getMaxLightsInScene() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1284,74 +1267,63 @@ namespace SceneUtil
|
|||||||
// Possible optimizations:
|
// Possible optimizations:
|
||||||
// - organize lights in a quad tree
|
// - organize lights in a quad tree
|
||||||
|
|
||||||
mLastFrameNumber = cv->getTraversalNumber();
|
|
||||||
|
|
||||||
// Don't use Camera::getViewMatrix, that one might be relative to another camera!
|
// Don't use Camera::getViewMatrix, that one might be relative to another camera!
|
||||||
const osg::RefMatrix* viewMatrix = cv->getCurrentRenderStage()->getInitialViewMatrix();
|
const osg::RefMatrix* viewMatrix = cv->getCurrentRenderStage()->getInitialViewMatrix();
|
||||||
const std::vector<LightManager::LightSourceViewBound>& lights
|
|
||||||
= mLightManager->getLightsInViewSpace(cv, viewMatrix, mLastFrameNumber);
|
|
||||||
|
|
||||||
// get the node bounds in view space
|
// Update light list if necessary
|
||||||
// NB do not node->getBound() * modelView, that would apply the node's transformation twice
|
// This makes sure we don't update it more than once per frame when rendering with multiple cameras
|
||||||
osg::BoundingSphere nodeBound;
|
if (mLastFrameNumber != cv->getTraversalNumber())
|
||||||
osg::Transform* transform = node->asTransform();
|
|
||||||
if (transform)
|
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < transform->getNumChildren(); ++i)
|
mLastFrameNumber = cv->getTraversalNumber();
|
||||||
nodeBound.expandBy(transform->getChild(i)->getBound());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
nodeBound = node->getBound();
|
|
||||||
osg::Matrixf mat = *cv->getModelViewMatrix();
|
|
||||||
transformBoundingSphere(mat, nodeBound);
|
|
||||||
|
|
||||||
mLightList.clear();
|
// Get the node bounds in view space
|
||||||
for (size_t i = 0; i < lights.size(); ++i)
|
// NB: do not node->getBound() * modelView, that would apply the node's transformation twice
|
||||||
{
|
osg::BoundingSphere nodeBound;
|
||||||
const LightManager::LightSourceViewBound& l = lights[i];
|
const osg::Transform* transform = node->asTransform();
|
||||||
|
if (transform)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < transform->getNumChildren(); ++i)
|
||||||
|
nodeBound.expandBy(transform->getChild(i)->getBound());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
nodeBound = node->getBound();
|
||||||
|
|
||||||
if (mIgnoredLightSources.count(l.mLightSource))
|
transformBoundingSphere(*cv->getModelViewMatrix(), nodeBound);
|
||||||
continue;
|
|
||||||
|
|
||||||
if (l.mViewBound.intersects(nodeBound))
|
const std::vector<LightManager::LightSourceViewBound>& lights
|
||||||
mLightList.push_back(&l);
|
= mLightManager->getLightsInViewSpace(cv->getCurrentCamera(), viewMatrix, mLastFrameNumber);
|
||||||
|
|
||||||
|
mLightList.clear();
|
||||||
|
for (const LightManager::LightSourceViewBound& light : lights)
|
||||||
|
{
|
||||||
|
if (mIgnoredLightSources.contains(light.mLightSource))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (light.mViewBound.intersects(nodeBound))
|
||||||
|
mLightList.push_back(&light);
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t maxLights = mLightManager->getMaxLights() - mLightManager->getStartLight();
|
||||||
|
|
||||||
|
if (mLightList.size() > maxLights)
|
||||||
|
{
|
||||||
|
// Sort by proximity to object: prefer closer lights with larger radius
|
||||||
|
std::sort(mLightList.begin(), mLightList.end(),
|
||||||
|
[&](const SceneUtil::LightManager::LightSourceViewBound* left,
|
||||||
|
const SceneUtil::LightManager::LightSourceViewBound* right) {
|
||||||
|
const float leftDist = (nodeBound.center() - left->mViewBound.center()).length2();
|
||||||
|
const float rightDist = (nodeBound.center() - right->mViewBound.center()).length2();
|
||||||
|
// A tricky way to compare normalized distance. This avoids division by near zero
|
||||||
|
return left->mViewBound.radius() * rightDist > right->mViewBound.radius() * leftDist;
|
||||||
|
});
|
||||||
|
|
||||||
|
mLightList.resize(maxLights);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mLightList.empty())
|
if (!mLightList.empty())
|
||||||
{
|
{
|
||||||
size_t maxLights = mLightManager->getMaxLights() - mLightManager->getStartLight();
|
cv->pushStateSet(mLightManager->getLightListStateSet(mLightList, mLastFrameNumber, viewMatrix));
|
||||||
|
|
||||||
osg::ref_ptr<osg::StateSet> stateset = nullptr;
|
|
||||||
|
|
||||||
if (mLightList.size() > maxLights)
|
|
||||||
{
|
|
||||||
LightManager::LightList lightList = mLightList;
|
|
||||||
|
|
||||||
if (mLightManager->usingFFP())
|
|
||||||
{
|
|
||||||
for (auto it = lightList.begin(); it != lightList.end() && lightList.size() > maxLights;)
|
|
||||||
{
|
|
||||||
osg::BoundingSphere bs = (*it)->mViewBound;
|
|
||||||
bs._radius = bs._radius * 2.0;
|
|
||||||
if (cv->getModelViewCullingStack().front().isCulled(bs))
|
|
||||||
it = lightList.erase(it);
|
|
||||||
else
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort by proximity to camera, then get rid of furthest away lights
|
|
||||||
std::sort(lightList.begin(), lightList.end(), sortLights);
|
|
||||||
while (lightList.size() > maxLights)
|
|
||||||
lightList.pop_back();
|
|
||||||
stateset = mLightManager->getLightListStateSet(
|
|
||||||
lightList, cv->getTraversalNumber(), cv->getCurrentRenderStage()->getInitialViewMatrix());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
stateset = mLightManager->getLightListStateSet(
|
|
||||||
mLightList, cv->getTraversalNumber(), cv->getCurrentRenderStage()->getInitialViewMatrix());
|
|
||||||
|
|
||||||
cv->pushStateSet(stateset);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -238,7 +238,7 @@ namespace SceneUtil
|
|||||||
void addLight(LightSource* lightSource, const osg::Matrixf& worldMat, size_t frameNum);
|
void addLight(LightSource* lightSource, const osg::Matrixf& worldMat, size_t frameNum);
|
||||||
|
|
||||||
const std::vector<LightSourceViewBound>& getLightsInViewSpace(
|
const std::vector<LightSourceViewBound>& getLightsInViewSpace(
|
||||||
osgUtil::CullVisitor* cv, const osg::RefMatrix* viewMatrix, size_t frameNum);
|
osg::Camera* camera, const osg::RefMatrix* viewMatrix, size_t frameNum);
|
||||||
|
|
||||||
osg::ref_ptr<osg::StateSet> getLightListStateSet(
|
osg::ref_ptr<osg::StateSet> getLightListStateSet(
|
||||||
const LightList& lightList, size_t frameNum, const osg::RefMatrix* viewMatrix);
|
const LightList& lightList, size_t frameNum, const osg::RefMatrix* viewMatrix);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user