]> git.mxchange.org Git - flightgear.git/blob - src/Model/model.hxx
Avoid a segfault when a named object is not found for an animation.
[flightgear.git] / src / Model / model.hxx
1 // model.hxx - 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 #ifndef __MODEL_HXX
7 #define __MODEL_HXX 1
8
9 #ifndef __cplusplus
10 # error This library requires C++
11 #endif
12
13 #include <vector>
14
15 SG_USING_STD(vector);
16
17 #include <plib/sg.h>
18
19
20 // Don't pull in the headers, since we don't need them here.
21 class ssgBranch;
22 class ssgCutout;
23 class ssgEntity;
24 class ssgRangeSelector;
25 class ssgSelector;
26 class ssgTransform;
27
28 class SGPropertyNode;
29 class SGInterpTable;
30 class FGCondition;
31 class FGLocation;
32
33
34 // Has anyone done anything *really* stupid, like making min and max macros?
35 #ifdef min
36 #undef min
37 #endif
38 #ifdef max
39 #undef max
40 #endif
41
42
43 class FG3DModel
44 {
45 public:
46
47   FG3DModel ();
48   virtual ~FG3DModel ();
49
50   virtual void init (const string &path);
51   virtual void update (double dt);
52
53   virtual ssgEntity * getSceneGraph () const { return (ssgEntity *)_model; }
54
55 private:
56
57   class Animation;
58   Animation * make_animation (const char * object_name, SGPropertyNode * node);
59
60                                 // Child models.
61   vector<FG3DModel *> _children;
62
63                                 // Animations
64   vector <Animation *> _animations;
65
66                                 // Scene graph
67   ssgBranch * _model;
68
69
70   \f
71   //////////////////////////////////////////////////////////////////////
72   // Internal classes for individual animations.
73   //////////////////////////////////////////////////////////////////////
74
75   /**
76    * Abstract base class for all animations.
77    */
78   class Animation
79   {
80   public:
81
82     Animation ();
83
84     virtual ~Animation ();
85
86     /**
87      * Initialize the animation.
88      *
89      * @param object The object to animate.
90      * @param props The property node with configuration information.
91      */
92     virtual void init (ssgEntity * object, SGPropertyNode * props) = 0;
93
94
95     /**
96      * Update the animation.
97      *
98      * @param dt The elapsed time in seconds since the last call.
99      */
100     virtual void update (double dt) = 0;
101
102   };
103
104
105   /**
106    * A no-op animation.
107    */
108   class NullAnimation : public Animation
109   {
110   public:
111     NullAnimation ();
112     virtual ~NullAnimation ();
113     virtual void init (ssgEntity * object, SGPropertyNode * props);
114     virtual void update (double dt);
115   private:
116     ssgBranch * _branch;
117   };
118
119   
120   /**
121    * A range, or level-of-detail (LOD) animation.
122    */
123   class RangeAnimation : public Animation
124   {
125   public:
126     RangeAnimation ();
127     virtual ~RangeAnimation ();
128     virtual void init (ssgEntity * object, SGPropertyNode * props);
129     virtual void update (double dt);
130   private:
131     ssgRangeSelector * _branch;
132   };
133
134
135   /**
136    * Animation to turn and face the screen.
137    */
138   class BillboardAnimation : public Animation
139   {
140   public:
141     BillboardAnimation ();
142     virtual ~BillboardAnimation ();
143     virtual void init (ssgEntity * object, SGPropertyNode * props);
144     virtual void update (double dt);
145   private:
146     ssgCutout * _branch;
147   };
148
149
150   /**
151    * Animation to select alternative versions of the same object.
152    */
153   class SelectAnimation : public Animation
154   {
155   public:
156     SelectAnimation ();
157     virtual ~SelectAnimation ();
158     virtual void init (ssgEntity * object, SGPropertyNode * props);
159     virtual void update (double dt);
160   private:
161     FGCondition * _condition;
162     ssgSelector * _selector;
163   };
164
165
166   /**
167    * Animation to spin an object around a center point.
168    *
169    * This animation rotates at a specific velocity.
170    */
171   class SpinAnimation : public Animation
172   {
173   public:
174     SpinAnimation ();
175     virtual ~SpinAnimation ();
176     virtual void init (ssgEntity * object, SGPropertyNode * props);
177     virtual void update (double dt);
178   private:
179     SGPropertyNode * _prop;
180     double _factor;
181     double _position_deg;
182     sgMat4 _matrix;
183     sgVec3 _center;
184     sgVec3 _axis;
185     ssgTransform * _transform;
186   };
187
188
189   /**
190    * Animation to rotate an object around a center point.
191    *
192    * This animation rotates to a specific position.
193    */
194   class RotateAnimation : public Animation
195   {
196   public:
197     RotateAnimation ();
198     virtual ~RotateAnimation ();
199     virtual void init (ssgEntity * object, SGPropertyNode * props);
200     virtual void update (double dt);
201   private:
202     SGPropertyNode * _prop;
203     double _offset_deg;
204     double _factor;
205     SGInterpTable * _table;
206     bool _has_min;
207     double _min_deg;
208     bool _has_max;
209     double _max_deg;
210     double _position_deg;
211     sgMat4 _matrix;
212     sgVec3 _center;
213     sgVec3 _axis;
214     ssgTransform * _transform;
215   };
216
217
218   /**
219    * Animation to slide along an axis.
220    */
221   class TranslateAnimation : public Animation
222   {
223   public:
224     TranslateAnimation ();
225     virtual ~TranslateAnimation ();
226     virtual void init (ssgEntity * object, SGPropertyNode * props);
227     virtual void update (double dt);
228   private:
229     SGPropertyNode * _prop;
230     double _offset_m;
231     double _factor;
232     SGInterpTable * _table;
233     bool _has_min;
234     double _min_m;
235     bool _has_max;
236     double _max_m;
237     double _position_m;
238     sgMat4 _matrix;
239     sgVec3 _axis;
240     ssgTransform * _transform;
241   };
242
243
244 };
245
246
247 class FGModelPlacement
248 {
249 public:
250
251   FGModelPlacement ();
252   virtual ~FGModelPlacement ();
253
254   virtual void init (const string &path);
255   virtual void update (double dt);
256
257   virtual ssgEntity * getSceneGraph () { return (ssgEntity *)_selector; }
258
259   virtual FGLocation * getFGLocation () { return _location; }
260
261   virtual bool getVisible () const;
262   virtual void setVisible (bool visible);
263
264   virtual double getLongitudeDeg () const { return _lon_deg; }
265   virtual double getLatitudeDeg () const { return _lat_deg; }
266   virtual double getElevationFt () const { return _elev_ft; }
267
268   virtual void setLongitudeDeg (double lon_deg);
269   virtual void setLatitudeDeg (double lat_deg);
270   virtual void setElevationFt (double elev_ft);
271   virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
272
273   virtual double getRollDeg () const { return _roll_deg; }
274   virtual double getPitchDeg () const { return _pitch_deg; }
275   virtual double getHeadingDeg () const { return _heading_deg; }
276
277   virtual void setRollDeg (double roll_deg);
278   virtual void setPitchDeg (double pitch_deg);
279   virtual void setHeadingDeg (double heading_deg);
280   virtual void setOrientation (double roll_deg, double pitch_deg,
281                                double heading_deg);
282
283 private:
284   
285   FG3DModel * _model;
286
287                                 // Geodetic position
288   double _lon_deg;
289   double _lat_deg;
290   double _elev_ft;
291
292                                 // Orientation
293   double _roll_deg;
294   double _pitch_deg;
295   double _heading_deg;
296
297   ssgSelector * _selector;
298   ssgTransform * _position;
299
300                                 // Location
301   FGLocation * _location;
302
303 };
304
305 #endif // __MODEL_HXX
306
307
308