Keep the light list StateSet cache for more than one frame

c++11
scrawl 10 years ago
parent 07937c741a
commit 1d198a5592

@ -145,7 +145,6 @@ namespace SceneUtil
{
mLightsInViewSpace = false;
mLights.clear();
mStateSetCache.clear();
}
void LightManager::addLight(LightSource* lightSource, osg::Matrix worldMat)
@ -180,7 +179,7 @@ namespace SceneUtil
// possible optimization: return a StateSet containing all requested lights plus some extra lights (if a suitable one exists)
size_t hash = 0;
for (unsigned int i=0; i<lightList.size();++i)
boost::hash_combine(hash, lightList[i]);
boost::hash_combine(hash, lightList[i]->getId());
LightStateSetMap::iterator found = mStateSetCache.find(hash);
if (found != mStateSetCache.end())
@ -190,10 +189,7 @@ namespace SceneUtil
std::vector<osg::ref_ptr<osg::Light> > lights;
for (unsigned int i=0; i<lightList.size();++i)
{
const LightSourceTransform& l = mLights[lightList[i]];
lights.push_back(l.mLightSource->getLight());
}
lights.push_back(lightList[i]->getLight());
osg::ref_ptr<LightStateAttribute> attr = new LightStateAttribute(mStartLight, lights);
@ -223,11 +219,22 @@ namespace SceneUtil
return mStartLight;
}
static int sLightId = 0;
LightSource::LightSource()
: mRadius(0.f)
{
setNodeMask(Mask_Lit);
setUpdateCallback(new CollectLightCallback);
mId = sLightId++;
}
LightSource::LightSource(const LightSource &copy, const osg::CopyOp &copyop)
: osg::Node(copy, copyop)
, mLight(copy.mLight)
, mRadius(copy.mRadius)
{
mId = sLightId++;
}
void LightListCallback::operator()(osg::Node *node, osg::NodeVisitor *nv)
@ -282,12 +289,12 @@ namespace SceneUtil
osg::Matrixf mat = *cv->getModelViewMatrix();
transformBoundingSphere(mat, nodeBound);
std::vector<int> lightList;
std::vector<LightSource*> lightList;
for (unsigned int i=0; i<lights.size(); ++i)
{
const LightManager::LightSourceTransform& l = lights[i];
if (l.mViewBound.intersects(nodeBound))
lightList.push_back(i);
lightList.push_back(l.mLightSource);
}
if (lightList.empty())

@ -20,18 +20,15 @@ namespace SceneUtil
// The activation radius
float mRadius;
int mId;
public:
META_Node(SceneUtil, SceneUtil::LightSource)
LightSource();
LightSource(const LightSource& copy, const osg::CopyOp& copyop)
: osg::Node(copy, copyop)
, mLight(copy.mLight)
, mRadius(copy.mRadius)
{
}
LightSource(const LightSource& copy, const osg::CopyOp& copyop);
float getRadius() const
{
@ -52,6 +49,11 @@ namespace SceneUtil
{
mLight = light;
}
int getId()
{
return mId;
}
};
/// All light sources must be a child of the LightManager node. The LightManager can be anywhere in the scene graph,
@ -83,8 +85,7 @@ namespace SceneUtil
const std::vector<LightSourceTransform>& getLights() const;
// Stores indices into the mLights vector
typedef std::vector<int> LightList;
typedef std::vector<LightSource*> LightList;
osg::ref_ptr<osg::StateSet> getLightListStateSet(const LightList& lightList);

Loading…
Cancel
Save