From 256391c3b40ef800f9b53ab97cf6b1fd1a79a495 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 12 Dec 2007 17:04:48 +0000 Subject: [PATCH] From Serge Lages, introduce readRef*File() methods which pass back ref_ptr<> rather than C pointers. --- include/osgDB/DatabasePager | 6 +-- include/osgDB/ReadFile | 83 ++++++++++++++++++++++++++++++++++++- include/osgText/Font | 4 ++ include/osgText/Font3D | 4 ++ src/osgDB/DatabasePager.cpp | 4 +- src/osgDB/ReadFile.cpp | 32 ++++++++++++++ src/osgText/Font.cpp | 53 +++++++++++++++++++++++ src/osgText/Font3D.cpp | 66 +++++++++++++++++++++++++++++ src/osgText/Text.cpp | 2 +- src/osgText/Text3D.cpp | 2 +- 10 files changed, 248 insertions(+), 8 deletions(-) diff --git a/include/osgDB/DatabasePager b/include/osgDB/DatabasePager index 7b80955a1..2a8dfb524 100644 --- a/include/osgDB/DatabasePager +++ b/include/osgDB/DatabasePager @@ -344,7 +344,7 @@ class OSGDB_EXPORT DatabasePager : public osg::NodeVisitor::DatabaseRequestHandl _activeGraphicsContexts.end(), bind1st(mem_fun(&osg::Texture::getTextureObject), texture)) - == _activeGraphicsContexts.size()); + == (int)_activeGraphicsContexts.size()); } inline static bool isCompiled(const osg::StateSet* stateSet, @@ -376,7 +376,7 @@ class OSGDB_EXPORT DatabasePager : public osg::NodeVisitor::DatabaseRequestHandl _activeGraphicsContexts.end(), bind1st(mem_fun(&osg::Texture::getTextureObject), texture)) - != _activeGraphicsContexts.size())) + != (int)_activeGraphicsContexts.size())) return false; } return true; @@ -402,7 +402,7 @@ class OSGDB_EXPORT DatabasePager : public osg::NodeVisitor::DatabaseRequestHandl _activeGraphicsContexts.end(), bind1st(mem_fun(&osg::Drawable::getDisplayList), drawable)) - == _activeGraphicsContexts.size()); + == (int)_activeGraphicsContexts.size()); } return true; } diff --git a/include/osgDB/ReadFile b/include/osgDB/ReadFile index 1335202da..38d7d97af 100644 --- a/include/osgDB/ReadFile +++ b/include/osgDB/ReadFile @@ -46,7 +46,6 @@ inline osg::Object* readObjectFile(const std::string& filename) return readObjectFile(filename,Registry::instance()->getOptions()); } - /** Read an osg::Image from file. * Return valid osg::Image on success, * return NULL on failure. @@ -133,6 +132,88 @@ inline osg::Node* readNodeFiles(osg::ArgumentParser& parser) return readNodeFiles(parser,Registry::instance()->getOptions()); } + +/** Read an osg::Object from file. + * Return an assigned osg::ref_ptr on success, + * return an osg::ref_ptr with a NULL pointer assigned to it on failure. + * Use the Options object to control cache operations and file search paths in osgDB::Registry. + * The osgDB::Registry is used to load the appropriate ReaderWriter plugin + * for the filename extension, and this plugin then handles the request + * to read the specified file.*/ +extern OSGDB_EXPORT osg::ref_ptr readRefObjectFile(const std::string& filename,const ReaderWriter::Options* options); + +/** Read an osg::Object from file. + * Return an assigned osg::ref_ptr on success, + * return an osg::ref_ptr with a NULL pointer assigned to it on failure. + * The osgDB::Registry is used to load the appropriate ReaderWriter plugin + * for the filename extension, and this plugin then handles the request + * to read the specified file.*/ +inline osg::ref_ptr readRefObjectFile(const std::string& filename) +{ + return readRefObjectFile(filename,Registry::instance()->getOptions()); +} + +/** Read an osg::Image from file. + * Return an assigned osg::ref_ptr on success, + * return an osg::ref_ptr with a NULL pointer assigned to it on failure. + * Use the Options object to control cache operations and file search paths in osgDB::Registry. + * The osgDB::Registry is used to load the appropriate ReaderWriter plugin + * for the filename extension, and this plugin then handles the request + * to read the specified file.*/ +extern OSGDB_EXPORT osg::ref_ptr readRefImageFile(const std::string& filename,const ReaderWriter::Options* options); + +/** Read an osg::Image from file. + * Return an assigned osg::ref_ptr on success, + * return an osg::ref_ptr with a NULL pointer assigned to it on failure. + * The osgDB::Registry is used to load the appropriate ReaderWriter plugin + * for the filename extension, and this plugin then handles the request + * to read the specified file.*/ +inline osg::ref_ptr readRefImageFile(const std::string& filename) +{ + return readRefImageFile(filename,Registry::instance()->getOptions()); +} + +/** Read an osg::HeightField from file. + * Return an assigned osg::ref_ptr on success, + * return an osg::ref_ptr with a NULL pointer assigned to it on failure. + * Use the Options object to control cache operations and file search paths in osgDB::Registry. + * The osgDB::Registry is used to load the appropriate ReaderWriter plugin + * for the filename extension, and this plugin then handles the request + * to read the specified file.*/ +extern OSGDB_EXPORT osg::ref_ptr readRefHeightFieldFile(const std::string& filename,const ReaderWriter::Options* options); + +/** Read an osg::HeightField from file. + * Return an assigned osg::ref_ptr on success, + * return an osg::ref_ptr with a NULL pointer assigned to it on failure. + * The osgDB::Registry is used to load the appropriate ReaderWriter plugin + * for the filename extension, and this plugin then handles the request + * to read the specified file.*/ +inline osg::ref_ptr readRefHeightFieldFile(const std::string& filename) +{ + return readRefHeightFieldFile(filename,Registry::instance()->getOptions()); +} + +/** Read an osg::Node from file. + * Return an assigned osg::ref_ptr on success, + * return an osg::ref_ptr with a NULL pointer assigned to it on failure. + * Use the Options object to control cache operations and file search paths in osgDB::Registry. + * The osgDB::Registry is used to load the appropriate ReaderWriter plugin + * for the filename extension, and this plugin then handles the request + * to read the specified file.*/ +extern OSGDB_EXPORT osg::ref_ptr readRefNodeFile(const std::string& filename,const ReaderWriter::Options* options); + +/** Read an osg::Node from file. + * Return an assigned osg::ref_ptr on success, + * return an osg::ref_ptr with a NULL pointer assigned to it on failure. + * The osgDB::Registry is used to load the appropriate ReaderWriter plugin + * for the filename extension, and this plugin then handles the request + * to read the specified file.*/ +inline osg::ref_ptr readRefNodeFile(const std::string& filename) +{ + return readRefNodeFile(filename,Registry::instance()->getOptions()); +} + + } #endif diff --git a/include/osgText/Font b/include/osgText/Font index b1b53b6ce..73580320d 100644 --- a/include/osgText/Font +++ b/include/osgText/Font @@ -59,6 +59,10 @@ extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename, const osgD /** read a font from specified stream.*/ extern OSGTEXT_EXPORT Font* readFontStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions = 0); +extern OSGTEXT_EXPORT osg::ref_ptr readRefFontFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions = 0); + +extern OSGTEXT_EXPORT osg::ref_ptr readRefFontStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions = 0); + extern OSGTEXT_EXPORT std::string findFontFile(const std::string& str); /** Pure virtual base class for fonts. diff --git a/include/osgText/Font3D b/include/osgText/Font3D index f8853138e..bece14be0 100644 --- a/include/osgText/Font3D +++ b/include/osgText/Font3D @@ -54,6 +54,10 @@ extern OSGTEXT_EXPORT Font3D* readFont3DFile(const std::string& filename, const /** read a font from specified stream.*/ extern OSGTEXT_EXPORT Font3D* readFont3DStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions = 0); +extern OSGTEXT_EXPORT osg::ref_ptr readRefFont3DFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions = 0); + +extern OSGTEXT_EXPORT osg::ref_ptr readRefFont3DStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions = 0); + extern OSGTEXT_EXPORT std::string findFont3DFile(const std::string& str); /** Pure virtual base class for fonts. diff --git a/src/osgDB/DatabasePager.cpp b/src/osgDB/DatabasePager.cpp index 3dbea3c77..f74c0d58a 100644 --- a/src/osgDB/DatabasePager.cpp +++ b/src/osgDB/DatabasePager.cpp @@ -687,13 +687,13 @@ void DatabasePager::run() // do *not* assume that we only have one DatabasePager, or that reaNodeFile is thread safe... static OpenThreads::Mutex s_serialize_readNodeFile_mutex; OpenThreads::ScopedLock lock(s_serialize_readNodeFile_mutex); - databaseRequest->_loadedModel = osgDB::readNodeFile(databaseRequest->_fileName, + databaseRequest->_loadedModel = osgDB::readRefNodeFile(databaseRequest->_fileName, databaseRequest->_loadOptions.get()); } else { // assume that we only have one DatabasePager, or that readNodeFile is thread safe... - databaseRequest->_loadedModel = osgDB::readNodeFile(databaseRequest->_fileName, + databaseRequest->_loadedModel = osgDB::readRefNodeFile(databaseRequest->_fileName, databaseRequest->_loadOptions.get()); } diff --git a/src/osgDB/ReadFile.cpp b/src/osgDB/ReadFile.cpp index 22b047898..7134ddefe 100644 --- a/src/osgDB/ReadFile.cpp +++ b/src/osgDB/ReadFile.cpp @@ -238,3 +238,35 @@ Node* osgDB::readNodeFiles(osg::ArgumentParser& arguments,const ReaderWriter::Op } } + +osg::ref_ptr osgDB::readRefObjectFile(const std::string& filename,const ReaderWriter::Options* options) +{ + ReaderWriter::ReadResult rr = Registry::instance()->readObject(filename,options); + if (rr.validObject()) return osg::ref_ptr(rr.getObject()); + if (rr.error()) notify(WARN) << rr.message() << std::endl; + return NULL; +} + +osg::ref_ptr osgDB::readRefImageFile(const std::string& filename,const ReaderWriter::Options* options) +{ + ReaderWriter::ReadResult rr = Registry::instance()->readImage(filename,options); + if (rr.validImage()) return osg::ref_ptr(rr.getImage()); + if (rr.error()) notify(WARN) << rr.message() << std::endl; + return NULL; +} + +osg::ref_ptr osgDB::readRefHeightFieldFile(const std::string& filename,const ReaderWriter::Options* options) +{ + ReaderWriter::ReadResult rr = Registry::instance()->readHeightField(filename,options); + if (rr.validHeightField()) return osg::ref_ptr(rr.getHeightField()); + if (rr.error()) notify(WARN) << rr.message() << std::endl; + return NULL; +} + +osg::ref_ptr osgDB::readRefNodeFile(const std::string& filename,const ReaderWriter::Options* options) +{ + ReaderWriter::ReadResult rr = Registry::instance()->readNode(filename,options); + if (rr.validNode()) return osg::ref_ptr(rr.getNode()); + if (rr.error()) notify(WARN) << rr.message() << std::endl; + return NULL; +} diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index d92c8d200..86bf6ac22 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -151,6 +151,59 @@ osgText::Font* osgText::readFontStream(std::istream& stream, const osgDB::Reader return 0; } +osg::ref_ptr osgText::readRefFontFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions) +{ + if (filename=="") return 0; + + std::string foundFile = findFontFile(filename); + if (foundFile.empty()) return 0; + + OpenThreads::ScopedLock lock(s_FontFileMutex); + + osg::ref_ptr localOptions; + if (!userOptions) + { + localOptions = new osgDB::ReaderWriter::Options; + localOptions->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_OBJECTS); + } + + osg::ref_ptr object = osgDB::readRefObjectFile(foundFile, userOptions ? userOptions : localOptions.get()); + + // if the object is a font then return it. + osgText::Font* font = dynamic_cast(object.get()); + if (font) return osg::ref_ptr(font); + + return 0; +} + +osg::ref_ptr osgText::readRefFontStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions) +{ + OpenThreads::ScopedLock lock(s_FontFileMutex); + + osg::ref_ptr localOptions; + if (!userOptions) + { + localOptions = new osgDB::ReaderWriter::Options; + localOptions->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_OBJECTS); + } + + // there should be a better way to get the FreeType ReaderWriter by name... + osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension("ttf"); + if (reader == 0) return 0; + osgDB::ReaderWriter::ReadResult rr = reader->readObject(stream, userOptions ? userOptions : localOptions.get()); + if (rr.error()) + { + osg::notify(osg::WARN) << rr.message() << std::endl; + return 0; + } + if (!rr.validObject()) return 0; + + // if the object is a font then return it. + osgText::Font* font = dynamic_cast(rr.getObject()); + if (font) return osg::ref_ptr(font); + + return 0; +} Font::Font(FontImplementation* implementation): osg::Object(true), diff --git a/src/osgText/Font3D.cpp b/src/osgText/Font3D.cpp index 7aafd8031..58f8585cb 100644 --- a/src/osgText/Font3D.cpp +++ b/src/osgText/Font3D.cpp @@ -168,6 +168,72 @@ osgText::Font3D* readFont3DStream(std::istream& stream, const osgDB::ReaderWrite return 0; } +osg::ref_ptr readRefFont3DFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions) +{ + if (filename=="") return 0; + + std::string foundFile = findFont3DFile(filename); + if (foundFile.empty()) return 0; + + OpenThreads::ScopedLock lock(s_Font3DFileMutex); + + osg::ref_ptr localOptions; + if (!userOptions) + { + localOptions = new osgDB::ReaderWriter::Options; + localOptions->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_OBJECTS); + // ** HACK to load Font3D instead of Font + localOptions->setPluginData("3D", (void*) 1); + } + else + { + userOptions->setPluginData("3D", (void*) 1); + } + + osg::ref_ptr object = osgDB::readRefObjectFile(foundFile, userOptions ? userOptions : localOptions.get()); + + // if the object is a font then return it. + osgText::Font3D* font3D = dynamic_cast(object.get()); + if (font3D) return osg::ref_ptr(font3D); + + return 0; +} + +osg::ref_ptr readRefFont3DStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions) +{ + OpenThreads::ScopedLock lock(s_Font3DFileMutex); + + osg::ref_ptr localOptions; + if (!userOptions) + { + localOptions = new osgDB::ReaderWriter::Options; + localOptions->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_OBJECTS); + localOptions->setPluginData("3D", (void*) 1); + } + else + { + userOptions->setPluginData("3D", (void*) 1); + } + + // there should be a better way to get the FreeType ReaderWriter by name... + osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension("ttf"); + if (reader == 0) return 0; + + osgDB::ReaderWriter::ReadResult rr = reader->readObject(stream, userOptions ? userOptions : localOptions.get()); + if (rr.error()) + { + osg::notify(osg::WARN) << rr.message() << std::endl; + return 0; + } + if (!rr.validObject()) return 0; + + // if the object is a font then return it. + osgText::Font3D* font3D = dynamic_cast(rr.getObject()); + if (font3D) return osg::ref_ptr(font3D); + + return 0; +} + Font3D::Font3D(Font3DImplementation* implementation): osg::Object(true), _depth(1), diff --git a/src/osgText/Text.cpp b/src/osgText/Text.cpp index 050693583..16148d056 100644 --- a/src/osgText/Text.cpp +++ b/src/osgText/Text.cpp @@ -86,7 +86,7 @@ void Text::setFont(Font* font) void Text::setFont(const std::string& fontfile) { - setFont(readFontFile(fontfile)); + setFont(readRefFontFile(fontfile).get()); } diff --git a/src/osgText/Text3D.cpp b/src/osgText/Text3D.cpp index 0f5f5b6b0..e6973a122 100644 --- a/src/osgText/Text3D.cpp +++ b/src/osgText/Text3D.cpp @@ -51,7 +51,7 @@ void Text3D::setFont(Font3D * font) void Text3D::setFont(const std::string & fontfile) { - setFont(readFont3DFile(fontfile)); + setFont(readRefFont3DFile(fontfile).get()); } String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last)