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 <string.h> // for strcmp()
19 #include <simgear/structure/exception.hxx>
20 #include <simgear/props/props.hxx>
21 #include <simgear/props/props_io.hxx>
22 #include <simgear/props/condition.hxx>
24 #include "animation.hxx"
30 bool sgUseDisplayList = true;
32 ////////////////////////////////////////////////////////////////////////
34 ////////////////////////////////////////////////////////////////////////
39 ////////////////////////////////////////////////////////////////////////
40 // Static utility functions.
41 ////////////////////////////////////////////////////////////////////////
44 model_filter_callback (ssgEntity * entity, int mask)
46 return model_filter ? 1 : 0;
50 * Callback to update an animation.
53 animation_callback (ssgEntity * entity, int mask)
55 return ((SGAnimation *)entity->getUserData())->update();
59 * Callback to restore the state after an animation.
62 restore_callback (ssgEntity * entity, int mask)
64 ((SGAnimation *)entity->getUserData())->restore();
69 * Callback and userData class for conditional rendering.
71 class SGConditionalRender : public ssgBase {
73 SGConditionalRender(SGCondition *c) : _condition(c) {}
74 bool test() { return _condition->test(); }
76 SGCondition *_condition;
80 model_condition_callback (ssgEntity * entity, int mask)
82 return ((SGConditionalRender *)entity->getUserData())->test();
87 * Locate a named SSG node in a branch.
90 find_named_node (ssgEntity * node, const char * name)
92 char * node_name = node->getName();
93 if (node_name != 0 && !strcmp(name, node_name))
95 else if (node->isAKindOf(ssgTypeBranch())) {
96 int nKids = node->getNumKids();
97 for (int i = 0; i < nKids; i++) {
99 find_named_node(((ssgBranch*)node)->getKid(i), name);
108 * Splice a branch in between all child nodes and their parents.
111 splice_branch (ssgBranch * branch, ssgEntity * child)
113 int nParents = child->getNumParents();
114 branch->addKid(child);
115 for (int i = 0; i < nParents; i++) {
116 ssgBranch * parent = child->getParent(i);
117 parent->replaceKid(child, branch);
122 * Make an offset matrix from rotations and position offset.
125 sgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
126 double x_off, double y_off, double z_off )
130 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
131 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
132 sgMultMat4(*result, pos_matrix, rot_matrix);
137 sgMakeAnimation( ssgBranch * model,
139 vector<SGPropertyNode_ptr> &name_nodes,
140 SGPropertyNode *prop_root,
141 SGPropertyNode_ptr node,
143 SGPath &texture_path,
144 set<ssgBranch *> &ignore_branches )
147 SGAnimation * animation = 0;
148 const char * type = node->getStringValue("type", "none");
149 if (!strcmp("none", type)) {
150 animation = new SGNullAnimation(node);
151 } else if (!strcmp("range", type)) {
152 animation = new SGRangeAnimation(prop_root, node);
153 } else if (!strcmp("billboard", type)) {
154 animation = new SGBillboardAnimation(node);
155 } else if (!strcmp("select", type)) {
156 animation = new SGSelectAnimation(prop_root, node);
157 } else if (!strcmp("spin", type)) {
158 animation = new SGSpinAnimation(prop_root, node, sim_time_sec );
159 } else if (!strcmp("timed", type)) {
160 animation = new SGTimedAnimation(node);
161 } else if (!strcmp("rotate", type)) {
162 animation = new SGRotateAnimation(prop_root, node);
163 } else if (!strcmp("translate", type)) {
164 animation = new SGTranslateAnimation(prop_root, node);
165 } else if (!strcmp("scale", type)) {
166 animation = new SGScaleAnimation(prop_root, node);
167 } else if (!strcmp("texrotate", type)) {
168 animation = new SGTexRotateAnimation(prop_root, node);
169 } else if (!strcmp("textranslate", type)) {
170 animation = new SGTexTranslateAnimation(prop_root, node);
171 } else if (!strcmp("texmultiple", type)) {
172 animation = new SGTexMultipleAnimation(prop_root, node);
173 } else if (!strcmp("blend", type)) {
174 animation = new SGBlendAnimation(prop_root, node);
176 } else if (!strcmp("alpha-test", type)) {
177 animation = new SGAlphaTestAnimation(node);
178 } else if (!strcmp("material", type)) {
179 animation = new SGMaterialAnimation(prop_root, node, texture_path);
180 } else if (!strcmp("flash", type)) {
181 animation = new SGFlashAnimation(node);
182 } else if (!strcmp("dist-scale", type)) {
183 animation = new SGDistScaleAnimation(node);
184 } else if (!strcmp("noshadow", type)) {
185 animation = new SGShadowAnimation(prop_root, node);
186 } else if (!strcmp("shader", type)) {
187 animation = new SGShaderAnimation(prop_root, node);
189 animation = new SGNullAnimation(node);
190 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
194 animation->setName((char *)name);
197 if (name_nodes.size() > 0) {
198 object = find_named_node(model, name_nodes[0]->getStringValue());
200 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name_nodes[0]->getStringValue()
209 if ( animation == 0 )
212 ssgBranch * branch = animation->getBranch();
213 splice_branch(branch, object);
215 for (unsigned int i = 1; i < name_nodes.size(); i++) {
216 const char * name = name_nodes[i]->getStringValue();
217 object = find_named_node(model, name);
219 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
223 ssgBranch * oldParent = object->getParent(0);
224 branch->addKid(object);
225 oldParent->removeKid(object);
229 if ( animation != 0 ) {
231 branch->setUserData(animation);
232 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
233 branch->setTravCallback(SSG_CALLBACK_POSTTRAV, restore_callback);
235 ignore_branches.insert( branch );
241 static void makeDList( ssgBranch *b, const set<ssgBranch *> &ignore )
243 int nb = b->getNumKids();
244 for (int i = 0; i<nb; i++) {
245 ssgEntity *e = b->getKid(i);
246 if (e->isAKindOf(ssgTypeLeaf())) {
247 if( ((ssgLeaf*)e)->getNumVertices() > 0)
248 ((ssgLeaf*)e)->makeDList();
249 } else if (e->isAKindOf(ssgTypeBranch()) && ignore.find((ssgBranch *)e) == ignore.end()) {
250 makeDList( (ssgBranch*)e, ignore );
255 class SGLoaderOptions : public ssgLoaderOptions {
257 SGLoaderOptions() { ssgSetCurrentOptions( this ); } // Install our own loader options at startup
258 void endLoad() {} // Avoid clearing the texture cache after every model load
261 static SGLoaderOptions loaderOptions;
264 ////////////////////////////////////////////////////////////////////////
266 ////////////////////////////////////////////////////////////////////////
269 sgLoad3DModel( const string &fg_root, const string &path,
270 SGPropertyNode *prop_root,
271 double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *),
274 ssgBranch * model = 0;
275 SGPropertyNode props;
277 // Load the 3D aircraft object itself
278 SGPath modelpath = path, texturepath = path;
279 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
280 SGPath tmp = fg_root;
281 tmp.append(modelpath.str());
282 modelpath = texturepath = tmp;
285 // Check for an XML wrapper
286 if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
287 readProperties(modelpath.str(), &props);
288 if (props.hasValue("/path")) {
289 modelpath = modelpath.dir();
290 modelpath.append(props.getStringValue("/path"));
291 if (props.hasValue("/texture-path")) {
292 texturepath = texturepath.dir();
293 texturepath.append(props.getStringValue("/texture-path"));
297 model = new ssgBranch;
301 // Assume that textures are in
302 // the same location as the XML file.
304 if (texturepath.extension() != "")
305 texturepath = texturepath.dir();
307 ssgTexturePath((char *)texturepath.c_str());
308 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
310 throw sg_io_exception("Failed to load 3D model",
311 sg_location(modelpath.str()));
313 // Set up the alignment node
314 ssgTransform * alignmainmodel = new ssgTransform;
315 if ( load_panel == 0 )
316 alignmainmodel->setTravCallback( SSG_CALLBACK_PRETRAV, model_filter_callback );
317 alignmainmodel->addKid(model);
319 sgMakeOffsetsMatrix(&res_matrix,
320 props.getFloatValue("/offsets/heading-deg", 0.0),
321 props.getFloatValue("/offsets/roll-deg", 0.0),
322 props.getFloatValue("/offsets/pitch-deg", 0.0),
323 props.getFloatValue("/offsets/x-m", 0.0),
324 props.getFloatValue("/offsets/y-m", 0.0),
325 props.getFloatValue("/offsets/z-m", 0.0));
326 alignmainmodel->setTransform(res_matrix);
331 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
332 for (i = 0; i < model_nodes.size(); i++) {
333 SGPropertyNode_ptr node = model_nodes[i];
334 ssgTransform * align = new ssgTransform;
336 sgMakeOffsetsMatrix(&res_matrix,
337 node->getFloatValue("offsets/heading-deg", 0.0),
338 node->getFloatValue("offsets/roll-deg", 0.0),
339 node->getFloatValue("offsets/pitch-deg", 0.0),
340 node->getFloatValue("offsets/x-m", 0.0),
341 node->getFloatValue("offsets/y-m", 0.0),
342 node->getFloatValue("offsets/z-m", 0.0));
343 align->setTransform(res_matrix);
346 const char * submodel = node->getStringValue("path");
348 kid = sgLoad3DModel( fg_root, submodel, prop_root, sim_time_sec, load_panel );
350 } catch (const sg_throwable &t) {
351 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
355 SGPropertyNode *cond = node->getNode("condition", false);
357 align->setUserData(new SGConditionalRender(sgReadCondition(prop_root, cond)));
358 align->setTravCallback(SSG_CALLBACK_PRETRAV, model_condition_callback);
362 align->setName(node->getStringValue("name", ""));
363 model->addKid(align);
368 vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
369 for (i = 0; i < panel_nodes.size(); i++) {
370 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
371 ssgEntity * panel = load_panel(panel_nodes[i]);
372 if (panel_nodes[i]->hasValue("name"))
373 panel->setName((char *)panel_nodes[i]->getStringValue("name"));
374 model->addKid(panel);
379 alignmainmodel->setUserData(data);
380 data->modelLoaded(path, &props, alignmainmodel);
383 set<ssgBranch *> ignore_branches;
384 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
385 for (i = 0; i < animation_nodes.size(); i++) {
386 const char * name = animation_nodes[i]->getStringValue("name", 0);
387 vector<SGPropertyNode_ptr> name_nodes =
388 animation_nodes[i]->getChildren("object-name");
389 sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
390 sim_time_sec, texturepath, ignore_branches);
393 #if PLIB_VERSION > 183
394 if ( model != 0 && sgUseDisplayList ) {
395 makeDList( model, ignore_branches );
399 int m = props.getIntValue("dump", 0);
401 model->print(stderr, "", m - 1);
403 return alignmainmodel;
407 sgSetModelFilter( bool filter )
409 bool old = model_filter;
410 model_filter = filter;
415 sgCheckAnimationBranch (ssgEntity * entity)
417 return entity->getTravCallback(SSG_CALLBACK_PRETRAV) == animation_callback;