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)
131 Point3D center = scenery.get_center();
134 Point3D geod( lon_deg * SGD_DEGREES_TO_RADIANS,
135 lat_deg * SGD_DEGREES_TO_RADIANS,
136 elev_ft * SG_FEET_TO_METER);
138 Point3D world_pos = sgGeodToCart( geod );
139 Point3D offset = world_pos - center;
142 sgMakeTransMat4( POS, offset.x(), offset.y(), offset.z() );
144 sgVec3 obj_bk, obj_rt, obj_up;
145 sgSetVec3( obj_bk, 1.0, 0.0, 0.0); // X axis
146 sgSetVec3( obj_rt, 0.0, 1.0, 0.0); // Y axis
147 sgSetVec3( obj_up, 0.0, 0.0, 1.0); // Z axis
149 sgMat4 ROT_lon, ROT_lat, ROT_hdg, ROT_pitch, ROT_roll;
150 sgMakeRotMat4( ROT_lon, lon_deg, obj_up );
151 sgMakeRotMat4( ROT_lat, 90 - lat_deg, obj_rt );
152 sgMakeRotMat4( ROT_hdg, -hdg_deg, obj_up );
153 sgMakeRotMat4( ROT_pitch, pitch_deg, obj_rt );
154 sgMakeRotMat4( ROT_roll, -roll_deg, obj_bk );
157 sgCopyMat4( TRANS, ROT_roll );
158 sgPostMultMat4( TRANS, ROT_pitch );
159 sgPostMultMat4( TRANS, ROT_hdg );
160 sgPostMultMat4( TRANS, ROT_lat );
161 sgPostMultMat4( TRANS, ROT_lon );
162 sgPostMultMat4( TRANS, POS );
164 sgSetCoord( obj_pos, TRANS );
169 ////////////////////////////////////////////////////////////////////////
170 // Implementation of FG3DModel
171 ////////////////////////////////////////////////////////////////////////
173 FG3DModel::FG3DModel ()
175 _selector(new ssgSelector),
176 _position(new ssgTransform)
180 FG3DModel::~FG3DModel ()
182 // since the nodes are attached to the scene graph, they'll be
183 // deleted automatically
185 for (int i = 0; i < _animations.size(); i++) {
186 Animation * tmp = _animations[i];
194 FG3DModel::init (const string &path)
196 SGPropertyNode props;
198 // Load the 3D aircraft object itself
199 SGPath xmlpath = globals->get_fg_root();
200 SGPath modelpath = path;
201 xmlpath.append(modelpath.str());
203 // Check for an XML wrapper
204 if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
205 readProperties(xmlpath.str(), &props);
206 if (props.hasValue("/path")) {
207 modelpath = modelpath.dir();
208 modelpath.append(props.getStringValue("/path"));
210 throw sg_exception("No path for model");
214 // Assume that textures are in
215 // the same location as the XML file.
216 ssgTexturePath((char *)xmlpath.dir().c_str());
217 _model = ssgLoad((char *)modelpath.c_str());
219 throw sg_exception("Failed to load 3D model");
222 vector<SGPropertyNode *> animation_nodes = props.getChildren("animation");
223 for (unsigned int i = 0; i < animation_nodes.size(); i++) {
224 vector<SGPropertyNode *> name_nodes =
225 animation_nodes[i]->getChildren("object-name");
226 if (name_nodes.size() < 1) {
227 SG_LOG(SG_INPUT, SG_ALERT, "No object-name given for transformation");
229 for (unsigned int j = 0; j < name_nodes.size(); j++) {
230 Animation * animation =
231 make_animation(name_nodes[j]->getStringValue(), animation_nodes[i]);
233 _animations.push_back(animation);
238 // Set up the alignment node
239 ssgTransform * align = new ssgTransform;
240 align->addKid(_model);
244 float h_rot = props.getFloatValue("/offsets/heading-deg", 0.0);
245 float p_rot = props.getFloatValue("/offsets/roll-deg", 0.0);
246 float r_rot = props.getFloatValue("/offsets/pitch-deg", 0.0);
247 float x_off = props.getFloatValue("/offsets/x-m", 0.0);
248 float y_off = props.getFloatValue("/offsets/y-m", 0.0);
249 float z_off = props.getFloatValue("/offsets/z-m", 0.0);
250 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
251 sgMakeTransMat4(off_matrix, x_off, y_off, z_off);
252 sgMultMat4(res_matrix, off_matrix, rot_matrix);
253 align->setTransform(res_matrix);
255 // Set up the position node
256 _position->addKid(align);
258 // Set up the selector node
259 _selector->addKid(_position);
260 _selector->clrTraversalMaskBits(SSGTRAV_HOT);
264 FG3DModel::update (int dt)
266 for (unsigned int i = 0; i < _animations.size(); i++)
267 _animations[i]->update(dt);
269 _selector->select(true);
272 world_coordinate(&obj_pos, _lat_deg, _lon_deg, _elev_ft,
273 _roll_deg, _pitch_deg, _heading_deg);
274 _position->setTransform(&obj_pos);
278 FG3DModel::getVisible () const
280 return _selector->getSelect();
284 FG3DModel::setVisible (bool visible)
286 _selector->select(visible);
290 FG3DModel::setPosition (double lon_deg, double lat_deg, double elev_ft)
298 FG3DModel::setOrientation (double roll_deg, double pitch_deg,
301 _roll_deg = roll_deg;
302 _pitch_deg = pitch_deg;
303 _heading_deg = heading_deg;
306 FG3DModel::Animation *
307 FG3DModel::make_animation (const char * object_name,
308 SGPropertyNode * node)
310 Animation * animation = 0;
311 const char * type = node->getStringValue("type");
312 if (!strcmp("none", type)) {
313 animation = new NullAnimation();
314 } else if (!strcmp("select", type)) {
315 animation = new SelectAnimation();
316 } else if (!strcmp("spin", type)) {
317 animation = new SpinAnimation();
318 } else if (!strcmp("rotate", type)) {
319 animation = new RotateAnimation();
320 } else if (!strcmp("translate", type)) {
321 animation = new TranslateAnimation();
323 animation = new NullAnimation();
324 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
327 ssgEntity * object = find_named_node(_model, object_name);
329 SG_LOG(SG_INPUT, SG_WARN, "Object " << object_name << " not found");
333 animation->init(object, node);
341 ////////////////////////////////////////////////////////////////////////
342 // Implementation of FG3DModel::Animation
343 ////////////////////////////////////////////////////////////////////////
345 FG3DModel::Animation::Animation ()
349 FG3DModel::Animation::~Animation ()
355 ////////////////////////////////////////////////////////////////////////
356 // Implementation of FG3DModel::NullAnimation
357 ////////////////////////////////////////////////////////////////////////
359 FG3DModel::NullAnimation::NullAnimation ()
360 : _branch(new ssgBranch)
364 FG3DModel::NullAnimation::~NullAnimation ()
370 FG3DModel::NullAnimation::init (ssgEntity * object,
371 SGPropertyNode * props)
373 splice_branch(_branch, object);
374 _branch->setName(props->getStringValue("name", 0));
378 FG3DModel::NullAnimation::update (int dt)
384 ////////////////////////////////////////////////////////////////////////
385 // Implementation of FG3DModel::SelectAnimation
386 ////////////////////////////////////////////////////////////////////////
388 FG3DModel::SelectAnimation::SelectAnimation ()
390 _selector(new ssgSelector)
394 FG3DModel::SelectAnimation::~SelectAnimation ()
401 FG3DModel::SelectAnimation::init (ssgEntity * object,
402 SGPropertyNode * props)
404 splice_branch(_selector, object);
405 _selector->setName(props->getStringValue("name", 0));
406 SGPropertyNode * node = props->getChild("condition");
408 _condition = fgReadCondition(node);
413 FG3DModel::SelectAnimation::update (int dt)
415 if (_condition != 0 && _condition->test())
416 _selector->select(0xffff);
418 _selector->select(0x0000);
423 ////////////////////////////////////////////////////////////////////////
424 // Implementation of FG3DModel::SpinAnimation
425 ////////////////////////////////////////////////////////////////////////
427 FG3DModel::SpinAnimation::SpinAnimation ()
431 _transform(new ssgTransform)
435 FG3DModel::SpinAnimation::~SpinAnimation ()
441 FG3DModel::SpinAnimation::init (ssgEntity * object,
442 SGPropertyNode * props)
444 // Splice in the new transform node
445 splice_branch(_transform, object);
446 _transform->setName(props->getStringValue("name", 0));
447 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
448 _factor = props->getDoubleValue("factor", 1.0);
449 _position_deg = props->getDoubleValue("starting-position-deg", 0);
450 _center[0] = props->getFloatValue("center/x-m", 0);
451 _center[1] = props->getFloatValue("center/y-m", 0);
452 _center[2] = props->getFloatValue("center/z-m", 0);
453 _axis[0] = props->getFloatValue("axis/x", 0);
454 _axis[1] = props->getFloatValue("axis/y", 0);
455 _axis[2] = props->getFloatValue("axis/z", 0);
456 sgNormalizeVec3(_axis);
460 FG3DModel::SpinAnimation::update (int dt)
462 float velocity_rpms = (_prop->getDoubleValue() * _factor / 60000.0);
463 _position_deg += (dt * velocity_rpms * 360);
464 while (_position_deg < 0)
465 _position_deg += 360.0;
466 while (_position_deg >= 360.0)
467 _position_deg -= 360.0;
468 set_rotation(_matrix, _position_deg, _center, _axis);
469 _transform->setTransform(_matrix);
474 ////////////////////////////////////////////////////////////////////////
475 // Implementation of FG3DModel::RotateAnimation
476 ////////////////////////////////////////////////////////////////////////
478 FG3DModel::RotateAnimation::RotateAnimation ()
487 _transform(new ssgTransform)
491 FG3DModel::RotateAnimation::~RotateAnimation ()
497 FG3DModel::RotateAnimation::init (ssgEntity * object,
498 SGPropertyNode * props)
500 // Splice in the new transform node
501 splice_branch(_transform, object);
502 _transform->setName(props->getStringValue("name", 0));
503 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
504 _offset_deg = props->getDoubleValue("offset-deg", 0.0);
505 _factor = props->getDoubleValue("factor", 1.0);
506 if (props->hasValue("min-deg")) {
508 _min_deg = props->getDoubleValue("min-deg");
510 if (props->hasValue("max-deg")) {
512 _max_deg = props->getDoubleValue("max-deg");
514 _position_deg = props->getDoubleValue("starting-position-deg", 0);
515 _center[0] = props->getFloatValue("center/x-m", 0);
516 _center[1] = props->getFloatValue("center/y-m", 0);
517 _center[2] = props->getFloatValue("center/z-m", 0);
518 _axis[0] = props->getFloatValue("axis/x", 0);
519 _axis[1] = props->getFloatValue("axis/y", 0);
520 _axis[2] = props->getFloatValue("axis/z", 0);
521 sgNormalizeVec3(_axis);
525 FG3DModel::RotateAnimation::update (int dt)
527 _position_deg = ((_prop->getDoubleValue() + _offset_deg) * _factor);
528 if (_has_min && _position_deg < _min_deg)
529 _position_deg = _min_deg;
530 if (_has_max && _position_deg > _max_deg)
531 _position_deg = _max_deg;
532 set_rotation(_matrix, _position_deg, _center, _axis);
533 _transform->setTransform(_matrix);
538 ////////////////////////////////////////////////////////////////////////
539 // Implementation of FG3DModel::TranslateAnimation
540 ////////////////////////////////////////////////////////////////////////
542 FG3DModel::TranslateAnimation::TranslateAnimation ()
551 _transform(new ssgTransform)
555 FG3DModel::TranslateAnimation::~TranslateAnimation ()
561 FG3DModel::TranslateAnimation::init (ssgEntity * object,
562 SGPropertyNode * props)
564 // Splice in the new transform node
565 splice_branch(_transform, object);
566 _transform->setName(props->getStringValue("name", 0));
567 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
568 _offset_m = props->getDoubleValue("offset-m", 0.0);
569 _factor = props->getDoubleValue("factor", 1.0);
570 if (props->hasValue("min-m")) {
572 _min_m = props->getDoubleValue("min-m");
574 if (props->hasValue("max-m")) {
576 _max_m = props->getDoubleValue("max-m");
578 _position_m = props->getDoubleValue("starting-position-m", 0);
579 _axis[0] = props->getFloatValue("axis/x", 0);
580 _axis[1] = props->getFloatValue("axis/y", 0);
581 _axis[2] = props->getFloatValue("axis/z", 0);
582 sgNormalizeVec3(_axis);
586 FG3DModel::TranslateAnimation::update (int dt)
588 _position_m = ((_prop->getDoubleValue() + _offset_m) * _factor);
589 if (_has_min && _position_m < _min_m)
590 _position_m = _min_m;
591 if (_has_max && _position_m > _max_m)
592 _position_m = _max_m;
593 set_translation(_matrix, _position_m, _axis);
594 _transform->setTransform(_matrix);