From 078fe9e84c655e786d7ddb1a3ac31a5d84a1e007 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 13 May 2009 19:44:27 +0000 Subject: [PATCH] Added support for Object, Image, HeightField and Shaders in FileCache --- include/osgDB/FileCache | 13 +++- src/osgDB/FileCache.cpp | 135 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 146 insertions(+), 2 deletions(-) diff --git a/include/osgDB/FileCache b/include/osgDB/FileCache index 1eed2c2ef..1fa264de2 100644 --- a/include/osgDB/FileCache +++ b/include/osgDB/FileCache @@ -34,10 +34,21 @@ class OSGDB_EXPORT FileCache : public osg::Referenced virtual bool existsInCache(const std::string& originalFileName) const; - virtual ReaderWriter::ReadResult readNode(const std::string& originalFileName, const osgDB::Options* options, bool buildKdTreeIfRequired=true) const; + virtual ReaderWriter::ReadResult readImage(const std::string& originalFileName, const osgDB::Options* options) const; + virtual ReaderWriter::WriteResult writeImage(const osg::Image& image, const std::string& originalFileName, const osgDB::Options* options) const; + virtual ReaderWriter::ReadResult readObject(const std::string& originalFileName, const osgDB::Options* options) const; + virtual ReaderWriter::WriteResult writeObject(const osg::Object& object, const std::string& originalFileName, const osgDB::Options* options) const; + + virtual ReaderWriter::ReadResult readHeightField(const std::string& originalFileName, const osgDB::Options* options) const; + virtual ReaderWriter::WriteResult writeHeightField(const osg::HeightField& hf, const std::string& originalFileName, const osgDB::Options* options) const; + + virtual ReaderWriter::ReadResult readNode(const std::string& originalFileName, const osgDB::Options* options, bool buildKdTreeIfRequired=true) const; virtual ReaderWriter::WriteResult writeNode(const osg::Node& node, const std::string& originalFileName, const osgDB::Options* options) const; + virtual ReaderWriter::ReadResult readShader(const std::string& originalFileName, const osgDB::Options* options) const; + virtual ReaderWriter::WriteResult writeShader(const osg::Shader& shader, const std::string& originalFileName, const osgDB::Options* options) const; + protected: virtual ~FileCache(); diff --git a/src/osgDB/FileCache.cpp b/src/osgDB/FileCache.cpp index e362a3d43..8c5a1ba53 100644 --- a/src/osgDB/FileCache.cpp +++ b/src/osgDB/FileCache.cpp @@ -51,6 +51,105 @@ bool FileCache::existsInCache(const std::string& originalFileName) const return osgDB::fileExists(createCacheFileName(originalFileName)); } +ReaderWriter::ReadResult FileCache::readObject(const std::string& originalFileName, const osgDB::Options* options) const +{ + std::string cacheFileName = createCacheFileName(originalFileName); + if (!cacheFileName.empty() && osgDB::fileExists(cacheFileName)) + { + osg::notify(osg::INFO)<<"FileCache::readObjectFromCache("<readObject(cacheFileName, options); + } + else + { + return 0; + } +} + +ReaderWriter::WriteResult FileCache::writeObject(const osg::Object& object, const std::string& originalFileName, const osgDB::Options* options) const +{ + std::string cacheFileName = createCacheFileName(originalFileName); + if (!cacheFileName.empty()) + { + std::string path = osgDB::getFilePath(cacheFileName); + + if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path)) + { + osg::notify(osg::NOTICE)<<"Could not create cache directory: "<writeObject(object, cacheFileName, options); + } + return ReaderWriter::WriteResult::FILE_NOT_HANDLED; +} + +ReaderWriter::ReadResult FileCache::readImage(const std::string& originalFileName, const osgDB::Options* options) const +{ + std::string cacheFileName = createCacheFileName(originalFileName); + if (!cacheFileName.empty() && osgDB::fileExists(cacheFileName)) + { + osg::notify(osg::INFO)<<"FileCache::readImageFromCache("<readImage(cacheFileName, options); + } + else + { + return 0; + } +} + +ReaderWriter::WriteResult FileCache::writeImage(const osg::Image& image, const std::string& originalFileName, const osgDB::Options* options) const +{ + std::string cacheFileName = createCacheFileName(originalFileName); + if (!cacheFileName.empty()) + { + std::string path = osgDB::getFilePath(cacheFileName); + + if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path)) + { + osg::notify(osg::NOTICE)<<"Could not create cache directory: "<writeImage(image, cacheFileName, options); + } + return ReaderWriter::WriteResult::FILE_NOT_HANDLED; +} + +ReaderWriter::ReadResult FileCache::readHeightField(const std::string& originalFileName, const osgDB::Options* options) const +{ + std::string cacheFileName = createCacheFileName(originalFileName); + if (!cacheFileName.empty() && osgDB::fileExists(cacheFileName)) + { + osg::notify(osg::INFO)<<"FileCache::readHeightFieldFromCache("<readHeightField(cacheFileName, options); + } + else + { + return 0; + } +} + +ReaderWriter::WriteResult FileCache::writeHeightField(const osg::HeightField& hf, const std::string& originalFileName, const osgDB::Options* options) const +{ + std::string cacheFileName = createCacheFileName(originalFileName); + if (!cacheFileName.empty()) + { + std::string path = osgDB::getFilePath(cacheFileName); + + if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path)) + { + osg::notify(osg::NOTICE)<<"Could not create cache directory: "<writeHeightField(hf, cacheFileName, options); + } + return ReaderWriter::WriteResult::FILE_NOT_HANDLED; +} + ReaderWriter::ReadResult FileCache::readNode(const std::string& originalFileName, const osgDB::Options* options, bool buildKdTreeIfRequired) const { std::string cacheFileName = createCacheFileName(originalFileName); @@ -78,8 +177,42 @@ ReaderWriter::WriteResult FileCache::writeNode(const osg::Node& node, const std: return ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE; } - osg::notify(osg::INFO)<<"FileCache::writeNodeToCache("<writeNode(node, cacheFileName, options); } return ReaderWriter::WriteResult::FILE_NOT_HANDLED; } + + +ReaderWriter::ReadResult FileCache::readShader(const std::string& originalFileName, const osgDB::Options* options) const +{ + std::string cacheFileName = createCacheFileName(originalFileName); + if (!cacheFileName.empty() && osgDB::fileExists(cacheFileName)) + { + osg::notify(osg::INFO)<<"FileCache::readShaderFromCache("<readShader(cacheFileName, options); + } + else + { + return 0; + } +} + +ReaderWriter::WriteResult FileCache::writeShader(const osg::Shader& shader, const std::string& originalFileName, const osgDB::Options* options) const +{ + std::string cacheFileName = createCacheFileName(originalFileName); + if (!cacheFileName.empty()) + { + std::string path = osgDB::getFilePath(cacheFileName); + + if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path)) + { + osg::notify(osg::NOTICE)<<"Could not create cache directory: "<writeShader(shader, cacheFileName, options); + } + return ReaderWriter::WriteResult::FILE_NOT_HANDLED; +}