1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
10 #include <string.h> // for strcmp()
15 #include <simgear/compiler.h>
16 #include <simgear/debug/logstream.hxx>
17 #include <simgear/math/interpolater.hxx>
18 #include <simgear/math/point3d.hxx>
19 #include <simgear/math/sg_geodesy.hxx>
20 #include <simgear/misc/exception.hxx>
21 #include <simgear/misc/sg_path.hxx>
23 #include <Main/fg_props.hxx>
24 #include <Main/globals.hxx>
25 #include <Main/location.hxx>
26 #include <Scenery/scenery.hxx>
29 #include "panelnode.hxx"
33 ////////////////////////////////////////////////////////////////////////
34 // Static utility functions.
35 ////////////////////////////////////////////////////////////////////////
38 * Callback to update an animation.
41 animation_callback (ssgEntity * entity, int mask)
43 ((Animation *)entity->getUserData())->update();
49 * Locate a named SSG node in a branch.
52 find_named_node (ssgEntity * node, const char * name)
54 char * node_name = node->getName();
55 if (node_name != 0 && !strcmp(name, node_name))
57 else if (node->isAKindOf(ssgTypeBranch())) {
58 int nKids = node->getNumKids();
59 for (int i = 0; i < nKids; i++) {
61 find_named_node(((ssgBranch*)node)->getKid(i), name);
70 * Splice a branch in between all child nodes and their parents.
73 splice_branch (ssgBranch * branch, ssgEntity * child)
75 int nParents = child->getNumParents();
76 branch->addKid(child);
77 for (int i = 0; i < nParents; i++) {
78 ssgBranch * parent = child->getParent(i);
79 parent->replaceKid(child, branch);
84 * Set up the transform matrix for a spin or rotation.
87 set_rotation (sgMat4 &matrix, double position_deg,
88 sgVec3 ¢er, sgVec3 &axis)
90 float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
92 float s = (float) sin ( temp_angle ) ;
93 float c = (float) cos ( temp_angle ) ;
94 float t = SG_ONE - c ;
96 // axis was normalized at load time
97 // hint to the compiler to put these into FP registers
102 matrix[0][0] = t * x * x + c ;
103 matrix[0][1] = t * y * x - s * z ;
104 matrix[0][2] = t * z * x + s * y ;
105 matrix[0][3] = SG_ZERO;
107 matrix[1][0] = t * x * y + s * z ;
108 matrix[1][1] = t * y * y + c ;
109 matrix[1][2] = t * z * y - s * x ;
110 matrix[1][3] = SG_ZERO;
112 matrix[2][0] = t * x * z - s * y ;
113 matrix[2][1] = t * y * z + s * x ;
114 matrix[2][2] = t * z * z + c ;
115 matrix[2][3] = SG_ZERO;
117 // hint to the compiler to put these into FP registers
122 matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
123 matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
124 matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
125 matrix[3][3] = SG_ONE;
129 * Set up the transform matrix for a translation.
132 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
135 sgScaleVec3(xyz, axis, position_m);
136 sgMakeTransMat4(matrix, xyz);
141 * Make an offset matrix from rotations and position offset.
144 make_offsets_matrix (sgMat4 * result, double h_rot, double p_rot, double r_rot,
145 double x_off, double y_off, double z_off)
149 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
150 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
151 sgMultMat4(*result, pos_matrix, rot_matrix);
156 * Read an interpolation table from properties.
158 static SGInterpTable *
159 read_interpolation_table (SGPropertyNode_ptr props)
161 SGPropertyNode_ptr table_node = props->getNode("interpolation");
162 if (table_node != 0) {
163 SGInterpTable * table = new SGInterpTable();
164 vector<SGPropertyNode_ptr> entries = table_node->getChildren("entry");
165 for (unsigned int i = 0; i < entries.size(); i++)
166 table->addEntry(entries[i]->getDoubleValue("ind", 0.0),
167 entries[i]->getDoubleValue("dep", 0.0));
176 make_animation (ssgBranch * model,
178 vector<SGPropertyNode_ptr> &name_nodes,
179 SGPropertyNode_ptr node)
181 Animation * animation = 0;
182 const char * type = node->getStringValue("type");
183 if (!strcmp("none", type)) {
184 animation = new NullAnimation(node);
185 } else if (!strcmp("range", type)) {
186 animation = new RangeAnimation(node);
187 } else if (!strcmp("billboard", type)) {
188 animation = new BillboardAnimation(node);
189 } else if (!strcmp("select", type)) {
190 animation = new SelectAnimation(node);
191 } else if (!strcmp("spin", type)) {
192 animation = new SpinAnimation(node);
193 } else if (!strcmp("rotate", type)) {
194 animation = new RotateAnimation(node);
195 } else if (!strcmp("translate", type)) {
196 animation = new TranslateAnimation(node);
198 animation = new NullAnimation(node);
199 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
203 animation->setName((char *)name);
206 if (name_nodes.size() > 0) {
207 object = find_named_node(model, name_nodes[0]->getStringValue());
209 SG_LOG(SG_INPUT, SG_WARN, "Object " << name_nodes[0]->getStringValue()
218 ssgBranch * branch = animation->getBranch();
219 splice_branch(branch, object);
221 for (int i = 1; i < name_nodes.size(); i++) {
222 const char * name = name_nodes[i]->getStringValue();
223 object = find_named_node(model, name);
225 SG_LOG(SG_INPUT, SG_WARN, "Object " << name << " not found");
229 ssgBranch * oldParent = object->getParent(0);
230 branch->addKid(object);
231 oldParent->removeKid(object);
234 branch->setUserData(animation);
235 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
240 ////////////////////////////////////////////////////////////////////////
242 ////////////////////////////////////////////////////////////////////////
245 fgLoad3DModel (const string &path)
247 ssgBranch * model = 0;
248 SGPropertyNode props;
250 // Load the 3D aircraft object itself
252 SGPath modelpath = path;
253 if ( path[ 0 ] == '/' || path[ 0 ] == '\\' || ( isalpha( path[ 0 ] ) && path[ 1 ] == ':' ) ) {
257 xmlpath = globals->get_fg_root();
258 xmlpath.append(modelpath.str());
261 // Check for an XML wrapper
262 if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
263 readProperties(xmlpath.str(), &props);
264 if (props.hasValue("/path")) {
265 modelpath = modelpath.dir();
266 modelpath.append(props.getStringValue("/path"));
269 model = new ssgBranch;
273 // Assume that textures are in
274 // the same location as the XML file.
276 ssgTexturePath((char *)xmlpath.dir().c_str());
277 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
279 throw sg_exception("Failed to load 3D model");
282 // Set up the alignment node
283 ssgTransform * align = new ssgTransform;
284 align->addKid(model);
286 make_offsets_matrix(&res_matrix,
287 props.getFloatValue("/offsets/heading-deg", 0.0),
288 props.getFloatValue("/offsets/roll-deg", 0.0),
289 props.getFloatValue("/offsets/pitch-deg", 0.0),
290 props.getFloatValue("/offsets/x-m", 0.0),
291 props.getFloatValue("/offsets/y-m", 0.0),
292 props.getFloatValue("/offsets/z-m", 0.0));
293 align->setTransform(res_matrix);
296 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
298 for (i = 0; i < animation_nodes.size(); i++) {
299 const char * name = animation_nodes[i]->getStringValue("name", 0);
300 vector<SGPropertyNode_ptr> name_nodes =
301 animation_nodes[i]->getChildren("object-name");
302 make_animation(model, name, name_nodes, animation_nodes[i]);
306 vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
307 for (i = 0; i < panel_nodes.size(); i++) {
308 printf("Reading a panel in model.cxx\n");
309 FGPanelNode * panel = new FGPanelNode(panel_nodes[i]);
310 model->addKid(panel);
314 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
315 for (i = 0; i < model_nodes.size(); i++) {
316 SGPropertyNode_ptr node = model_nodes[i];
317 ssgTransform * align = new ssgTransform;
319 make_offsets_matrix(&res_matrix,
320 node->getFloatValue("offsets/heading-deg", 0.0),
321 node->getFloatValue("offsets/roll-deg", 0.0),
322 node->getFloatValue("offsets/pitch-deg", 0.0),
323 node->getFloatValue("offsets/x-m", 0.0),
324 node->getFloatValue("offsets/y-m", 0.0),
325 node->getFloatValue("offsets/z-m", 0.0));
326 align->setTransform(res_matrix);
328 ssgBranch * kid = fgLoad3DModel(node->getStringValue("path"));
330 model->addKid(align);
338 ////////////////////////////////////////////////////////////////////////
339 // Implementation of Animation
340 ////////////////////////////////////////////////////////////////////////
342 Animation::Animation (SGPropertyNode_ptr props, ssgBranch * branch)
345 _branch->setName(props->getStringValue("name", 0));
348 Animation::~Animation ()
354 ////////////////////////////////////////////////////////////////////////
355 // Implementation of NullAnimation
356 ////////////////////////////////////////////////////////////////////////
358 NullAnimation::NullAnimation (SGPropertyNode_ptr props)
359 : Animation(props, new ssgBranch)
363 NullAnimation::~NullAnimation ()
368 NullAnimation::update ()
374 ////////////////////////////////////////////////////////////////////////
375 // Implementation of RangeAnimation
376 ////////////////////////////////////////////////////////////////////////
378 RangeAnimation::RangeAnimation (SGPropertyNode_ptr props)
379 : Animation(props, new ssgRangeSelector)
381 float ranges[] = { props->getFloatValue("min-m", 0),
382 props->getFloatValue("max-m", 5000) };
383 ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
387 RangeAnimation::~RangeAnimation ()
392 RangeAnimation::update ()
398 ////////////////////////////////////////////////////////////////////////
399 // Implementation of BillboardAnimation
400 ////////////////////////////////////////////////////////////////////////
402 BillboardAnimation::BillboardAnimation (SGPropertyNode_ptr props)
403 : Animation(props, new ssgCutout(props->getBoolValue("spherical", true)))
407 BillboardAnimation::~BillboardAnimation ()
412 BillboardAnimation::update ()
418 ////////////////////////////////////////////////////////////////////////
419 // Implementation of SelectAnimation
420 ////////////////////////////////////////////////////////////////////////
422 SelectAnimation::SelectAnimation (SGPropertyNode_ptr props)
423 : Animation(props, new ssgSelector),
426 SGPropertyNode_ptr node = props->getChild("condition");
428 _condition = fgReadCondition(node);
431 SelectAnimation::~SelectAnimation ()
437 SelectAnimation::update ()
439 if (_condition != 0 && _condition->test())
440 ((ssgSelector *)_branch)->select(0xffff);
442 ((ssgSelector *)_branch)->select(0x0000);
447 ////////////////////////////////////////////////////////////////////////
448 // Implementation of SpinAnimation
449 ////////////////////////////////////////////////////////////////////////
451 SpinAnimation::SpinAnimation (SGPropertyNode_ptr props)
452 : Animation(props, new ssgTransform),
453 _prop(fgGetNode(props->getStringValue("property", "/null"), true)),
454 _factor(props->getDoubleValue("factor", 1.0)),
455 _position_deg(props->getDoubleValue("starting-position-deg", 0)),
456 _last_time_sec(globals->get_sim_time_sec())
458 _center[0] = props->getFloatValue("center/x-m", 0);
459 _center[1] = props->getFloatValue("center/y-m", 0);
460 _center[2] = props->getFloatValue("center/z-m", 0);
461 _axis[0] = props->getFloatValue("axis/x", 0);
462 _axis[1] = props->getFloatValue("axis/y", 0);
463 _axis[2] = props->getFloatValue("axis/z", 0);
464 sgNormalizeVec3(_axis);
467 SpinAnimation::~SpinAnimation ()
472 SpinAnimation::update ()
474 double sim_time = globals->get_sim_time_sec();
475 double dt = sim_time - _last_time_sec;
476 _last_time_sec = sim_time;
478 float velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
479 _position_deg += (dt * velocity_rpms * 360);
480 while (_position_deg < 0)
481 _position_deg += 360.0;
482 while (_position_deg >= 360.0)
483 _position_deg -= 360.0;
484 set_rotation(_matrix, _position_deg, _center, _axis);
485 ((ssgTransform *)_branch)->setTransform(_matrix);
490 ////////////////////////////////////////////////////////////////////////
491 // Implementation of RotateAnimation
492 ////////////////////////////////////////////////////////////////////////
494 RotateAnimation::RotateAnimation (SGPropertyNode_ptr props)
495 : Animation(props, new ssgTransform),
496 _prop(fgGetNode(props->getStringValue("property", "/null"), true)),
497 _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
498 _factor(props->getDoubleValue("factor", 1.0)),
499 _table(read_interpolation_table(props)),
500 _has_min(props->hasValue("min-deg")),
501 _min_deg(props->getDoubleValue("min-deg")),
502 _has_max(props->hasValue("max-deg")),
503 _max_deg(props->getDoubleValue("max-deg")),
504 _position_deg(props->getDoubleValue("starting-position-deg", 0))
506 _center[0] = props->getFloatValue("center/x-m", 0);
507 _center[1] = props->getFloatValue("center/y-m", 0);
508 _center[2] = props->getFloatValue("center/z-m", 0);
509 _axis[0] = props->getFloatValue("axis/x", 0);
510 _axis[1] = props->getFloatValue("axis/y", 0);
511 _axis[2] = props->getFloatValue("axis/z", 0);
512 sgNormalizeVec3(_axis);
515 RotateAnimation::~RotateAnimation ()
521 RotateAnimation::update ()
524 _position_deg = (_prop->getDoubleValue() + _offset_deg) * _factor;
525 if (_has_min && _position_deg < _min_deg)
526 _position_deg = _min_deg;
527 if (_has_max && _position_deg > _max_deg)
528 _position_deg = _max_deg;
530 _position_deg = _table->interpolate(_prop->getDoubleValue());
532 set_rotation(_matrix, _position_deg, _center, _axis);
533 ((ssgTransform *)_branch)->setTransform(_matrix);
538 ////////////////////////////////////////////////////////////////////////
539 // Implementation of TranslateAnimation
540 ////////////////////////////////////////////////////////////////////////
542 TranslateAnimation::TranslateAnimation (SGPropertyNode_ptr props)
543 : Animation(props, new ssgTransform),
544 _prop(fgGetNode(props->getStringValue("property", "/null"), true)),
545 _offset_m(props->getDoubleValue("offset-m", 0.0)),
546 _factor(props->getDoubleValue("factor", 1.0)),
547 _table(read_interpolation_table(props)),
548 _has_min(props->hasValue("min-m")),
549 _min_m(props->getDoubleValue("min-m")),
550 _has_max(props->hasValue("max-m")),
551 _max_m(props->getDoubleValue("max-m")),
552 _position_m(props->getDoubleValue("starting-position-m", 0))
554 _axis[0] = props->getFloatValue("axis/x", 0);
555 _axis[1] = props->getFloatValue("axis/y", 0);
556 _axis[2] = props->getFloatValue("axis/z", 0);
557 sgNormalizeVec3(_axis);
560 TranslateAnimation::~TranslateAnimation ()
566 TranslateAnimation::update ()
569 _position_m = (_prop->getDoubleValue() + _offset_m) * _factor;
570 if (_has_min && _position_m < _min_m)
571 _position_m = _min_m;
572 if (_has_max && _position_m > _max_m)
573 _position_m = _max_m;
575 _position_m = _table->interpolate(_prop->getDoubleValue());
577 set_translation(_matrix, _position_m, _axis);
578 ((ssgTransform *)_branch)->setTransform(_matrix);
583 ////////////////////////////////////////////////////////////////////////
584 // Implementation of FGModelPlacement.
585 ////////////////////////////////////////////////////////////////////////
587 FGModelPlacement::FGModelPlacement ()
594 _selector(new ssgSelector),
595 _position(new ssgTransform),
596 _location(new FGLocation)
600 FGModelPlacement::~FGModelPlacement ()
605 FGModelPlacement::init (const string &path)
607 ssgBranch * model = fgLoad3DModel(path);
609 _position->addKid(model);
610 _selector->addKid(_position);
611 _selector->clrTraversalMaskBits(SSGTRAV_HOT);
615 FGModelPlacement::update ()
617 _location->setPosition( _lon_deg, _lat_deg, _elev_ft );
618 _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
621 sgCopyMat4(POS, _location->getTransformMatrix());
624 sgCopyVec3(trans, _location->get_view_pos());
626 for(int i = 0; i < 4; i++) {
627 float tmp = POS[i][3];
628 for( int j=0; j<3; j++ ) {
629 POS[i][j] += (tmp * trans[j]);
632 _position->setTransform(POS);
636 FGModelPlacement::getVisible () const
638 return (_selector->getSelect() != 0);
642 FGModelPlacement::setVisible (bool visible)
644 _selector->select(visible);
648 FGModelPlacement::setLongitudeDeg (double lon_deg)
654 FGModelPlacement::setLatitudeDeg (double lat_deg)
660 FGModelPlacement::setElevationFt (double elev_ft)
666 FGModelPlacement::setPosition (double lon_deg, double lat_deg, double elev_ft)
674 FGModelPlacement::setRollDeg (double roll_deg)
676 _roll_deg = roll_deg;
680 FGModelPlacement::setPitchDeg (double pitch_deg)
682 _pitch_deg = pitch_deg;
686 FGModelPlacement::setHeadingDeg (double heading_deg)
688 _heading_deg = heading_deg;
692 FGModelPlacement::setOrientation (double roll_deg, double pitch_deg,
695 _roll_deg = roll_deg;
696 _pitch_deg = pitch_deg;
697 _heading_deg = heading_deg;