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 ((SGAnimation *)entity->getUserData())->update();
61 * Locate a named SSG node in a branch.
64 find_named_node (ssgEntity * node, const char * name)
66 char * node_name = node->getName();
67 if (node_name != 0 && !strcmp(name, node_name))
69 else if (node->isAKindOf(ssgTypeBranch())) {
70 int nKids = node->getNumKids();
71 for (int i = 0; i < nKids; i++) {
73 find_named_node(((ssgBranch*)node)->getKid(i), name);
82 * Splice a branch in between all child nodes and their parents.
85 splice_branch (ssgBranch * branch, ssgEntity * child)
87 int nParents = child->getNumParents();
88 branch->addKid(child);
89 for (int i = 0; i < nParents; i++) {
90 ssgBranch * parent = child->getParent(i);
91 parent->replaceKid(child, branch);
96 * Make an offset matrix from rotations and position offset.
99 sgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
100 double x_off, double y_off, double z_off )
104 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
105 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
106 sgMultMat4(*result, pos_matrix, rot_matrix);
111 sgMakeAnimation( ssgBranch * model,
113 vector<SGPropertyNode_ptr> &name_nodes,
114 SGPropertyNode *prop_root,
115 SGPropertyNode_ptr node,
116 double sim_time_sec )
118 SGAnimation * animation = 0;
119 const char * type = node->getStringValue("type", "none");
120 if (!strcmp("none", type)) {
121 animation = new SGNullAnimation(node);
122 } else if (!strcmp("range", type)) {
123 animation = new SGRangeAnimation(prop_root, node);
124 } else if (!strcmp("billboard", type)) {
125 animation = new SGBillboardAnimation(node);
126 } else if (!strcmp("select", type)) {
127 animation = new SGSelectAnimation(prop_root, node);
128 } else if (!strcmp("spin", type)) {
129 animation = new SGSpinAnimation(prop_root, node, sim_time_sec );
130 } else if (!strcmp("timed", type)) {
131 animation = new SGTimedAnimation(node);
132 } else if (!strcmp("rotate", type)) {
133 animation = new SGRotateAnimation(prop_root, node);
134 } else if (!strcmp("translate", type)) {
135 animation = new SGTranslateAnimation(prop_root, node);
136 } else if (!strcmp("scale", type)) {
137 animation = new SGScaleAnimation(prop_root, node);
138 } else if (!strcmp("texrotate", type)) {
139 animation = new SGTexRotateAnimation(prop_root, node);
140 } else if (!strcmp("textranslate", type)) {
141 animation = new SGTexTranslateAnimation(prop_root, node);
142 } else if (!strcmp("texmultiple", type)) {
143 animation = new SGTexMultipleAnimation(prop_root, node);
144 } else if (!strcmp("blend", type)) {
145 animation = new SGBlendAnimation(prop_root, node);
146 } else if (!strcmp("alpha-test", type)) {
147 animation = new SGAlphaTestAnimation(node);
149 animation = new SGNullAnimation(node);
150 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
154 animation->setName((char *)name);
157 if (name_nodes.size() > 0) {
158 object = find_named_node(model, name_nodes[0]->getStringValue());
160 SG_LOG(SG_INPUT, SG_WARN, "Object " << name_nodes[0]->getStringValue()
169 if ( animation == 0 )
172 ssgBranch * branch = animation->getBranch();
173 splice_branch(branch, object);
175 for (unsigned int i = 1; i < name_nodes.size(); i++) {
176 const char * name = name_nodes[i]->getStringValue();
177 object = find_named_node(model, name);
179 SG_LOG(SG_INPUT, SG_WARN, "Object " << name << " not found");
183 ssgBranch * oldParent = object->getParent(0);
184 branch->addKid(object);
185 oldParent->removeKid(object);
189 branch->setUserData(animation);
190 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
196 ////////////////////////////////////////////////////////////////////////
198 ////////////////////////////////////////////////////////////////////////
201 sgLoad3DModel( const string &fg_root, const string &path,
202 SGPropertyNode *prop_root,
203 double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *) )
205 ssgBranch * model = 0;
206 SGPropertyNode props;
208 // Load the 3D aircraft object itself
209 SGPath modelpath = path;
210 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
211 SGPath tmp = fg_root;
212 tmp.append(modelpath.str());
216 // Check for an XML wrapper
217 if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
218 readProperties(modelpath.str(), &props);
219 if (props.hasValue("/path")) {
220 modelpath = modelpath.dir();
221 modelpath.append(props.getStringValue("/path"));
224 model = new ssgBranch;
228 // Assume that textures are in
229 // the same location as the XML file.
231 ssgTexturePath((char *)modelpath.dir().c_str());
232 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
234 throw sg_exception("Failed to load 3D model");
237 // Set up the alignment node
238 ssgTransform * alignmainmodel = new ssgTransform;
239 if ( load_panel == 0 )
240 alignmainmodel->setTravCallback( SSG_CALLBACK_PRETRAV, model_filter_callback );
241 alignmainmodel->addKid(model);
243 sgMakeOffsetsMatrix(&res_matrix,
244 props.getFloatValue("/offsets/heading-deg", 0.0),
245 props.getFloatValue("/offsets/roll-deg", 0.0),
246 props.getFloatValue("/offsets/pitch-deg", 0.0),
247 props.getFloatValue("/offsets/x-m", 0.0),
248 props.getFloatValue("/offsets/y-m", 0.0),
249 props.getFloatValue("/offsets/z-m", 0.0));
250 alignmainmodel->setTransform(res_matrix);
256 vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
257 for (i = 0; i < panel_nodes.size(); i++) {
258 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
259 ssgEntity * panel = load_panel(panel_nodes[i]);
260 if (panel_nodes[i]->hasValue("name"))
261 panel->setName((char *)panel_nodes[i]->getStringValue("name"));
262 model->addKid(panel);
267 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
268 for (i = 0; i < model_nodes.size(); i++) {
269 SGPropertyNode_ptr node = model_nodes[i];
270 ssgTransform * align = new ssgTransform;
272 sgMakeOffsetsMatrix(&res_matrix,
273 node->getFloatValue("offsets/heading-deg", 0.0),
274 node->getFloatValue("offsets/roll-deg", 0.0),
275 node->getFloatValue("offsets/pitch-deg", 0.0),
276 node->getFloatValue("offsets/x-m", 0.0),
277 node->getFloatValue("offsets/y-m", 0.0),
278 node->getFloatValue("offsets/z-m", 0.0));
279 align->setTransform(res_matrix);
281 ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
282 prop_root, sim_time_sec, load_panel );
284 align->setName(node->getStringValue("name", ""));
285 model->addKid(align);
289 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
290 for (i = 0; i < animation_nodes.size(); i++) {
291 const char * name = animation_nodes[i]->getStringValue("name", 0);
292 vector<SGPropertyNode_ptr> name_nodes =
293 animation_nodes[i]->getChildren("object-name");
294 sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
298 return alignmainmodel;
302 sgSetModelFilter( bool filter )
304 bool old = model_filter;
305 model_filter = filter;