![]() |
VR-Forces 4.0.4 Class Documentation
|
00001 /****************************************************************************** 00002 ** Copyright (c) 2008 MAK Technologies, Inc. 00003 ** All rights reserved. 00004 ******************************************************************************/ 00005 /****************************************************************************** 00006 ** $RCSfile: DtOsgFileCache.h,v $ $Revision: 1.22 $ $State: Exp $ 00007 ******************************************************************************/ 00008 00012 00013 #pragma once 00014 #include <vrvOsg/vrvOsg.h> 00015 #include <vrvOsg/DtOsgReferencedCollection.h> 00016 #include <vrvCore/DtFileCache.h> 00017 #include <vrvUtil/signalslib.h> 00018 #include <osgDB/ReaderWriter> 00019 00020 #include <boost/unordered_map.hpp> 00021 #include <boost/unordered_set.hpp> 00022 #include <osgDB/ReaderWriter> 00023 #define _WINSOCKAPI_ 00024 #include <boost/thread/mutex.hpp> 00025 #include <boost/signals2.hpp> 00026 00027 namespace osg 00028 { 00029 class Drawable; 00030 class GraphicsContext; 00031 class Node; 00032 class Texture; 00033 } 00034 00035 namespace makVrv 00036 { 00037 class DtMeifConverter; 00038 00043 class DT_DLL_VRVOSG DtOsgFileCache : public DtFileCache 00044 { 00045 public: 00046 00049 friend class DtOsgFileCacheCreator; 00050 00052 virtual ~DtOsgFileCache(); 00053 00055 virtual void releaseResources() 00056 { 00057 releaseResources(0); 00058 } 00059 00061 virtual void releaseResources(osg::State* state); 00062 00066 osg::Node* readNodeFile( const std::string& inputFilename, 00067 const LoadFileOptions& loadFileOptions); 00068 00071 inline osg::ref_ptr<osg::Node> findNodeFile( const std::string& inputFilename ) 00072 { 00073 return ts_findNodeFile(inputFilename); 00074 } 00075 00079 osg::Image* readImageFile( const std::string& inputFilename, 00080 const LoadFileOptions& loadFileOptions); 00081 00084 virtual osg::Texture* getTextureInstance(const std::string& filePath, 00085 osg::Texture::FilterMode minFilter, osg::Texture::FilterMode magFilter, 00086 osg::Texture::WrapMode s, osg::Texture::WrapMode t, 00087 const LoadFileOptions& loadFileOptions); 00088 00090 virtual void encodeOptionsString(const LoadFileOptions& loadFileOptions, 00091 osgDB::ReaderWriter::Options& options) const; 00092 00094 virtual void decodeOptions(const osgDB::ReaderWriter::Options& options, 00095 LoadFileOptions& loadFileOptions) const; 00096 00101 boost::signals2::signal<void (osg::Node*, const std::string& origFileName, 00102 const std::string& actualFileName)> signal_nodeFileLoaded; 00103 00108 boost::signals2::signal<void (osg::Image*, const std::string& origFileName, 00109 const std::string& actualFileName)> signal_imageFileLoaded; 00110 00114 boost::signals2::signal<void (const std::string& filename, 00115 osgDB::ReaderWriter::ReadResult&,const osgDB::ReaderWriter::Options*)> 00116 signal_readNodeCallback; 00117 00121 boost::signals2::signal<void (const std::string& filename, 00122 osgDB::ReaderWriter::ReadResult&,const osgDB::ReaderWriter::Options*)> 00123 signal_readImageCallback; 00124 00125 class TextureCollection : public osg::Referenced 00126 { 00127 public: 00128 union KeyType 00129 { 00130 unsigned short key; 00131 struct 00132 { 00133 unsigned int min : 4; 00134 unsigned int max : 4; 00135 unsigned int wrap_s : 4; 00136 unsigned int wrap_t : 4; 00137 }; 00138 }; 00139 00140 // \Returns trush if the entire texture collection should be flushed. 00141 bool flush() 00142 { 00143 TextureList replacementList; 00144 00145 TextureList::iterator i = myTextures.begin(); 00146 TextureList::iterator e = myTextures.end(); 00147 for(;i != e;++i) 00148 { 00149 if(i->second->referenceCount() > 1) 00150 { 00151 replacementList.push_back(*i); 00152 } 00153 } 00154 if(replacementList.size() != myTextures.size()) 00155 { 00156 myTextures.swap(replacementList); 00157 } 00158 return myTextures.size() == 0; 00159 } 00160 00161 void releaseGLObjects() 00162 { 00163 TextureList::iterator i = myTextures.begin(); 00164 TextureList::iterator e = myTextures.end(); 00165 for(;i != e;++i) 00166 { 00167 i->second->releaseGLObjects(); 00168 } 00169 } 00170 00171 bool findTexture(unsigned int minFilter, unsigned int magFilter, 00172 unsigned int wrapS, unsigned wrapT, osg::ref_ptr<osg::Texture>& texture) 00173 { 00174 KeyType k; 00175 k.min = ((minFilter ^ 0x2600) & 0x3) + ((minFilter ^ 0x2600) >> 7); 00176 k.max = ((magFilter ^ 0x2600) & 0x3) + ((magFilter ^ 0x2600) >> 7); 00177 k.wrap_s = (wrapS & 0x0F) | ((wrapS & 0x10) >> 3); 00178 k.wrap_t = (wrapT & 0x0F) | ((wrapT & 0x10) >> 3); 00179 00180 TextureList::iterator i = myTextures.begin(); 00181 TextureList::iterator e = myTextures.end(); 00182 for(;i != e;++i) 00183 { 00184 if(i->first.key == k.key) 00185 { 00186 texture = i->second; 00187 return true; 00188 } 00189 } 00190 return false; 00191 } 00192 00193 void addTexture(osg::Texture* texture) 00194 { 00195 KeyTexturePair pair; 00196 pair.first.min = ((texture->getFilter(osg::Texture::MIN_FILTER) ^ 0x2600) & 0x3) + 00197 ((texture->getFilter(osg::Texture::MIN_FILTER) ^ 0x2600) >> 7); 00198 00199 pair.first.max = ((texture->getFilter(osg::Texture::MAG_FILTER) ^ 0x2600) & 0x3) + 00200 ((texture->getFilter(osg::Texture::MAG_FILTER) ^ 0x2600) >> 7); 00201 00202 pair.first.wrap_s = (texture->getWrap(osg::Texture::WRAP_S) & 0x0F) | 00203 ((texture->getWrap(osg::Texture::WRAP_S) & 0x10) >> 3); 00204 00205 pair.first.wrap_t = (texture->getWrap(osg::Texture::WRAP_S) & 0x0F) | 00206 ((texture->getWrap(osg::Texture::WRAP_S) & 0x10) >> 3); 00207 pair.second = texture; 00208 myTextures.push_back(pair); 00209 } 00210 00211 typedef std::pair<KeyType,osg::ref_ptr<osg::Texture> > KeyTexturePair; 00212 typedef std::vector<KeyTexturePair> TextureList; 00213 TextureList myTextures; 00214 }; 00215 00216 protected: 00217 00218 friend class DtReadFileCallback; 00219 00221 virtual osg::ref_ptr<osg::Node> ts_readNodeFile( const std::string& inputFilename, 00222 const LoadFileOptions& loadFileOptions); 00223 00225 virtual osg::ref_ptr<osg::Node> ts_findNodeFile( const std::string& inputFilename ); 00226 00228 virtual osg::ref_ptr<osg::Image> ts_readImageFile( const std::string& inputFilename, 00229 const LoadFileOptions& loadFileOptions); 00230 00232 void ts_encryptImage( const std::string& inputFileName, 00233 const std::string& outputFilename, bool printToConsole); 00234 00236 bool ts_resolveRelativeFilename( const LoadFileOptions& loadFileOptions, 00237 std::string& absoluteInputFilename ); 00238 00240 virtual void ts_clearInstancingCache(); 00241 00243 virtual void ts_flushInstancingCache( bool multipass, 00244 bool flushNodes, bool flushTextures, bool flushImages); 00245 00247 DtOsgFileCache(DtDe& de); 00248 00252 std::string ts_buildAcceleratedFilename( const std::string& filePathIn, 00253 const std::string& cachedDir, 00254 const std::string& cachedExtension) const; 00255 00258 virtual bool save(const std::string& origFilename, 00259 const std::string& cachedFilename, bool optimize); 00260 00262 bool shouldSaveFile( const std::string& filename); 00263 00265 bool saveToAcceleratedFile( const std::string& origFileName, 00266 osg::Node* nodeRef, const std::string& cachedFilename, 00267 bool writeExternalReferences); 00268 00270 virtual void addMetaFlightOptions(std::string& optionsString); 00271 00272 protected: 00273 00274 typedef boost::unordered_map<std::string,std::string> RelativePathCache; 00275 00276 DtOsgReferencedCollection<osg::Node> myNodeCollection; 00277 DtOsgReferencedCollection<TextureCollection> myTextureCollection; 00278 DtOsgReferencedCollection<osg::Image> myImageCollection; 00279 00280 unsigned int myMaxFlushTextureCount; 00281 unsigned int myMaxFlushNodeCount; 00282 unsigned int myMaxFlushImageCount; 00283 00284 boost::mutex myRelativePathMutex; 00285 RelativePathCache myRelativePathCache; 00286 DtMeifConverter* myImageCompressor; 00287 }; 00288 00291 class DT_DLL_VRVOSG DtOsgFileCacheCreator : public DtFileCacheCreator 00292 { 00293 00294 public: 00295 00297 virtual DtFileCache* create(DtDe& de); 00298 00299 }; 00300 } 00301 00302 // Include the inl to solve a circular dependency problem for gcc 00303 #include <vrvOsg/DtOsgReferencedCollection.inl> 00304