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/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>
27 #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
52 SGPath modelpath = path;
53 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
55 tmp.append(modelpath.str());
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"));
67 model = new ssgBranch;
71 // Assume that textures are in
72 // the same location as the XML file.
74 ssgTexturePath((char *)modelpath.dir().c_str());
75 model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
77 throw sg_exception("Failed to load 3D model");
80 // Set up the alignment node
81 ssgTransform * alignmainmodel = new ssgTransform;
82 alignmainmodel->addKid(model);
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);
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);
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],
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;
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);
130 ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
131 prop_root, sim_time_sec );
133 model->addKid(align);
136 return alignmainmodel;
140 // end of model_panel.cxx