From 987e923790f91ef1e62a9464409a5d1c6d351ee1 Mon Sep 17 00:00:00 2001 From: scrawl Date: Sun, 12 Apr 2015 19:44:48 +0200 Subject: [PATCH] LightManager optimization --- components/sceneutil/lightmanager.cpp | 67 ++++++++++++++++++--------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/components/sceneutil/lightmanager.cpp b/components/sceneutil/lightmanager.cpp index 5bccd53b3..1aaf7ab40 100644 --- a/components/sceneutil/lightmanager.cpp +++ b/components/sceneutil/lightmanager.cpp @@ -15,17 +15,32 @@ namespace SceneUtil { - class LightStateAttribute : public osg::Light + // Resets the modelview matrix to just the view matrix before applying lights. + class LightStateAttribute : public osg::StateAttribute { public: - LightStateAttribute() {} + LightStateAttribute() : mIndex(0) {} + LightStateAttribute(unsigned int index, const std::vector >& lights) : mIndex(index), mLights(lights) {} - LightStateAttribute(const LightStateAttribute& light,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) - : osg::Light(light,copyop) {} + LightStateAttribute(const LightStateAttribute& copy,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) + : osg::StateAttribute(copy,copyop), mIndex(copy.mIndex), mLights(copy.mLights) {} - LightStateAttribute(const osg::Light& light,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) - : osg::Light(light,copyop) {} + unsigned int getMember() const + { + return mIndex; + } + virtual bool getModeUsage(ModeUsage & usage) const + { + for (unsigned int i=0; isetLightNum(i+mIndex); + mLights[i]->apply(state); + } state.applyModelViewMatrix(modelViewMatrix); } + + private: + unsigned int mIndex; + + std::vector > mLights; }; // Set on a LightSource. Adds the light source to its light manager for the current frame. @@ -133,6 +153,9 @@ namespace SceneUtil LightSourceTransform l; l.mLightSource = lightSource; l.mWorldMatrix = worldMat; + lightSource->getLight()->setPosition(osg::Vec4f(worldMat.getTrans().x(), + worldMat.getTrans().y(), + worldMat.getTrans().z(), 1.f)); mLights.push_back(l); } @@ -164,24 +187,22 @@ namespace SceneUtil return found->second; else { - osg::ref_ptr stateset (new osg::StateSet); + + std::vector > lights; for (unsigned int i=0; igetLight(); - - osg::ref_ptr clonedLight = new LightStateAttribute(*light, osg::CopyOp::DEEP_COPY_ALL); - clonedLight->setPosition(mLights[lightIndex].mWorldMatrix.preMult(light->getPosition())); + const LightSourceTransform& l = mLights[lightList[i]]; + lights.push_back(l.mLightSource->getLight()); + } - clonedLight->setLightNum(i+mStartLight); + osg::ref_ptr attr = new LightStateAttribute(mStartLight, lights); - // don't use setAttributeAndModes, that does not support light indices! - stateset->setAttribute(clonedLight, osg::StateAttribute::ON); - stateset->setAssociatedModes(clonedLight, osg::StateAttribute::ON); + osg::ref_ptr stateset = new osg::StateSet; - //stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF); + // don't use setAttributeAndModes, that does not support light indices! + stateset->setAttribute(attr, osg::StateAttribute::ON); + stateset->setAssociatedModes(attr, osg::StateAttribute::ON); - } mStateSetCache.insert(std::make_pair(hash, stateset)); return stateset; }