]> git.mxchange.org Git - flightgear.git/blob - src/Main/model.cxx
- added an 'offset' parameter for animations (it's applied before
[flightgear.git] / src / Main / 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 <plib/sg.h>
11 #include <plib/ssg.h>
12
13 #include <simgear/compiler.h>
14 #include <simgear/debug/logstream.hxx>
15 #include <simgear/misc/exception.hxx>
16 #include <simgear/misc/sg_path.hxx>
17
18 #include "globals.hxx"
19 #include "fg_props.hxx"
20 #include "viewmgr.hxx"
21 #include "model.hxx"
22
23 extern unsigned long int fgSimTime; // FIXME: this is ugly
24
25 extern ssgRoot * scene;         // FIXME: from main.cxx
26
27 FGAircraftModel current_model;  // FIXME: add to globals
28
29
30 static ssgEntity *
31 find_named_node (ssgEntity * node, const string &name)
32 {
33   char * node_name = node->getName();
34   if (node_name != 0 && name == node_name)
35     return node;
36   else if (node->isAKindOf(ssgTypeBranch())) {
37     int nKids = node->getNumKids();
38     for (int i = 0; i < nKids; i++) {
39       ssgEntity * result =
40         find_named_node(((ssgBranch*)node)->getKid(i), name);
41       if (result != 0)
42         return result;
43     }
44   } 
45   return 0;
46 }
47
48 FGAircraftModel::FGAircraftModel ()
49   : _model(0),
50     _selector(new ssgSelector),
51     _position(new ssgTransform)
52 {
53 }
54
55 FGAircraftModel::~FGAircraftModel ()
56 {
57   // since the nodes are attached to the scene graph, they'll be
58   // deleted automatically
59 }
60
61 void 
62 FGAircraftModel::init ()
63 {
64   // TODO: optionally load an XML file with a pointer to the 3D object
65   // and placement and animation info
66
67   SGPropertyNode props;
68
69   SG_LOG(SG_INPUT, SG_INFO, "Initializing aircraft 3D model");
70
71                                 // Load the 3D aircraft object itself
72   SGPath path = globals->get_fg_root();
73   path.append(fgGetString("/sim/model/path", "Models/Geometry/glider.ac"));
74
75   if (path.str().substr(path.str().size() - 4, 4) == ".xml") {
76     readProperties(path.str(), &props);
77     if (props.hasValue("/path")) {
78       path = path.dir();;
79       path.append(props.getStringValue("/path"));
80     } else {
81       path = globals->get_fg_root();
82       path.append("Models/Geometry/glider.ac");
83     }
84   }
85
86   ssgTexturePath((char *)path.dir().c_str());
87   _model = ssgLoad((char *)path.c_str());
88   if (_model == 0) {
89     _model = ssgLoad((char *)"Models/Geometry/glider.ac");
90     if (_model == 0)
91       throw sg_exception("Failed to load an aircraft model");
92   }
93
94                                 // Load animations
95   vector<SGPropertyNode *> animation_nodes =
96     props.getChildren("animation");
97   for (int i = 0; i < animation_nodes.size(); i++) {
98     _animations.push_back(read_animation(animation_nodes[i]));
99   }
100
101                                 // Set up the alignment node
102   ssgTransform * align = new ssgTransform;
103   align->addKid(_model);
104   sgMat4 rot_matrix;
105   sgMat4 off_matrix;
106   sgMat4 res_matrix;
107   float h_rot = props.getFloatValue("/offsets/heading-deg", 0.0);
108   float p_rot = props.getFloatValue("/offsets/roll-deg", 0.0);
109   float r_rot = props.getFloatValue("/offsets/pitch-deg", 0.0);
110   float x_off = props.getFloatValue("/offsets/x-m", 0.0);
111   float y_off = props.getFloatValue("/offsets/y-m", 0.0);
112   float z_off = props.getFloatValue("/offsets/z-m", 0.0);
113   sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
114   sgMakeTransMat4(off_matrix, x_off, y_off, z_off);
115   sgMultMat4(res_matrix, off_matrix, rot_matrix);
116   align->setTransform(res_matrix);
117
118                                 // Set up the position node
119   _position->addKid(align);
120
121                                 // Set up the selector node
122   _selector->addKid(_position);
123   _selector->clrTraversalMaskBits(SSGTRAV_HOT);
124   scene->addKid(_selector);
125 }
126
127 void 
128 FGAircraftModel::bind ()
129 {
130 }
131
132 void 
133 FGAircraftModel::unbind ()
134 {
135 }
136
137 void
138 FGAircraftModel::update (int dt)
139 {
140   _current_timestamp.stamp();
141   long elapsed_ms = (_current_timestamp - _last_timestamp) / 1000;
142   _last_timestamp.stamp();
143
144   if (globals->get_viewmgr()->get_current() == 0) {
145     _selector->select(false);
146   } else {
147     for (int i = 0; i < _animations.size(); i++)
148       do_animation(_animations[i], elapsed_ms);
149
150     _selector->select(true);
151     FGViewerRPH *pilot_view =
152       (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
153     
154     sgMat4 sgTRANS;
155     sgMakeTransMat4( sgTRANS, pilot_view->get_view_pos() );
156     
157     sgVec3 ownship_up;
158     sgSetVec3( ownship_up, 0.0, 0.0, 1.0);
159     
160     sgMat4 sgROT;
161     sgMakeRotMat4( sgROT, -90.0, ownship_up );
162     
163     sgMat4 sgTUX;
164     sgCopyMat4( sgTUX, sgROT );
165     sgPostMultMat4( sgTUX, pilot_view->get_VIEW_ROT() );
166     sgPostMultMat4( sgTUX, sgTRANS );
167     
168     sgCoord tuxpos;
169     sgSetCoord( &tuxpos, sgTUX );
170     _position->setTransform( &tuxpos );
171   }
172
173 }
174
175 FGAircraftModel::Animation
176 FGAircraftModel::read_animation (const SGPropertyNode * node)
177 {
178   Animation animation;
179
180                                 // Figure out the animation type
181   string type_name = node->getStringValue("type");
182   if (type_name == "spin") {
183     SG_LOG(SG_INPUT, SG_INFO, "Reading spin animation");
184     animation.type = Animation::Spin;
185   } else if (type_name == "rotate") {
186     SG_LOG(SG_INPUT, SG_INFO, "Reading rotate animation");
187     animation.type = Animation::Rotate;
188   } else if (type_name == "none") {
189     SG_LOG(SG_INPUT, SG_INFO, "Reading disabled animation");
190     animation.type = Animation::None;
191     return animation;
192   } else {
193     animation.type = Animation::None;
194     SG_LOG(SG_INPUT, SG_ALERT, "Unknown animation type " << type_name);
195     return animation;
196   }
197
198                                 // Find the object to be animated
199   string object_name = node->getStringValue("object-name");
200   ssgEntity * target = find_named_node(_model, object_name);
201   if (target != 0) {
202     SG_LOG(SG_INPUT, SG_INFO, "  Target object is " << object_name);
203   } else {
204     animation.type = Animation::None;
205     SG_LOG(SG_INPUT, SG_ALERT, "Object " << object_name
206            << " not found in model");
207     return animation;
208   }
209
210                                 // Splice a transform node into the tree
211   animation.transform = new ssgTransform;
212   int nParents = target->getNumParents();
213   animation.transform->addKid(target);
214   for (int i = 0; i < nParents; i++) {
215     ssgBranch * parent = target->getParent(i);
216     parent->replaceKid(target, animation.transform);
217   }
218
219                                 // Get the node
220   animation.prop =
221     fgGetNode(node->getStringValue("property", "/null"), true);
222
223   animation.position = node->getFloatValue("initial-position", 0);
224   animation.offset = node->getFloatValue("offset", 0);
225   animation.factor = node->getFloatValue("factor", 1);
226
227                                 // Get the center and axis
228   animation.center[0] = node->getFloatValue("center/x-m", 0);
229   animation.center[1] = node->getFloatValue("center/y-m", 0);
230   animation.center[2] = node->getFloatValue("center/z-m", 0);
231   animation.axis[0] = node->getFloatValue("axis/x", 0);
232   animation.axis[1] = node->getFloatValue("axis/y", 1);
233   animation.axis[2] = node->getFloatValue("axis/z", 0);
234
235   sgNormalizeVec3(animation.axis);
236
237   return animation;
238 }
239
240 void
241 FGAircraftModel::do_animation (Animation &animation, long elapsed_ms)
242 {
243   switch (animation.type) {
244   case Animation::None:
245     return;
246   case Animation::Spin:
247   {
248     float velocity_rpms = (animation.prop->getDoubleValue()
249                            * animation.factor / 60000.0);
250     animation.position += (elapsed_ms * velocity_rpms * 360);
251     animation.setRotation();
252     return;
253   }
254   case Animation::Rotate: {
255     animation.position = ((animation.prop->getFloatValue()
256                            + animation.offset)
257                           * animation.factor);
258     animation.setRotation();
259     return;
260   }
261   default:
262     return;
263   }
264 }
265
266 /* 
267  * Transform to rotate an object around its local axis
268  * from a relative frame of reference at center -- NHV
269  */
270 void
271 FGAircraftModel::Animation::setRotation()
272 {
273  float temp_angle = -position * SG_DEGREES_TO_RADIANS ;
274  
275  float s = (float) sin ( temp_angle ) ;
276  float c = (float) cos ( temp_angle ) ;
277  float t = SG_ONE - c ;
278
279  // axis was normalized at load time 
280  // hint to the compiler to put these into FP registers
281  float x = axis[0];
282  float y = axis[1];
283  float z = axis[2];
284
285  sgMat4 matrix;
286  matrix[0][0] = t * x * x + c ;
287  matrix[0][1] = t * y * x - s * z ;
288  matrix[0][2] = t * z * x + s * y ;
289  matrix[0][3] = SG_ZERO;
290  
291  matrix[1][0] = t * x * y + s * z ;
292  matrix[1][1] = t * y * y + c ;
293  matrix[1][2] = t * z * y - s * x ;
294  matrix[1][3] = SG_ZERO;
295  
296  matrix[2][0] = t * x * z - s * y ;
297  matrix[2][1] = t * y * z + s * x ;
298  matrix[2][2] = t * z * z + c ;
299  matrix[2][3] = SG_ZERO;
300
301   // hint to the compiler to put these into FP registers
302  x = center[0];
303  y = center[1];
304  z = center[2];
305  
306  matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
307  matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
308  matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
309  matrix[3][3] = SG_ONE;
310  
311  transform->setTransform(matrix);
312 }
313
314
315 // end of model.cxx