]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/model.cxx
Add a blend animation
[simgear.git] / simgear / scene / model / model.cxx
1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #include <simgear_config.h>
8 #endif
9
10 #include <simgear/compiler.h>
11
12 #include <string.h>             // for strcmp()
13
14 #include <vector>
15
16 #include <plib/sg.h>
17 #include <plib/ssg.h>
18 #include <plib/ul.h>
19
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>
24
25 #include "animation.hxx"
26
27 #include "model.hxx"
28
29 SG_USING_STD(vector);
30
31
32 \f
33 ////////////////////////////////////////////////////////////////////////
34 // Static utility functions.
35 ////////////////////////////////////////////////////////////////////////
36
37 /**
38  * Callback to update an animation.
39  */
40 static int
41 animation_callback (ssgEntity * entity, int mask)
42 {
43     ((SGAnimation *)entity->getUserData())->update();
44     return true;
45 }
46
47
48 /**
49  * Locate a named SSG node in a branch.
50  */
51 static ssgEntity *
52 find_named_node (ssgEntity * node, const char * name)
53 {
54   char * node_name = node->getName();
55   if (node_name != 0 && !strcmp(name, node_name))
56     return node;
57   else if (node->isAKindOf(ssgTypeBranch())) {
58     int nKids = node->getNumKids();
59     for (int i = 0; i < nKids; i++) {
60       ssgEntity * result =
61         find_named_node(((ssgBranch*)node)->getKid(i), name);
62       if (result != 0)
63         return result;
64     }
65   } 
66   return 0;
67 }
68
69 /**
70  * Splice a branch in between all child nodes and their parents.
71  */
72 static void
73 splice_branch (ssgBranch * branch, ssgEntity * child)
74 {
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);
80   }
81 }
82
83 /**
84  * Make an offset matrix from rotations and position offset.
85  */
86 void
87 sgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
88                      double x_off, double y_off, double z_off )
89 {
90   sgMat4 rot_matrix;
91   sgMat4 pos_matrix;
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);
95 }
96
97
98 void
99 sgMakeAnimation( ssgBranch * model,
100                  const char * name,
101                  vector<SGPropertyNode_ptr> &name_nodes,
102                  SGPropertyNode *prop_root,
103                  SGPropertyNode_ptr node,
104                  double sim_time_sec )
105 {
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);
132   } else if (!strcmp("blend", type)) {
133     animation = new SGBlendAnimation(prop_root, node);
134   } else {
135     animation = new SGNullAnimation(node);
136     SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
137   }
138
139   if (name != 0)
140       animation->setName((char *)name);
141
142   ssgEntity * object;
143   if (name_nodes.size() > 0) {
144     object = find_named_node(model, name_nodes[0]->getStringValue());
145     if (object == 0) {
146       SG_LOG(SG_INPUT, SG_WARN, "Object " << name_nodes[0]->getStringValue()
147              << " not found");
148       delete animation;
149       animation = 0;
150     }
151   } else {
152     object = model;
153   }
154   
155   ssgBranch * branch = animation->getBranch();
156   splice_branch(branch, object);
157
158   for (unsigned int i = 1; i < name_nodes.size(); i++) {
159       const char * name = name_nodes[i]->getStringValue();
160       object = find_named_node(model, name);
161       if (object == 0) {
162           SG_LOG(SG_INPUT, SG_WARN, "Object " << name << " not found");
163           delete animation;
164           animation = 0;
165       }
166       ssgBranch * oldParent = object->getParent(0);
167       branch->addKid(object);
168       oldParent->removeKid(object);
169   }
170
171   animation->init();
172   branch->setUserData(animation);
173   branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
174 }
175
176
177
178 \f
179 ////////////////////////////////////////////////////////////////////////
180 // Global functions.
181 ////////////////////////////////////////////////////////////////////////
182
183 ssgBranch *
184 sgLoad3DModel( const string &fg_root, const string &path,
185                SGPropertyNode *prop_root,
186                double sim_time_sec )
187 {
188   ssgBranch * model = 0;
189   SGPropertyNode props;
190
191                                 // Load the 3D aircraft object itself
192   SGPath xmlpath;
193   SGPath modelpath = path;
194   if ( ulIsAbsolutePathName( path.c_str() ) ) {
195     xmlpath = modelpath;
196   }
197   else {
198     xmlpath = fg_root;
199     xmlpath.append(modelpath.str());
200   }
201
202                                 // Check for an XML wrapper
203   if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
204     readProperties(xmlpath.str(), &props);
205     if (props.hasValue("/path")) {
206       modelpath = modelpath.dir();
207       modelpath.append(props.getStringValue("/path"));
208     } else {
209       if (model == 0)
210         model = new ssgBranch;
211     }
212   }
213
214                                 // Assume that textures are in
215                                 // the same location as the XML file.
216   if (model == 0) {
217     ssgTexturePath((char *)xmlpath.dir().c_str());
218     model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
219     if (model == 0)
220       throw sg_exception("Failed to load 3D model");
221   }
222
223                                 // Set up the alignment node
224   ssgTransform * alignmainmodel = new ssgTransform;
225   alignmainmodel->addKid(model);
226   sgMat4 res_matrix;
227   sgMakeOffsetsMatrix(&res_matrix,
228                       props.getFloatValue("/offsets/heading-deg", 0.0),
229                       props.getFloatValue("/offsets/roll-deg", 0.0),
230                       props.getFloatValue("/offsets/pitch-deg", 0.0),
231                       props.getFloatValue("/offsets/x-m", 0.0),
232                       props.getFloatValue("/offsets/y-m", 0.0),
233                       props.getFloatValue("/offsets/z-m", 0.0));
234   alignmainmodel->setTransform(res_matrix);
235
236   unsigned int i;
237
238                                 // Load animations
239   vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
240   for (i = 0; i < animation_nodes.size(); i++) {
241     const char * name = animation_nodes[i]->getStringValue("name", 0);
242     vector<SGPropertyNode_ptr> name_nodes =
243       animation_nodes[i]->getChildren("object-name");
244     sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
245                      sim_time_sec);
246   }
247
248                                 // Load sub-models
249   vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
250   for (i = 0; i < model_nodes.size(); i++) {
251     SGPropertyNode_ptr node = model_nodes[i];
252     ssgTransform * align = new ssgTransform;
253     sgMat4 res_matrix;
254     sgMakeOffsetsMatrix(&res_matrix,
255                         node->getFloatValue("offsets/heading-deg", 0.0),
256                         node->getFloatValue("offsets/roll-deg", 0.0),
257                         node->getFloatValue("offsets/pitch-deg", 0.0),
258                         node->getFloatValue("offsets/x-m", 0.0),
259                         node->getFloatValue("offsets/y-m", 0.0),
260                         node->getFloatValue("offsets/z-m", 0.0));
261     align->setTransform(res_matrix);
262
263     ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
264                                      prop_root, sim_time_sec );
265     align->addKid(kid);
266     model->addKid(align);
267   }
268
269   return alignmainmodel;
270 }
271
272
273 // end of model.cxx