]> git.mxchange.org Git - flightgear.git/blob - src/Model/model.cxx
Get rid of non-portable fmax call.
[flightgear.git] / src / Model / model.cxx
1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #  include <config.h>
8 #endif
9
10 #include <string.h>             // for strcmp()
11
12 #include <plib/sg.h>
13 #include <plib/ssg.h>
14
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>
21
22 #include <Main/globals.hxx>
23 #include <Main/location.hxx>
24 #include <Scenery/scenery.hxx>
25
26 #include "model.hxx"
27
28
29 \f
30 ////////////////////////////////////////////////////////////////////////
31 // Static utility functions.
32 ////////////////////////////////////////////////////////////////////////
33
34 /**
35  * Locate a named SSG node in a branch.
36  */
37 static ssgEntity *
38 find_named_node (ssgEntity * node, const char * name)
39 {
40   char * node_name = node->getName();
41   if (node_name != 0 && !strcmp(name, node_name))
42     return node;
43   else if (node->isAKindOf(ssgTypeBranch())) {
44     int nKids = node->getNumKids();
45     for (int i = 0; i < nKids; i++) {
46       ssgEntity * result =
47         find_named_node(((ssgBranch*)node)->getKid(i), name);
48       if (result != 0)
49         return result;
50     }
51   } 
52   return 0;
53 }
54
55 /**
56  * Splice a branch in between all child nodes and their parents.
57  */
58 static void
59 splice_branch (ssgBranch * branch, ssgEntity * child)
60 {
61   int nParents = child->getNumParents();
62   branch->addKid(child);
63   for (int i = 0; i < nParents; i++) {
64     ssgBranch * parent = child->getParent(i);
65     parent->replaceKid(child, branch);
66   }
67 }
68
69 /**
70  * Set up the transform matrix for a spin or rotation.
71  */
72 static void
73 set_rotation (sgMat4 &matrix, double position_deg,
74               sgVec3 &center, sgVec3 &axis)
75 {
76  float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
77  
78  float s = (float) sin ( temp_angle ) ;
79  float c = (float) cos ( temp_angle ) ;
80  float t = SG_ONE - c ;
81
82  // axis was normalized at load time 
83  // hint to the compiler to put these into FP registers
84  float x = axis[0];
85  float y = axis[1];
86  float z = axis[2];
87
88  matrix[0][0] = t * x * x + c ;
89  matrix[0][1] = t * y * x - s * z ;
90  matrix[0][2] = t * z * x + s * y ;
91  matrix[0][3] = SG_ZERO;
92  
93  matrix[1][0] = t * x * y + s * z ;
94  matrix[1][1] = t * y * y + c ;
95  matrix[1][2] = t * z * y - s * x ;
96  matrix[1][3] = SG_ZERO;
97  
98  matrix[2][0] = t * x * z - s * y ;
99  matrix[2][1] = t * y * z + s * x ;
100  matrix[2][2] = t * z * z + c ;
101  matrix[2][3] = SG_ZERO;
102
103   // hint to the compiler to put these into FP registers
104  x = center[0];
105  y = center[1];
106  z = center[2];
107  
108  matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
109  matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
110  matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
111  matrix[3][3] = SG_ONE;
112 }
113
114 /**
115  * Set up the transform matrix for a translation.
116  */
117 static void
118 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
119 {
120   sgVec3 xyz;
121   sgScaleVec3(xyz, axis, position_m);
122   sgMakeTransMat4(matrix, xyz);
123 }
124
125
126 \f
127 ////////////////////////////////////////////////////////////////////////
128 // Implementation of FG3DModel
129 ////////////////////////////////////////////////////////////////////////
130
131 FG3DModel::FG3DModel ()
132   : _model(0),
133     _selector(new ssgSelector),
134     _position(new ssgTransform)
135 {
136 }
137
138 FG3DModel::~FG3DModel ()
139 {
140   // since the nodes are attached to the scene graph, they'll be
141   // deleted automatically
142
143   for (int i = 0; i < _animations.size(); i++) {
144     Animation * tmp = _animations[i];
145     _animations[i] = 0;
146     delete tmp;
147   }
148
149 }
150
151 void 
152 FG3DModel::init (const string &path)
153 {
154   SGPropertyNode props;
155
156                                 // Load the 3D aircraft object itself
157   SGPath xmlpath = globals->get_fg_root();
158   SGPath modelpath = path;
159   xmlpath.append(modelpath.str());
160
161                                 // Check for an XML wrapper
162   if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
163     readProperties(xmlpath.str(), &props);
164     if (props.hasValue("/path")) {
165       modelpath = modelpath.dir();
166       modelpath.append(props.getStringValue("/path"));
167     } else {
168       throw sg_exception("No path for model");
169     }
170   }
171
172                                 // Assume that textures are in
173                                 // the same location as the XML file.
174   ssgTexturePath((char *)xmlpath.dir().c_str());
175   _model = ssgLoad((char *)modelpath.c_str());
176   if (_model == 0)
177     throw sg_exception("Failed to load 3D model");
178
179                                 // Load animations
180   vector<SGPropertyNode *> animation_nodes = props.getChildren("animation");
181   for (unsigned int i = 0; i < animation_nodes.size(); i++) {
182     vector<SGPropertyNode *> name_nodes =
183       animation_nodes[i]->getChildren("object-name");
184     if (name_nodes.size() < 1) {
185       SG_LOG(SG_INPUT, SG_ALERT, "No object-name given for transformation");
186     } else {
187       for (unsigned int j = 0; j < name_nodes.size(); j++) {
188         Animation * animation =
189           make_animation(name_nodes[j]->getStringValue(), animation_nodes[i]);
190         if (animation != 0)
191           _animations.push_back(animation);
192       }
193     }
194   }
195
196                                 // Set up the alignment node
197   ssgTransform * align = new ssgTransform;
198   align->addKid(_model);
199   sgMat4 rot_matrix;
200   sgMat4 off_matrix;
201   sgMat4 res_matrix;
202   float h_rot = props.getFloatValue("/offsets/heading-deg", 0.0);
203   float p_rot = props.getFloatValue("/offsets/roll-deg", 0.0);
204   float r_rot = props.getFloatValue("/offsets/pitch-deg", 0.0);
205   float x_off = props.getFloatValue("/offsets/x-m", 0.0);
206   float y_off = props.getFloatValue("/offsets/y-m", 0.0);
207   float z_off = props.getFloatValue("/offsets/z-m", 0.0);
208   sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
209   sgMakeTransMat4(off_matrix, x_off, y_off, z_off);
210   sgMultMat4(res_matrix, off_matrix, rot_matrix);
211   align->setTransform(res_matrix);
212
213                                 // Set up the position node
214   _position->addKid(align);
215
216                                 // Set up the selector node
217   _selector->addKid(_position);
218   _selector->clrTraversalMaskBits(SSGTRAV_HOT);
219
220
221                                 // Set up a location class
222   _location = (FGLocation *) new FGLocation;
223
224 }
225
226 void
227 FG3DModel::update (int dt)
228 {
229   for (unsigned int i = 0; i < _animations.size(); i++)
230     _animations[i]->update(dt);
231
232   sgCoord obj_pos;
233
234   sgMat4 POS, TRANS;
235
236   _location->setPosition( _lon_deg, _lat_deg, _elev_ft );
237   _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
238   sgCopyMat4(TRANS, _location->getTransformMatrix());
239   sgMakeTransMat4( POS, _location->get_view_pos() );
240
241   sgPostMultMat4( TRANS, POS );
242
243   sgSetCoord( &obj_pos, TRANS );
244
245   _position->setTransform(&obj_pos);
246 }
247
248 bool
249 FG3DModel::getVisible () const
250 {
251   return _selector->getSelect();
252 }
253
254 void
255 FG3DModel::setVisible (bool visible)
256 {
257   _selector->select(visible);
258 }
259
260 void
261 FG3DModel::setPosition (double lon_deg, double lat_deg, double elev_ft)
262 {
263   _lon_deg = lon_deg;
264   _lat_deg = lat_deg;
265   _elev_ft = elev_ft;
266 }
267
268 void
269 FG3DModel::setOrientation (double roll_deg, double pitch_deg,
270                            double heading_deg)
271 {
272   _roll_deg = roll_deg;
273   _pitch_deg = pitch_deg;
274   _heading_deg = heading_deg;
275 }
276
277 FG3DModel::Animation *
278 FG3DModel::make_animation (const char * object_name,
279                                  SGPropertyNode * node)
280 {
281   Animation * animation = 0;
282   const char * type = node->getStringValue("type");
283   if (!strcmp("none", type)) {
284     animation = new NullAnimation();
285   } else if (!strcmp("select", type)) {
286     animation = new SelectAnimation();
287   } else if (!strcmp("spin", type)) {
288     animation = new SpinAnimation();
289   } else if (!strcmp("rotate", type)) {
290     animation = new RotateAnimation();
291   } else if (!strcmp("translate", type)) {
292     animation = new TranslateAnimation();
293   } else {
294     animation = new NullAnimation();
295     SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
296   }
297
298   ssgEntity * object = find_named_node(_model, object_name);
299   if (object == 0) {
300     SG_LOG(SG_INPUT, SG_WARN, "Object " << object_name << " not found");
301     delete animation;
302     animation = 0;
303   } else {
304     animation->init(object, node);
305   }
306
307   return animation;
308 }
309
310
311 \f
312 ////////////////////////////////////////////////////////////////////////
313 // Implementation of FG3DModel::Animation
314 ////////////////////////////////////////////////////////////////////////
315
316 FG3DModel::Animation::Animation ()
317 {
318 }
319
320 FG3DModel::Animation::~Animation ()
321 {
322 }
323
324
325 \f
326 ////////////////////////////////////////////////////////////////////////
327 // Implementation of FG3DModel::NullAnimation
328 ////////////////////////////////////////////////////////////////////////
329
330 FG3DModel::NullAnimation::NullAnimation ()
331   : _branch(new ssgBranch)
332 {
333 }
334
335 FG3DModel::NullAnimation::~NullAnimation ()
336 {
337   _branch = 0;
338 }
339
340 void
341 FG3DModel::NullAnimation::init (ssgEntity * object,
342                                       SGPropertyNode * props)
343 {
344   splice_branch(_branch, object);
345   _branch->setName(props->getStringValue("name", 0));
346 }
347
348 void
349 FG3DModel::NullAnimation::update (int dt)
350 {
351 }
352
353
354 \f
355 ////////////////////////////////////////////////////////////////////////
356 // Implementation of FG3DModel::SelectAnimation
357 ////////////////////////////////////////////////////////////////////////
358
359 FG3DModel::SelectAnimation::SelectAnimation ()
360   : _condition(0),
361     _selector(new ssgSelector)
362 {
363 }
364
365 FG3DModel::SelectAnimation::~SelectAnimation ()
366 {
367   delete _condition;
368   _selector = 0;
369 }
370
371 void
372 FG3DModel::SelectAnimation::init (ssgEntity * object,
373                                       SGPropertyNode * props)
374 {
375   splice_branch(_selector, object);
376   _selector->setName(props->getStringValue("name", 0));
377   SGPropertyNode * node = props->getChild("condition");
378   if (node != 0) {
379     _condition = fgReadCondition(node);
380   }
381 }
382
383 void
384 FG3DModel::SelectAnimation::update (int dt)
385 {
386   if (_condition != 0 && _condition->test()) 
387     _selector->select(0xffff);
388   else
389     _selector->select(0x0000);
390 }
391
392
393 \f
394 ////////////////////////////////////////////////////////////////////////
395 // Implementation of FG3DModel::SpinAnimation
396 ////////////////////////////////////////////////////////////////////////
397
398 FG3DModel::SpinAnimation::SpinAnimation ()
399   : _prop(0),
400     _factor(0),
401     _position_deg(0),
402     _transform(new ssgTransform)
403 {
404 }
405
406 FG3DModel::SpinAnimation::~SpinAnimation ()
407 {
408   _transform = 0;
409 }
410
411 void
412 FG3DModel::SpinAnimation::init (ssgEntity * object,
413                                       SGPropertyNode * props)
414 {
415                                 // Splice in the new transform node
416   splice_branch(_transform, object);
417   _transform->setName(props->getStringValue("name", 0));
418   _prop = fgGetNode(props->getStringValue("property", "/null"), true);
419   _factor = props->getDoubleValue("factor", 1.0);
420   _position_deg = props->getDoubleValue("starting-position-deg", 0);
421   _center[0] = props->getFloatValue("center/x-m", 0);
422   _center[1] = props->getFloatValue("center/y-m", 0);
423   _center[2] = props->getFloatValue("center/z-m", 0);
424   _axis[0] = props->getFloatValue("axis/x", 0);
425   _axis[1] = props->getFloatValue("axis/y", 0);
426   _axis[2] = props->getFloatValue("axis/z", 0);
427   sgNormalizeVec3(_axis);
428 }
429
430 void
431 FG3DModel::SpinAnimation::update (int dt)
432 {
433   float velocity_rpms = (_prop->getDoubleValue() * _factor / 60000.0);
434   _position_deg += (dt * velocity_rpms * 360);
435   while (_position_deg < 0)
436     _position_deg += 360.0;
437   while (_position_deg >= 360.0)
438     _position_deg -= 360.0;
439   set_rotation(_matrix, _position_deg, _center, _axis);
440   _transform->setTransform(_matrix);
441 }
442
443
444 \f
445 ////////////////////////////////////////////////////////////////////////
446 // Implementation of FG3DModel::RotateAnimation
447 ////////////////////////////////////////////////////////////////////////
448
449 FG3DModel::RotateAnimation::RotateAnimation ()
450   : _prop(0),
451     _offset_deg(0.0),
452     _factor(1.0),
453     _has_min(false),
454     _min_deg(0.0),
455     _has_max(false),
456     _max_deg(1.0),
457     _position_deg(0.0),
458     _transform(new ssgTransform)
459 {
460 }
461
462 FG3DModel::RotateAnimation::~RotateAnimation ()
463 {
464   _transform = 0;
465 }
466
467 void
468 FG3DModel::RotateAnimation::init (ssgEntity * object,
469                                         SGPropertyNode * props)
470 {
471                                 // Splice in the new transform node
472   splice_branch(_transform, object);
473   _transform->setName(props->getStringValue("name", 0));
474   _prop = fgGetNode(props->getStringValue("property", "/null"), true);
475   _offset_deg = props->getDoubleValue("offset-deg", 0.0);
476   _factor = props->getDoubleValue("factor", 1.0);
477   if (props->hasValue("min-deg")) {
478     _has_min = true;
479     _min_deg = props->getDoubleValue("min-deg");
480   }
481   if (props->hasValue("max-deg")) {
482     _has_max = true;
483     _max_deg = props->getDoubleValue("max-deg");
484   }
485   _position_deg = props->getDoubleValue("starting-position-deg", 0);
486   _center[0] = props->getFloatValue("center/x-m", 0);
487   _center[1] = props->getFloatValue("center/y-m", 0);
488   _center[2] = props->getFloatValue("center/z-m", 0);
489   _axis[0] = props->getFloatValue("axis/x", 0);
490   _axis[1] = props->getFloatValue("axis/y", 0);
491   _axis[2] = props->getFloatValue("axis/z", 0);
492   sgNormalizeVec3(_axis);
493 }
494
495 void
496 FG3DModel::RotateAnimation::update (int dt)
497 {
498   _position_deg = ((_prop->getDoubleValue() + _offset_deg) * _factor);
499   if (_has_min && _position_deg < _min_deg)
500     _position_deg = _min_deg;
501   if (_has_max && _position_deg > _max_deg)
502     _position_deg = _max_deg;
503   set_rotation(_matrix, _position_deg, _center, _axis);
504   _transform->setTransform(_matrix);
505 }
506
507
508 \f
509 ////////////////////////////////////////////////////////////////////////
510 // Implementation of FG3DModel::TranslateAnimation
511 ////////////////////////////////////////////////////////////////////////
512
513 FG3DModel::TranslateAnimation::TranslateAnimation ()
514   : _prop(0),
515     _offset_m(0.0),
516     _factor(1.0),
517     _has_min(false),
518     _min_m(0.0),
519     _has_max(false),
520     _max_m(1.0),
521     _position_m(0.0),
522     _transform(new ssgTransform)
523 {
524 }
525
526 FG3DModel::TranslateAnimation::~TranslateAnimation ()
527 {
528   _transform = 0;
529 }
530
531 void
532 FG3DModel::TranslateAnimation::init (ssgEntity * object,
533                                         SGPropertyNode * props)
534 {
535                                 // Splice in the new transform node
536   splice_branch(_transform, object);
537   _transform->setName(props->getStringValue("name", 0));
538   _prop = fgGetNode(props->getStringValue("property", "/null"), true);
539   _offset_m = props->getDoubleValue("offset-m", 0.0);
540   _factor = props->getDoubleValue("factor", 1.0);
541   if (props->hasValue("min-m")) {
542     _has_min = true;
543     _min_m = props->getDoubleValue("min-m");
544   }
545   if (props->hasValue("max-m")) {
546     _has_max = true;
547     _max_m = props->getDoubleValue("max-m");
548   }
549   _position_m = props->getDoubleValue("starting-position-m", 0);
550   _axis[0] = props->getFloatValue("axis/x", 0);
551   _axis[1] = props->getFloatValue("axis/y", 0);
552   _axis[2] = props->getFloatValue("axis/z", 0);
553   sgNormalizeVec3(_axis);
554 }
555
556 void
557 FG3DModel::TranslateAnimation::update (int dt)
558 {
559   _position_m = ((_prop->getDoubleValue() + _offset_m) * _factor);
560   if (_has_min && _position_m < _min_m)
561     _position_m = _min_m;
562   if (_has_max && _position_m > _max_m)
563     _position_m = _max_m;
564   set_translation(_matrix, _position_m, _axis);
565   _transform->setTransform(_matrix);
566 }
567
568
569 // end of model.cxx
570
571
572
573