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 ////////////////////////////////////////////////////////////////////////
33 // Static utility functions.
34 ////////////////////////////////////////////////////////////////////////
37 * Callback to update an animation.
40 animation_callback (ssgEntity * entity, int mask)
42 ((SGAnimation *)entity->getUserData())->update();
48 * Locate a named SSG node in a branch.
51 find_named_node (ssgEntity * node, const char * name)
53 char * node_name = node->getName();
54 if (node_name != 0 && !strcmp(name, node_name))
56 else if (node->isAKindOf(ssgTypeBranch())) {
57 int nKids = node->getNumKids();
58 for (int i = 0; i < nKids; i++) {
60 find_named_node(((ssgBranch*)node)->getKid(i), name);
69 * Splice a branch in between all child nodes and their parents.
72 splice_branch (ssgBranch * branch, ssgEntity * child)
74 int nParents = child->getNumParents();
75 branch->addKid(child);
76 for (int i = 0; i < nParents; i++) {
77 ssgBranch * parent = child->getParent(i);
78 parent->replaceKid(child, branch);
83 * Make an offset matrix from rotations and position offset.
86 sgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
87 double x_off, double y_off, double z_off )
91 sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
92 sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
93 sgMultMat4(*result, pos_matrix, rot_matrix);
98 sgMakeAnimation( ssgBranch * model,
100 vector<SGPropertyNode_ptr> &name_nodes,
101 SGPropertyNode *prop_root,
102 SGPropertyNode_ptr node,
103 double sim_time_sec )
105 SGAnimation * animation = 0;
106 const char * type = node->getStringValue("type", "none");
107 if (!strcmp("none", type)) {
108 animation = new SGNullAnimation(node);
109 } else if (!strcmp("range", type)) {
110 animation = new SGRangeAnimation(node);
111 } else if (!strcmp("billboard", type)) {
112 animation = new SGBillboardAnimation(node);
113 } else if (!strcmp("select", type)) {
114 animation = new SGSelectAnimation(prop_root, node);
115 } else if (!strcmp("spin", type)) {
116 animation = new SGSpinAnimation(prop_root, node, sim_time_sec );
117 } else if (!strcmp("timed", type)) {
118 animation = new SGTimedAnimation(node);
119 } else if (!strcmp("rotate", type)) {
120 animation = new SGRotateAnimation(prop_root, node);
121 } else if (!strcmp("translate", type)) {
122 animation = new SGTranslateAnimation(prop_root, node);
123 } else if (!strcmp("scale", type)) {
124 animation = new SGScaleAnimation(prop_root, node);
125 } else if (!strcmp("texrotate", type)) {
126 animation = new SGTexRotateAnimation(prop_root, node);
127 } else if (!strcmp("textranslate", type)) {
128 animation = new SGTexTranslateAnimation(prop_root, node);
129 } else if (!strcmp("texmultiple", type)) {
130 animation = new SGTexMultipleAnimation(prop_root, node);
131 } else if (!strcmp("blend", type)) {
132 animation = new SGBlendAnimation(prop_root, node);
134 animation = new SGNullAnimation(node);
135 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
139 animation->setName((char *)name);
142 if (name_nodes.size() > 0) {
143 object = find_named_node(model, name_nodes[0]->getStringValue());
145 SG_LOG(SG_INPUT, SG_WARN, "Object " << name_nodes[0]->getStringValue()
154 ssgBranch * branch = animation->getBranch();
155 splice_branch(branch, object);
157 for (unsigned int i = 1; i < name_nodes.size(); i++) {
158 const char * name = name_nodes[i]->getStringValue();
159 object = find_named_node(model, name);
161 SG_LOG(SG_INPUT, SG_WARN, "Object " << name << " not found");
165 ssgBranch * oldParent = object->getParent(0);
166 branch->addKid(object);
167 oldParent->removeKid(object);
171 branch->setUserData(animation);
172 branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
178 ////////////////////////////////////////////////////////////////////////
180 ////////////////////////////////////////////////////////////////////////
183 sgLoad3DModel( const string &fg_root, const string &path,
184 SGPropertyNode *prop_root,
185 double sim_time_sec )
187 ssgBranch * model = 0;
188 SGPropertyNode props;
190 // Load the 3D aircraft object itself
191 SGPath modelpath = path;
192 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
193 SGPath tmp = fg_root;
194 tmp.append(modelpath.str());
198 // Check for an XML wrapper
199 if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
200 readProperties(modelpath.str(), &props);
201 if (props.hasValue("/path")) {
202 modelpath = modelpath.dir();
203 modelpath.append(props.getStringValue("/path"));
206 model = new ssgBranch;
210 // Assume that textures are in
211 // the same location as the XML file.
213 ssgTexturePath((char *)modelpath.dir().c_str());
214 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
216 throw sg_exception("Failed to load 3D model");
219 // Set up the alignment node
220 ssgTransform * alignmainmodel = new ssgTransform;
221 alignmainmodel->addKid(model);
223 sgMakeOffsetsMatrix(&res_matrix,
224 props.getFloatValue("/offsets/heading-deg", 0.0),
225 props.getFloatValue("/offsets/roll-deg", 0.0),
226 props.getFloatValue("/offsets/pitch-deg", 0.0),
227 props.getFloatValue("/offsets/x-m", 0.0),
228 props.getFloatValue("/offsets/y-m", 0.0),
229 props.getFloatValue("/offsets/z-m", 0.0));
230 alignmainmodel->setTransform(res_matrix);
235 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
236 for (i = 0; i < animation_nodes.size(); i++) {
237 const char * name = animation_nodes[i]->getStringValue("name", 0);
238 vector<SGPropertyNode_ptr> name_nodes =
239 animation_nodes[i]->getChildren("object-name");
240 sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
245 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
246 for (i = 0; i < model_nodes.size(); i++) {
247 SGPropertyNode_ptr node = model_nodes[i];
248 ssgTransform * align = new ssgTransform;
250 sgMakeOffsetsMatrix(&res_matrix,
251 node->getFloatValue("offsets/heading-deg", 0.0),
252 node->getFloatValue("offsets/roll-deg", 0.0),
253 node->getFloatValue("offsets/pitch-deg", 0.0),
254 node->getFloatValue("offsets/x-m", 0.0),
255 node->getFloatValue("offsets/y-m", 0.0),
256 node->getFloatValue("offsets/z-m", 0.0));
257 align->setTransform(res_matrix);
259 ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
260 prop_root, sim_time_sec );
262 model->addKid(align);
265 return alignmainmodel;