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