]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/model.cxx
Jim Wilson:
[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 #include <set>
16
17 #include <plib/sg.h>
18 #include <plib/ssg.h>
19 #include <plib/ul.h>
20
21 #include <simgear/structure/exception.hxx>
22 #include <simgear/misc/sg_path.hxx>
23 #include <simgear/props/props.hxx>
24 #include <simgear/props/props_io.hxx>
25
26 #include "animation.hxx"
27 #include "model.hxx"
28
29 SG_USING_STD(vector);
30 SG_USING_STD(set);
31
32 bool sgUseDisplayList = true;
33 \f
34 ////////////////////////////////////////////////////////////////////////
35 // Global state
36 ////////////////////////////////////////////////////////////////////////
37 static bool
38 model_filter = true;
39
40 \f
41 ////////////////////////////////////////////////////////////////////////
42 // Static utility functions.
43 ////////////////////////////////////////////////////////////////////////
44
45 static int
46 model_filter_callback (ssgEntity * entity, int mask)
47 {
48   return model_filter ? 1 : 0;
49 }
50
51 /**
52  * Callback to update an animation.
53  */
54 static int
55 animation_callback (ssgEntity * entity, int mask)
56 {
57     return ((SGAnimation *)entity->getUserData())->update();
58 }
59
60 /**
61  * Callback to restore the state after an animation.
62  */
63 static int
64 restore_callback (ssgEntity * entity, int mask)
65 {
66     ((SGAnimation *)entity->getUserData())->restore();
67     return 1;
68 }
69
70
71 /**
72  * Locate a named SSG node in a branch.
73  */
74 static ssgEntity *
75 find_named_node (ssgEntity * node, const char * name)
76 {
77   char * node_name = node->getName();
78   if (node_name != 0 && !strcmp(name, node_name))
79     return node;
80   else if (node->isAKindOf(ssgTypeBranch())) {
81     int nKids = node->getNumKids();
82     for (int i = 0; i < nKids; i++) {
83       ssgEntity * result =
84         find_named_node(((ssgBranch*)node)->getKid(i), name);
85       if (result != 0)
86         return result;
87     }
88   } 
89   return 0;
90 }
91
92 /**
93  * Splice a branch in between all child nodes and their parents.
94  */
95 static void
96 splice_branch (ssgBranch * branch, ssgEntity * child)
97 {
98   int nParents = child->getNumParents();
99   branch->addKid(child);
100   for (int i = 0; i < nParents; i++) {
101     ssgBranch * parent = child->getParent(i);
102     parent->replaceKid(child, branch);
103   }
104 }
105
106 /**
107  * Make an offset matrix from rotations and position offset.
108  */
109 void
110 sgMakeOffsetsMatrix( sgMat4 * result, double h_rot, double p_rot, double r_rot,
111                      double x_off, double y_off, double z_off )
112 {
113   sgMat4 rot_matrix;
114   sgMat4 pos_matrix;
115   sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
116   sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
117   sgMultMat4(*result, pos_matrix, rot_matrix);
118 }
119
120
121 void
122 sgMakeAnimation( ssgBranch * model,
123                  const char * name,
124                  vector<SGPropertyNode_ptr> &name_nodes,
125                  SGPropertyNode *prop_root,
126                  SGPropertyNode_ptr node,
127                  double sim_time_sec,
128                  set<ssgBranch *> &ignore_branches )
129 {
130   bool ignore = false;
131   SGAnimation * animation = 0;
132   const char * type = node->getStringValue("type", "none");
133   if (!strcmp("none", type)) {
134     animation = new SGNullAnimation(node);
135   } else if (!strcmp("range", type)) {
136     animation = new SGRangeAnimation(prop_root, node);
137   } else if (!strcmp("billboard", type)) {
138     animation = new SGBillboardAnimation(node);
139   } else if (!strcmp("select", type)) {
140     animation = new SGSelectAnimation(prop_root, node);
141   } else if (!strcmp("spin", type)) {
142     animation = new SGSpinAnimation(prop_root, node, sim_time_sec );
143   } else if (!strcmp("timed", type)) {
144     animation = new SGTimedAnimation(node);
145   } else if (!strcmp("rotate", type)) {
146     animation = new SGRotateAnimation(prop_root, node);
147   } else if (!strcmp("translate", type)) {
148     animation = new SGTranslateAnimation(prop_root, node);
149   } else if (!strcmp("scale", type)) {
150     animation = new SGScaleAnimation(prop_root, node);
151   } else if (!strcmp("texrotate", type)) {
152     animation = new SGTexRotateAnimation(prop_root, node);
153   } else if (!strcmp("textranslate", type)) {
154     animation = new SGTexTranslateAnimation(prop_root, node);
155   } else if (!strcmp("texmultiple", type)) {
156     animation = new SGTexMultipleAnimation(prop_root, node);
157   } else if (!strcmp("blend", type)) {
158     animation = new SGBlendAnimation(prop_root, node);
159     ignore = true;
160   } else if (!strcmp("alpha-test", type)) {
161     animation = new SGAlphaTestAnimation(node);
162   } else if (!strcmp("material-emission", type)) {
163     animation = new SGEmissionAnimation(prop_root, node);
164   } else if (!strcmp("flash", type)) {
165     animation = new SGFlashAnimation(node);
166   } else if (!strcmp("dist-scale", type)) {
167     animation = new SGDistScaleAnimation(node);
168   } else {
169     animation = new SGNullAnimation(node);
170     SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
171   }
172
173   if (name != 0)
174       animation->setName((char *)name);
175
176   ssgEntity * object;
177   if (name_nodes.size() > 0) {
178     object = find_named_node(model, name_nodes[0]->getStringValue());
179     if (object == 0) {
180       SG_LOG(SG_INPUT, SG_ALERT, "Object " << name_nodes[0]->getStringValue()
181              << " not found");
182       delete animation;
183       animation = 0;
184     }
185   } else {
186     object = model;
187   }
188
189   if ( animation == 0 )
190      return;
191   
192   ssgBranch * branch = animation->getBranch();
193   splice_branch(branch, object);
194
195   for (unsigned int i = 1; i < name_nodes.size(); i++) {
196       const char * name = name_nodes[i]->getStringValue();
197       object = find_named_node(model, name);
198       if (object == 0) {
199           SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
200           delete animation;
201           animation = 0;
202       } else {
203           ssgBranch * oldParent = object->getParent(0);
204           branch->addKid(object);
205           oldParent->removeKid(object);
206       }
207   }
208
209   if ( animation != 0 ) {
210     animation->init();
211     branch->setUserData(animation);
212     branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
213     branch->setTravCallback(SSG_CALLBACK_POSTTRAV, restore_callback);
214     if ( ignore ) {
215       ignore_branches.insert( branch );
216     }
217   }
218 }
219
220
221 static void makeDList( ssgBranch *b, const set<ssgBranch *> &ignore )
222 {
223   int nb = b->getNumKids();
224   for (int i = 0; i<nb; i++) {
225     ssgEntity *e = b->getKid(i);
226     if (e->isAKindOf(ssgTypeLeaf())) {
227       ((ssgLeaf*)e)->makeDList();
228     } else if (e->isAKindOf(ssgTypeBranch()) && ignore.find((ssgBranch *)e) == ignore.end()) {
229       makeDList( (ssgBranch*)e, ignore );
230     }
231   }
232 }
233
234
235 \f
236 ////////////////////////////////////////////////////////////////////////
237 // Global functions.
238 ////////////////////////////////////////////////////////////////////////
239
240 ssgBranch *
241 sgLoad3DModel( const string &fg_root, const string &path,
242                SGPropertyNode *prop_root,
243                double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *) )
244 {
245   ssgBranch * model = 0;
246   SGPropertyNode props;
247
248                                 // Load the 3D aircraft object itself
249   SGPath modelpath = path, texturepath = path;
250   if ( !ulIsAbsolutePathName( path.c_str() ) ) {
251     SGPath tmp = fg_root;
252     tmp.append(modelpath.str());
253     modelpath = texturepath = tmp;
254   }
255
256                                 // Check for an XML wrapper
257   if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
258     readProperties(modelpath.str(), &props);
259     if (props.hasValue("/path")) {
260       modelpath = modelpath.dir();
261       modelpath.append(props.getStringValue("/path"));
262       if (props.hasValue("/texture-path")) {
263         texturepath = texturepath.dir();
264         texturepath.append(props.getStringValue("/texture-path"));
265       }
266     } else {
267       if (model == 0)
268         model = new ssgBranch;
269     }
270   }
271
272                                 // Assume that textures are in
273                                 // the same location as the XML file.
274   if (model == 0) {
275     if (texturepath.extension() != "")
276           texturepath = texturepath.dir();
277
278     ssgTexturePath((char *)texturepath.c_str());
279     model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
280     if (model == 0)
281       throw sg_exception("Failed to load 3D model");
282   }
283                                 // Set up the alignment node
284   ssgTransform * alignmainmodel = new ssgTransform;
285   if ( load_panel == 0 )
286     alignmainmodel->setTravCallback( SSG_CALLBACK_PRETRAV, model_filter_callback );
287   alignmainmodel->addKid(model);
288   sgMat4 res_matrix;
289   sgMakeOffsetsMatrix(&res_matrix,
290                       props.getFloatValue("/offsets/heading-deg", 0.0),
291                       props.getFloatValue("/offsets/roll-deg", 0.0),
292                       props.getFloatValue("/offsets/pitch-deg", 0.0),
293                       props.getFloatValue("/offsets/x-m", 0.0),
294                       props.getFloatValue("/offsets/y-m", 0.0),
295                       props.getFloatValue("/offsets/z-m", 0.0));
296   alignmainmodel->setTransform(res_matrix);
297
298   unsigned int i;
299
300                                 // Load sub-models
301   vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
302   for (i = 0; i < model_nodes.size(); i++) {
303     SGPropertyNode_ptr node = model_nodes[i];
304     ssgTransform * align = new ssgTransform;
305     sgMat4 res_matrix;
306     sgMakeOffsetsMatrix(&res_matrix,
307                         node->getFloatValue("offsets/heading-deg", 0.0),
308                         node->getFloatValue("offsets/roll-deg", 0.0),
309                         node->getFloatValue("offsets/pitch-deg", 0.0),
310                         node->getFloatValue("offsets/x-m", 0.0),
311                         node->getFloatValue("offsets/y-m", 0.0),
312                         node->getFloatValue("offsets/z-m", 0.0));
313     align->setTransform(res_matrix);
314
315     ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
316                                      prop_root, sim_time_sec, load_panel );
317     align->addKid(kid);
318     align->setName(node->getStringValue("name", ""));
319     model->addKid(align);
320   }
321
322                                 // Load animations
323   set<ssgBranch *> ignore_branches;
324   vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
325   for (i = 0; i < animation_nodes.size(); i++) {
326     const char * name = animation_nodes[i]->getStringValue("name", 0);
327     vector<SGPropertyNode_ptr> name_nodes =
328       animation_nodes[i]->getChildren("object-name");
329     sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
330                      sim_time_sec, ignore_branches);
331   }
332
333 #if PLIB_VERSION > 183
334   if ( model != 0 && sgUseDisplayList ) {
335      makeDList( model, ignore_branches );
336   }
337 #endif
338
339   if ( load_panel ) {
340                                 // Load panels
341     vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
342     for (i = 0; i < panel_nodes.size(); i++) {
343         SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
344         ssgEntity * panel = load_panel(panel_nodes[i]);
345         if (panel_nodes[i]->hasValue("name"))
346             panel->setName((char *)panel_nodes[i]->getStringValue("name"));
347         model->addKid(panel);
348     }
349   }
350
351   return alignmainmodel;
352 }
353
354 bool
355 sgSetModelFilter( bool filter )
356 {
357   bool old = model_filter;
358   model_filter = filter;
359   return old;
360 }
361
362
363 // end of model.cxx