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/misc/exception.hxx>
18 #include <simgear/misc/sg_path.hxx>
20 #include "globals.hxx"
21 #include "fg_props.hxx"
22 #include "viewmgr.hxx"
25 extern ssgRoot * cockpit; // FIXME: from main.cxx
27 FGAircraftModel current_model; // FIXME: add to globals
31 ////////////////////////////////////////////////////////////////////////
32 // Static utility functions.
33 ////////////////////////////////////////////////////////////////////////
36 * Locate a named SSG node in a branch.
39 find_named_node (ssgEntity * node, const char * name)
41 char * node_name = node->getName();
42 if (node_name != 0 && !strcmp(name, node_name))
44 else if (node->isAKindOf(ssgTypeBranch())) {
45 int nKids = node->getNumKids();
46 for (int i = 0; i < nKids; i++) {
48 find_named_node(((ssgBranch*)node)->getKid(i), name);
57 * Splice a branch in between all child nodes and their parents.
60 splice_branch (ssgBranch * branch, ssgEntity * child)
62 int nParents = child->getNumParents();
63 branch->addKid(child);
64 for (int i = 0; i < nParents; i++) {
65 ssgBranch * parent = child->getParent(i);
66 parent->replaceKid(child, branch);
71 * Set up the transform matrix for a spin or rotation.
74 set_rotation (sgMat4 &matrix, double position_deg,
75 sgVec3 ¢er, sgVec3 &axis)
77 float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
79 float s = (float) sin ( temp_angle ) ;
80 float c = (float) cos ( temp_angle ) ;
81 float t = SG_ONE - c ;
83 // axis was normalized at load time
84 // hint to the compiler to put these into FP registers
89 matrix[0][0] = t * x * x + c ;
90 matrix[0][1] = t * y * x - s * z ;
91 matrix[0][2] = t * z * x + s * y ;
92 matrix[0][3] = SG_ZERO;
94 matrix[1][0] = t * x * y + s * z ;
95 matrix[1][1] = t * y * y + c ;
96 matrix[1][2] = t * z * y - s * x ;
97 matrix[1][3] = SG_ZERO;
99 matrix[2][0] = t * x * z - s * y ;
100 matrix[2][1] = t * y * z + s * x ;
101 matrix[2][2] = t * z * z + c ;
102 matrix[2][3] = SG_ZERO;
104 // hint to the compiler to put these into FP registers
109 matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
110 matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
111 matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
112 matrix[3][3] = SG_ONE;
116 * Set up the transform matrix for a translation.
119 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
122 sgScaleVec3(xyz, axis, position_m);
123 sgMakeTransMat4(matrix, xyz);
128 ////////////////////////////////////////////////////////////////////////
129 // Implementation of FGAircraftModel
130 ////////////////////////////////////////////////////////////////////////
132 FGAircraftModel::FGAircraftModel ()
134 _selector(new ssgSelector),
135 _position(new ssgTransform)
139 FGAircraftModel::~FGAircraftModel ()
141 // since the nodes are attached to the scene graph, they'll be
142 // deleted automatically
144 for (int i = 0; i < _animations.size(); i++) {
145 Animation * tmp = _animations[i];
153 FGAircraftModel::init ()
155 // TODO: optionally load an XML file with a pointer to the 3D object
156 // and placement and animation info
158 SGPropertyNode props;
160 SG_LOG(SG_INPUT, SG_INFO, "Initializing aircraft 3D model");
162 // Load the 3D aircraft object itself
163 // DCL - the xml parser requires the full path but the ssgLoader doesn't
164 // so lets have two paths.
165 SGPath xmlpath = globals->get_fg_root();
166 SGPath modelpath = (string)fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
167 xmlpath.append(modelpath.str());
169 if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
170 readProperties(xmlpath.str(), &props);
171 if (props.hasValue("/path")) {
172 modelpath = modelpath.dir();
173 modelpath.append(props.getStringValue("/path"));
175 modelpath = "Models/Geometry/glider.ac";
179 ssgTexturePath((char *)xmlpath.dir().c_str());
180 _model = ssgLoad((char *)modelpath.c_str());
182 _model = ssgLoad((char *)"Models/Geometry/glider.ac");
184 throw sg_exception("Failed to load an aircraft model");
188 vector<SGPropertyNode *> animation_nodes = props.getChildren("animation");
189 for (unsigned int i = 0; i < animation_nodes.size(); i++) {
190 vector<SGPropertyNode *> name_nodes =
191 animation_nodes[i]->getChildren("object-name");
192 if (name_nodes.size() < 1) {
193 SG_LOG(SG_INPUT, SG_ALERT, "No object-name given for transformation");
195 for (unsigned int j = 0; j < name_nodes.size(); j++) {
196 Animation * animation =
197 make_animation(name_nodes[j]->getStringValue(), animation_nodes[i]);
199 _animations.push_back(animation);
204 // Set up the alignment node
205 ssgTransform * align = new ssgTransform;
206 align->addKid(_model);
210 float h_rot = props.getFloatValue("/offsets/heading-deg", 0.0);
211 float p_rot = props.getFloatValue("/offsets/roll-deg", 0.0);
212 float r_rot = props.getFloatValue("/offsets/pitch-deg", 0.0);
213 float x_off = props.getFloatValue("/offsets/x-m", 0.0);
214 float y_off = props.getFloatValue("/offsets/y-m", 0.0);
215 float z_off = props.getFloatValue("/offsets/z-m", 0.0);
216 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
217 sgMakeTransMat4(off_matrix, x_off, y_off, z_off);
218 sgMultMat4(res_matrix, off_matrix, rot_matrix);
219 align->setTransform(res_matrix);
221 // Set up the position node
222 _position->addKid(align);
224 // Set up the selector node
225 _selector->addKid(_position);
226 _selector->clrTraversalMaskBits(SSGTRAV_HOT);
227 cockpit->addKid(_selector);
231 FGAircraftModel::bind ()
236 FGAircraftModel::unbind ()
241 FGAircraftModel::update (int dt)
243 sgMat4 MODEL_ROT, LOCAL;
246 int view_number = globals->get_viewmgr()->get_current();
248 if (view_number == 0 && !fgGetBool("/sim/view/internal")) {
249 _selector->select(false);
251 for (unsigned int i = 0; i < _animations.size(); i++)
252 _animations[i]->update(dt);
254 _selector->select(true);
255 FGViewer *current_view =
256 (FGViewer *)globals->get_viewmgr()->get_view( view_number );
258 // FIXME: this class needs to be unlinked from the viewer
259 // get transform for current position in the world...
260 sgMakeTransMat4( sgTRANS, current_view->getRelativeViewPos() );
262 // get a copy of the LOCAL rotation from the current view...
263 sgCopyMat4( LOCAL, current_view->get_LOCAL_ROT() );
265 // Make the MODEL Rotation (just reordering the LOCAL matrix
266 // and flipping the model over on its feet)...
267 MODEL_ROT[0][0] = -LOCAL[2][0];
268 MODEL_ROT[0][1] = -LOCAL[2][1];
269 MODEL_ROT[0][2] = -LOCAL[2][2];
270 MODEL_ROT[0][3] = SG_ZERO;
271 MODEL_ROT[1][0] = LOCAL[1][0];
272 MODEL_ROT[1][1] = LOCAL[1][1];
273 MODEL_ROT[1][2] = LOCAL[1][2];
274 MODEL_ROT[1][3] = SG_ZERO;
275 MODEL_ROT[2][0] = LOCAL[0][0];
276 MODEL_ROT[2][1] = LOCAL[0][1];
277 MODEL_ROT[2][2] = LOCAL[0][2];
278 MODEL_ROT[2][3] = SG_ZERO;
280 // add the position data to the matrix
281 MODEL_ROT[3][0] = SG_ZERO;
282 MODEL_ROT[3][1] = SG_ZERO;
283 MODEL_ROT[3][2] = SG_ZERO;
284 MODEL_ROT[3][3] = SG_ONE;
286 sgPostMultMat4( MODEL_ROT, sgTRANS );
289 sgSetCoord( &tuxpos, MODEL_ROT );
290 _position->setTransform( &tuxpos );
294 FGAircraftModel::Animation *
295 FGAircraftModel::make_animation (const char * object_name,
296 SGPropertyNode * node)
298 Animation * animation = 0;
299 const char * type = node->getStringValue("type");
300 if (!strcmp("none", type)) {
301 animation = new NullAnimation();
302 } else if (!strcmp("select", type)) {
303 animation = new SelectAnimation();
304 } else if (!strcmp("spin", type)) {
305 animation = new SpinAnimation();
306 } else if (!strcmp("rotate", type)) {
307 animation = new RotateAnimation();
308 } else if (!strcmp("translate", type)) {
309 animation = new TranslateAnimation();
311 animation = new NullAnimation();
312 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
315 ssgEntity * object = find_named_node(_model, object_name);
317 SG_LOG(SG_INPUT, SG_WARN, "Object " << object_name << " not found");
321 animation->init(object, node);
329 ////////////////////////////////////////////////////////////////////////
330 // Implementation of FGAircraftModel::Animation
331 ////////////////////////////////////////////////////////////////////////
333 FGAircraftModel::Animation::Animation ()
337 FGAircraftModel::Animation::~Animation ()
343 ////////////////////////////////////////////////////////////////////////
344 // Implementation of FGAircraftModel::NullAnimation
345 ////////////////////////////////////////////////////////////////////////
347 FGAircraftModel::NullAnimation::NullAnimation ()
348 : _branch(new ssgBranch)
352 FGAircraftModel::NullAnimation::~NullAnimation ()
358 FGAircraftModel::NullAnimation::init (ssgEntity * object,
359 SGPropertyNode * props)
361 splice_branch(_branch, object);
362 _branch->setName(props->getStringValue("name", 0));
366 FGAircraftModel::NullAnimation::update (int dt)
372 ////////////////////////////////////////////////////////////////////////
373 // Implementation of FGAircraftModel::SelectAnimation
374 ////////////////////////////////////////////////////////////////////////
376 FGAircraftModel::SelectAnimation::SelectAnimation ()
378 _selector(new ssgSelector)
382 FGAircraftModel::SelectAnimation::~SelectAnimation ()
389 FGAircraftModel::SelectAnimation::init (ssgEntity * object,
390 SGPropertyNode * props)
392 splice_branch(_selector, object);
393 _selector->setName(props->getStringValue("name", 0));
394 SGPropertyNode * node = props->getChild("condition");
396 _condition = fgReadCondition(node);
401 FGAircraftModel::SelectAnimation::update (int dt)
403 if (_condition != 0 && _condition->test())
404 _selector->select(0xffff);
406 _selector->select(0x0000);
411 ////////////////////////////////////////////////////////////////////////
412 // Implementation of FGAircraftModel::SpinAnimation
413 ////////////////////////////////////////////////////////////////////////
415 FGAircraftModel::SpinAnimation::SpinAnimation ()
419 _transform(new ssgTransform)
423 FGAircraftModel::SpinAnimation::~SpinAnimation ()
429 FGAircraftModel::SpinAnimation::init (ssgEntity * object,
430 SGPropertyNode * props)
432 // Splice in the new transform node
433 splice_branch(_transform, object);
434 _transform->setName(props->getStringValue("name", 0));
435 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
436 _factor = props->getDoubleValue("factor", 1.0);
437 _position_deg = props->getDoubleValue("starting-position-deg", 0);
438 _center[0] = props->getFloatValue("center/x-m", 0);
439 _center[1] = props->getFloatValue("center/y-m", 0);
440 _center[2] = props->getFloatValue("center/z-m", 0);
441 _axis[0] = props->getFloatValue("axis/x", 0);
442 _axis[1] = props->getFloatValue("axis/y", 0);
443 _axis[2] = props->getFloatValue("axis/z", 0);
444 sgNormalizeVec3(_axis);
448 FGAircraftModel::SpinAnimation::update (int dt)
450 float velocity_rpms = (_prop->getDoubleValue() * _factor / 60000.0);
451 _position_deg += (dt * velocity_rpms * 360);
452 while (_position_deg < 0)
453 _position_deg += 360.0;
454 while (_position_deg >= 360.0)
455 _position_deg -= 360.0;
456 set_rotation(_matrix, _position_deg, _center, _axis);
457 _transform->setTransform(_matrix);
462 ////////////////////////////////////////////////////////////////////////
463 // Implementation of FGAircraftModel::RotateAnimation
464 ////////////////////////////////////////////////////////////////////////
466 FGAircraftModel::RotateAnimation::RotateAnimation ()
475 _transform(new ssgTransform)
479 FGAircraftModel::RotateAnimation::~RotateAnimation ()
485 FGAircraftModel::RotateAnimation::init (ssgEntity * object,
486 SGPropertyNode * props)
488 // Splice in the new transform node
489 splice_branch(_transform, object);
490 _transform->setName(props->getStringValue("name", 0));
491 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
492 _offset_deg = props->getDoubleValue("offset-deg", 0.0);
493 _factor = props->getDoubleValue("factor", 1.0);
494 if (props->hasValue("min-deg")) {
496 _min_deg = props->getDoubleValue("min-deg");
498 if (props->hasValue("max-deg")) {
500 _max_deg = props->getDoubleValue("max-deg");
502 _position_deg = props->getDoubleValue("starting-position-deg", 0);
503 _center[0] = props->getFloatValue("center/x-m", 0);
504 _center[1] = props->getFloatValue("center/y-m", 0);
505 _center[2] = props->getFloatValue("center/z-m", 0);
506 _axis[0] = props->getFloatValue("axis/x", 0);
507 _axis[1] = props->getFloatValue("axis/y", 0);
508 _axis[2] = props->getFloatValue("axis/z", 0);
509 sgNormalizeVec3(_axis);
513 FGAircraftModel::RotateAnimation::update (int dt)
515 _position_deg = ((_prop->getDoubleValue() + _offset_deg) * _factor);
516 if (_has_min && _position_deg < _min_deg)
517 _position_deg = _min_deg;
518 if (_has_max && _position_deg > _max_deg)
519 _position_deg = _max_deg;
520 set_rotation(_matrix, _position_deg, _center, _axis);
521 _transform->setTransform(_matrix);
526 ////////////////////////////////////////////////////////////////////////
527 // Implementation of FGAircraftModel::TranslateAnimation
528 ////////////////////////////////////////////////////////////////////////
530 FGAircraftModel::TranslateAnimation::TranslateAnimation ()
539 _transform(new ssgTransform)
543 FGAircraftModel::TranslateAnimation::~TranslateAnimation ()
549 FGAircraftModel::TranslateAnimation::init (ssgEntity * object,
550 SGPropertyNode * props)
552 // Splice in the new transform node
553 splice_branch(_transform, object);
554 _transform->setName(props->getStringValue("name", 0));
555 _prop = fgGetNode(props->getStringValue("property", "/null"), true);
556 _offset_m = props->getDoubleValue("offset-m", 0.0);
557 _factor = props->getDoubleValue("factor", 1.0);
558 if (props->hasValue("min-m")) {
560 _min_m = props->getDoubleValue("min-m");
562 if (props->hasValue("max-m")) {
564 _max_m = props->getDoubleValue("max-m");
566 _position_m = props->getDoubleValue("starting-position-m", 0);
567 _axis[0] = props->getFloatValue("axis/x", 0);
568 _axis[1] = props->getFloatValue("axis/y", 0);
569 _axis[2] = props->getFloatValue("axis/z", 0);
570 sgNormalizeVec3(_axis);
574 FGAircraftModel::TranslateAnimation::update (int dt)
576 _position_m = ((_prop->getDoubleValue() + _offset_m) * _factor);
577 if (_has_min && _position_m < _min_m)
578 _position_m = _min_m;
579 if (_has_max && _position_m > _max_m)
580 _position_m = _max_m;
581 set_translation(_matrix, _position_m, _axis);
582 _transform->setTransform(_matrix);