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);
171 animation = new SGNullAnimation(node);
172 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
176 animation->setName((char *)name);
179 if (name_nodes.size() > 0) {
180 object = find_named_node(model, name_nodes[0]->getStringValue());
182 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name_nodes[0]->getStringValue()
191 if ( animation == 0 )
194 ssgBranch * branch = animation->getBranch();
195 splice_branch(branch, object);
197 for (unsigned int i = 1; i < name_nodes.size(); i++) {
198 const char * name = name_nodes[i]->getStringValue();
199 object = find_named_node(model, name);
201 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
205 ssgBranch * oldParent = object->getParent(0);
206 branch->addKid(object);
207 oldParent->removeKid(object);
211 if ( animation != 0 ) {
213 branch->setUserData(animation);
214 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
215 branch->setTravCallback(SSG_CALLBACK_POSTTRAV, restore_callback);
217 ignore_branches.insert( branch );
223 static void makeDList( ssgBranch *b, const set<ssgBranch *> &ignore )
225 int nb = b->getNumKids();
226 for (int i = 0; i<nb; i++) {
227 ssgEntity *e = b->getKid(i);
228 if (e->isAKindOf(ssgTypeLeaf())) {
229 if( ((ssgLeaf*)e)->getNumVertices() > 0)
230 ((ssgLeaf*)e)->makeDList();
231 } else if (e->isAKindOf(ssgTypeBranch()) && ignore.find((ssgBranch *)e) == ignore.end()) {
232 makeDList( (ssgBranch*)e, ignore );
239 ////////////////////////////////////////////////////////////////////////
241 ////////////////////////////////////////////////////////////////////////
244 sgLoad3DModel( const string &fg_root, const string &path,
245 SGPropertyNode *prop_root,
246 double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *) )
248 ssgBranch * model = 0;
249 SGPropertyNode props;
251 // Load the 3D aircraft object itself
252 SGPath modelpath = path, texturepath = path;
253 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
254 SGPath tmp = fg_root;
255 tmp.append(modelpath.str());
256 modelpath = texturepath = tmp;
259 // Check for an XML wrapper
260 if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
261 readProperties(modelpath.str(), &props);
262 if (props.hasValue("/path")) {
263 modelpath = modelpath.dir();
264 modelpath.append(props.getStringValue("/path"));
265 if (props.hasValue("/texture-path")) {
266 texturepath = texturepath.dir();
267 texturepath.append(props.getStringValue("/texture-path"));
271 model = new ssgBranch;
275 // Assume that textures are in
276 // the same location as the XML file.
278 if (texturepath.extension() != "")
279 texturepath = texturepath.dir();
281 ssgTexturePath((char *)texturepath.c_str());
282 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
284 throw sg_exception("Failed to load 3D model");
286 // Set up the alignment node
287 ssgTransform * alignmainmodel = new ssgTransform;
288 if ( load_panel == 0 )
289 alignmainmodel->setTravCallback( SSG_CALLBACK_PRETRAV, model_filter_callback );
290 alignmainmodel->addKid(model);
292 sgMakeOffsetsMatrix(&res_matrix,
293 props.getFloatValue("/offsets/heading-deg", 0.0),
294 props.getFloatValue("/offsets/roll-deg", 0.0),
295 props.getFloatValue("/offsets/pitch-deg", 0.0),
296 props.getFloatValue("/offsets/x-m", 0.0),
297 props.getFloatValue("/offsets/y-m", 0.0),
298 props.getFloatValue("/offsets/z-m", 0.0));
299 alignmainmodel->setTransform(res_matrix);
304 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
305 for (i = 0; i < model_nodes.size(); i++) {
306 SGPropertyNode_ptr node = model_nodes[i];
307 ssgTransform * align = new ssgTransform;
309 sgMakeOffsetsMatrix(&res_matrix,
310 node->getFloatValue("offsets/heading-deg", 0.0),
311 node->getFloatValue("offsets/roll-deg", 0.0),
312 node->getFloatValue("offsets/pitch-deg", 0.0),
313 node->getFloatValue("offsets/x-m", 0.0),
314 node->getFloatValue("offsets/y-m", 0.0),
315 node->getFloatValue("offsets/z-m", 0.0));
316 align->setTransform(res_matrix);
318 ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
319 prop_root, sim_time_sec, load_panel );
321 align->setName(node->getStringValue("name", ""));
322 model->addKid(align);
327 vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
328 for (i = 0; i < panel_nodes.size(); i++) {
329 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
330 ssgEntity * panel = load_panel(panel_nodes[i]);
331 if (panel_nodes[i]->hasValue("name"))
332 panel->setName((char *)panel_nodes[i]->getStringValue("name"));
333 model->addKid(panel);
337 set<ssgBranch *> ignore_branches;
338 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
339 for (i = 0; i < animation_nodes.size(); i++) {
340 const char * name = animation_nodes[i]->getStringValue("name", 0);
341 vector<SGPropertyNode_ptr> name_nodes =
342 animation_nodes[i]->getChildren("object-name");
343 sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
344 sim_time_sec, texturepath, ignore_branches);
347 #if PLIB_VERSION > 183
348 if ( model != 0 && sgUseDisplayList ) {
349 makeDList( model, ignore_branches );
354 return alignmainmodel;
358 sgSetModelFilter( bool filter )
360 bool old = model_filter;
361 model_filter = filter;
366 sgCheckAnimationBranch (ssgEntity * entity)
368 return entity->getTravCallback(SSG_CALLBACK_PRETRAV) == animation_callback;