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