]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/model.cxx
f8740adc0e7767eeb6b378593b7249e491657e2e
[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_ALERT, "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_ALERT, "Object " << name << " not found");
193           delete animation;
194           animation = 0;
195       } else {
196           ssgBranch * oldParent = object->getParent(0);
197           branch->addKid(object);
198           oldParent->removeKid(object);
199       }
200   }
201
202   if ( animation != 0 ) {
203     animation->init();
204     branch->setUserData(animation);
205     branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
206     branch->setTravCallback(SSG_CALLBACK_POSTTRAV, restore_callback);
207   }
208 }
209
210
211 static void makeDList( ssgBranch *b )
212 {
213   int nb = b->getNumKids();
214   for (int i = 0; i<nb; i++) {
215     ssgEntity *e = b->getKid(i);
216     if (e->isAKindOf(ssgTypeLeaf())) {
217       ((ssgLeaf*)e)->makeDList();
218     } else if (e->isAKindOf(ssgTypeBranch())) {
219       makeDList( (ssgBranch*)e );
220     }
221   }
222 }
223
224
225 \f
226 ////////////////////////////////////////////////////////////////////////
227 // Global functions.
228 ////////////////////////////////////////////////////////////////////////
229
230 ssgBranch *
231 sgLoad3DModel( const string &fg_root, const string &path,
232                SGPropertyNode *prop_root,
233                double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *) )
234 {
235   ssgBranch * model = 0;
236   SGPropertyNode props;
237
238                                 // Load the 3D aircraft object itself
239   SGPath modelpath = path, texturepath = path;
240   if ( !ulIsAbsolutePathName( path.c_str() ) ) {
241     SGPath tmp = fg_root;
242     tmp.append(modelpath.str());
243     modelpath = texturepath = tmp;
244   }
245
246                                 // Check for an XML wrapper
247   if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
248     readProperties(modelpath.str(), &props);
249     if (props.hasValue("/path")) {
250       modelpath = modelpath.dir();
251       modelpath.append(props.getStringValue("/path"));
252       if (props.hasValue("/texture-path")) {
253         texturepath = texturepath.dir();
254         texturepath.append(props.getStringValue("/texture-path"));
255       }
256     } else {
257       if (model == 0)
258         model = new ssgBranch;
259     }
260   }
261
262                                 // Assume that textures are in
263                                 // the same location as the XML file.
264   if (model == 0) {
265     if (texturepath.extension() != "")
266           texturepath = texturepath.dir();
267
268     ssgTexturePath((char *)texturepath.c_str());
269     model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
270     if (model == 0)
271       throw sg_exception("Failed to load 3D model");
272   }
273
274                                 // Set up the alignment node
275   ssgTransform * alignmainmodel = new ssgTransform;
276   if ( load_panel == 0 )
277     alignmainmodel->setTravCallback( SSG_CALLBACK_PRETRAV, model_filter_callback );
278   alignmainmodel->addKid(model);
279   sgMat4 res_matrix;
280   sgMakeOffsetsMatrix(&res_matrix,
281                       props.getFloatValue("/offsets/heading-deg", 0.0),
282                       props.getFloatValue("/offsets/roll-deg", 0.0),
283                       props.getFloatValue("/offsets/pitch-deg", 0.0),
284                       props.getFloatValue("/offsets/x-m", 0.0),
285                       props.getFloatValue("/offsets/y-m", 0.0),
286                       props.getFloatValue("/offsets/z-m", 0.0));
287   alignmainmodel->setTransform(res_matrix);
288
289   unsigned int i;
290
291   if ( load_panel ) {
292                                 // Load panels
293     vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
294     for (i = 0; i < panel_nodes.size(); i++) {
295         SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
296         ssgEntity * panel = load_panel(panel_nodes[i]);
297         if (panel_nodes[i]->hasValue("name"))
298             panel->setName((char *)panel_nodes[i]->getStringValue("name"));
299         model->addKid(panel);
300     }
301   }
302
303                                 // Load sub-models
304   vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
305   for (i = 0; i < model_nodes.size(); i++) {
306     SGPropertyNode_ptr node = model_nodes[i];
307     ssgTransform * align = new ssgTransform;
308     sgMat4 res_matrix;
309     sgMakeOffsetsMatrix(&res_matrix,
310                         node->getFloatValue("offsets/heading-deg", 0.0),
311                         node->getFloatValue("offsets/roll-deg", 0.0),
312                         node->getFloatValue("offsets/pitch-deg", 0.0),
313                         node->getFloatValue("offsets/x-m", 0.0),
314                         node->getFloatValue("offsets/y-m", 0.0),
315                         node->getFloatValue("offsets/z-m", 0.0));
316     align->setTransform(res_matrix);
317
318     ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
319                                      prop_root, sim_time_sec, load_panel );
320     align->addKid(kid);
321     align->setName(node->getStringValue("name", ""));
322     model->addKid(align);
323   }
324
325                                 // Load animations
326   vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
327   for (i = 0; i < animation_nodes.size(); i++) {
328     const char * name = animation_nodes[i]->getStringValue("name", 0);
329     vector<SGPropertyNode_ptr> name_nodes =
330       animation_nodes[i]->getChildren("object-name");
331     sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
332                      sim_time_sec);
333   }
334
335   if ( model != 0 ) {
336     makeDList( model );
337   }
338
339   return alignmainmodel;
340 }
341
342 bool
343 sgSetModelFilter( bool filter )
344 {
345   bool old = model_filter;
346   model_filter = filter;
347   return old;
348 }
349
350
351 // end of model.cxx