From 25bf4a66269c25d595e538062b08735628491fc0 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 29 Aug 2006 15:05:05 +0000 Subject: [PATCH] To the TextureAtlasBuilder added detection of instance where a single drawable has multiple textures on the same texture unit inherited down to it from above. --- src/osgUtil/Optimizer.cpp | 71 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/src/osgUtil/Optimizer.cpp b/src/osgUtil/Optimizer.cpp index 682692dc2..6fd02395f 100644 --- a/src/osgUtil/Optimizer.cpp +++ b/src/osgUtil/Optimizer.cpp @@ -3764,8 +3764,65 @@ void Optimizer::TextureAtlasVisitor::optimize() // build the atlas' _builder.buildAtlas(); + + + typedef std::set StateSetSet; + typedef std::map DrawableStateSetMap; + DrawableStateSetMap dssm; + for(sitr = _statesetMap.begin(); + sitr != _statesetMap.end(); + ++sitr) + { + Drawables& drawables = sitr->second; + for(Drawables::iterator ditr = drawables.begin(); + ditr != drawables.end(); + ++ditr) + { + dssm[(*ditr)->asGeometry()].insert(sitr->first); + } + } - + Drawables drawablesThatHaveMultipleTexturesOnOneUnit; + for(DrawableStateSetMap::iterator ditr = dssm.begin(); + ditr != dssm.end(); + ++ditr) + { + osg::Drawable* drawable = ditr->first; + StateSetSet& ssm = ditr->second; + if (ssm.size()>1) + { + typedef std::map UnitTextureMap; + UnitTextureMap unitTextureMap; + for(StateSetSet::iterator ssm_itr = ssm.begin(); + ssm_itr != ssm.end(); + ++ssm_itr) + { + osg::StateSet* ss = *ssm_itr; + unsigned int numTextureUnits = ss->getTextureAttributeList().size(); + for(unsigned int unit=0; unit(ss->getTextureAttribute(unit, osg::StateAttribute::TEXTURE)); + if (texture) unitTextureMap[unit].insert(texture); + } + } + bool drawablesHasMultiTextureOnOneUnit = false; + for(UnitTextureMap::iterator utm_itr = unitTextureMap.begin(); + utm_itr != unitTextureMap.end() && !drawablesHasMultiTextureOnOneUnit; + ++utm_itr) + { + if (utm_itr->second.size()>1) + { + drawablesHasMultiTextureOnOneUnit = true; + } + } + if (drawablesHasMultiTextureOnOneUnit) + { + drawablesThatHaveMultipleTexturesOnOneUnit.insert(drawable); + } + + } + } + // remap the textures in the StateSet's for(sitr = _statesetMap.begin(); sitr != _statesetMap.end(); @@ -3790,13 +3847,21 @@ void Optimizer::TextureAtlasVisitor::optimize() // first check to see if all drawables are ok for applying texturematrix to. bool canTexMatBeFlattenedToAllDrawables = true; for(Drawables::iterator ditr = drawables.begin(); - ditr != drawables.end(); + ditr != drawables.end() && canTexMatBeFlattenedToAllDrawables; ++ditr) { osg::Geometry* geom = (*ditr)->asGeometry(); osg::Vec2Array* texcoords = geom ? dynamic_cast(geom->getTexCoordArray(unit)) : 0; - if (!texcoords) canTexMatBeFlattenedToAllDrawables = false; + if (!texcoords) + { + canTexMatBeFlattenedToAllDrawables = false; + } + + if (drawablesThatHaveMultipleTexturesOnOneUnit.count(*ditr)!=0) + { + canTexMatBeFlattenedToAllDrawables = false; + } } if (canTexMatBeFlattenedToAllDrawables)