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/OsgDebug.hxx>
9 #include <simgear/scene/util/OsgMath.hxx>
10 #include <simgear/debug/logstream.hxx>
12 #include <osg/Transform>
14 #include <boost/bind.hpp>
17 #include <cstring> // for strlen
19 // FGNasalModelData class. If sgLoad3DModel() is called with a pointer to
20 // such a class, then it lets modelLoaded() run the <load> script, and the
21 // destructor the <unload> script. The latter happens when the model branch
22 // is removed from the scene graph.
24 unsigned int FGNasalModelData::_max_module_id = 0;
25 FGNasalModelDataList FGNasalModelData::_loaded_models;
27 typedef osg::ref_ptr<osg::Node> NodeRef;
28 typedef nasal::Ghost<NodeRef> NasalNode;
31 * Get position (lat, lon, elevation) and orientation (heading, pitch, roll) of
34 static naRef f_node_getPose( const osg::Node& node,
35 const nasal::CallContext& ctx )
37 osg::NodePathList parent_paths = node.getParentalNodePaths();
38 for( osg::NodePathList::const_iterator path = parent_paths.begin();
39 path != parent_paths.end();
42 osg::Matrix local_to_world = osg::computeLocalToWorld(*path);
43 if( !local_to_world.valid() )
46 SGGeod coord = SGGeod::fromCart( toSG(local_to_world.getTrans()) );
47 if( !coord.isValid() )
50 osg::Matrix local_frame = makeZUpFrameRelative(coord),
52 inv_local.invert_4x3(local_frame);
53 local_to_world.postMult(inv_local);
55 SGQuatd rotate = toSG(local_to_world.getRotate());
56 double hdg, pitch, roll;
57 rotate.getEulerDeg(hdg, pitch, roll);
59 nasal::Hash pose(ctx.to_nasal(coord), ctx.c);
60 pose.set("heading", hdg);
61 pose.set("pitch", pitch);
62 pose.set("roll", roll);
63 return pose.get_naRef();
69 //------------------------------------------------------------------------------
70 FGNasalModelData::FGNasalModelData( SGPropertyNode *root,
71 const std::string& path,
74 SGPropertyNode* unload,
77 _root(root), _prop(prop),
78 _load(load), _unload(unload),
80 _module_id( _max_module_id++ )
82 _loaded_models.push_back(this);
88 "New model with attached script(s) "
89 "(branch = " << branch << ","
90 " path = " << simgear::getNodePathString(branch) << ")"
94 //------------------------------------------------------------------------------
95 FGNasalModelData::~FGNasalModelData()
97 _loaded_models.remove(this);
103 "Removed model with script(s) (branch = " << _branch.get() << ")"
107 //------------------------------------------------------------------------------
108 void FGNasalModelData::load()
111 m << "__model" << _module_id;
114 SG_LOG(SG_NASAL, SG_DEBUG, "Loading nasal module " << _module.c_str());
116 const char *s = _load ? _load->getStringValue() : "";
117 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
119 // Add _module_id to script local hash to allow placing canvasses on objects
121 nasal::Hash module = nasalSys->getGlobals().createHash(_module);
122 module.set("_module_id", _module_id);
124 if( !NasalNode::isInit() )
126 NasalNode::init("osg.Node")
127 .method("getPose", &f_node_getPose);
130 module.set("_model", _branch);
133 arg[0] = nasalSys->propNodeGhost(_root);
134 arg[1] = nasalSys->propNodeGhost(_prop);
135 nasalSys->createModule(_module.c_str(), _path.c_str(), s, strlen(s),
139 //------------------------------------------------------------------------------
140 void FGNasalModelData::unload()
145 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
147 SG_LOG(SG_NASAL, SG_WARN, "Trying to run an <unload> script "
148 "without Nasal subsystem present.");
152 SG_LOG(SG_NASAL, SG_DEBUG, "Unloading nasal module " << _module.c_str());
156 const char *s = _unload->getStringValue();
157 nasalSys->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _root);
160 nasalSys->deleteModule(_module.c_str());
163 //------------------------------------------------------------------------------
164 osg::Node* FGNasalModelData::getNode()
166 return _branch.get();
169 //------------------------------------------------------------------------------
170 FGNasalModelData* FGNasalModelData::getByModuleId(unsigned int id)
172 FGNasalModelDataList::iterator it = std::find_if
174 _loaded_models.begin(),
175 _loaded_models.end(),
176 boost::bind(&FGNasalModelData::_module_id, _1) == id
179 if( it != _loaded_models.end() )
185 //------------------------------------------------------------------------------
186 FGNasalModelDataProxy::~FGNasalModelDataProxy()
188 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
189 // when necessary, register Nasal module to be destroyed/unloaded
190 // in the main thread.
191 if ((_data.valid())&&(nasalSys))
192 nasalSys->registerToUnload(_data);
195 //------------------------------------------------------------------------------
196 void FGNasalModelDataProxy::modelLoaded( const std::string& path,
197 SGPropertyNode *prop,
203 SGPropertyNode *nasal = prop->getNode("nasal");
207 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
214 "Can not load model script(s) (Nasal subsystem not available)."
219 SGPropertyNode* load = nasal->getNode("load");
220 SGPropertyNode* unload = nasal->getNode("unload");
222 if ((!load) && (!unload))
225 _data = new FGNasalModelData(_root, path, prop, load, unload, branch);
227 // register Nasal module to be created and loaded in the main thread.
228 nasalSys->registerToLoad(_data);