From 711cc512c5b831e3a7e9c574b51ebb1a1e7f4763 Mon Sep 17 00:00:00 2001 From: James Turner Date: Mon, 10 Jan 2022 15:05:03 +0000 Subject: [PATCH] Effects: pass the model XML path through to makeEffects This gives knowledge of how to resolve relative paths, to makeEffects invocation of findDataFile, and hence gives a more familiar lookup ordering, for Aircraft-supplied effects. SF-ID: https://sourceforge.net/p/flightgear/codetickets/2678/ --- simgear/scene/material/Effect.hxx | 3 ++- simgear/scene/material/makeEffect.cxx | 8 ++++---- simgear/scene/model/SGReaderWriterXML.cxx | 4 ++-- simgear/scene/model/model.cxx | 20 +++++++++++++++----- simgear/scene/model/model.hxx | 15 +++++++++------ 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/simgear/scene/material/Effect.hxx b/simgear/scene/material/Effect.hxx index 438533a3..7bd0258f 100644 --- a/simgear/scene/material/Effect.hxx +++ b/simgear/scene/material/Effect.hxx @@ -160,7 +160,8 @@ size_t hash_value(const Effect::Key&); Effect* makeEffect(const std::string& name, bool realizeTechniques, - const SGReaderWriterOptions* options); + const SGReaderWriterOptions* options, + const SGPath& modelPath = SGPath{}); Effect* makeEffect(SGPropertyNode* prop, bool realizeTechniques, diff --git a/simgear/scene/material/makeEffect.cxx b/simgear/scene/material/makeEffect.cxx index ae222bee..c130c8f3 100644 --- a/simgear/scene/material/makeEffect.cxx +++ b/simgear/scene/material/makeEffect.cxx @@ -114,7 +114,8 @@ void mergePropertyTrees(SGPropertyNode* resultNode, Effect* makeEffect(const string& name, bool realizeTechniques, - const SGReaderWriterOptions* options) + const SGReaderWriterOptions* options, + const SGPath& modelPath) { { OpenThreads::ScopedLock lock(effectMutex); @@ -125,8 +126,7 @@ Effect* makeEffect(const string& name, } string effectFileName(name); effectFileName += ".eff"; - string absFileName - = SGModelLib::findDataFile(effectFileName, options); + string absFileName = SGModelLib::findDataFile(effectFileName, options, modelPath); if (absFileName.empty()) { simgear::reportFailure(simgear::LoadFailure::NotFound, simgear::ErrorCode::LoadEffectsShaders, "Couldn't find Effect:" + effectFileName); return nullptr; @@ -197,7 +197,7 @@ Effect* makeEffect(SGPropertyNode* prop, Effect* parent = 0; if (inheritProp) { //prop->removeChild("inherits-from"); - parent = makeEffect(inheritProp->getStringValue(), false, options); + parent = makeEffect(inheritProp->getStringValue(), false, options, filePath); if (parent) { Effect::Key key; key.unmerged = prop; diff --git a/simgear/scene/model/SGReaderWriterXML.cxx b/simgear/scene/model/SGReaderWriterXML.cxx index 55bf60ec..2a908b42 100644 --- a/simgear/scene/model/SGReaderWriterXML.cxx +++ b/simgear/scene/model/SGReaderWriterXML.cxx @@ -559,8 +559,8 @@ sgLoad3DModel_internal(const SGPath& path, // Some material animations (eventually all) are actually effects. makeEffectAnimations(animation_nodes, effect_nodes); { - ref_ptr modelWithEffects - = instantiateEffects(group.get(), effect_nodes, options.get()); + ref_ptr modelWithEffects = instantiateEffects(group.get(), effect_nodes, options.get(), + path.dirPath()); group = static_cast(modelWithEffects.get()); } diff --git a/simgear/scene/model/model.cxx b/simgear/scene/model/model.cxx index f22fddd6..db86832f 100644 --- a/simgear/scene/model/model.cxx +++ b/simgear/scene/model/model.cxx @@ -234,7 +234,7 @@ public: typedef std::map EffectMap; using SplicingVisitor::apply; MakeEffectVisitor(const SGReaderWriterOptions* options = 0) - : _options(options) + : _options(options), _modelPath(SGPath{}) { } virtual void apply(osg::Group& node); @@ -246,10 +246,17 @@ public: _currentEffectParent = effect; } SGPropertyNode* getDefaultEffect() { return _currentEffectParent; } + + void setModelPath(const SGPath& p) + { + _modelPath = p; + } + protected: EffectMap _effectMap; SGPropertyNode_ptr _currentEffectParent; osg::ref_ptr _options; + SGPath _modelPath; }; void MakeEffectVisitor::apply(osg::Group& node) @@ -287,7 +294,7 @@ void MakeEffectVisitor::apply(osg::Geode& geode) makeParametersFromStateSet(ssRoot, ss); SGPropertyNode_ptr effectRoot = new SGPropertyNode; effect::mergePropertyTrees(effectRoot, ssRoot, _currentEffectParent); - Effect* effect = makeEffect(effectRoot, true, _options.get()); + Effect* effect = makeEffect(effectRoot, true, _options.get(), _modelPath); EffectGeode* eg = dynamic_cast(&geode); if (eg) { eg->setEffect(effect); @@ -332,7 +339,8 @@ protected: ref_ptr instantiateEffects(osg::Node* modelGroup, PropertyList& effectProps, - const SGReaderWriterOptions* options) + const SGReaderWriterOptions* options, + const SGPath& modelPath) { SGPropertyNode_ptr defaultEffectPropRoot; MakeEffectVisitor visitor(options); @@ -357,13 +365,15 @@ ref_ptr instantiateEffects(osg::Node* modelGroup, if (!defaultEffectPropRoot) defaultEffectPropRoot = DefaultEffect::instance()->getEffect(); visitor.setDefaultEffect(defaultEffectPropRoot.ptr()); + visitor.setModelPath(modelPath); modelGroup->accept(visitor); osg::NodeList& result = visitor.getResults(); return ref_ptr(result[0].get()); } ref_ptr instantiateMaterialEffects(osg::Node* modelGroup, - const SGReaderWriterOptions* options) + const SGReaderWriterOptions* options, + const SGPath& modelPath) { SGPropertyNode_ptr effect; @@ -390,7 +400,7 @@ ref_ptr instantiateMaterialEffects(osg::Node* modelGroup, effect->addChild("default")->setBoolValue(true); effectProps.push_back(effect); - return instantiateEffects(modelGroup, effectProps, options); + return instantiateEffects(modelGroup, effectProps, options, modelPath); } } diff --git a/simgear/scene/model/model.hxx b/simgear/scene/model/model.hxx index 4e719527..3f747859 100644 --- a/simgear/scene/model/model.hxx +++ b/simgear/scene/model/model.hxx @@ -102,7 +102,8 @@ public: osg::ref_ptr instantiateEffects(osg::Node* model, PropertyList& effectProps, - const SGReaderWriterOptions* options); + const SGReaderWriterOptions* options, + const SGPath& currentDir = SGPath{}); /** * Apply a set of material-defined effects to a model @@ -112,9 +113,10 @@ instantiateEffects(osg::Node* model, * * returns a copy if any nodes are changed */ - osg::ref_ptr - instantiateMaterialEffects(osg::Node* model, - const SGReaderWriterOptions* options); +osg::ref_ptr +instantiateMaterialEffects(osg::Node* model, + const SGReaderWriterOptions* options, + const SGPath& modelPath = SGPath{}); /** * Transform an OSG subgraph by substituting the Effects and @@ -127,10 +129,11 @@ instantiateEffects(osg::Node* model, inline osg::ref_ptr instantiateEffects(osg::Node* model, - const SGReaderWriterOptions* options) + const SGReaderWriterOptions* options, + const SGPath& currentDir = SGPath{}) { PropertyList effectProps; - return instantiateEffects(model, effectProps, options); + return instantiateEffects(model, effectProps, options, currentDir); } } #endif // __MODEL_HXX