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