To set a path when loading model files, use an osg ReaderWriter::Options object.
Put locks in ModelRegistry::readNode and ModelRegistry::readImage to avoid
conflicts when files are loaded from both the pager and the main thread.
<File
RelativePath="..\..\simgear\misc\zfstream.hxx">
</File>
+ <File
+ RelativePath="..\..\simgear\misc\PathOptions.cxx">
+ </File>
+ <File
+ RelativePath="..\..\simgear\misc\PathOptions.hxx">
+ </File>
</Filter>
<Filter
Name="Lib_sgroute"
texcoord.hxx \
zfstream.hxx \
interpolator.hxx \
- stdint.hxx
+ stdint.hxx \
+ PathOptions.hxx
libsgmisc_a_SOURCES = \
sg_path.cxx \
tabbed_values.cxx \
texcoord.cxx \
zfstream.cxx \
- interpolator.cxx
+ interpolator.cxx \
+ PathOptions.cxx
noinst_PROGRAMS = tabbed_value_test swap_test
--- /dev/null
+// PathOptions.cxx -- make an osgDB Options object from a path
+// Copyright (C) 2007 Tim Moore timoore@redhat.com
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// $Id$
+
+#include <osgDB/Registry>
+
+#include <PathOptions.hxx>
+
+using namespace simgear;
+
+osgDB::ReaderWriter::Options* simgear::makeOptionsFromPath(const SGPath& path)
+{
+ using namespace osgDB;
+ ReaderWriter::Options *options
+ = new ReaderWriter::Options(*(Registry::instance()->getOptions()));
+ options->setDatabasePath(path.str());
+ return options;
+}
--- /dev/null
+// PathOptions.hxx -- make an osgDB Options object from a path
+// Copyright (C) 2007 Tim Moore timoore@redhat.com
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// $Id$
+
+#ifndef PATHOPTIONSHXX
+#define PATHOPTIONSHXX 1
+
+#include <osgDB/ReaderWriter>
+#include <simgear/misc/sg_path.hxx>
+
+namespace simgear
+{
+osgDB::ReaderWriter::Options* makeOptionsFromPath(const SGPath&);
+}
+#endif
_tex_cache_iter = _tex_cache.find(fname);
if (_tex_cache_iter == _tex_cache.end())
{
- osg::Texture2D* texture = SGLoadTexture2D(fname, _wrapu, _wrapv,
+ osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
mipmap ? -1 : 0);
texture->setMaxAnisotropy( SGGetTextureFilter());
state->setTextureAttributeAndModes(0, texture);
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "ModelRegistry.hxx"
+#include <OpenThreads/ScopedLock>
+
#include <osg/observer_ptr>
#include <osg/ref_ptr>
#include <osg/Group>
ModelRegistry::readImage(const string& fileName,
const ReaderWriter::Options* opt)
{
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(readerMutex);
CallbackMap::iterator iter
= imageCallbackMap.find(getFileExtension(fileName));
if (iter != imageCallbackMap.end() && iter->second.valid())
return iter->second->readImage(fileName, opt);
- string absFileName = findDataFile(fileName);
+ string absFileName = findDataFile(fileName, opt);
if (!fileExists(absFileName)) {
SG_LOG(SG_IO, SG_ALERT, "Cannot find image file \""
<< fileName << "\"");
res->addObserver(databaseReference);
// Update liveries
- SGTextureUpdateVisitor liveryUpdate(getDataFilePathList());
+ SGTextureUpdateVisitor liveryUpdate(opt->getDatabasePathList());
res->accept(liveryUpdate);
return res;
}
{
string fileSansExtension = getNameLessExtension(name);
string osgFileName = fileSansExtension + ".osg";
- string absFileName = findDataFile(osgFileName);
+ string absFileName = findDataFile(osgFileName, opt);
return absFileName;
}
ModelRegistry::readNode(const string& fileName,
const ReaderWriter::Options* opt)
{
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(readerMutex);
Registry* registry = Registry::instance();
ReaderWriter::ReadResult res;
Node* cached = 0;
{
ModelRegistryCallbackProxy<ACCallback> g_acRegister("ac");
}
-
-
-ReaderWriter::ReadResult
-OSGFileCallback::readImage(const string& fileName,
- const ReaderWriter::Options* opt)
-{
- return Registry::instance()->readImageImplementation(fileName, opt);
-}
-
-ReaderWriter::ReadResult
-OSGFileCallback::readNode(const string& fileName,
- const ReaderWriter::Options* opt)
-{
- return Registry::instance()->readNodeImplementation(fileName, opt);
-}
#ifndef _SG_MODELREGISTRY_HXX
#define _SG_MODELREGISTRY_HXX 1
+#include <OpenThreads/ReentrantMutex>
+
#include <osg/ref_ptr>
#include <osg/Node>
#include <osgDB/FileUtils>
CallbackMap imageCallbackMap;
CallbackMap nodeCallbackMap;
osg::ref_ptr<DefaultCallback> _defaultCallback;
+ // Protect against simultaneous calls from main thread (MP models)
+ // and pager thread.
+ OpenThreads::ReentrantMutex readerMutex;
};
// Callback that only loads the file without any caching or
->addNodeCallbackForExtension(extension, new T(extension));
}
};
-
-// Callback for file extensions that load files using the default OSG
-// implementation.
-
-class OSGFileCallback : public osgDB::Registry::ReadFileCallback {
-public:
- virtual osgDB::ReaderWriter::ReadResult
- readImage(const std::string& fileName,
- const osgDB::ReaderWriter::Options* opt);
- virtual osgDB::ReaderWriter::ReadResult
- readNode(const std::string& fileName,
- const osgDB::ReaderWriter::Options* opt);
-};
-
}
#endif // _SG_MODELREGISTRY_HXX
bool
SGAnimation::animate(osg::Node* node, const SGPropertyNode* configNode,
- SGPropertyNode* modelRoot)
+ SGPropertyNode* modelRoot,
+ const osgDB::ReaderWriter::Options* options)
{
std::string type = configNode->getStringValue("type", "none");
if (type == "alpha-test") {
SGSelectAnimation animInst(configNode, modelRoot);
animInst.apply(node);
} else if (type == "shader") {
- SGShaderAnimation animInst(configNode, modelRoot);
+ SGShaderAnimation animInst(configNode, modelRoot, options);
animInst.apply(node);
} else if (type == "textranslate" || type == "texrotate" ||
type == "texmultiple") {
#include <osg/Texture2D>
#include <osg/TexMat>
+#include <osgDB/ReaderWriter>
#include <simgear/props/props.hxx>
#include <simgear/misc/sg_path.hxx>
virtual ~SGAnimation();
static bool animate(osg::Node* node, const SGPropertyNode* configNode,
- SGPropertyNode* modelRoot);
+ SGPropertyNode* modelRoot,
+ const osgDB::ReaderWriter::Options* options);
protected:
void apply(osg::Node* node);
class SGShaderAnimation : public SGAnimation {
public:
SGShaderAnimation(const SGPropertyNode* configNode,
- SGPropertyNode* modelRoot);
+ SGPropertyNode* modelRoot,
+ const osgDB::ReaderWriter::Options* options);
virtual osg::Group* createAnimationGroup(osg::Group& parent);
private:
class UpdateCallback;
SG_USING_STD(vector);
osg::Texture2D*
-SGLoadTexture2D(const std::string& path, bool wrapu, bool wrapv, int)
+SGLoadTexture2D(const std::string& path,
+ const osgDB::ReaderWriter::Options* options,
+ bool wrapu, bool wrapv, int)
{
- osg::Image* image = osgDB::readImageFile(path);
+ osg::Image* image;
+ if (options)
+ image = osgDB::readImageFile(path, options);
+ else
+ image = osgDB::readImageFile(path);
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setImage(image);
texture->setDataVariance(osg::Object::STATIC);
}
}
- osgDB::FilePathList pathList = osgDB::getDataFilePathList();
- osgDB::Registry::instance()->initFilePathLists();
+ osg::ref_ptr<osgDB::ReaderWriter::Options> options
+ = new osgDB::ReaderWriter::Options(*osgDB::Registry::instance()
+ ->getOptions());
// Assume that textures are in
// the same location as the XML file.
if (!model) {
- if (texturepath.extension() != "")
+ if (texturepath.extension() != "")
texturepath = texturepath.dir();
- osgDB::Registry::instance()->getDataFilePathList().push_front(texturepath.str());
+ options->setDatabasePath(texturepath.str());
+ if (!externalTexturePath.str().empty())
+ options->getDatabasePathList().push_back(externalTexturePath.str());
- model = osgDB::readNodeFile(modelpath.str());
- if (model == 0)
- throw sg_io_exception("Failed to load 3D model",
- sg_location(modelpath.str()));
+ model = osgDB::readNodeFile(modelpath.str(), options.get());
+ if (model == 0)
+ throw sg_io_exception("Failed to load 3D model",
+ sg_location(modelpath.str()));
}
- osgDB::Registry::instance()->getDataFilePathList().push_front(externalTexturePath.str());
-
// Set up the alignment node
osg::ref_ptr<osg::MatrixTransform> alignmainmodel = new osg::MatrixTransform;
alignmainmodel->addChild(model.get());
animation_nodes = props.getChildren("animation");
for (unsigned i = 0; i < animation_nodes.size(); ++i)
/// OSGFIXME: duh, why not only model?????
- SGAnimation::animate(alignmainmodel.get(), animation_nodes[i], prop_root);
-
- // restore old path list
- osgDB::setDataFilePathList(pathList);
+ SGAnimation::animate(alignmainmodel.get(), animation_nodes[i], prop_root,
+ options.get());
if (props.hasChild("debug-outfile")) {
std::string outputfile = props.getStringValue("debug-outfile",
#include <osg/Node>
#include <osg/Texture2D>
+#include <osgDB/ReaderWriter>
#include <simgear/misc/sg_path.hxx>
#include <simgear/props/props.hxx>
osg::Texture2D*
-SGLoadTexture2D(const std::string& path, bool wrapu = true,
- bool wrapv = true, int mipmaplevels = -1);
+SGLoadTexture2D(const std::string& path,
+ const osgDB::ReaderWriter::Options* options = 0,
+ bool wrapu = true, bool wrapv = true, int mipmaplevels = -1);
inline osg::Texture2D*
-SGLoadTexture2D(const SGPath& path, bool wrapu = true, bool wrapv = true,
+SGLoadTexture2D(const SGPath& path,
+ const osgDB::ReaderWriter::Options* options = 0,
+ bool wrapu = true, bool wrapv = true,
int mipmaplevels = -1)
{
- return SGLoadTexture2D(path.str(), wrapu, wrapv, mipmaplevels);
+ return SGLoadTexture2D(path.str(), options, wrapu, wrapv, mipmaplevels);
}
#endif // __MODEL_HXX
SGShaderAnimation::SGShaderAnimation(const SGPropertyNode* configNode,
- SGPropertyNode* modelRoot) :
+ SGPropertyNode* modelRoot,
+ const osgDB::ReaderWriter::Options*
+ options) :
SGAnimation(configNode, modelRoot)
{
const SGPropertyNode* node = configNode->getChild("texture");
if (node)
- _effect_texture = SGLoadTexture2D(node->getStringValue());
+ _effect_texture = SGLoadTexture2D(node->getStringValue(), options);
}
namespace {
#include <osg/TexMat>
#include <simgear/math/sg_random.h>
+#include <simgear/misc/PathOptions.hxx>
#include <simgear/debug/logstream.hxx>
#include <simgear/scene/model/model.hxx>
#include <simgear/math/polar3d.hxx>
#include "cloudfield.hxx"
#include "cloud.hxx"
+using namespace simgear;
// #if defined(__MINGW32__)
// #define isnan(x) _isnan(x)
// #endif
{
osg::StateSet *stateSet = new osg::StateSet;
- SGPath colorPath(path);
- colorPath.append(colorTexture);
- stateSet->setTextureAttribute(0, SGLoadTexture2D(colorPath));
+ osg::ref_ptr<osgDB::ReaderWriter::Options> options
+ = makeOptionsFromPath(path);
+ stateSet->setTextureAttribute(0, SGLoadTexture2D(colorTexture,
+ options.get()));
stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
osg::TexEnv* texEnv = new osg::TexEnv;
#include <simgear/constants.h>
#include <simgear/screen/colors.hxx>
#include <simgear/scene/model/model.hxx>
+#include <simgear/misc/PathOptions.hxx>
#include "sphere.hxx"
#include "moon.hxx"
+using namespace simgear;
+
// Constructor
SGMoon::SGMoon( void ) :
prev_moon_angle(-1)
stateSet->setRenderBinDetails(-5, "RenderBin");
// set up the orb state
- path.append( "moon.rgba" );
+ osg::ref_ptr<osgDB::ReaderWriter::Options> options
+ = makeOptionsFromPath(path);
- osg::Texture2D* texture = SGLoadTexture2D(path);
+ osg::Texture2D* texture = SGLoadTexture2D("moon.rgba", options.get());
stateSet->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
osg::TexEnv* texEnv = new osg::TexEnv;
texEnv->setMode(osg::TexEnv::MODULATE);
#include <osg/Texture2D>
#include <osgDB/ReadFile>
+#include <simgear/misc/PathOptions.hxx>
#include <simgear/screen/colors.hxx>
#include <simgear/scene/model/model.hxx>
#include "oursun.hxx"
+using namespace simgear;
+
// Constructor
SGSun::SGSun( void ) {
prev_sun_angle = -9999.0;
env_node = property_tree_Node;
- SGPath ihalopath = path, ohalopath = path;
-
+ osg::ref_ptr<osgDB::ReaderWriter::Options> options
+ = makeOptionsFromPath(path);
// build the ssg scene graph sub tree for the sky and connected
// into the provide scene graph branch
sun_transform = new osg::MatrixTransform;
stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
-
osg::Geode* geode = new osg::Geode;
stateSet = geode->getOrCreateStateSet();
stateSet->setRenderBinDetails(-6, "RenderBin");
// set up the sun-state
- path.append( "sun.rgba" );
- osg::Texture2D* texture = SGLoadTexture2D(path);
+ osg::Texture2D* texture = SGLoadTexture2D("sun.rgba", options.get());
stateSet->setTextureAttributeAndModes(0, texture);
// Build scenegraph
sun_transform->addChild( geode );
-
// set up the inner-halo state
geode = new osg::Geode;
stateSet = geode->getOrCreateStateSet();
stateSet->setRenderBinDetails(-7, "RenderBin");
- ihalopath.append( "inner_halo.rgba" );
- texture = SGLoadTexture2D(ihalopath);
+ texture = SGLoadTexture2D("inner_halo.rgba", options.get());
stateSet->setTextureAttributeAndModes(0, texture);
// Build ssg structure
geode->addDrawable(geometry);
sun_transform->addChild( geode );
-
// set up the outer halo state
stateSet = geode->getOrCreateStateSet();
stateSet->setRenderBinDetails(-8, "RenderBin");
- ohalopath.append( "outer_halo.rgba" );
- texture = SGLoadTexture2D(ohalopath);
+ texture = SGLoadTexture2D("outer_halo.rgba", options.get());
stateSet->setTextureAttributeAndModes(0, texture);
// Build ssg structure