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>
32 ////////////////////////////////////////////////////////////////////////
33 // Static utility functions.
34 ////////////////////////////////////////////////////////////////////////
37 * Locate a named SSG node in a branch.
40 find_named_node (ssgEntity * node, const char * name)
42 char * node_name = node->getName();
43 if (node_name != 0 && !strcmp(name, node_name))
45 else if (node->isAKindOf(ssgTypeBranch())) {
46 int nKids = node->getNumKids();
47 for (int i = 0; i < nKids; i++) {
49 find_named_node(((ssgBranch*)node)->getKid(i), name);
58 * Splice a branch in between all child nodes and their parents.
61 splice_branch (ssgBranch * branch, ssgEntity * child)
63 int nParents = child->getNumParents();
64 branch->addKid(child);
65 for (int i = 0; i < nParents; i++) {
66 ssgBranch * parent = child->getParent(i);
67 parent->replaceKid(child, branch);
72 * Set up the transform matrix for a spin or rotation.
75 set_rotation (sgMat4 &matrix, double position_deg,
76 sgVec3 ¢er, sgVec3 &axis)
78 float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
80 float s = (float) sin ( temp_angle ) ;
81 float c = (float) cos ( temp_angle ) ;
82 float t = SG_ONE - c ;
84 // axis was normalized at load time
85 // hint to the compiler to put these into FP registers
90 matrix[0][0] = t * x * x + c ;
91 matrix[0][1] = t * y * x - s * z ;
92 matrix[0][2] = t * z * x + s * y ;
93 matrix[0][3] = SG_ZERO;
95 matrix[1][0] = t * x * y + s * z ;
96 matrix[1][1] = t * y * y + c ;
97 matrix[1][2] = t * z * y - s * x ;
98 matrix[1][3] = SG_ZERO;
100 matrix[2][0] = t * x * z - s * y ;
101 matrix[2][1] = t * y * z + s * x ;
102 matrix[2][2] = t * z * z + c ;
103 matrix[2][3] = SG_ZERO;
105 // hint to the compiler to put these into FP registers
110 matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
111 matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
112 matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
113 matrix[3][3] = SG_ONE;
117 * Set up the transform matrix for a translation.
120 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
123 sgScaleVec3(xyz, axis, position_m);
124 sgMakeTransMat4(matrix, xyz);
129 * Make an offset matrix from rotations and position offset.
132 make_offsets_matrix (sgMat4 * result, double h_rot, double p_rot, double r_rot,
133 double x_off, double y_off, double z_off)
137 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
138 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
139 sgMultMat4(*result, pos_matrix, rot_matrix);
144 * Read an interpolation table from properties.
146 static SGInterpTable *
147 read_interpolation_table (const SGPropertyNode * props)
149 const SGPropertyNode * table_node = props->getNode("interpolation");
150 if (table_node != 0) {
151 SGInterpTable * table = new SGInterpTable();
152 vector<SGPropertyNode_ptr> entries = table_node->getChildren("entry");
153 for (int i = 0; i < entries.size(); i++)
154 table->addEntry(entries[i]->getDoubleValue("ind", 0.0),
155 entries[i]->getDoubleValue("dep", 0.0));
164 ////////////////////////////////////////////////////////////////////////
165 // Implementation of FG3DModel
166 ////////////////////////////////////////////////////////////////////////
168 FG3DModel::FG3DModel ()
173 FG3DModel::~FG3DModel ()
175 // since the nodes are attached to the scene graph, they'll be
176 // deleted automatically
179 for (i = 0; i < _animations.size(); i++)
180 delete _animations[i];
184 FG3DModel::init (const string &path)
186 SGPropertyNode props;
188 // Load the 3D aircraft object itself
189 SGPath xmlpath = globals->get_fg_root();
190 SGPath modelpath = path;
191 xmlpath.append(modelpath.str());
193 // Check for an XML wrapper
194 if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
195 readProperties(xmlpath.str(), &props);
196 if (props.hasValue("/path")) {
197 modelpath = modelpath.dir();
198 modelpath.append(props.getStringValue("/path"));
200 throw sg_exception("No path for model");
204 // Assume that textures are in
205 // the same location as the XML file.
206 ssgTexturePath((char *)xmlpath.dir().c_str());
207 _model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
209 throw sg_exception("Failed to load 3D model");
211 // Set up the alignment node
212 ssgTransform * align = new ssgTransform;
213 align->addKid(_model);
215 make_offsets_matrix(&res_matrix,
216 props.getFloatValue("/offsets/heading-deg", 0.0),
217 props.getFloatValue("/offsets/roll-deg", 0.0),
218 props.getFloatValue("/offsets/pitch-deg", 0.0),
219 props.getFloatValue("/offsets/x-m", 0.0),
220 props.getFloatValue("/offsets/y-m", 0.0),
221 props.getFloatValue("/offsets/z-m", 0.0));
222 align->setTransform(res_matrix);
225 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
227 for (i = 0; i < animation_nodes.size(); i++) {
228 vector<SGPropertyNode_ptr> name_nodes =
229 animation_nodes[i]->getChildren("object-name");
230 if (name_nodes.size() < 1) {
231 Animation * animation = make_animation(0, animation_nodes[i]);
233 for (unsigned int j = 0; j < name_nodes.size(); j++) {
234 Animation * animation =
235 make_animation(name_nodes[j]->getStringValue(), animation_nodes[i]);
237 _animations.push_back(animation);
243 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
244 for (i = 0; i < model_nodes.size(); i++) {
245 SGPropertyNode_ptr node = model_nodes[i];
246 ssgTransform * align = new ssgTransform;
248 make_offsets_matrix(&res_matrix,
249 node->getFloatValue("offsets/heading-deg", 0.0),
250 node->getFloatValue("offsets/roll-deg", 0.0),
251 node->getFloatValue("offsets/pitch-deg", 0.0),
252 node->getFloatValue("offsets/x-m", 0.0),
253 node->getFloatValue("offsets/y-m", 0.0),
254 node->getFloatValue("offsets/z-m", 0.0));
255 align->setTransform(res_matrix);
256 FG3DModel * kid = new FG3DModel;
257 kid->init(node->getStringValue("path"));
258 align->addKid(kid->getSceneGraph());
259 _model->addKid(align);
260 _children.push_back(kid);
265 FG3DModel::update (double dt)
269 for (i = 0; i < _children.size(); i++)
270 _children[i]->update(dt);
271 for (i = 0; i < _animations.size(); i++)
272 _animations[i]->update(dt);
275 FG3DModel::Animation *
276 FG3DModel::make_animation (const char * object_name,
277 SGPropertyNode * node)
279 Animation * animation = 0;
280 const char * type = node->getStringValue("type");
281 if (!strcmp("none", type)) {
282 animation = new NullAnimation();
283 } else if (!strcmp("range", type)) {
284 animation = new RangeAnimation();
285 } else if (!strcmp("billboard", type)) {
286 animation = new BillboardAnimation();
287 } else if (!strcmp("select", type)) {
288 animation = new SelectAnimation();
289 } else if (!strcmp("spin", type)) {
290 animation = new SpinAnimation();
291 } else if (!strcmp("rotate", type)) {
292 animation = new RotateAnimation();
293 } else if (!strcmp("translate", type)) {
294 animation = new TranslateAnimation();
296 animation = new NullAnimation();
297 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
301 if (object_name != 0) {
302 object = find_named_node(_model, object_name);
304 SG_LOG(SG_INPUT, SG_WARN, "Object " << object_name << " not found");
312 animation->init(object, node);
318 ////////////////////////////////////////////////////////////////////////
319 // Implementation of FG3DModel::Animation
320 ////////////////////////////////////////////////////////////////////////
322 FG3DModel::Animation::Animation ()
326 FG3DModel::Animation::~Animation ()
332 ////////////////////////////////////////////////////////////////////////
333 // Implementation of FG3DModel::NullAnimation
334 ////////////////////////////////////////////////////////////////////////
336 FG3DModel::NullAnimation::NullAnimation ()
337 : _branch(new ssgBranch)
341 FG3DModel::NullAnimation::~NullAnimation ()
347 FG3DModel::NullAnimation::init (ssgEntity * object,
348 SGPropertyNode * props)
350 splice_branch(_branch, object);
351 _branch->setName(props->getStringValue("name", 0));
355 FG3DModel::NullAnimation::update (double dt)
361 ////////////////////////////////////////////////////////////////////////
362 // Implementation of FG3DModel::RangeAnimation
363 ////////////////////////////////////////////////////////////////////////
365 FG3DModel::RangeAnimation::RangeAnimation ()
366 : _branch(new ssgRangeSelector)
370 FG3DModel::RangeAnimation::~RangeAnimation ()
376 FG3DModel::RangeAnimation::init (ssgEntity * object,
377 SGPropertyNode * props)
380 splice_branch(_branch, object);
381 _branch->setName(props->getStringValue("name", 0));
382 ranges[0] = props->getFloatValue("min-m", 0);
383 ranges[1] = props->getFloatValue("max-m", 5000);
384 _branch->setRanges(ranges, 2);
388 FG3DModel::RangeAnimation::update (double dt)
394 ////////////////////////////////////////////////////////////////////////
395 // Implementation of FG3DModel::BillboardAnimation
396 ////////////////////////////////////////////////////////////////////////
398 FG3DModel::BillboardAnimation::BillboardAnimation ()
401 // Note: we cannot allocate the branch until we know whether
402 // it can rotate around the x axis as well as the z axis.
405 FG3DModel::BillboardAnimation::~BillboardAnimation ()
411 FG3DModel::BillboardAnimation::init (ssgEntity * object,
412 SGPropertyNode * props)
414 _branch = new ssgCutout(props->getBoolValue("spherical", true));
415 splice_branch(_branch, object);
416 _branch->setName(props->getStringValue("name", 0));
420 FG3DModel::BillboardAnimation::update (double dt)
426 ////////////////////////////////////////////////////////////////////////
427 // Implementation of FG3DModel::SelectAnimation
428 ////////////////////////////////////////////////////////////////////////
430 FG3DModel::SelectAnimation::SelectAnimation ()
432 _selector(new ssgSelector)
436 FG3DModel::SelectAnimation::~SelectAnimation ()
443 FG3DModel::SelectAnimation::init (ssgEntity * object,
444 SGPropertyNode * props)
446 splice_branch(_selector, object);
447 _selector->setName(props->getStringValue("name", 0));
448 SGPropertyNode * node = props->getChild("condition");
450 _condition = fgReadCondition(node);
455 FG3DModel::SelectAnimation::update (double dt)
457 if (_condition != 0 && _condition->test())
458 _selector->select(0xffff);
460 _selector->select(0x0000);
465 ////////////////////////////////////////////////////////////////////////
466 // Implementation of FG3DModel::SpinAnimation
467 ////////////////////////////////////////////////////////////////////////
469 FG3DModel::SpinAnimation::SpinAnimation ()
473 _transform(new ssgTransform)
477 FG3DModel::SpinAnimation::~SpinAnimation ()
483 FG3DModel::SpinAnimation::init (ssgEntity * object,
484 SGPropertyNode * props)
486 // Splice in the new transform node
487 splice_branch(_transform, object);
488 _transform->setName(props->getStringValue("name", 0));
489 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
490 _factor = props->getDoubleValue("factor", 1.0);
491 _position_deg = props->getDoubleValue("starting-position-deg", 0);
492 _center[0] = props->getFloatValue("center/x-m", 0);
493 _center[1] = props->getFloatValue("center/y-m", 0);
494 _center[2] = props->getFloatValue("center/z-m", 0);
495 _axis[0] = props->getFloatValue("axis/x", 0);
496 _axis[1] = props->getFloatValue("axis/y", 0);
497 _axis[2] = props->getFloatValue("axis/z", 0);
498 sgNormalizeVec3(_axis);
502 FG3DModel::SpinAnimation::update (double dt)
504 float velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
505 _position_deg += (dt * velocity_rpms * 360);
506 while (_position_deg < 0)
507 _position_deg += 360.0;
508 while (_position_deg >= 360.0)
509 _position_deg -= 360.0;
510 set_rotation(_matrix, _position_deg, _center, _axis);
511 _transform->setTransform(_matrix);
516 ////////////////////////////////////////////////////////////////////////
517 // Implementation of FG3DModel::RotateAnimation
518 ////////////////////////////////////////////////////////////////////////
520 FG3DModel::RotateAnimation::RotateAnimation ()
530 _transform(new ssgTransform)
534 FG3DModel::RotateAnimation::~RotateAnimation ()
541 FG3DModel::RotateAnimation::init (ssgEntity * object,
542 SGPropertyNode * props)
544 // Splice in the new transform node
545 splice_branch(_transform, object);
546 _transform->setName(props->getStringValue("name", 0));
547 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
548 _offset_deg = props->getDoubleValue("offset-deg", 0.0);
549 _factor = props->getDoubleValue("factor", 1.0);
550 _table = read_interpolation_table(props);
551 if (props->hasValue("min-deg")) {
553 _min_deg = props->getDoubleValue("min-deg");
555 if (props->hasValue("max-deg")) {
557 _max_deg = props->getDoubleValue("max-deg");
559 _position_deg = props->getDoubleValue("starting-position-deg", 0);
560 _center[0] = props->getFloatValue("center/x-m", 0);
561 _center[1] = props->getFloatValue("center/y-m", 0);
562 _center[2] = props->getFloatValue("center/z-m", 0);
563 _axis[0] = props->getFloatValue("axis/x", 0);
564 _axis[1] = props->getFloatValue("axis/y", 0);
565 _axis[2] = props->getFloatValue("axis/z", 0);
566 sgNormalizeVec3(_axis);
570 FG3DModel::RotateAnimation::update (double dt)
573 _position_deg = (_prop->getDoubleValue() + _offset_deg) * _factor;
574 if (_has_min && _position_deg < _min_deg)
575 _position_deg = _min_deg;
576 if (_has_max && _position_deg > _max_deg)
577 _position_deg = _max_deg;
579 _position_deg = _table->interpolate(_prop->getDoubleValue());
581 set_rotation(_matrix, _position_deg, _center, _axis);
582 _transform->setTransform(_matrix);
587 ////////////////////////////////////////////////////////////////////////
588 // Implementation of FG3DModel::TranslateAnimation
589 ////////////////////////////////////////////////////////////////////////
591 FG3DModel::TranslateAnimation::TranslateAnimation ()
601 _transform(new ssgTransform)
605 FG3DModel::TranslateAnimation::~TranslateAnimation ()
612 FG3DModel::TranslateAnimation::init (ssgEntity * object,
613 SGPropertyNode * props)
615 // Splice in the new transform node
616 splice_branch(_transform, object);
617 _transform->setName(props->getStringValue("name", 0));
618 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
619 _offset_m = props->getDoubleValue("offset-m", 0.0);
620 _factor = props->getDoubleValue("factor", 1.0);
621 _table = read_interpolation_table(props);
622 if (props->hasValue("min-m")) {
624 _min_m = props->getDoubleValue("min-m");
626 if (props->hasValue("max-m")) {
628 _max_m = props->getDoubleValue("max-m");
630 _position_m = props->getDoubleValue("starting-position-m", 0);
631 _axis[0] = props->getFloatValue("axis/x", 0);
632 _axis[1] = props->getFloatValue("axis/y", 0);
633 _axis[2] = props->getFloatValue("axis/z", 0);
634 sgNormalizeVec3(_axis);
638 FG3DModel::TranslateAnimation::update (double dt)
641 _position_m = (_prop->getDoubleValue() + _offset_m) * _factor;
642 if (_has_min && _position_m < _min_m)
643 _position_m = _min_m;
644 if (_has_max && _position_m > _max_m)
645 _position_m = _max_m;
647 _position_m = _table->interpolate(_prop->getDoubleValue());
649 set_translation(_matrix, _position_m, _axis);
650 _transform->setTransform(_matrix);
655 ////////////////////////////////////////////////////////////////////////
656 // Implementation of FGModelPlacement.
657 ////////////////////////////////////////////////////////////////////////
659 FGModelPlacement::FGModelPlacement ()
660 : _model(new FG3DModel),
667 _selector(new ssgSelector),
668 _position(new ssgTransform),
669 _location(new FGLocation)
673 FGModelPlacement::~FGModelPlacement ()
680 FGModelPlacement::init (const string &path)
683 _position->addKid(_model->getSceneGraph());
684 _selector->addKid(_position);
685 _selector->clrTraversalMaskBits(SSGTRAV_HOT);
689 FGModelPlacement::update (double dt)
693 _location->setPosition( _lon_deg, _lat_deg, _elev_ft );
694 _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
697 sgCopyMat4(POS, _location->getTransformMatrix());
700 sgCopyVec3(trans, _location->get_view_pos());
702 for(int i = 0; i < 4; i++) {
703 float tmp = POS[i][3];
704 for( int j=0; j<3; j++ ) {
705 POS[i][j] += (tmp * trans[j]);
708 _position->setTransform(POS);
712 FGModelPlacement::getVisible () const
714 return (_selector->getSelect() != 0);
718 FGModelPlacement::setVisible (bool visible)
720 _selector->select(visible);
724 FGModelPlacement::setLongitudeDeg (double lon_deg)
730 FGModelPlacement::setLatitudeDeg (double lat_deg)
736 FGModelPlacement::setElevationFt (double elev_ft)
742 FGModelPlacement::setPosition (double lon_deg, double lat_deg, double elev_ft)
750 FGModelPlacement::setRollDeg (double roll_deg)
752 _roll_deg = roll_deg;
756 FGModelPlacement::setPitchDeg (double pitch_deg)
758 _pitch_deg = pitch_deg;
762 FGModelPlacement::setHeadingDeg (double heading_deg)
764 _heading_deg = heading_deg;
768 FGModelPlacement::setOrientation (double roll_deg, double pitch_deg,
771 _roll_deg = roll_deg;
772 _pitch_deg = pitch_deg;
773 _heading_deg = heading_deg;