Slightly more efficient applying of light state

move
scrawl 9 years ago
parent 3859c58a8a
commit 3e854a84a2

@ -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<osg::StateSet> stateset = new osg::StateSet;
std::vector<osg::ref_ptr<osg::Light> > lights;
for (unsigned int i=0; i<lightList.size();++i)
{
std::vector<osg::ref_ptr<osg::Light> > lights;
lights.push_back(lightList[i]->mLightSource->getLight(frameNum));
osg::ref_ptr<LightStateAttribute> attr = new LightStateAttribute(mStartLight+i, lights);
}
// 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<LightStateAttribute> 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<lightList.size(); ++i)
{
osg::ref_ptr<LightStateAttribute> dummy = new LightStateAttribute(mStartLight+i, std::vector<osg::ref_ptr<osg::Light> >());
stateset->setAttribute(dummy, osg::StateAttribute::ON);
}
stateSetCache.insert(std::make_pair(hash, stateset));

Loading…
Cancel
Save