| 1 | // acmodel.cxx - manage a 3D aircraft model. |
| 2 | // Written by David Megginson, started 2002. |
| 3 | // |
| 4 | // This file is in the Public Domain, and comes with no warranty. |
| 5 | |
| 6 | #ifdef HAVE_CONFIG_H |
| 7 | # include <config.h> |
| 8 | #endif |
| 9 | |
| 10 | #include <cstring> // for strcmp() |
| 11 | |
| 12 | #include <simgear/compiler.h> |
| 13 | #include <simgear/debug/ErrorReportingCallback.hxx> |
| 14 | #include <simgear/debug/logstream.hxx> |
| 15 | |
| 16 | #include <simgear/structure/exception.hxx> |
| 17 | #include <simgear/misc/sg_path.hxx> |
| 18 | #include <simgear/scene/model/animation.hxx> |
| 19 | #include <simgear/scene/model/placement.hxx> |
| 20 | #include <simgear/scene/util/SGNodeMasks.hxx> |
| 21 | #include <simgear/scene/model/modellib.hxx> |
| 22 | |
| 23 | #include <Main/globals.hxx> |
| 24 | #include <Main/fg_props.hxx> |
| 25 | #include <Viewer/renderer.hxx> |
| 26 | #include <Viewer/viewmgr.hxx> |
| 27 | #include <Viewer/view.hxx> |
| 28 | #include <Scenery/scenery.hxx> |
| 29 | #include <Sound/fg_fx.hxx> |
| 30 | #include <GUI/Highlight.hxx> |
| 31 | |
| 32 | #include "acmodel.hxx" |
| 33 | |
| 34 | |
| 35 | static simgear::SGModelLib::NodeRef |
| 36 | fgLoad3DModelPanel(const SGPath& path, SGPropertyNode* prop_root) |
| 37 | { |
| 38 | const bool loadPanels = true; |
| 39 | const bool autoTooltipsMaster = fgGetBool("/sim/rendering/automatic-animation-tooltips/enabled"); |
| 40 | const int autoTooltipsMasterMax = fgGetInt("/sim/rendering/automatic-animation-tooltips/max-count"); |
| 41 | SG_LOG(SG_INPUT, SG_DEBUG, "" |
| 42 | << " autoTooltipsMaster=" << autoTooltipsMaster |
| 43 | << " autoTooltipsMasterMax=" << autoTooltipsMasterMax |
| 44 | ); |
| 45 | auto node = simgear::SGModelLib::loadModel(path.utf8Str(), prop_root, NULL, loadPanels, autoTooltipsMaster, autoTooltipsMasterMax); |
| 46 | if (node) |
| 47 | node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT); |
| 48 | return node; |
| 49 | } |
| 50 | |
| 51 | //////////////////////////////////////////////////////////////////////// |
| 52 | // Implementation of FGAircraftModel |
| 53 | //////////////////////////////////////////////////////////////////////// |
| 54 | |
| 55 | FGAircraftModel::FGAircraftModel() |
| 56 | : _velocity(SGVec3d::zeros()) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | FGAircraftModel::~FGAircraftModel () |
| 61 | { |
| 62 | shutdown(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | // Gathers information about all animated nodes and the properties that they |
| 67 | // depend on, and registers with Highlight::add_property_node(). |
| 68 | // |
| 69 | struct VisitorHighlight : osg::NodeVisitor |
| 70 | { |
| 71 | VisitorHighlight() |
| 72 | { |
| 73 | m_highlight = globals->get_subsystem<Highlight>(); |
| 74 | setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN); |
| 75 | } |
| 76 | std::string spaces() |
| 77 | { |
| 78 | return std::string(m_level*4, ' '); |
| 79 | } |
| 80 | virtual void apply(osg::Node& node) |
| 81 | { |
| 82 | SG_LOG(SG_GENERAL, SG_DEBUG, spaces() << "node: " << node.libraryName() << "::" << node.className()); |
| 83 | m_level += 1; |
| 84 | traverse(node); |
| 85 | m_level -= 1; |
| 86 | } |
| 87 | virtual void apply(osg::Group& group) |
| 88 | { |
| 89 | // Parent nodes of <group> are animated by properties in |
| 90 | // <m_highlight_names>, so register the association between <group> and |
| 91 | // these properties. |
| 92 | SG_LOG(SG_GENERAL, SG_DEBUG, spaces() << "group: " << group.libraryName() << "::" << group.className()); |
| 93 | for (auto name: m_highlight_names) |
| 94 | { |
| 95 | if (m_highlight) { |
| 96 | m_highlight->addPropertyNode(name, &group); |
| 97 | } |
| 98 | } |
| 99 | m_level += 1; |
| 100 | traverse(group); |
| 101 | m_level -= 1; |
| 102 | } |
| 103 | virtual void apply( osg::Transform& node ) |
| 104 | { |
| 105 | SG_LOG(SG_GENERAL, SG_DEBUG, spaces() << "transform: " |
| 106 | << node.libraryName() << "::" << node.className() |
| 107 | << ": " << typeid(node).name() |
| 108 | ); |
| 109 | |
| 110 | SGSharedPtr<SGExpressiond const> expression = TransformExpression(&node); |
| 111 | int num_added = 0; |
| 112 | if (expression) { |
| 113 | // All <nodes>'s child nodes will be affected by <expression> so |
| 114 | // add all of <expression>'s properties to <m_highlight_names> so |
| 115 | // that we can call Highlight::add_property_node() for each child |
| 116 | // node. |
| 117 | std::set<const SGPropertyNode*> properties; |
| 118 | expression->collectDependentProperties(properties); |
| 119 | SG_LOG(SG_GENERAL, SG_DEBUG, spaces() << typeid(node).name() << ":"); |
| 120 | for (auto p: properties) { |
| 121 | SG_LOG(SG_GENERAL, SG_DEBUG, spaces() << " " << p->getPath(true)); |
| 122 | num_added += 1; |
| 123 | m_highlight_names.push_back(p->getPath(true /*simplify*/)); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | m_level += 1; |
| 128 | traverse(node); |
| 129 | m_level -= 1; |
| 130 | |
| 131 | // Remove the names we appended to <m_highlight_names> earlier. |
| 132 | assert((int) m_highlight_names.size() >= num_added); |
| 133 | m_highlight_names.resize(m_highlight_names.size() - num_added); |
| 134 | } |
| 135 | unsigned int m_level = 0; // Only used to indent diagnostics. |
| 136 | std::vector<std::string> m_highlight_names; |
| 137 | Highlight* m_highlight = nullptr; |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | void |
| 142 | FGAircraftModel::init () |
| 143 | { |
| 144 | if (_aircraft.get()) { |
| 145 | SG_LOG(SG_AIRCRAFT, SG_ALERT, "FGAircraftModel::init: already inited"); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | simgear::ErrorReportContext ec("primary-aircraft", "yes"); |
| 150 | _fx = new FGFX("fx"); |
| 151 | _fx->init(); |
| 152 | |
| 153 | SGPropertyNode_ptr sim = fgGetNode("/sim", true); |
| 154 | for (auto model : sim->getChildren("model")) { |
| 155 | std::string path = model->getStringValue("path", "Models/Geometry/glider.ac"); |
| 156 | std::string usage = model->getStringValue("usage", "external"); |
| 157 | |
| 158 | simgear::ErrorReportContext ec("aircraft-model", path); |
| 159 | |
| 160 | SGPath resolvedPath = globals->resolve_aircraft_path(path); |
| 161 | if (resolvedPath.isNull()) { |
| 162 | simgear::reportFailure(simgear::LoadFailure::NotFound, |
| 163 | simgear::ErrorCode::XMLModelLoad, |
| 164 | "Failed to find aircraft model", SGPath::fromUtf8(path)); |
| 165 | SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to find aircraft model: " << path); |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | osg::ref_ptr<osg::Node> node; |
| 170 | try { |
| 171 | node = fgLoad3DModelPanel( resolvedPath, globals->get_props()); |
| 172 | } catch (const sg_exception &ex) { |
| 173 | simgear::reportFailure(simgear::LoadFailure::BadData, |
| 174 | simgear::ErrorCode::XMLModelLoad, |
| 175 | "Failed to load aircraft model:" + ex.getFormattedMessage(), |
| 176 | ex.getLocation()); |
| 177 | SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to load aircraft from " << path << ':'); |
| 178 | SG_LOG(SG_AIRCRAFT, SG_ALERT, " " << ex.getFormattedMessage()); |
| 179 | } |
| 180 | |
| 181 | if (usage == "interior") { |
| 182 | // interior model |
| 183 | if (!_interior.get()) { |
| 184 | _interior.reset(new SGModelPlacement); |
| 185 | _interior->init(node); |
| 186 | } else { |
| 187 | _interior->add(node); |
| 188 | } |
| 189 | } else { |
| 190 | // normal / exterior model |
| 191 | if (!_aircraft.get()) { |
| 192 | _aircraft.reset(new SGModelPlacement); |
| 193 | _aircraft->init(node); |
| 194 | } else { |
| 195 | _aircraft->add(node); |
| 196 | } |
| 197 | } // of model usage switch |
| 198 | } // of models iteration |
| 199 | |
| 200 | // no models loaded, load the glider instead |
| 201 | if (!_aircraft.get()) { |
| 202 | SG_LOG(SG_AIRCRAFT, SG_ALERT, "(Falling back to glider.ac.)"); |
| 203 | auto model = fgLoad3DModelPanel(SGPath::fromUtf8("Models/Geometry/glider.ac"), |
| 204 | globals->get_props()); |
| 205 | _aircraft.reset(new SGModelPlacement); |
| 206 | _aircraft->init(model); |
| 207 | |
| 208 | } |
| 209 | |
| 210 | osg::Node* node = _aircraft->getSceneGraph(); |
| 211 | globals->get_scenery()->get_aircraft_branch()->addChild(node); |
| 212 | |
| 213 | if (_interior.get()) { |
| 214 | node = _interior->getSceneGraph(); |
| 215 | globals->get_scenery()->get_interior_branch()->addChild(node); |
| 216 | } |
| 217 | |
| 218 | // Register animated nodes and associated properties with Highlight. |
| 219 | VisitorHighlight visitor_highlight; |
| 220 | visitor_highlight.traverse(*node); |
| 221 | } |
| 222 | |
| 223 | void |
| 224 | FGAircraftModel::reinit() |
| 225 | { |
| 226 | shutdown(); |
| 227 | init(); |
| 228 | // TODO globally create signals for all subsystems (re)initialized |
| 229 | fgSetBool("/sim/signals/model-reinit", true); |
| 230 | } |
| 231 | |
| 232 | void |
| 233 | FGAircraftModel::shutdown() |
| 234 | { |
| 235 | FGScenery* scenery = globals->get_scenery(); |
| 236 | |
| 237 | if (_aircraft.get()) { |
| 238 | if (scenery && scenery->get_aircraft_branch()) { |
| 239 | scenery->get_aircraft_branch()->removeChild(_aircraft->getSceneGraph()); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (_interior.get()) { |
| 244 | if (scenery && scenery->get_interior_branch()) { |
| 245 | scenery->get_interior_branch()->removeChild(_interior->getSceneGraph()); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | _aircraft.reset(); |
| 250 | _interior.reset(); |
| 251 | |
| 252 | if (_fx) { |
| 253 | // becuase the sound-manager keeps a ref to our FX itself, we need to manually call |
| 254 | // shutdown() to unregister from the Sound-manager, otherwise the ref will persist, |
| 255 | // and prevent us re-registering a new FGFX on reset/reinit |
| 256 | _fx->shutdown(); |
| 257 | _fx.reset(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void |
| 262 | FGAircraftModel::bind () |
| 263 | { |
| 264 | _speed_n = fgGetNode("velocities/speed-north-fps", true); |
| 265 | _speed_e = fgGetNode("velocities/speed-east-fps", true); |
| 266 | _speed_d = fgGetNode("velocities/speed-down-fps", true); |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | FGAircraftModel::unbind () |
| 271 | { |
| 272 | _speed_n.reset(); |
| 273 | _speed_e.reset(); |
| 274 | _speed_d.reset(); |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | FGAircraftModel::update (double dt) |
| 279 | { |
| 280 | int view_number = globals->get_viewmgr()->getCurrentViewIndex(); |
| 281 | int is_internal = fgGetBool("/sim/current-view/internal"); |
| 282 | |
| 283 | if (view_number == 0 && !is_internal) { |
| 284 | _aircraft->setVisible(false); |
| 285 | } else { |
| 286 | _aircraft->setVisible(true); |
| 287 | } |
| 288 | |
| 289 | double heading, pitch, roll; |
| 290 | globals->get_aircraft_orientation(heading, pitch, roll); |
| 291 | SGQuatd orient = SGQuatd::fromYawPitchRollDeg(heading, pitch, roll); |
| 292 | |
| 293 | SGGeod pos = globals->get_aircraft_position(); |
| 294 | |
| 295 | _aircraft->setPosition(pos); |
| 296 | _aircraft->setOrientation(orient); |
| 297 | _aircraft->update(); |
| 298 | |
| 299 | if (_interior.get()) { |
| 300 | _interior->setPosition(pos); |
| 301 | _interior->setOrientation(orient); |
| 302 | _interior->update(); |
| 303 | } |
| 304 | |
| 305 | // update model's audio sample values |
| 306 | _fx->set_position_geod( pos ); |
| 307 | _fx->set_orientation( orient ); |
| 308 | |
| 309 | _velocity = SGVec3d( _speed_n->getDoubleValue(), |
| 310 | _speed_e->getDoubleValue(), |
| 311 | _speed_d->getDoubleValue() ); |
| 312 | _fx->set_velocity( _velocity ); |
| 313 | |
| 314 | float temp_c = fgGetFloat("/environment/temperature-degc"); |
| 315 | float humidity = fgGetFloat("/environment/relative-humidity"); |
| 316 | float pressure = fgGetFloat("/environment/pressure-inhg")*SG_INHG_TO_PA/1000.0f; |
| 317 | _fx->set_atmosphere( temp_c, humidity, pressure ); |
| 318 | |
| 319 | // _fx->update() is run via SGSoundMgr, don't need to call it here |
| 320 | } |
| 321 | |
| 322 | |
| 323 | // Register the subsystem. |
| 324 | SGSubsystemMgr::Registrant<FGAircraftModel> registrantFGAircraftModel( |
| 325 | SGSubsystemMgr::DISPLAY); |
| 326 | |
| 327 | // end of model.cxx |