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()
20 #include <simgear/structure/exception.hxx>
21 #include <simgear/misc/sg_path.hxx>
22 #include <simgear/props/props.hxx>
23 #include <simgear/props/props_io.hxx>
25 #include "animation.hxx"
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();
70 * Locate a named SSG node in a branch.
73 find_named_node (ssgEntity * node, const char * name)
75 char * node_name = node->getName();
76 if (node_name != 0 && !strcmp(name, node_name))
78 else if (node->isAKindOf(ssgTypeBranch())) {
79 int nKids = node->getNumKids();
80 for (int i = 0; i < nKids; i++) {
82 find_named_node(((ssgBranch*)node)->getKid(i), name);
91 * Splice a branch in between all child nodes and their parents.
94 splice_branch (ssgBranch * branch, ssgEntity * child)
96 int nParents = child->getNumParents();
97 branch->addKid(child);
98 for (int i = 0; i < nParents; i++) {
99 ssgBranch * parent = child->getParent(i);
100 parent->replaceKid(child, branch);
105 * Make an offset matrix from rotations and position offset.
108 sgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
109 double x_off, double y_off, double z_off )
113 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
114 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
115 sgMultMat4(*result, pos_matrix, rot_matrix);
120 sgMakeAnimation( ssgBranch * model,
122 vector<SGPropertyNode_ptr> &name_nodes,
123 SGPropertyNode *prop_root,
124 SGPropertyNode_ptr node,
125 double sim_time_sec )
127 SGAnimation * animation = 0;
128 const char * type = node->getStringValue("type", "none");
129 if (!strcmp("none", type)) {
130 animation = new SGNullAnimation(node);
131 } else if (!strcmp("range", type)) {
132 animation = new SGRangeAnimation(prop_root, node);
133 } else if (!strcmp("billboard", type)) {
134 animation = new SGBillboardAnimation(node);
135 } else if (!strcmp("select", type)) {
136 animation = new SGSelectAnimation(prop_root, node);
137 } else if (!strcmp("spin", type)) {
138 animation = new SGSpinAnimation(prop_root, node, sim_time_sec );
139 } else if (!strcmp("timed", type)) {
140 animation = new SGTimedAnimation(node);
141 } else if (!strcmp("rotate", type)) {
142 animation = new SGRotateAnimation(prop_root, node);
143 } else if (!strcmp("translate", type)) {
144 animation = new SGTranslateAnimation(prop_root, node);
145 } else if (!strcmp("scale", type)) {
146 animation = new SGScaleAnimation(prop_root, node);
147 } else if (!strcmp("texrotate", type)) {
148 animation = new SGTexRotateAnimation(prop_root, node);
149 } else if (!strcmp("textranslate", type)) {
150 animation = new SGTexTranslateAnimation(prop_root, node);
151 } else if (!strcmp("texmultiple", type)) {
152 animation = new SGTexMultipleAnimation(prop_root, node);
153 } else if (!strcmp("blend", type)) {
154 animation = new SGBlendAnimation(prop_root, node);
155 } else if (!strcmp("alpha-test", type)) {
156 animation = new SGAlphaTestAnimation(node);
157 } else if (!strcmp("flash", type)) {
158 animation = new SGFlashAnimation(node);
159 } else if (!strcmp("dist-scale", type)) {
160 animation = new SGDistScaleAnimation(node);
162 animation = new SGNullAnimation(node);
163 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
167 animation->setName((char *)name);
170 if (name_nodes.size() > 0) {
171 object = find_named_node(model, name_nodes[0]->getStringValue());
173 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name_nodes[0]->getStringValue()
182 if ( animation == 0 )
185 ssgBranch * branch = animation->getBranch();
186 splice_branch(branch, object);
188 for (unsigned int i = 1; i < name_nodes.size(); i++) {
189 const char * name = name_nodes[i]->getStringValue();
190 object = find_named_node(model, name);
192 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
196 ssgBranch * oldParent = object->getParent(0);
197 branch->addKid(object);
198 oldParent->removeKid(object);
202 if ( animation != 0 ) {
204 branch->setUserData(animation);
205 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
206 branch->setTravCallback(SSG_CALLBACK_POSTTRAV, restore_callback);
213 ////////////////////////////////////////////////////////////////////////
215 ////////////////////////////////////////////////////////////////////////
218 sgLoad3DModel( const string &fg_root, const string &path,
219 SGPropertyNode *prop_root,
220 double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *) )
222 ssgBranch * model = 0;
223 SGPropertyNode props;
225 // Load the 3D aircraft object itself
226 SGPath modelpath = path, texturepath = path;
227 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
228 SGPath tmp = fg_root;
229 tmp.append(modelpath.str());
230 modelpath = texturepath = tmp;
233 // Check for an XML wrapper
234 if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
235 readProperties(modelpath.str(), &props);
236 if (props.hasValue("/path")) {
237 modelpath = modelpath.dir();
238 modelpath.append(props.getStringValue("/path"));
239 if (props.hasValue("/texture-path")) {
240 texturepath = texturepath.dir();
241 texturepath.append(props.getStringValue("/texture-path"));
245 model = new ssgBranch;
249 // Assume that textures are in
250 // the same location as the XML file.
252 if (texturepath.extension() != "")
253 texturepath = texturepath.dir();
255 ssgTexturePath((char *)texturepath.c_str());
256 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
258 throw sg_exception("Failed to load 3D model");
261 // Set up the alignment node
262 ssgTransform * alignmainmodel = new ssgTransform;
263 if ( load_panel == 0 )
264 alignmainmodel->setTravCallback( SSG_CALLBACK_PRETRAV, model_filter_callback );
265 alignmainmodel->addKid(model);
267 sgMakeOffsetsMatrix(&res_matrix,
268 props.getFloatValue("/offsets/heading-deg", 0.0),
269 props.getFloatValue("/offsets/roll-deg", 0.0),
270 props.getFloatValue("/offsets/pitch-deg", 0.0),
271 props.getFloatValue("/offsets/x-m", 0.0),
272 props.getFloatValue("/offsets/y-m", 0.0),
273 props.getFloatValue("/offsets/z-m", 0.0));
274 alignmainmodel->setTransform(res_matrix);
280 vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
281 for (i = 0; i < panel_nodes.size(); i++) {
282 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
283 ssgEntity * panel = load_panel(panel_nodes[i]);
284 if (panel_nodes[i]->hasValue("name"))
285 panel->setName((char *)panel_nodes[i]->getStringValue("name"));
286 model->addKid(panel);
291 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
292 for (i = 0; i < model_nodes.size(); i++) {
293 SGPropertyNode_ptr node = model_nodes[i];
294 ssgTransform * align = new ssgTransform;
296 sgMakeOffsetsMatrix(&res_matrix,
297 node->getFloatValue("offsets/heading-deg", 0.0),
298 node->getFloatValue("offsets/roll-deg", 0.0),
299 node->getFloatValue("offsets/pitch-deg", 0.0),
300 node->getFloatValue("offsets/x-m", 0.0),
301 node->getFloatValue("offsets/y-m", 0.0),
302 node->getFloatValue("offsets/z-m", 0.0));
303 align->setTransform(res_matrix);
305 ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
306 prop_root, sim_time_sec, load_panel );
308 align->setName(node->getStringValue("name", ""));
309 model->addKid(align);
313 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
314 for (i = 0; i < animation_nodes.size(); i++) {
315 const char * name = animation_nodes[i]->getStringValue("name", 0);
316 vector<SGPropertyNode_ptr> name_nodes =
317 animation_nodes[i]->getChildren("object-name");
318 sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
322 return alignmainmodel;
326 sgSetModelFilter( bool filter )
328 bool old = model_filter;
329 model_filter = filter;