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.
6 #include <simgear/compiler.h>
8 #include <string.h> // for strcmp()
16 #include <simgear/misc/exception.hxx>
17 #include <simgear/misc/sg_path.hxx>
18 #include <simgear/props/props.hxx>
19 #include <simgear/props/props_io.hxx>
20 #include <simgear/scene/model/animation.hxx>
28 ////////////////////////////////////////////////////////////////////////
29 // Static utility functions.
30 ////////////////////////////////////////////////////////////////////////
33 * Callback to update an animation.
36 animation_callback (ssgEntity * entity, int mask)
38 ((Animation *)entity->getUserData())->update();
44 * Locate a named SSG node in a branch.
47 find_named_node (ssgEntity * node, const char * name)
49 char * node_name = node->getName();
50 if (node_name != 0 && !strcmp(name, node_name))
52 else if (node->isAKindOf(ssgTypeBranch())) {
53 int nKids = node->getNumKids();
54 for (int i = 0; i < nKids; i++) {
56 find_named_node(((ssgBranch*)node)->getKid(i), name);
65 * Splice a branch in between all child nodes and their parents.
68 splice_branch (ssgBranch * branch, ssgEntity * child)
70 int nParents = child->getNumParents();
71 branch->addKid(child);
72 for (int i = 0; i < nParents; i++) {
73 ssgBranch * parent = child->getParent(i);
74 parent->replaceKid(child, branch);
79 * Make an offset matrix from rotations and position offset.
82 fgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
83 double x_off, double y_off, double z_off )
87 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
88 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
89 sgMultMat4(*result, pos_matrix, rot_matrix);
94 fgMakeAnimation( ssgBranch * model,
96 vector<SGPropertyNode_ptr> &name_nodes,
97 SGPropertyNode *prop_root,
98 SGPropertyNode_ptr node,
101 Animation * animation = 0;
102 const char * type = node->getStringValue("type", "none");
103 if (!strcmp("none", type)) {
104 animation = new NullAnimation(node);
105 } else if (!strcmp("range", type)) {
106 animation = new RangeAnimation(node);
107 } else if (!strcmp("billboard", type)) {
108 animation = new BillboardAnimation(node);
109 } else if (!strcmp("select", type)) {
110 animation = new SelectAnimation(prop_root, node);
111 } else if (!strcmp("spin", type)) {
112 animation = new SpinAnimation(prop_root, node, sim_time_sec );
113 } else if (!strcmp("timed", type)) {
114 animation = new TimedAnimation(node);
115 } else if (!strcmp("rotate", type)) {
116 animation = new RotateAnimation(prop_root, node);
117 } else if (!strcmp("translate", type)) {
118 animation = new TranslateAnimation(prop_root, node);
120 animation = new NullAnimation(node);
121 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
125 animation->setName((char *)name);
128 if (name_nodes.size() > 0) {
129 object = find_named_node(model, name_nodes[0]->getStringValue());
131 SG_LOG(SG_INPUT, SG_WARN, "Object " << name_nodes[0]->getStringValue()
140 ssgBranch * branch = animation->getBranch();
141 splice_branch(branch, object);
143 for (unsigned int i = 1; i < name_nodes.size(); i++) {
144 const char * name = name_nodes[i]->getStringValue();
145 object = find_named_node(model, name);
147 SG_LOG(SG_INPUT, SG_WARN, "Object " << name << " not found");
151 ssgBranch * oldParent = object->getParent(0);
152 branch->addKid(object);
153 oldParent->removeKid(object);
157 branch->setUserData(animation);
158 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
164 ////////////////////////////////////////////////////////////////////////
166 ////////////////////////////////////////////////////////////////////////
169 fgLoad3DModel( const string &fg_root, const string &path,
170 SGPropertyNode *prop_root,
171 double sim_time_sec )
173 ssgBranch * model = 0;
174 SGPropertyNode props;
176 // Load the 3D aircraft object itself
178 SGPath modelpath = path;
179 if ( ulIsAbsolutePathName( path.c_str() ) ) {
184 xmlpath.append(modelpath.str());
187 // Check for an XML wrapper
188 if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
189 readProperties(xmlpath.str(), &props);
190 if (props.hasValue("/path")) {
191 modelpath = modelpath.dir();
192 modelpath.append(props.getStringValue("/path"));
195 model = new ssgBranch;
199 // Assume that textures are in
200 // the same location as the XML file.
202 ssgTexturePath((char *)xmlpath.dir().c_str());
203 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
205 throw sg_exception("Failed to load 3D model");
208 // Set up the alignment node
209 ssgTransform * alignmainmodel = new ssgTransform;
210 alignmainmodel->addKid(model);
212 fgMakeOffsetsMatrix(&res_matrix,
213 props.getFloatValue("/offsets/heading-deg", 0.0),
214 props.getFloatValue("/offsets/roll-deg", 0.0),
215 props.getFloatValue("/offsets/pitch-deg", 0.0),
216 props.getFloatValue("/offsets/x-m", 0.0),
217 props.getFloatValue("/offsets/y-m", 0.0),
218 props.getFloatValue("/offsets/z-m", 0.0));
219 alignmainmodel->setTransform(res_matrix);
224 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
225 for (i = 0; i < animation_nodes.size(); i++) {
226 const char * name = animation_nodes[i]->getStringValue("name", 0);
227 vector<SGPropertyNode_ptr> name_nodes =
228 animation_nodes[i]->getChildren("object-name");
229 fgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
234 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
235 for (i = 0; i < model_nodes.size(); i++) {
236 SGPropertyNode_ptr node = model_nodes[i];
237 ssgTransform * align = new ssgTransform;
239 fgMakeOffsetsMatrix(&res_matrix,
240 node->getFloatValue("offsets/heading-deg", 0.0),
241 node->getFloatValue("offsets/roll-deg", 0.0),
242 node->getFloatValue("offsets/pitch-deg", 0.0),
243 node->getFloatValue("offsets/x-m", 0.0),
244 node->getFloatValue("offsets/y-m", 0.0),
245 node->getFloatValue("offsets/z-m", 0.0));
246 align->setTransform(res_matrix);
248 ssgBranch * kid = fgLoad3DModel( fg_root, node->getStringValue("path"),
249 prop_root, sim_time_sec );
251 model->addKid(align);
254 return alignmainmodel;