1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
10 #include <simgear/compiler.h>
12 #include <string.h> // for strcmp()
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>
26 #include "panelnode.hxx"
29 #include "model_panel.hxx"
35 ////////////////////////////////////////////////////////////////////////
37 ////////////////////////////////////////////////////////////////////////
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.
44 fgLoad3DModelPanel( const string &fg_root, const string &path,
45 SGPropertyNode *prop_root,
48 ssgBranch * model = 0;
51 // Load the 3D aircraft object itself
53 SGPath modelpath = path;
54 if ( ulIsAbsolutePathName( path.c_str() ) ) {
59 xmlpath.append(modelpath.str());
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"));
70 model = new ssgBranch;
74 // Assume that textures are in
75 // the same location as the XML file.
77 ssgTexturePath((char *)xmlpath.dir().c_str());
78 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
80 throw sg_exception("Failed to load 3D model");
83 // Set up the alignment node
84 ssgTransform * alignmainmodel = new ssgTransform;
85 alignmainmodel->addKid(model);
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);
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);
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],
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;
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);
133 ssgBranch * kid = fgLoad3DModel( fg_root, node->getStringValue("path"),
134 prop_root, sim_time_sec );
136 model->addKid(align);
139 return alignmainmodel;
143 // end of model_panel.cxx