From 3e854a84a2454aecad5a1d7529df99e04800bc45 Mon Sep 17 00:00:00 2001 From: scrawl Date: Wed, 17 Feb 2016 02:27:40 +0100 Subject: [PATCH] Slightly more efficient applying of light state --- components/sceneutil/lightmanager.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/components/sceneutil/lightmanager.cpp b/components/sceneutil/lightmanager.cpp index 4d8edec42..3950d0aea 100644 --- a/components/sceneutil/lightmanager.cpp +++ b/components/sceneutil/lightmanager.cpp @@ -45,6 +45,8 @@ namespace SceneUtil virtual void apply(osg::State& state) const { + if (mLights.empty()) + return; osg::Matrix modelViewMatrix = state.getModelViewMatrix(); state.applyModelViewMatrix(state.getInitialViewMatrix()); @@ -193,16 +195,25 @@ namespace SceneUtil else { osg::ref_ptr stateset = new osg::StateSet; - + std::vector > lights; for (unsigned int i=0; i > lights; lights.push_back(lightList[i]->mLightSource->getLight(frameNum)); - osg::ref_ptr attr = new LightStateAttribute(mStartLight+i, lights); - // don't use setAttributeAndModes, that does not support light indices! - stateset->setAttribute(attr, osg::StateAttribute::ON); - stateset->setAssociatedModes(attr, osg::StateAttribute::ON); + } + // the first light state attribute handles the actual state setting for all lights + // it's best to batch these up so that we don't need to touch the modelView matrix more than necessary + osg::ref_ptr attr = new LightStateAttribute(mStartLight, lights); + // don't use setAttributeAndModes, that does not support light indices! + stateset->setAttribute(attr, osg::StateAttribute::ON); + stateset->setAssociatedModes(attr, osg::StateAttribute::ON); + + // need to push some dummy attributes to ensure proper state tracking + // lights need to reset to their default when the StateSet is popped + for (unsigned int i=1; i dummy = new LightStateAttribute(mStartLight+i, std::vector >()); + stateset->setAttribute(dummy, osg::StateAttribute::ON); } stateSetCache.insert(std::make_pair(hash, stateset));