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/props/props.hxx>
23 #include <simgear/props/props_io.hxx>
25 #include "animation.hxx"
31 bool sgUseDisplayList = true;
33 ////////////////////////////////////////////////////////////////////////
35 ////////////////////////////////////////////////////////////////////////
40 ////////////////////////////////////////////////////////////////////////
41 // Static utility functions.
42 ////////////////////////////////////////////////////////////////////////
45 model_filter_callback (ssgEntity * entity, int mask)
47 return model_filter ? 1 : 0;
51 * Callback to update an animation.
54 animation_callback (ssgEntity * entity, int mask)
56 return ((SGAnimation *)entity->getUserData())->update();
60 * Callback to restore the state after an animation.
63 restore_callback (ssgEntity * entity, int mask)
65 ((SGAnimation *)entity->getUserData())->restore();
71 * Locate a named SSG node in a branch.
74 find_named_node (ssgEntity * node, const char * name)
76 char * node_name = node->getName();
77 if (node_name != 0 && !strcmp(name, node_name))
79 else if (node->isAKindOf(ssgTypeBranch())) {
80 int nKids = node->getNumKids();
81 for (int i = 0; i < nKids; i++) {
83 find_named_node(((ssgBranch*)node)->getKid(i), name);
92 * Splice a branch in between all child nodes and their parents.
95 splice_branch (ssgBranch * branch, ssgEntity * child)
97 int nParents = child->getNumParents();
98 branch->addKid(child);
99 for (int i = 0; i < nParents; i++) {
100 ssgBranch * parent = child->getParent(i);
101 parent->replaceKid(child, branch);
106 * Make an offset matrix from rotations and position offset.
109 sgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
110 double x_off, double y_off, double z_off )
114 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
115 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
116 sgMultMat4(*result, pos_matrix, rot_matrix);
121 sgMakeAnimation( ssgBranch * model,
123 vector<SGPropertyNode_ptr> &name_nodes,
124 SGPropertyNode *prop_root,
125 SGPropertyNode_ptr node,
127 SGPath &texture_path,
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("material", type)) {
163 animation = new SGMaterialAnimation(prop_root, node, texture_path);
164 } else if (!strcmp("flash", type)) {
165 animation = new SGFlashAnimation(node);
166 } else if (!strcmp("dist-scale", type)) {
167 animation = new SGDistScaleAnimation(node);
168 } else if (!strcmp("noshadow", type)) {
169 animation = new SGShadowAnimation(prop_root, node);
170 } else if (!strcmp("shader", type)) {
171 animation = new SGShaderAnimation(prop_root, node);
173 animation = new SGNullAnimation(node);
174 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
178 animation->setName((char *)name);
181 if (name_nodes.size() > 0) {
182 object = find_named_node(model, name_nodes[0]->getStringValue());
184 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name_nodes[0]->getStringValue()
193 if ( animation == 0 )
196 ssgBranch * branch = animation->getBranch();
197 splice_branch(branch, object);
199 for (unsigned int i = 1; i < name_nodes.size(); i++) {
200 const char * name = name_nodes[i]->getStringValue();
201 object = find_named_node(model, name);
203 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
207 ssgBranch * oldParent = object->getParent(0);
208 branch->addKid(object);
209 oldParent->removeKid(object);
213 if ( animation != 0 ) {
215 branch->setUserData(animation);
216 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
217 branch->setTravCallback(SSG_CALLBACK_POSTTRAV, restore_callback);
219 ignore_branches.insert( branch );
225 static void makeDList( ssgBranch *b, const set<ssgBranch *> &ignore )
227 int nb = b->getNumKids();
228 for (int i = 0; i<nb; i++) {
229 ssgEntity *e = b->getKid(i);
230 if (e->isAKindOf(ssgTypeLeaf())) {
231 if( ((ssgLeaf*)e)->getNumVertices() > 0)
232 ((ssgLeaf*)e)->makeDList();
233 } else if (e->isAKindOf(ssgTypeBranch()) && ignore.find((ssgBranch *)e) == ignore.end()) {
234 makeDList( (ssgBranch*)e, ignore );
241 ////////////////////////////////////////////////////////////////////////
243 ////////////////////////////////////////////////////////////////////////
246 sgLoad3DModel( const string &fg_root, const string &path,
247 SGPropertyNode *prop_root,
248 double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *),
251 ssgBranch * model = 0;
252 SGPropertyNode props;
254 // Load the 3D aircraft object itself
255 SGPath modelpath = path, texturepath = path;
256 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
257 SGPath tmp = fg_root;
258 tmp.append(modelpath.str());
259 modelpath = texturepath = tmp;
262 // Check for an XML wrapper
263 if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
264 readProperties(modelpath.str(), &props);
265 if (props.hasValue("/path")) {
266 modelpath = modelpath.dir();
267 modelpath.append(props.getStringValue("/path"));
268 if (props.hasValue("/texture-path")) {
269 texturepath = texturepath.dir();
270 texturepath.append(props.getStringValue("/texture-path"));
274 model = new ssgBranch;
278 // Assume that textures are in
279 // the same location as the XML file.
281 if (texturepath.extension() != "")
282 texturepath = texturepath.dir();
284 ssgTexturePath((char *)texturepath.c_str());
285 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
287 throw sg_io_exception("Failed to load 3D model",
288 sg_location(modelpath.str()));
290 // Set up the alignment node
291 ssgTransform * alignmainmodel = new ssgTransform;
292 if ( load_panel == 0 )
293 alignmainmodel->setTravCallback( SSG_CALLBACK_PRETRAV, model_filter_callback );
294 alignmainmodel->addKid(model);
296 sgMakeOffsetsMatrix(&res_matrix,
297 props.getFloatValue("/offsets/heading-deg", 0.0),
298 props.getFloatValue("/offsets/roll-deg", 0.0),
299 props.getFloatValue("/offsets/pitch-deg", 0.0),
300 props.getFloatValue("/offsets/x-m", 0.0),
301 props.getFloatValue("/offsets/y-m", 0.0),
302 props.getFloatValue("/offsets/z-m", 0.0));
303 alignmainmodel->setTransform(res_matrix);
308 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
309 for (i = 0; i < model_nodes.size(); i++) {
310 SGPropertyNode_ptr node = model_nodes[i];
311 ssgTransform * align = new ssgTransform;
313 sgMakeOffsetsMatrix(&res_matrix,
314 node->getFloatValue("offsets/heading-deg", 0.0),
315 node->getFloatValue("offsets/roll-deg", 0.0),
316 node->getFloatValue("offsets/pitch-deg", 0.0),
317 node->getFloatValue("offsets/x-m", 0.0),
318 node->getFloatValue("offsets/y-m", 0.0),
319 node->getFloatValue("offsets/z-m", 0.0));
320 align->setTransform(res_matrix);
322 ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
323 prop_root, sim_time_sec, load_panel );
325 align->setName(node->getStringValue("name", ""));
326 model->addKid(align);
331 vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
332 for (i = 0; i < panel_nodes.size(); i++) {
333 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
334 ssgEntity * panel = load_panel(panel_nodes[i]);
335 if (panel_nodes[i]->hasValue("name"))
336 panel->setName((char *)panel_nodes[i]->getStringValue("name"));
337 model->addKid(panel);
342 model->setUserData(data);
343 data->modelLoaded(path, &props, model);
346 set<ssgBranch *> ignore_branches;
347 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
348 for (i = 0; i < animation_nodes.size(); i++) {
349 const char * name = animation_nodes[i]->getStringValue("name", 0);
350 vector<SGPropertyNode_ptr> name_nodes =
351 animation_nodes[i]->getChildren("object-name");
352 sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
353 sim_time_sec, texturepath, ignore_branches);
356 #if PLIB_VERSION > 183
357 if ( model != 0 && sgUseDisplayList ) {
358 makeDList( model, ignore_branches );
362 int m = props.getIntValue("dump", 0);
364 model->print(stderr, "", m - 1);
366 return alignmainmodel;
370 sgSetModelFilter( bool filter )
372 bool old = model_filter;
373 model_filter = filter;
378 sgCheckAnimationBranch (ssgEntity * entity)
380 return entity->getTravCallback(SSG_CALLBACK_PRETRAV) == animation_callback;