Canvas placement on dynamic models
1. A canvas can be placed on dynamically created models. Implemented by adding a new placement factory. 2. Added a "loaded" property in every "models/model[...]" path to track when a model is finally loaded by OSG. See https://forum.flightgear.org/viewtopic.php?f=30&t=38318
This commit is contained in:
parent
1dd6ab3cfb
commit
d09046f9a2
3 changed files with 139 additions and 22 deletions
|
@ -22,8 +22,10 @@
|
||||||
#include <Main/fg_props.hxx>
|
#include <Main/fg_props.hxx>
|
||||||
#include <Scripting/NasalModelData.hxx>
|
#include <Scripting/NasalModelData.hxx>
|
||||||
#include <Viewer/CameraGroup.hxx>
|
#include <Viewer/CameraGroup.hxx>
|
||||||
|
#include <Model/modelmgr.hxx>
|
||||||
|
|
||||||
#include <simgear/canvas/Canvas.hxx>
|
#include <simgear/canvas/Canvas.hxx>
|
||||||
|
#include <simgear/scene/model/placement.hxx>
|
||||||
|
|
||||||
namespace sc = simgear::canvas;
|
namespace sc = simgear::canvas;
|
||||||
|
|
||||||
|
@ -54,6 +56,31 @@ static sc::Placements addSceneObjectPlacement( SGPropertyNode* placement,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
static sc::Placements addDynamicModelPlacement(SGPropertyNode* placement,
|
||||||
|
sc::CanvasPtr canvas)
|
||||||
|
{
|
||||||
|
const string dyn_model_path = placement->getStringValue("model-path");
|
||||||
|
if (dyn_model_path.empty())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto modelmgr = globals->get_subsystem<FGModelMgr>();
|
||||||
|
if (!modelmgr)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
FGModelMgr::Instance* model_instance = modelmgr->findInstanceByNodePath(dyn_model_path);
|
||||||
|
if (!model_instance || !model_instance->model || !model_instance->model->getSceneGraph())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return FGODGauge::set_texture(
|
||||||
|
model_instance->model->getSceneGraph(),
|
||||||
|
placement,
|
||||||
|
canvas->getTexture(),
|
||||||
|
canvas->getCullCallback(),
|
||||||
|
canvas);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
CanvasMgr::CanvasMgr():
|
CanvasMgr::CanvasMgr():
|
||||||
simgear::canvas::CanvasMgr( fgGetNode("/canvas/by-index", true) ),
|
simgear::canvas::CanvasMgr( fgGetNode("/canvas/by-index", true) ),
|
||||||
|
@ -85,6 +112,7 @@ void CanvasMgr::init()
|
||||||
});
|
});
|
||||||
|
|
||||||
sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
|
sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
|
||||||
|
sc::Canvas::addPlacementFactory("dynamic-model", &addDynamicModelPlacement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
simgear::canvas::CanvasMgr::init();
|
simgear::canvas::CanvasMgr::init();
|
||||||
|
@ -97,6 +125,7 @@ void CanvasMgr::shutdown()
|
||||||
|
|
||||||
sc::Canvas::removePlacementFactory("object");
|
sc::Canvas::removePlacementFactory("object");
|
||||||
sc::Canvas::removePlacementFactory("scenery-object");
|
sc::Canvas::removePlacementFactory("scenery-object");
|
||||||
|
sc::Canvas::removePlacementFactory("dynamic-model");
|
||||||
|
|
||||||
_gui_camera = 0;
|
_gui_camera = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,13 +25,59 @@
|
||||||
#include <Main/fg_props.hxx>
|
#include <Main/fg_props.hxx>
|
||||||
#include <Scenery/scenery.hxx>
|
#include <Scenery/scenery.hxx>
|
||||||
|
|
||||||
|
#include <osg/ProxyNode>
|
||||||
|
|
||||||
#include "modelmgr.hxx"
|
#include "modelmgr.hxx"
|
||||||
|
|
||||||
using namespace simgear;
|
using namespace simgear;
|
||||||
|
|
||||||
// OSGFIXME
|
namespace {
|
||||||
// extern SGShadowVolume *shadows;
|
|
||||||
|
class CheckInstanceModelLoadedVisitor : public osg::NodeVisitor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CheckInstanceModelLoadedVisitor() :
|
||||||
|
osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR, osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
|
||||||
|
{}
|
||||||
|
|
||||||
|
~CheckInstanceModelLoadedVisitor() = default;
|
||||||
|
|
||||||
|
void apply(osg::Node& node) override
|
||||||
|
{
|
||||||
|
if (!_loaded)
|
||||||
|
return;
|
||||||
|
traverse(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
void apply(osg::ProxyNode& node) override
|
||||||
|
{
|
||||||
|
if (!_loaded)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < node.getNumFileNames(); ++i) {
|
||||||
|
if (node.getFileName(i).empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Check if this is already loaded.
|
||||||
|
if (i < node.getNumChildren() && node.getChild(i))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
_loaded=false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
traverse(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isLoaded() const
|
||||||
|
{
|
||||||
|
return _loaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool _loaded = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // of anonymous namespace
|
||||||
|
|
||||||
FGModelMgr::FGModelMgr ()
|
FGModelMgr::FGModelMgr ()
|
||||||
{
|
{
|
||||||
|
@ -76,6 +122,10 @@ FGModelMgr::add_model (SGPropertyNode * node)
|
||||||
const char *model_path = node->getStringValue("path", "Models/Geometry/glider.ac");
|
const char *model_path = node->getStringValue("path", "Models/Geometry/glider.ac");
|
||||||
osg::Node *object;
|
osg::Node *object;
|
||||||
|
|
||||||
|
Instance * instance = new Instance;
|
||||||
|
instance->loaded_node = node->addChild("loaded");
|
||||||
|
instance->loaded_node->setBoolValue(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::string fullPath = simgear::SGModelLib::findDataFile(model_path);
|
std::string fullPath = simgear::SGModelLib::findDataFile(model_path);
|
||||||
object = SGModelLib::loadDeferredModel(fullPath, globals->get_props());
|
object = SGModelLib::loadDeferredModel(fullPath, globals->get_props());
|
||||||
|
@ -85,7 +135,6 @@ FGModelMgr::add_model (SGPropertyNode * node)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Instance * instance = new Instance;
|
|
||||||
SGModelPlacement *model = new SGModelPlacement;
|
SGModelPlacement *model = new SGModelPlacement;
|
||||||
instance->model = model;
|
instance->model = model;
|
||||||
instance->node = node;
|
instance->node = node;
|
||||||
|
@ -217,6 +266,7 @@ void FGModelMgr::update(double dt)
|
||||||
model->setHeadingDeg(heading);
|
model->setHeadingDeg(heading);
|
||||||
|
|
||||||
instance->model->update();
|
instance->model->update();
|
||||||
|
instance->checkLoaded();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,31 +289,55 @@ FGModelMgr::remove_instance (Instance * instance)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FGModelMgr::Instance*
|
||||||
|
FGModelMgr::findInstanceByNodePath(const std::string& node_path) const
|
||||||
|
{
|
||||||
|
if (node_path.empty())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
SGPropertyNode* node = fgGetNode(node_path, false);
|
||||||
|
if (!node)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
auto it = std::find_if(_instances.begin(), _instances.end(),
|
||||||
|
[node](const Instance* instance)
|
||||||
|
{ return instance->node == node; });
|
||||||
|
|
||||||
|
if (it == _instances.end()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *it;
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// Implementation of FGModelMgr::Instance
|
// Implementation of FGModelMgr::Instance
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
FGModelMgr::Instance::Instance ()
|
|
||||||
: model(0),
|
|
||||||
node(0),
|
|
||||||
lon_deg_node(0),
|
|
||||||
lat_deg_node(0),
|
|
||||||
elev_ft_node(0),
|
|
||||||
roll_deg_node(0),
|
|
||||||
pitch_deg_node(0),
|
|
||||||
heading_deg_node(0),
|
|
||||||
shadow(false)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
FGModelMgr::Instance::~Instance ()
|
FGModelMgr::Instance::~Instance ()
|
||||||
{
|
{
|
||||||
delete model;
|
delete model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FGModelMgr::Instance::checkLoaded() const
|
||||||
|
{
|
||||||
|
if (!model)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (loaded_node->getBoolValue()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckInstanceModelLoadedVisitor cilv;
|
||||||
|
model->getSceneGraph()->accept(cilv);
|
||||||
|
const bool loadedNow = cilv.isLoaded();
|
||||||
|
|
||||||
|
if (loadedNow) {
|
||||||
|
loaded_node->setBoolValue(true);
|
||||||
|
}
|
||||||
|
return loadedNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// Implementation of FGModelMgr::Listener
|
// Implementation of FGModelMgr::Listener
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
#include <simgear/compiler.h> // for SG_USING_STD
|
#include <simgear/compiler.h> // for SG_USING_STD
|
||||||
#include <simgear/structure/subsystem_mgr.hxx>
|
#include <simgear/structure/subsystem_mgr.hxx>
|
||||||
|
|
||||||
|
#include <Scripting/NasalModelData.hxx>
|
||||||
|
|
||||||
// Don't pull in headers, since we don't need them here.
|
// Don't pull in headers, since we don't need them here.
|
||||||
class SGPropertyNode;
|
class SGPropertyNode;
|
||||||
class SGModelPlacement;
|
class SGModelPlacement;
|
||||||
|
@ -23,6 +25,7 @@ class SGModelPlacement;
|
||||||
class FGModelMgr : public SGSubsystem
|
class FGModelMgr : public SGSubsystem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A dynamically-placed model using properties.
|
* A dynamically-placed model using properties.
|
||||||
*
|
*
|
||||||
|
@ -38,9 +41,8 @@ public:
|
||||||
*/
|
*/
|
||||||
struct Instance
|
struct Instance
|
||||||
{
|
{
|
||||||
Instance ();
|
|
||||||
virtual ~Instance ();
|
virtual ~Instance ();
|
||||||
SGModelPlacement * model;
|
SGModelPlacement * model = nullptr;
|
||||||
SGPropertyNode_ptr node;
|
SGPropertyNode_ptr node;
|
||||||
SGPropertyNode_ptr lon_deg_node;
|
SGPropertyNode_ptr lon_deg_node;
|
||||||
SGPropertyNode_ptr lat_deg_node;
|
SGPropertyNode_ptr lat_deg_node;
|
||||||
|
@ -48,7 +50,10 @@ public:
|
||||||
SGPropertyNode_ptr roll_deg_node;
|
SGPropertyNode_ptr roll_deg_node;
|
||||||
SGPropertyNode_ptr pitch_deg_node;
|
SGPropertyNode_ptr pitch_deg_node;
|
||||||
SGPropertyNode_ptr heading_deg_node;
|
SGPropertyNode_ptr heading_deg_node;
|
||||||
bool shadow;
|
SGPropertyNode_ptr loaded_node;
|
||||||
|
bool shadow = false;
|
||||||
|
|
||||||
|
bool checkLoaded() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
FGModelMgr ();
|
FGModelMgr ();
|
||||||
|
@ -86,6 +91,15 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual void remove_instance (Instance * instance);
|
virtual void remove_instance (Instance * instance);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds an instance in the model manager from a given node path in the property tree.
|
||||||
|
* A possible path could be "models/model[0]"
|
||||||
|
*
|
||||||
|
* NOTE: the manager will delete the instance as well.
|
||||||
|
*/
|
||||||
|
Instance* findInstanceByNodePath(const std::string& nodePath) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* Listener class that adds models at runtime.
|
* Listener class that adds models at runtime.
|
||||||
|
|
Loading…
Reference in a new issue