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/misc/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"
33 ////////////////////////////////////////////////////////////////////////
34 // Static utility functions.
35 ////////////////////////////////////////////////////////////////////////
38 * Callback to update an animation.
41 animation_callback (ssgEntity * entity, int mask)
43 ((SGAnimation *)entity->getUserData())->update();
49 * Locate a named SSG node in a branch.
52 find_named_node (ssgEntity * node, const char * name)
54 char * node_name = node->getName();
55 if (node_name != 0 && !strcmp(name, node_name))
57 else if (node->isAKindOf(ssgTypeBranch())) {
58 int nKids = node->getNumKids();
59 for (int i = 0; i < nKids; i++) {
61 find_named_node(((ssgBranch*)node)->getKid(i), name);
70 * Splice a branch in between all child nodes and their parents.
73 splice_branch (ssgBranch * branch, ssgEntity * child)
75 int nParents = child->getNumParents();
76 branch->addKid(child);
77 for (int i = 0; i < nParents; i++) {
78 ssgBranch * parent = child->getParent(i);
79 parent->replaceKid(child, branch);
84 * Make an offset matrix from rotations and position offset.
87 sgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
88 double x_off, double y_off, double z_off )
92 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
93 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
94 sgMultMat4(*result, pos_matrix, rot_matrix);
99 sgMakeAnimation( ssgBranch * model,
101 vector<SGPropertyNode_ptr> &name_nodes,
102 SGPropertyNode *prop_root,
103 SGPropertyNode_ptr node,
104 double sim_time_sec )
106 SGAnimation * animation = 0;
107 const char * type = node->getStringValue("type", "none");
108 if (!strcmp("none", type)) {
109 animation = new SGNullAnimation(node);
110 } else if (!strcmp("range", type)) {
111 animation = new SGRangeAnimation(node);
112 } else if (!strcmp("billboard", type)) {
113 animation = new SGBillboardAnimation(node);
114 } else if (!strcmp("select", type)) {
115 animation = new SGSelectAnimation(prop_root, node);
116 } else if (!strcmp("spin", type)) {
117 animation = new SGSpinAnimation(prop_root, node, sim_time_sec );
118 } else if (!strcmp("timed", type)) {
119 animation = new SGTimedAnimation(node);
120 } else if (!strcmp("rotate", type)) {
121 animation = new SGRotateAnimation(prop_root, node);
122 } else if (!strcmp("translate", type)) {
123 animation = new SGTranslateAnimation(prop_root, node);
124 } else if (!strcmp("scale", type)) {
125 animation = new SGScaleAnimation(prop_root, node);
126 } else if (!strcmp("texrotate", type)) {
127 animation = new SGTexRotateAnimation(prop_root, node);
128 } else if (!strcmp("textranslate", type)) {
129 animation = new SGTexTranslateAnimation(prop_root, node);
130 } else if (!strcmp("texmultiple", type)) {
131 animation = new SGTexMultipleAnimation(prop_root, node);
133 animation = new SGNullAnimation(node);
134 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
138 animation->setName((char *)name);
141 if (name_nodes.size() > 0) {
142 object = find_named_node(model, name_nodes[0]->getStringValue());
144 SG_LOG(SG_INPUT, SG_WARN, "Object " << name_nodes[0]->getStringValue()
153 ssgBranch * branch = animation->getBranch();
154 splice_branch(branch, object);
156 for (unsigned int i = 1; i < name_nodes.size(); i++) {
157 const char * name = name_nodes[i]->getStringValue();
158 object = find_named_node(model, name);
160 SG_LOG(SG_INPUT, SG_WARN, "Object " << name << " not found");
164 ssgBranch * oldParent = object->getParent(0);
165 branch->addKid(object);
166 oldParent->removeKid(object);
170 branch->setUserData(animation);
171 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
177 ////////////////////////////////////////////////////////////////////////
179 ////////////////////////////////////////////////////////////////////////
182 sgLoad3DModel( const string &fg_root, const string &path,
183 SGPropertyNode *prop_root,
184 double sim_time_sec )
186 ssgBranch * model = 0;
187 SGPropertyNode props;
189 // Load the 3D aircraft object itself
191 SGPath modelpath = path;
192 if ( ulIsAbsolutePathName( path.c_str() ) ) {
197 xmlpath.append(modelpath.str());
200 // Check for an XML wrapper
201 if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
202 readProperties(xmlpath.str(), &props);
203 if (props.hasValue("/path")) {
204 modelpath = modelpath.dir();
205 modelpath.append(props.getStringValue("/path"));
208 model = new ssgBranch;
212 // Assume that textures are in
213 // the same location as the XML file.
215 ssgTexturePath((char *)xmlpath.dir().c_str());
216 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
218 throw sg_exception("Failed to load 3D model");
221 // Set up the alignment node
222 ssgTransform * alignmainmodel = new ssgTransform;
223 alignmainmodel->addKid(model);
225 sgMakeOffsetsMatrix(&res_matrix,
226 props.getFloatValue("/offsets/heading-deg", 0.0),
227 props.getFloatValue("/offsets/roll-deg", 0.0),
228 props.getFloatValue("/offsets/pitch-deg", 0.0),
229 props.getFloatValue("/offsets/x-m", 0.0),
230 props.getFloatValue("/offsets/y-m", 0.0),
231 props.getFloatValue("/offsets/z-m", 0.0));
232 alignmainmodel->setTransform(res_matrix);
237 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
238 for (i = 0; i < animation_nodes.size(); i++) {
239 const char * name = animation_nodes[i]->getStringValue("name", 0);
240 vector<SGPropertyNode_ptr> name_nodes =
241 animation_nodes[i]->getChildren("object-name");
242 sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
247 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
248 for (i = 0; i < model_nodes.size(); i++) {
249 SGPropertyNode_ptr node = model_nodes[i];
250 ssgTransform * align = new ssgTransform;
252 sgMakeOffsetsMatrix(&res_matrix,
253 node->getFloatValue("offsets/heading-deg", 0.0),
254 node->getFloatValue("offsets/roll-deg", 0.0),
255 node->getFloatValue("offsets/pitch-deg", 0.0),
256 node->getFloatValue("offsets/x-m", 0.0),
257 node->getFloatValue("offsets/y-m", 0.0),
258 node->getFloatValue("offsets/z-m", 0.0));
259 align->setTransform(res_matrix);
261 ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
262 prop_root, sim_time_sec );
264 model->addKid(align);
267 return alignmainmodel;