]> git.mxchange.org Git - flightgear.git/blob - src/Model/model_panel.cxx
Moved src/Model/loader.[ch]xx and src/Model/model.[ch]xx to
[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/misc/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 xmlpath;
53   SGPath modelpath = path;
54   if ( ulIsAbsolutePathName( path.c_str() ) ) {
55     xmlpath = modelpath;
56   }
57   else {
58     xmlpath = fg_root;
59     xmlpath.append(modelpath.str());
60   }
61
62                                 // Check for an XML wrapper
63   if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
64     readProperties(xmlpath.str(), &props);
65     if (props.hasValue("/path")) {
66       modelpath = modelpath.dir();
67       modelpath.append(props.getStringValue("/path"));
68     } else {
69       if (model == 0)
70         model = new ssgBranch;
71     }
72   }
73
74                                 // Assume that textures are in
75                                 // the same location as the XML file.
76   if (model == 0) {
77     ssgTexturePath((char *)xmlpath.dir().c_str());
78     model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
79     if (model == 0)
80       throw sg_exception("Failed to load 3D model");
81   }
82
83                                 // Set up the alignment node
84   ssgTransform * alignmainmodel = new ssgTransform;
85   alignmainmodel->addKid(model);
86   sgMat4 res_matrix;
87   fgMakeOffsetsMatrix(&res_matrix,
88                       props.getFloatValue("/offsets/heading-deg", 0.0),
89                       props.getFloatValue("/offsets/roll-deg", 0.0),
90                       props.getFloatValue("/offsets/pitch-deg", 0.0),
91                       props.getFloatValue("/offsets/x-m", 0.0),
92                       props.getFloatValue("/offsets/y-m", 0.0),
93                       props.getFloatValue("/offsets/z-m", 0.0));
94   alignmainmodel->setTransform(res_matrix);
95
96   unsigned int i;
97
98                                 // Load panels
99   vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
100   for (i = 0; i < panel_nodes.size(); i++) {
101     SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
102     FGPanelNode * panel = new FGPanelNode(panel_nodes[i]);
103     if (panel_nodes[i]->hasValue("name"))
104         panel->setName((char *)panel_nodes[i]->getStringValue("name"));
105     model->addKid(panel);
106   }
107
108                                 // Load animations
109   vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
110   for (i = 0; i < animation_nodes.size(); i++) {
111     const char * name = animation_nodes[i]->getStringValue("name", 0);
112     vector<SGPropertyNode_ptr> name_nodes =
113       animation_nodes[i]->getChildren("object-name");
114     fgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
115                      sim_time_sec);
116   }
117
118                                 // Load sub-models
119   vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
120   for (i = 0; i < model_nodes.size(); i++) {
121     SGPropertyNode_ptr node = model_nodes[i];
122     ssgTransform * align = new ssgTransform;
123     sgMat4 res_matrix;
124     fgMakeOffsetsMatrix(&res_matrix,
125                         node->getFloatValue("offsets/heading-deg", 0.0),
126                         node->getFloatValue("offsets/roll-deg", 0.0),
127                         node->getFloatValue("offsets/pitch-deg", 0.0),
128                         node->getFloatValue("offsets/x-m", 0.0),
129                         node->getFloatValue("offsets/y-m", 0.0),
130                         node->getFloatValue("offsets/z-m", 0.0));
131     align->setTransform(res_matrix);
132
133     ssgBranch * kid = fgLoad3DModel( fg_root, node->getStringValue("path"),
134                                      prop_root, sim_time_sec );
135     align->addKid(kid);
136     model->addKid(align);
137   }
138
139   return alignmainmodel;
140 }
141
142
143 // end of model_panel.cxx