]> git.mxchange.org Git - flightgear.git/blob - src/Model/model_panel.cxx
ignore resets for now because every z/Z key press would trigger a call to NOAA. We...
[flightgear.git] / src / Model / model_panel.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 <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 #include <simgear/scene/model/animation.hxx>
25 #include <simgear/scene/model/model.hxx>
26
27 #include "panelnode.hxx"
28
29 #include "model_panel.hxx"
30
31 SG_USING_STD(vector);
32
33
34 \f
35 ////////////////////////////////////////////////////////////////////////
36 // Global functions.
37 ////////////////////////////////////////////////////////////////////////
38
39 // FIXME: should attempt to share more of the code with
40 // fgLoad3DModel() since these routines are almost identical with the
41 // exception of the panel node section.
42
43 ssgBranch *
44 fgLoad3DModelPanel( const string &fg_root, const string &path,
45                     SGPropertyNode *prop_root,
46                     double sim_time_sec )
47 {
48   ssgBranch * model = 0;
49   SGPropertyNode props;
50
51                                 // Load the 3D aircraft object itself
52   SGPath modelpath = path;
53   if ( !ulIsAbsolutePathName( path.c_str() ) ) {
54     SGPath tmp = fg_root;
55     tmp.append(modelpath.str());
56     modelpath = tmp;
57   }
58
59                                 // Check for an XML wrapper
60   if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
61     readProperties(modelpath.str(), &props);
62     if (props.hasValue("/path")) {
63       modelpath = modelpath.dir();
64       modelpath.append(props.getStringValue("/path"));
65     } else {
66       if (model == 0)
67         model = new ssgBranch;
68     }
69   }
70
71                                 // Assume that textures are in
72                                 // the same location as the XML file.
73   if (model == 0) {
74     ssgTexturePath((char *)modelpath.dir().c_str());
75     model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
76     if (model == 0)
77       throw sg_exception("Failed to load 3D model");
78   }
79
80                                 // Set up the alignment node
81   ssgTransform * alignmainmodel = new ssgTransform;
82   alignmainmodel->addKid(model);
83   sgMat4 res_matrix;
84   sgMakeOffsetsMatrix(&res_matrix,
85                       props.getFloatValue("/offsets/heading-deg", 0.0),
86                       props.getFloatValue("/offsets/roll-deg", 0.0),
87                       props.getFloatValue("/offsets/pitch-deg", 0.0),
88                       props.getFloatValue("/offsets/x-m", 0.0),
89                       props.getFloatValue("/offsets/y-m", 0.0),
90                       props.getFloatValue("/offsets/z-m", 0.0));
91   alignmainmodel->setTransform(res_matrix);
92
93   unsigned int i;
94
95                                 // Load panels
96   vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
97   for (i = 0; i < panel_nodes.size(); i++) {
98     SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
99     FGPanelNode * panel = new FGPanelNode(panel_nodes[i]);
100     if (panel_nodes[i]->hasValue("name"))
101         panel->setName((char *)panel_nodes[i]->getStringValue("name"));
102     model->addKid(panel);
103   }
104
105                                 // Load animations
106   vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
107   for (i = 0; i < animation_nodes.size(); i++) {
108     const char * name = animation_nodes[i]->getStringValue("name", 0);
109     vector<SGPropertyNode_ptr> name_nodes =
110       animation_nodes[i]->getChildren("object-name");
111     sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
112                      sim_time_sec);
113   }
114
115                                 // Load sub-models
116   vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
117   for (i = 0; i < model_nodes.size(); i++) {
118     SGPropertyNode_ptr node = model_nodes[i];
119     ssgTransform * align = new ssgTransform;
120     sgMat4 res_matrix;
121     sgMakeOffsetsMatrix(&res_matrix,
122                         node->getFloatValue("offsets/heading-deg", 0.0),
123                         node->getFloatValue("offsets/roll-deg", 0.0),
124                         node->getFloatValue("offsets/pitch-deg", 0.0),
125                         node->getFloatValue("offsets/x-m", 0.0),
126                         node->getFloatValue("offsets/y-m", 0.0),
127                         node->getFloatValue("offsets/z-m", 0.0));
128     align->setTransform(res_matrix);
129
130     ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
131                                      prop_root, sim_time_sec );
132     align->addKid(kid);
133     model->addKid(align);
134   }
135
136   return alignmainmodel;
137 }
138
139
140 // end of model_panel.cxx