1 #include "texturemanager.hpp"
2 
3 #include <osg/Stats>
4 #include <osg/Texture2D>
5 
6 #include <components/resource/scenemanager.hpp>
7 #include <components/resource/imagemanager.hpp>
8 #include <components/resource/objectcache.hpp>
9 
10 namespace Terrain
11 {
12 
TextureManager(Resource::SceneManager * sceneMgr)13 TextureManager::TextureManager(Resource::SceneManager *sceneMgr)
14     : ResourceManager(sceneMgr->getVFS())
15     , mSceneManager(sceneMgr)
16 {
17 
18 }
19 
20 struct UpdateTextureFilteringFunctor
21 {
UpdateTextureFilteringFunctorTerrain::UpdateTextureFilteringFunctor22     UpdateTextureFilteringFunctor(Resource::SceneManager* sceneMgr)
23         : mSceneManager(sceneMgr)
24     {
25     }
26     Resource::SceneManager* mSceneManager;
27 
operator ()Terrain::UpdateTextureFilteringFunctor28     void operator()(std::string, osg::Object* obj)
29     {
30         mSceneManager->applyFilterSettings(static_cast<osg::Texture2D*>(obj));
31     }
32 };
33 
updateTextureFiltering()34 void TextureManager::updateTextureFiltering()
35 {
36     UpdateTextureFilteringFunctor f(mSceneManager);
37     mCache->call(f);
38 }
39 
getTexture(const std::string & name)40 osg::ref_ptr<osg::Texture2D> TextureManager::getTexture(const std::string &name)
41 {
42     // don't bother with case folding, since there is only one way of referring to terrain textures we can assume the case is always the same
43     osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(name);
44     if (obj)
45         return static_cast<osg::Texture2D*>(obj.get());
46     else
47     {
48         osg::ref_ptr<osg::Texture2D> texture (new osg::Texture2D(mSceneManager->getImageManager()->getImage(name)));
49         texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
50         texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
51         mSceneManager->applyFilterSettings(texture);
52         mCache->addEntryToObjectCache(name, texture.get());
53         return texture;
54     }
55 }
56 
reportStats(unsigned int frameNumber,osg::Stats * stats) const57 void TextureManager::reportStats(unsigned int frameNumber, osg::Stats *stats) const
58 {
59     stats->setAttribute(frameNumber, "Terrain Texture", mCache->getCacheSize());
60 }
61 
62 
63 
64 }
65