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.
7 #include <simgear_config.h>
10 #include <simgear/compiler.h>
12 #include <string.h> // for strcmp()
21 #include <simgear/structure/exception.hxx>
22 #include <simgear/misc/sg_path.hxx>
23 #include <simgear/props/props.hxx>
24 #include <simgear/props/props_io.hxx>
26 #include "animation.hxx"
32 bool sgUseDisplayList = true;
34 ////////////////////////////////////////////////////////////////////////
36 ////////////////////////////////////////////////////////////////////////
41 ////////////////////////////////////////////////////////////////////////
42 // Static utility functions.
43 ////////////////////////////////////////////////////////////////////////
46 model_filter_callback (ssgEntity * entity, int mask)
48 return model_filter ? 1 : 0;
52 * Callback to update an animation.
55 animation_callback (ssgEntity * entity, int mask)
57 return ((SGAnimation *)entity->getUserData())->update();
61 * Callback to restore the state after an animation.
64 restore_callback (ssgEntity * entity, int mask)
66 ((SGAnimation *)entity->getUserData())->restore();
72 * Locate a named SSG node in a branch.
75 find_named_node (ssgEntity * node, const char * name)
77 char * node_name = node->getName();
78 if (node_name != 0 && !strcmp(name, node_name))
80 else if (node->isAKindOf(ssgTypeBranch())) {
81 int nKids = node->getNumKids();
82 for (int i = 0; i < nKids; i++) {
84 find_named_node(((ssgBranch*)node)->getKid(i), name);
93 * Splice a branch in between all child nodes and their parents.
96 splice_branch (ssgBranch * branch, ssgEntity * child)
98 int nParents = child->getNumParents();
99 branch->addKid(child);
100 for (int i = 0; i < nParents; i++) {
101 ssgBranch * parent = child->getParent(i);
102 parent->replaceKid(child, branch);
107 * Make an offset matrix from rotations and position offset.
110 sgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
111 double x_off, double y_off, double z_off )
115 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
116 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
117 sgMultMat4(*result, pos_matrix, rot_matrix);
122 sgMakeAnimation( ssgBranch * model,
124 vector<SGPropertyNode_ptr> &name_nodes,
125 SGPropertyNode *prop_root,
126 SGPropertyNode_ptr node,
128 set<ssgBranch *> &ignore_branches )
131 SGAnimation * animation = 0;
132 const char * type = node->getStringValue("type", "none");
133 if (!strcmp("none", type)) {
134 animation = new SGNullAnimation(node);
135 } else if (!strcmp("range", type)) {
136 animation = new SGRangeAnimation(prop_root, node);
137 } else if (!strcmp("billboard", type)) {
138 animation = new SGBillboardAnimation(node);
139 } else if (!strcmp("select", type)) {
140 animation = new SGSelectAnimation(prop_root, node);
141 } else if (!strcmp("spin", type)) {
142 animation = new SGSpinAnimation(prop_root, node, sim_time_sec );
143 } else if (!strcmp("timed", type)) {
144 animation = new SGTimedAnimation(node);
145 } else if (!strcmp("rotate", type)) {
146 animation = new SGRotateAnimation(prop_root, node);
147 } else if (!strcmp("translate", type)) {
148 animation = new SGTranslateAnimation(prop_root, node);
149 } else if (!strcmp("scale", type)) {
150 animation = new SGScaleAnimation(prop_root, node);
151 } else if (!strcmp("texrotate", type)) {
152 animation = new SGTexRotateAnimation(prop_root, node);
153 } else if (!strcmp("textranslate", type)) {
154 animation = new SGTexTranslateAnimation(prop_root, node);
155 } else if (!strcmp("texmultiple", type)) {
156 animation = new SGTexMultipleAnimation(prop_root, node);
157 } else if (!strcmp("blend", type)) {
158 animation = new SGBlendAnimation(prop_root, node);
160 } else if (!strcmp("alpha-test", type)) {
161 animation = new SGAlphaTestAnimation(node);
162 } else if (!strcmp("flash", type)) {
163 animation = new SGFlashAnimation(node);
164 } else if (!strcmp("dist-scale", type)) {
165 animation = new SGDistScaleAnimation(node);
167 animation = new SGNullAnimation(node);
168 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
172 animation->setName((char *)name);
175 if (name_nodes.size() > 0) {
176 object = find_named_node(model, name_nodes[0]->getStringValue());
178 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name_nodes[0]->getStringValue()
187 if ( animation == 0 )
190 ssgBranch * branch = animation->getBranch();
191 splice_branch(branch, object);
193 for (unsigned int i = 1; i < name_nodes.size(); i++) {
194 const char * name = name_nodes[i]->getStringValue();
195 object = find_named_node(model, name);
197 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
201 ssgBranch * oldParent = object->getParent(0);
202 branch->addKid(object);
203 oldParent->removeKid(object);
207 if ( animation != 0 ) {
209 branch->setUserData(animation);
210 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
211 branch->setTravCallback(SSG_CALLBACK_POSTTRAV, restore_callback);
213 ignore_branches.insert( branch );
219 static void makeDList( ssgBranch *b, const set<ssgBranch *> &ignore )
221 int nb = b->getNumKids();
222 for (int i = 0; i<nb; i++) {
223 ssgEntity *e = b->getKid(i);
224 if (e->isAKindOf(ssgTypeLeaf())) {
225 ((ssgLeaf*)e)->makeDList();
226 } else if (e->isAKindOf(ssgTypeBranch()) && ignore.find((ssgBranch *)e) == ignore.end()) {
227 makeDList( (ssgBranch*)e, ignore );
234 ////////////////////////////////////////////////////////////////////////
236 ////////////////////////////////////////////////////////////////////////
239 sgLoad3DModel( const string &fg_root, const string &path,
240 SGPropertyNode *prop_root,
241 double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *) )
243 ssgBranch * model = 0;
244 SGPropertyNode props;
246 // Load the 3D aircraft object itself
247 SGPath modelpath = path, texturepath = path;
248 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
249 SGPath tmp = fg_root;
250 tmp.append(modelpath.str());
251 modelpath = texturepath = tmp;
254 // Check for an XML wrapper
255 if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
256 readProperties(modelpath.str(), &props);
257 if (props.hasValue("/path")) {
258 modelpath = modelpath.dir();
259 modelpath.append(props.getStringValue("/path"));
260 if (props.hasValue("/texture-path")) {
261 texturepath = texturepath.dir();
262 texturepath.append(props.getStringValue("/texture-path"));
266 model = new ssgBranch;
270 // Assume that textures are in
271 // the same location as the XML file.
273 if (texturepath.extension() != "")
274 texturepath = texturepath.dir();
276 ssgTexturePath((char *)texturepath.c_str());
277 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
279 throw sg_exception("Failed to load 3D model");
281 // Set up the alignment node
282 ssgTransform * alignmainmodel = new ssgTransform;
283 if ( load_panel == 0 )
284 alignmainmodel->setTravCallback( SSG_CALLBACK_PRETRAV, model_filter_callback );
285 alignmainmodel->addKid(model);
287 sgMakeOffsetsMatrix(&res_matrix,
288 props.getFloatValue("/offsets/heading-deg", 0.0),
289 props.getFloatValue("/offsets/roll-deg", 0.0),
290 props.getFloatValue("/offsets/pitch-deg", 0.0),
291 props.getFloatValue("/offsets/x-m", 0.0),
292 props.getFloatValue("/offsets/y-m", 0.0),
293 props.getFloatValue("/offsets/z-m", 0.0));
294 alignmainmodel->setTransform(res_matrix);
299 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
300 for (i = 0; i < model_nodes.size(); i++) {
301 SGPropertyNode_ptr node = model_nodes[i];
302 ssgTransform * align = new ssgTransform;
304 sgMakeOffsetsMatrix(&res_matrix,
305 node->getFloatValue("offsets/heading-deg", 0.0),
306 node->getFloatValue("offsets/roll-deg", 0.0),
307 node->getFloatValue("offsets/pitch-deg", 0.0),
308 node->getFloatValue("offsets/x-m", 0.0),
309 node->getFloatValue("offsets/y-m", 0.0),
310 node->getFloatValue("offsets/z-m", 0.0));
311 align->setTransform(res_matrix);
313 ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
314 prop_root, sim_time_sec, load_panel );
316 align->setName(node->getStringValue("name", ""));
317 model->addKid(align);
321 set<ssgBranch *> ignore_branches;
322 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
323 for (i = 0; i < animation_nodes.size(); i++) {
324 const char * name = animation_nodes[i]->getStringValue("name", 0);
325 vector<SGPropertyNode_ptr> name_nodes =
326 animation_nodes[i]->getChildren("object-name");
327 sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
328 sim_time_sec, ignore_branches);
331 #if PLIB_VERSION > 183
332 if ( model != 0 && sgUseDisplayList ) {
333 makeDList( model, ignore_branches );
339 vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
340 for (i = 0; i < panel_nodes.size(); i++) {
341 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
342 ssgEntity * panel = load_panel(panel_nodes[i]);
343 if (panel_nodes[i]->hasValue("name"))
344 panel->setName((char *)panel_nodes[i]->getStringValue("name"));
345 model->addKid(panel);
349 return alignmainmodel;
353 sgSetModelFilter( bool filter )
355 bool old = model_filter;
356 model_filter = filter;