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/point3d.hxx>
18 #include <simgear/math/sg_geodesy.hxx>
19 #include <simgear/misc/exception.hxx>
20 #include <simgear/misc/sg_path.hxx>
22 #include <Main/globals.hxx>
23 #include <Scenery/scenery.hxx>
29 ////////////////////////////////////////////////////////////////////////
30 // Static utility functions.
31 ////////////////////////////////////////////////////////////////////////
34 * Locate a named SSG node in a branch.
37 find_named_node (ssgEntity * node, const char * name)
39 char * node_name = node->getName();
40 if (node_name != 0 && !strcmp(name, node_name))
42 else if (node->isAKindOf(ssgTypeBranch())) {
43 int nKids = node->getNumKids();
44 for (int i = 0; i < nKids; i++) {
46 find_named_node(((ssgBranch*)node)->getKid(i), name);
55 * Splice a branch in between all child nodes and their parents.
58 splice_branch (ssgBranch * branch, ssgEntity * child)
60 int nParents = child->getNumParents();
61 branch->addKid(child);
62 for (int i = 0; i < nParents; i++) {
63 ssgBranch * parent = child->getParent(i);
64 parent->replaceKid(child, branch);
69 * Set up the transform matrix for a spin or rotation.
72 set_rotation (sgMat4 &matrix, double position_deg,
73 sgVec3 ¢er, sgVec3 &axis)
75 float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
77 float s = (float) sin ( temp_angle ) ;
78 float c = (float) cos ( temp_angle ) ;
79 float t = SG_ONE - c ;
81 // axis was normalized at load time
82 // hint to the compiler to put these into FP registers
87 matrix[0][0] = t * x * x + c ;
88 matrix[0][1] = t * y * x - s * z ;
89 matrix[0][2] = t * z * x + s * y ;
90 matrix[0][3] = SG_ZERO;
92 matrix[1][0] = t * x * y + s * z ;
93 matrix[1][1] = t * y * y + c ;
94 matrix[1][2] = t * z * y - s * x ;
95 matrix[1][3] = SG_ZERO;
97 matrix[2][0] = t * x * z - s * y ;
98 matrix[2][1] = t * y * z + s * x ;
99 matrix[2][2] = t * z * z + c ;
100 matrix[2][3] = SG_ZERO;
102 // hint to the compiler to put these into FP registers
107 matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
108 matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
109 matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
110 matrix[3][3] = SG_ONE;
114 * Set up the transform matrix for a translation.
117 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
120 sgScaleVec3(xyz, axis, position_m);
121 sgMakeTransMat4(matrix, xyz);
125 // TODO: once this is working, look at Norm's optimized version
127 world_coordinate( sgCoord *obj_pos,
128 double lat_deg, double lon_deg, double elev_ft,
129 double roll_deg, double pitch_deg, double hdg_deg)
132 double sea_level_radius_m;
135 // Convert from geodetic to geocentric
137 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
138 elev_ft * SG_FEET_TO_METER,
142 Point3D center = scenery.get_center();
144 Point3D geod = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
147 geod.setz(geod.radius() + elev_ft * SG_FEET_TO_METER);
149 Point3D world_pos = sgPolarToCart3d( geod );
150 Point3D offset = world_pos - center;
153 sgMakeTransMat4( POS, offset.x(), offset.y(), offset.z() );
156 sgVec3 obj_bk, obj_rt, obj_up;
157 sgSetVec3( obj_bk, 1.0, 0.0, 0.0); // X axis
158 sgSetVec3( obj_rt, 0.0, 1.0, 0.0); // Y axis
159 sgSetVec3( obj_up, 0.0, 0.0, 1.0); // Z axis
161 sgMat4 ROT_lon, ROT_lat, ROT_hdg, ROT_pitch, ROT_roll;
162 sgMakeRotMat4( ROT_lon, lon_deg, obj_up );
163 sgMakeRotMat4( ROT_lat, 90 - lat_deg, obj_rt );
164 sgMakeRotMat4( ROT_hdg, -hdg_deg, obj_up );
165 sgMakeRotMat4( ROT_pitch, pitch_deg, obj_rt );
166 sgMakeRotMat4( ROT_roll, -roll_deg, obj_bk );
169 sgCopyMat4( TRANS, ROT_roll );
170 sgPostMultMat4( TRANS, ROT_pitch );
171 sgPostMultMat4( TRANS, ROT_hdg );
172 sgPostMultMat4( TRANS, ROT_lat );
173 sgPostMultMat4( TRANS, ROT_lon );
174 sgPostMultMat4( TRANS, POS );
176 sgSetCoord( obj_pos, TRANS );
181 ////////////////////////////////////////////////////////////////////////
182 // Implementation of FG3DModel
183 ////////////////////////////////////////////////////////////////////////
185 FG3DModel::FG3DModel ()
187 _selector(new ssgSelector),
188 _position(new ssgTransform)
192 FG3DModel::~FG3DModel ()
194 // since the nodes are attached to the scene graph, they'll be
195 // deleted automatically
197 for (int i = 0; i < _animations.size(); i++) {
198 Animation * tmp = _animations[i];
206 FG3DModel::init (const string &path)
208 SGPropertyNode props;
210 // Load the 3D aircraft object itself
211 SGPath xmlpath = globals->get_fg_root();
212 SGPath modelpath = path;
213 xmlpath.append(modelpath.str());
215 // Check for an XML wrapper
216 if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
217 readProperties(xmlpath.str(), &props);
218 if (props.hasValue("/path")) {
219 modelpath = modelpath.dir();
220 modelpath.append(props.getStringValue("/path"));
222 throw sg_exception("No path for model");
226 // Assume that textures are in
227 // the same location as the XML file.
228 ssgTexturePath((char *)xmlpath.dir().c_str());
229 _model = ssgLoad((char *)modelpath.c_str());
231 throw sg_exception("Failed to load 3D model");
234 vector<SGPropertyNode *> animation_nodes = props.getChildren("animation");
235 for (unsigned int i = 0; i < animation_nodes.size(); i++) {
236 vector<SGPropertyNode *> name_nodes =
237 animation_nodes[i]->getChildren("object-name");
238 if (name_nodes.size() < 1) {
239 SG_LOG(SG_INPUT, SG_ALERT, "No object-name given for transformation");
241 for (unsigned int j = 0; j < name_nodes.size(); j++) {
242 Animation * animation =
243 make_animation(name_nodes[j]->getStringValue(), animation_nodes[i]);
245 _animations.push_back(animation);
250 // Set up the alignment node
251 ssgTransform * align = new ssgTransform;
252 align->addKid(_model);
256 float h_rot = props.getFloatValue("/offsets/heading-deg", 0.0);
257 float p_rot = props.getFloatValue("/offsets/roll-deg", 0.0);
258 float r_rot = props.getFloatValue("/offsets/pitch-deg", 0.0);
259 float x_off = props.getFloatValue("/offsets/x-m", 0.0);
260 float y_off = props.getFloatValue("/offsets/y-m", 0.0);
261 float z_off = props.getFloatValue("/offsets/z-m", 0.0);
262 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
263 sgMakeTransMat4(off_matrix, x_off, y_off, z_off);
264 sgMultMat4(res_matrix, off_matrix, rot_matrix);
265 align->setTransform(res_matrix);
267 // Set up the position node
268 _position->addKid(align);
270 // Set up the selector node
271 _selector->addKid(_position);
272 _selector->clrTraversalMaskBits(SSGTRAV_HOT);
276 FG3DModel::update (int dt)
278 for (unsigned int i = 0; i < _animations.size(); i++)
279 _animations[i]->update(dt);
281 _selector->select(true);
284 world_coordinate(&obj_pos, _lat_deg, _lon_deg, _elev_ft,
285 _roll_deg, _pitch_deg, _heading_deg);
286 _position->setTransform(&obj_pos);
290 FG3DModel::getVisible () const
292 return _selector->getSelect();
296 FG3DModel::setVisible (bool visible)
298 _selector->select(visible);
302 FG3DModel::setPosition (double lon_deg, double lat_deg, double elev_ft)
310 FG3DModel::setOrientation (double roll_deg, double pitch_deg,
313 _roll_deg = roll_deg;
314 _pitch_deg = pitch_deg;
315 _heading_deg = heading_deg;
318 FG3DModel::Animation *
319 FG3DModel::make_animation (const char * object_name,
320 SGPropertyNode * node)
322 Animation * animation = 0;
323 const char * type = node->getStringValue("type");
324 if (!strcmp("none", type)) {
325 animation = new NullAnimation();
326 } else if (!strcmp("select", type)) {
327 animation = new SelectAnimation();
328 } else if (!strcmp("spin", type)) {
329 animation = new SpinAnimation();
330 } else if (!strcmp("rotate", type)) {
331 animation = new RotateAnimation();
332 } else if (!strcmp("translate", type)) {
333 animation = new TranslateAnimation();
335 animation = new NullAnimation();
336 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
339 ssgEntity * object = find_named_node(_model, object_name);
341 SG_LOG(SG_INPUT, SG_WARN, "Object " << object_name << " not found");
345 animation->init(object, node);
353 ////////////////////////////////////////////////////////////////////////
354 // Implementation of FG3DModel::Animation
355 ////////////////////////////////////////////////////////////////////////
357 FG3DModel::Animation::Animation ()
361 FG3DModel::Animation::~Animation ()
367 ////////////////////////////////////////////////////////////////////////
368 // Implementation of FG3DModel::NullAnimation
369 ////////////////////////////////////////////////////////////////////////
371 FG3DModel::NullAnimation::NullAnimation ()
372 : _branch(new ssgBranch)
376 FG3DModel::NullAnimation::~NullAnimation ()
382 FG3DModel::NullAnimation::init (ssgEntity * object,
383 SGPropertyNode * props)
385 splice_branch(_branch, object);
386 _branch->setName(props->getStringValue("name", 0));
390 FG3DModel::NullAnimation::update (int dt)
396 ////////////////////////////////////////////////////////////////////////
397 // Implementation of FG3DModel::SelectAnimation
398 ////////////////////////////////////////////////////////////////////////
400 FG3DModel::SelectAnimation::SelectAnimation ()
402 _selector(new ssgSelector)
406 FG3DModel::SelectAnimation::~SelectAnimation ()
413 FG3DModel::SelectAnimation::init (ssgEntity * object,
414 SGPropertyNode * props)
416 splice_branch(_selector, object);
417 _selector->setName(props->getStringValue("name", 0));
418 SGPropertyNode * node = props->getChild("condition");
420 _condition = fgReadCondition(node);
425 FG3DModel::SelectAnimation::update (int dt)
427 if (_condition != 0 && _condition->test())
428 _selector->select(0xffff);
430 _selector->select(0x0000);
435 ////////////////////////////////////////////////////////////////////////
436 // Implementation of FG3DModel::SpinAnimation
437 ////////////////////////////////////////////////////////////////////////
439 FG3DModel::SpinAnimation::SpinAnimation ()
443 _transform(new ssgTransform)
447 FG3DModel::SpinAnimation::~SpinAnimation ()
453 FG3DModel::SpinAnimation::init (ssgEntity * object,
454 SGPropertyNode * props)
456 // Splice in the new transform node
457 splice_branch(_transform, object);
458 _transform->setName(props->getStringValue("name", 0));
459 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
460 _factor = props->getDoubleValue("factor", 1.0);
461 _position_deg = props->getDoubleValue("starting-position-deg", 0);
462 _center[0] = props->getFloatValue("center/x-m", 0);
463 _center[1] = props->getFloatValue("center/y-m", 0);
464 _center[2] = props->getFloatValue("center/z-m", 0);
465 _axis[0] = props->getFloatValue("axis/x", 0);
466 _axis[1] = props->getFloatValue("axis/y", 0);
467 _axis[2] = props->getFloatValue("axis/z", 0);
468 sgNormalizeVec3(_axis);
472 FG3DModel::SpinAnimation::update (int dt)
474 float velocity_rpms = (_prop->getDoubleValue() * _factor / 60000.0);
475 _position_deg += (dt * velocity_rpms * 360);
476 while (_position_deg < 0)
477 _position_deg += 360.0;
478 while (_position_deg >= 360.0)
479 _position_deg -= 360.0;
480 set_rotation(_matrix, _position_deg, _center, _axis);
481 _transform->setTransform(_matrix);
486 ////////////////////////////////////////////////////////////////////////
487 // Implementation of FG3DModel::RotateAnimation
488 ////////////////////////////////////////////////////////////////////////
490 FG3DModel::RotateAnimation::RotateAnimation ()
499 _transform(new ssgTransform)
503 FG3DModel::RotateAnimation::~RotateAnimation ()
509 FG3DModel::RotateAnimation::init (ssgEntity * object,
510 SGPropertyNode * props)
512 // Splice in the new transform node
513 splice_branch(_transform, object);
514 _transform->setName(props->getStringValue("name", 0));
515 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
516 _offset_deg = props->getDoubleValue("offset-deg", 0.0);
517 _factor = props->getDoubleValue("factor", 1.0);
518 if (props->hasValue("min-deg")) {
520 _min_deg = props->getDoubleValue("min-deg");
522 if (props->hasValue("max-deg")) {
524 _max_deg = props->getDoubleValue("max-deg");
526 _position_deg = props->getDoubleValue("starting-position-deg", 0);
527 _center[0] = props->getFloatValue("center/x-m", 0);
528 _center[1] = props->getFloatValue("center/y-m", 0);
529 _center[2] = props->getFloatValue("center/z-m", 0);
530 _axis[0] = props->getFloatValue("axis/x", 0);
531 _axis[1] = props->getFloatValue("axis/y", 0);
532 _axis[2] = props->getFloatValue("axis/z", 0);
533 sgNormalizeVec3(_axis);
537 FG3DModel::RotateAnimation::update (int dt)
539 _position_deg = ((_prop->getDoubleValue() + _offset_deg) * _factor);
540 if (_has_min && _position_deg < _min_deg)
541 _position_deg = _min_deg;
542 if (_has_max && _position_deg > _max_deg)
543 _position_deg = _max_deg;
544 set_rotation(_matrix, _position_deg, _center, _axis);
545 _transform->setTransform(_matrix);
550 ////////////////////////////////////////////////////////////////////////
551 // Implementation of FG3DModel::TranslateAnimation
552 ////////////////////////////////////////////////////////////////////////
554 FG3DModel::TranslateAnimation::TranslateAnimation ()
563 _transform(new ssgTransform)
567 FG3DModel::TranslateAnimation::~TranslateAnimation ()
573 FG3DModel::TranslateAnimation::init (ssgEntity * object,
574 SGPropertyNode * props)
576 // Splice in the new transform node
577 splice_branch(_transform, object);
578 _transform->setName(props->getStringValue("name", 0));
579 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
580 _offset_m = props->getDoubleValue("offset-m", 0.0);
581 _factor = props->getDoubleValue("factor", 1.0);
582 if (props->hasValue("min-m")) {
584 _min_m = props->getDoubleValue("min-m");
586 if (props->hasValue("max-m")) {
588 _max_m = props->getDoubleValue("max-m");
590 _position_m = props->getDoubleValue("starting-position-m", 0);
591 _axis[0] = props->getFloatValue("axis/x", 0);
592 _axis[1] = props->getFloatValue("axis/y", 0);
593 _axis[2] = props->getFloatValue("axis/z", 0);
594 sgNormalizeVec3(_axis);
598 FG3DModel::TranslateAnimation::update (int dt)
600 _position_m = ((_prop->getDoubleValue() + _offset_m) * _factor);
601 if (_has_min && _position_m < _min_m)
602 _position_m = _min_m;
603 if (_has_max && _position_m > _max_m)
604 _position_m = _max_m;
605 set_translation(_matrix, _position_m, _axis);
606 _transform->setTransform(_matrix);