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