]> git.mxchange.org Git - flightgear.git/blob - src/Model/model_panel.cxx
Roy Vegard Ovesen:
[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 sub-models
106   vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
107   for (i = 0; i < model_nodes.size(); i++) {
108     SGPropertyNode_ptr node = model_nodes[i];
109     ssgTransform * align = new ssgTransform;
110     sgMat4 res_matrix;
111     sgMakeOffsetsMatrix(&res_matrix,
112                         node->getFloatValue("offsets/heading-deg", 0.0),
113                         node->getFloatValue("offsets/roll-deg", 0.0),
114                         node->getFloatValue("offsets/pitch-deg", 0.0),
115                         node->getFloatValue("offsets/x-m", 0.0),
116                         node->getFloatValue("offsets/y-m", 0.0),
117                         node->getFloatValue("offsets/z-m", 0.0));
118     align->setTransform(res_matrix);
119
120     ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
121                                      prop_root, sim_time_sec );
122     align->addKid(kid);
123     align->setName(node->getStringValue("name", ""));
124     model->addKid(align);
125   }
126
127                                 // Load animations
128   vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
129   for (i = 0; i < animation_nodes.size(); i++) {
130     const char * name = animation_nodes[i]->getStringValue("name", 0);
131     vector<SGPropertyNode_ptr> name_nodes =
132       animation_nodes[i]->getChildren("object-name");
133     sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
134                      sim_time_sec);
135   }
136
137   return alignmainmodel;
138 }
139
140
141 // end of model_panel.cxx