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.
13 #include <simgear/compiler.h>
14 #include <simgear/debug/logstream.hxx>
15 #include <simgear/misc/exception.hxx>
16 #include <simgear/misc/sg_path.hxx>
18 #include "globals.hxx"
19 #include "fg_props.hxx"
20 #include "viewmgr.hxx"
23 extern unsigned long int fgSimTime; // FIXME: this is ugly
25 extern ssgRoot * scene; // FIXME: from main.cxx
27 FGAircraftModel current_model; // FIXME: add to globals
31 find_named_node (ssgEntity * node, const string &name)
33 char * node_name = node->getName();
34 if (node_name != 0 && name == node_name)
36 else if (node->isAKindOf(ssgTypeBranch())) {
37 int nKids = node->getNumKids();
38 for (int i = 0; i < nKids; i++) {
40 find_named_node(((ssgBranch*)node)->getKid(i), name);
48 FGAircraftModel::FGAircraftModel ()
50 _selector(new ssgSelector),
51 _position(new ssgTransform)
55 FGAircraftModel::~FGAircraftModel ()
57 // since the nodes are attached to the scene graph, they'll be
58 // deleted automatically
62 FGAircraftModel::init ()
64 // TODO: optionally load an XML file with a pointer to the 3D object
65 // and placement and animation info
69 SG_LOG(SG_INPUT, SG_INFO, "Initializing aircraft 3D model");
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"));
75 if (path.str().substr(path.str().size() - 4, 4) == ".xml") {
76 readProperties(path.str(), &props);
77 if (props.hasValue("/path")) {
79 path.append(props.getStringValue("/path"));
81 path = globals->get_fg_root();
82 path.append("Models/Geometry/glider.ac");
86 ssgTexturePath((char *)path.dir().c_str());
87 _model = ssgLoad((char *)path.c_str());
89 _model = ssgLoad((char *)"Models/Geometry/glider.ac");
91 throw sg_exception("Failed to load an aircraft model");
95 vector<SGPropertyNode *> animation_nodes =
96 props.getChildren("animation");
97 for (unsigned int i = 0; i < animation_nodes.size(); i++) {
98 vector<SGPropertyNode *> name_nodes =
99 animation_nodes[i]->getChildren("object-name");
100 if (name_nodes.size() < 1) {
101 SG_LOG(SG_INPUT, SG_ALERT, "No object-name given for transformation");
103 for (unsigned int j = 0; j < name_nodes.size(); j++) {
104 _animations.push_back(read_animation(name_nodes[j]->getStringValue(),
105 animation_nodes[i]));
110 // Set up the alignment node
111 ssgTransform * align = new ssgTransform;
112 align->addKid(_model);
116 float h_rot = props.getFloatValue("/offsets/heading-deg", 0.0);
117 float p_rot = props.getFloatValue("/offsets/roll-deg", 0.0);
118 float r_rot = props.getFloatValue("/offsets/pitch-deg", 0.0);
119 float x_off = props.getFloatValue("/offsets/x-m", 0.0);
120 float y_off = props.getFloatValue("/offsets/y-m", 0.0);
121 float z_off = props.getFloatValue("/offsets/z-m", 0.0);
122 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
123 sgMakeTransMat4(off_matrix, x_off, y_off, z_off);
124 sgMultMat4(res_matrix, off_matrix, rot_matrix);
125 align->setTransform(res_matrix);
127 // Set up the position node
128 _position->addKid(align);
130 // Set up the selector node
131 _selector->addKid(_position);
132 _selector->clrTraversalMaskBits(SSGTRAV_HOT);
133 scene->addKid(_selector);
137 FGAircraftModel::bind ()
142 FGAircraftModel::unbind ()
147 FGAircraftModel::update (int dt)
149 _current_timestamp.stamp();
150 long elapsed_ms = (_current_timestamp - _last_timestamp) / 1000;
151 _last_timestamp.stamp();
153 int view_number = globals->get_viewmgr()->get_current();
155 if (view_number == 0 && !fgGetBool("cockpit")) {
156 _selector->select(false);
158 for (unsigned int i = 0; i < _animations.size(); i++)
159 do_animation(_animations[i], elapsed_ms);
161 _selector->select(true);
162 FGViewerRPH *pilot_view =
163 (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
166 sgMakeTransMat4( sgTRANS, pilot_view->get_view_pos() );
169 sgSetVec3( ownship_up, 0.0, 0.0, 1.0);
172 sgMakeRotMat4( sgROT, -90.0, ownship_up );
175 sgCopyMat4( sgTUX, sgROT );
177 sgCopyMat4( VIEW_ROT, pilot_view->get_VIEW_ROT());
178 if (view_number == 0) {
179 // FIXME: orientation is not applied
180 // correctly when view is not forward
181 sgMakeRotMat4( sgROT, -pilot_view->get_view_offset()
182 * SGD_RADIANS_TO_DEGREES, pilot_view->get_world_up() );
183 sgPostMultMat4( VIEW_ROT, sgROT );
185 sgPostMultMat4( sgTUX, VIEW_ROT );
186 sgPostMultMat4( sgTUX, sgTRANS );
189 sgSetCoord( &tuxpos, sgTUX );
190 _position->setTransform( &tuxpos );
195 FGAircraftModel::Animation
196 FGAircraftModel::read_animation (const string &object_name,
197 const SGPropertyNode * node)
201 // Find the object to be animated
202 ssgEntity * target = find_named_node(_model, object_name);
204 SG_LOG(SG_INPUT, SG_INFO, " Target object is " << object_name);
206 animation.type = Animation::None;
207 SG_LOG(SG_INPUT, SG_ALERT, "Object " << object_name
208 << " not found in model");
212 // Figure out the animation type
213 string type_name = node->getStringValue("type");
214 if (type_name == "spin") {
215 SG_LOG(SG_INPUT, SG_INFO, "Reading spin animation");
216 animation.type = Animation::Spin;
217 } else if (type_name == "rotate") {
218 SG_LOG(SG_INPUT, SG_INFO, "Reading rotate animation");
219 animation.type = Animation::Rotate;
220 } else if (type_name == "none") {
221 SG_LOG(SG_INPUT, SG_INFO, "Reading disabled animation");
222 animation.type = Animation::None;
225 animation.type = Animation::None;
226 SG_LOG(SG_INPUT, SG_ALERT, "Unknown animation type " << type_name);
230 // Splice a transform node into the tree
231 animation.transform = new ssgTransform;
232 int nParents = target->getNumParents();
233 animation.transform->addKid(target);
234 for (int i = 0; i < nParents; i++) {
235 ssgBranch * parent = target->getParent(i);
236 parent->replaceKid(target, animation.transform);
241 fgGetNode(node->getStringValue("property", "/null"), true);
243 animation.position = node->getFloatValue("initial-position", 0);
244 animation.offset = node->getFloatValue("offset", 0);
245 if (node->hasValue("min")) {
246 animation.has_min = true;
247 animation.min = node->getFloatValue("min");
249 animation.has_min = false;
251 if (node->hasValue("max")) {
252 animation.has_max = true;
253 animation.max = node->getFloatValue("max");
255 animation.has_max = false;
257 animation.factor = node->getFloatValue("factor", 1);
259 // Get the center and axis
260 animation.center[0] = node->getFloatValue("center/x-m", 0);
261 animation.center[1] = node->getFloatValue("center/y-m", 0);
262 animation.center[2] = node->getFloatValue("center/z-m", 0);
263 animation.axis[0] = node->getFloatValue("axis/x", 0);
264 animation.axis[1] = node->getFloatValue("axis/y", 0);
265 animation.axis[2] = node->getFloatValue("axis/z", 0);
267 sgNormalizeVec3(animation.axis);
273 FGAircraftModel::do_animation (Animation &animation, long elapsed_ms)
275 switch (animation.type) {
276 case Animation::None:
278 case Animation::Spin:
280 float velocity_rpms = (animation.prop->getDoubleValue()
281 * animation.factor / 60000.0);
282 animation.position += (elapsed_ms * velocity_rpms * 360);
283 animation.setRotation();
286 case Animation::Rotate: {
287 animation.position = ((animation.prop->getFloatValue()
290 if (animation.has_min && animation.position < animation.min)
291 animation.position = animation.min;
292 if (animation.has_max && animation.position > animation.max)
293 animation.position = animation.max;
294 animation.setRotation();
303 * Transform to rotate an object around its local axis
304 * from a relative frame of reference at center -- NHV
307 FGAircraftModel::Animation::setRotation()
309 float temp_angle = -position * SG_DEGREES_TO_RADIANS ;
311 float s = (float) sin ( temp_angle ) ;
312 float c = (float) cos ( temp_angle ) ;
313 float t = SG_ONE - c ;
315 // axis was normalized at load time
316 // hint to the compiler to put these into FP registers
322 matrix[0][0] = t * x * x + c ;
323 matrix[0][1] = t * y * x - s * z ;
324 matrix[0][2] = t * z * x + s * y ;
325 matrix[0][3] = SG_ZERO;
327 matrix[1][0] = t * x * y + s * z ;
328 matrix[1][1] = t * y * y + c ;
329 matrix[1][2] = t * z * y - s * x ;
330 matrix[1][3] = SG_ZERO;
332 matrix[2][0] = t * x * z - s * y ;
333 matrix[2][1] = t * y * z + s * x ;
334 matrix[2][2] = t * z * z + c ;
335 matrix[2][3] = SG_ZERO;
337 // hint to the compiler to put these into FP registers
342 matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
343 matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
344 matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
345 matrix[3][3] = SG_ONE;
347 transform->setTransform(matrix);