From 9f6be131a4eb1a156de7092d824389abb3037237 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 26 Aug 2006 21:38:22 +0000 Subject: [PATCH] Further work of TextureAtlasBuilder --- src/osgUtil/Optimizer.cpp | 62 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/osgUtil/Optimizer.cpp b/src/osgUtil/Optimizer.cpp index c0ad89614..b70d1bb0e 100644 --- a/src/osgUtil/Optimizer.cpp +++ b/src/osgUtil/Optimizer.cpp @@ -2985,6 +2985,8 @@ void Optimizer::TextureAtlasBuilder::addSource(const osg::Texture2D* texture) void Optimizer::TextureAtlasBuilder::buildAtlas() { + // assign the source to the atlas + _atlasList.clear(); for(SourceList::iterator sitr = _sourceList.begin(); sitr != _sourceList.end(); ++sitr) @@ -3012,6 +3014,35 @@ void Optimizer::TextureAtlasBuilder::buildAtlas() } } } + + // build the atlas which are suitable for use, and discard the rest. + AtlasList activeAtlasList; + for(AtlasList::iterator aitr = _atlasList.begin(); + aitr != _atlasList.end(); + ++aitr) + { + Atlas* atlas = aitr->get(); + + if (atlas->_sourceList.size()==1) + { + // no point building an atlas with only one entry + // so disconnect the source. + Source* source = atlas->_sourceList[1].get(); + source->_atlas = 0; + atlas->_sourceList.clear(); + } + + if (!(atlas->_sourceList.empty())) + { + activeAtlasList.push_back(atlas); + atlas->clampToNearestPowerOfTwoSize(); + atlas->copySources(); + } + } + + // keep only the active atlas' + _atlasList.swap(activeAtlasList); + } osg::Image* Optimizer::TextureAtlasBuilder::getImageAtlas(unsigned int i) @@ -3133,7 +3164,12 @@ bool Optimizer::TextureAtlasBuilder::Source::suitableForAtlas(unsigned int maxim osg::Matrix Optimizer::TextureAtlasBuilder::Source::computeTextureMatrix() const { - return osg::Matrix(); + if (!_atlas) return osg::Matrix(); + if (!_image) return osg::Matrix(); + if (!(_atlas->_image)) return osg::Matrix(); + + return osg::Matrix::scale(float(_image->s())/float(_atlas->_image->s()), float(_image->t())/float(_atlas->_image->t()), 1.0)* + osg::Matrix::translate(float(_x)/float(_atlas->_image->s()), float(_y)/float(_atlas->_image->t()), 1.0); } bool Optimizer::TextureAtlasBuilder::Atlas::doesSourceFit(Source* source) @@ -3376,7 +3412,29 @@ void Optimizer::TextureAtlasBuilder::Atlas::copySources() itr !=_sourceList.end(); ++itr) { - osg::notify(osg::NOTICE)<<"Copying image "<<(*itr)->_image->getFileName()<<" to "<<(*itr)->_x<<" ,"<<(*itr)->_y<get(); + Atlas* atlas = source->_atlas; + + if (atlas) + { + osg::notify(osg::NOTICE)<<"Copying image "<_image->getFileName()<<" to "<_x<<" ,"<_y<_image.get(); + osg::Image* atlasImage = atlas->_image.get(); + + unsigned int x = source->_y; + unsigned int y = source->_y; + unsigned int rowWidth = sourceImage->getRowSizeInBytes(); + for(int t=0; tt(); ++t, ++y) + { + unsigned char* destPtr = atlasImage->data(x, y); + const unsigned char* sourcePtr = sourceImage->data(0, t); + for(unsigned int i=0; i