2 #include "NasalModelData.hxx"
3 #include "NasalSys.hxx"
4 #include <Main/globals.hxx>
6 #include <simgear/math/SGMath.hxx>
7 #include <simgear/nasal/cppbind/Ghost.hxx>
8 #include <simgear/scene/util/OsgMath.hxx>
9 #include <simgear/debug/logstream.hxx>
11 #include <osg/Transform>
13 #include <boost/bind.hpp>
16 #include <cstring> // for strlen
18 // FGNasalModelData class. If sgLoad3DModel() is called with a pointer to
19 // such a class, then it lets modelLoaded() run the <load> script, and the
20 // destructor the <unload> script. The latter happens when the model branch
21 // is removed from the scene graph.
23 unsigned int FGNasalModelData::_max_module_id = 0;
24 FGNasalModelDataList FGNasalModelData::_loaded_models;
26 typedef osg::ref_ptr<osg::Node> NodeRef;
27 typedef nasal::Ghost<NodeRef> NasalNode;
30 * Get position (lat, lon, elevation) and orientation (heading, pitch, roll) of
33 static naRef f_node_getPose( const osg::Node& node,
34 const nasal::CallContext& ctx )
36 osg::NodePathList parent_paths = node.getParentalNodePaths();
37 for( osg::NodePathList::const_iterator path = parent_paths.begin();
38 path != parent_paths.end();
41 osg::Matrix local_to_world = osg::computeLocalToWorld(*path);
42 if( !local_to_world.valid() )
45 SGGeod coord = SGGeod::fromCart( toSG(local_to_world.getTrans()) );
46 if( !coord.isValid() )
49 osg::Matrix local_frame = makeZUpFrameRelative(coord),
51 inv_local.invert_4x3(local_frame);
52 local_to_world.postMult(inv_local);
54 SGQuatd rotate = toSG(local_to_world.getRotate());
55 double hdg, pitch, roll;
56 rotate.getEulerDeg(hdg, pitch, roll);
58 nasal::Hash pose(ctx.to_nasal(coord), ctx.c);
59 pose.set("heading", hdg);
60 pose.set("pitch", pitch);
61 pose.set("roll", roll);
62 return pose.get_naRef();
68 //------------------------------------------------------------------------------
69 FGNasalModelData::FGNasalModelData( SGPropertyNode *root,
70 const std::string& path,
73 SGPropertyNode* unload,
76 _root(root), _prop(prop),
77 _load(load), _unload(unload),
79 _module_id( _max_module_id++ )
81 _loaded_models.push_back(this);
84 //------------------------------------------------------------------------------
85 FGNasalModelData::~FGNasalModelData()
87 _loaded_models.remove(this);
90 //------------------------------------------------------------------------------
91 void FGNasalModelData::load()
94 m << "__model" << _module_id;
97 SG_LOG(SG_NASAL, SG_DEBUG, "Loading nasal module " << _module.c_str());
99 const char *s = _load ? _load->getStringValue() : "";
100 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
102 // Add _module_id to script local hash to allow placing canvasses on objects
104 nasal::Hash module = nasalSys->getGlobals().createHash(_module);
105 module.set("_module_id", _module_id);
107 if( !NasalNode::isInit() )
109 NasalNode::init("osg.Node")
110 .method("getPose", &f_node_getPose);
113 module.set("_model", NodeRef(_branch));
116 arg[0] = nasalSys->propNodeGhost(_root);
117 arg[1] = nasalSys->propNodeGhost(_prop);
118 nasalSys->createModule(_module.c_str(), _path.c_str(), s, strlen(s),
122 //------------------------------------------------------------------------------
123 void FGNasalModelData::unload()
128 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
130 SG_LOG(SG_NASAL, SG_WARN, "Trying to run an <unload> script "
131 "without Nasal subsystem present.");
135 SG_LOG(SG_NASAL, SG_DEBUG, "Unloading nasal module " << _module.c_str());
139 const char *s = _unload->getStringValue();
140 nasalSys->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _root);
143 nasalSys->deleteModule(_module.c_str());
146 //------------------------------------------------------------------------------
147 osg::Node* FGNasalModelData::getNode()
152 //------------------------------------------------------------------------------
153 FGNasalModelData* FGNasalModelData::getByModuleId(unsigned int id)
155 FGNasalModelDataList::iterator it = std::find_if
157 _loaded_models.begin(),
158 _loaded_models.end(),
159 boost::bind(&FGNasalModelData::_module_id, _1) == id
162 if( it != _loaded_models.end() )
168 //------------------------------------------------------------------------------
169 FGNasalModelDataProxy::~FGNasalModelDataProxy()
171 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
172 // when necessary, register Nasal module to be destroyed/unloaded
173 // in the main thread.
174 if ((_data.valid())&&(nasalSys))
175 nasalSys->registerToUnload(_data);
178 //------------------------------------------------------------------------------
179 void FGNasalModelDataProxy::modelLoaded( const std::string& path,
180 SGPropertyNode *prop,
183 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
185 SG_LOG(SG_NASAL, SG_WARN, "Trying to run a <load> script "
186 "without Nasal subsystem present.");
193 SGPropertyNode *nasal = prop->getNode("nasal");
197 SGPropertyNode* load = nasal->getNode("load");
198 SGPropertyNode* unload = nasal->getNode("unload");
200 if ((!load) && (!unload))
203 _data = new FGNasalModelData(_root, path, prop, load, unload, branch);
205 // register Nasal module to be created and loaded in the main thread.
206 nasalSys->registerToLoad(_data);