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