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