]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/model.cxx
Modified Files:
[simgear.git] / simgear / scene / model / model.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 <simgear_config.h>
8 #endif
9
10 #include <string.h>             // for strcmp()
11
12 #include <osg/observer_ptr>
13 #include <osg/ref_ptr>
14 #include <osg/Group>
15 #include <osg/NodeCallback>
16 #include <osg/Switch>
17 #include <osg/MatrixTransform>
18 #include <osgDB/Archive>
19 #include <osgDB/FileNameUtils>
20 #include <osgDB/FileUtils>
21 #include <osgDB/ReadFile>
22 #include <osgDB/Registry>
23 #include <osgDB/SharedStateManager>
24 #include <osgUtil/Optimizer>
25
26 #include <simgear/scene/util/SGStateAttributeVisitor.hxx>
27 #include <simgear/scene/util/SGTextureStateAttributeVisitor.hxx>
28
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/props/props.hxx>
31 #include <simgear/props/props_io.hxx>
32 #include <simgear/props/condition.hxx>
33
34 #include "animation.hxx"
35 #include "model.hxx"
36
37 SG_USING_STD(vector);
38 SG_USING_STD(set);
39
40 static inline
41 int nMipMaps(int s)
42 {
43   s = s >> 2;
44   int n = 0;
45   do {
46     ++n;
47     s = s >> 1;
48   } while(s);
49   return n;
50 }
51
52 // Little helper class that holds an extra reference to a
53 // loaded 3d model.
54 // Since we clone all structural nodes from our 3d models,
55 // the database pager will only see one single reference to
56 // top node of the model and expire it relatively fast.
57 // We attach that extra reference to every model cloned from
58 // a base model in the pager. When that cloned model is deleted
59 // this extra reference is deleted too. So if there are no
60 // cloned models left the model will expire.
61 class SGDatabaseReference : public osg::Observer {
62 public:
63   SGDatabaseReference(osg::Referenced* referenced) :
64     mReferenced(referenced)
65   { }
66   virtual void objectDeleted(void*)
67   {
68     mReferenced = 0;
69   }
70 private:
71   osg::ref_ptr<osg::Referenced> mReferenced;
72 };
73
74 // Visitor for 
75 class SGTextureUpdateVisitor : public SGTextureStateAttributeVisitor {
76 public:
77   SGTextureUpdateVisitor(const osgDB::FilePathList& pathList) :
78     mPathList(pathList)
79   { }
80   osg::Texture2D* textureReplace(int unit, osg::StateSet::RefAttributePair& refAttr)
81   {
82     osg::Texture2D* texture = dynamic_cast<osg::Texture2D*>(refAttr.first.get());
83     if (!texture)
84       return 0;
85     
86     osg::ref_ptr<osg::Image> image = texture->getImage(0);
87     if (!image)
88       return 0;
89
90     // The currently loaded file name
91     std::string fullFilePath = image->getFileName();
92     // The short name
93     std::string fileName = osgDB::getSimpleFileName(fullFilePath);
94     // The name that should be found with the current database path
95     std::string fullLiveryFile = osgDB::findFileInPath(fileName, mPathList);
96     // If they are identical then there is nothing to do
97     if (fullLiveryFile == fullFilePath)
98       return 0;
99
100     image = osgDB::readImageFile(fullLiveryFile);
101     if (!image)
102       return 0;
103
104     osg::CopyOp copyOp(osg::CopyOp::DEEP_COPY_ALL &
105                        ~osg::CopyOp::DEEP_COPY_IMAGES);
106     texture = static_cast<osg::Texture2D*>(copyOp(texture));
107     if (!texture)
108       return 0;
109     texture->setImage(image.get());
110     return texture;
111   }
112   virtual void apply(osg::StateSet* stateSet)
113   {
114     if (!stateSet)
115       return;
116
117     // get a copy that we can safely modify the statesets values.
118     osg::StateSet::TextureAttributeList attrList;
119     attrList = stateSet->getTextureAttributeList();
120     for (unsigned unit = 0; unit < attrList.size(); ++unit) {
121       osg::StateSet::AttributeList::iterator i;
122       i = attrList[unit].begin();
123       while (i != attrList[unit].end()) {
124         osg::Texture2D* texture = textureReplace(unit, i->second);
125         if (texture) {
126           stateSet->removeTextureAttribute(unit, i->second.first.get());
127           stateSet->setTextureAttribute(unit, texture, i->second.second);
128           stateSet->setTextureMode(unit, GL_TEXTURE_2D, osg::StateAttribute::ON);
129         }
130         ++i;
131       }
132     }
133   }
134
135 private:
136   osgDB::FilePathList mPathList;
137 };
138
139 class SGTexCompressionVisitor : public SGTextureStateAttributeVisitor {
140 public:
141   SGTexCompressionVisitor(osg::Texture::InternalFormatMode formatMode) :
142     mFormatMode(formatMode)
143   { }
144
145   virtual void apply(int, osg::StateSet::RefAttributePair& refAttr)
146   {
147     osg::Texture2D* texture = dynamic_cast<osg::Texture2D*>(refAttr.first.get());
148     if (!texture)
149       return;
150     
151     osg::Image* image = texture->getImage(0);
152     if (!image)
153       return;
154
155     int s = image->s();
156     int t = image->t();
157
158     int mipmaplevels = 0;
159     if (s < t) {
160       mipmaplevels = nMipMaps(s);
161     } else {
162       mipmaplevels = nMipMaps(t);
163     }
164     texture->setNumMipmapLevels(mipmaplevels);
165
166     if (s <= t && 32 <= s) {
167       texture->setInternalFormatMode(mFormatMode);
168     } else if (t < s && 32 <= t) {
169       texture->setInternalFormatMode(mFormatMode);
170     }
171   }
172
173 private:
174   osg::Texture::InternalFormatMode mFormatMode;
175 };
176
177 class SGAcMaterialCrippleVisitor : public SGStateAttributeVisitor {
178 public:
179   virtual void apply(osg::StateSet::RefAttributePair& refAttr)
180   {
181     osg::Material* material = dynamic_cast<osg::Material*>(refAttr.first.get());
182     if (!material)
183       return;
184     material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
185   }
186 };
187
188 class SGReadFileCallback :
189   public osgDB::Registry::ReadFileCallback {
190 public:
191   virtual osgDB::ReaderWriter::ReadResult
192   readImage(const std::string& fileName, const osgDB::ReaderWriter::Options* opt)
193   {
194     std::string absFileName = osgDB::findDataFile(fileName);
195     if (!osgDB::fileExists(absFileName)) {
196       SG_LOG(SG_IO, SG_ALERT, "Cannot find image file \""
197              << fileName << "\"");
198       return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
199     }
200
201     osgDB::Registry* registry = osgDB::Registry::instance();
202     osgDB::ReaderWriter::ReadResult res = registry->readImageImplementation(absFileName, opt);
203     if (res.loadedFromCache())
204       SG_LOG(SG_IO, SG_INFO, "Returning cached image \""
205              << res.getImage()->getFileName() << "\"");
206     else
207       SG_LOG(SG_IO, SG_INFO, "Reading image \""
208              << res.getImage()->getFileName() << "\"");
209
210     return res;
211   }
212
213   virtual osgDB::ReaderWriter::ReadResult
214   readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* opt)
215   {
216     std::string absFileName = osgDB::findDataFile(fileName);
217     if (!osgDB::fileExists(absFileName)) {
218       SG_LOG(SG_IO, SG_ALERT, "Cannot find model file \""
219              << fileName << "\"");
220       return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
221     }
222
223     osgDB::Registry* registry = osgDB::Registry::instance();
224     osgDB::ReaderWriter::ReadResult res;
225     res = registry->readNodeImplementation(absFileName, opt);
226     if (!res.validNode())
227       return res;
228
229     if (res.loadedFromCache()) {
230       SG_LOG(SG_IO, SG_INFO, "Returning cached model \""
231              << absFileName << "\"");
232     } else {
233       SG_LOG(SG_IO, SG_INFO, "Reading model \""
234              << absFileName << "\"");
235
236       if (osgDB::getLowerCaseFileExtension(absFileName) == "ac") {
237         osg::Matrix m(1, 0, 0, 0,
238                       0, 0, 1, 0,
239                       0, -1, 0, 0,
240                       0, 0, 0, 1);
241         
242         osg::ref_ptr<osg::Group> root = new osg::Group;
243         osg::MatrixTransform* transform = new osg::MatrixTransform;
244         root->addChild(transform);
245         
246         transform->setDataVariance(osg::Object::STATIC);
247         transform->setMatrix(m);
248         transform->addChild(res.getNode());
249         
250         res = osgDB::ReaderWriter::ReadResult(0);
251         
252         osgUtil::Optimizer optimizer;
253         unsigned opts = osgUtil::Optimizer::FLATTEN_STATIC_TRANSFORMS;
254         optimizer.optimize(root.get(), opts);
255         
256         // Ok, this step is questionable.
257         // It is there to have the same visual appearance of ac objects for the
258         // first cut. Osg's ac3d loader will correctly set materials from the
259         // ac file. But the old plib loader used GL_AMBIENT_AND_DIFFUSE for the
260         // materials that in effect igored the ambient part specified in the
261         // file. We emulate that for the first cut here by changing all ac models
262         // here. But in the long term we should use the unchanged model and fix
263         // the input files instead ...
264         SGAcMaterialCrippleVisitor matCriple;
265         root->accept(matCriple);
266         
267         res = osgDB::ReaderWriter::ReadResult(root.get());
268       }
269       
270       osgUtil::Optimizer optimizer;
271       unsigned opts = 0;
272       // Don't use this one. It will break animation names ...
273       // opts |= osgUtil::Optimizer::REMOVE_REDUNDANT_NODES;
274       
275       // opts |= osgUtil::Optimizer::REMOVE_LOADED_PROXY_NODES;
276       // opts |= osgUtil::Optimizer::COMBINE_ADJACENT_LODS;
277       // opts |= osgUtil::Optimizer::SHARE_DUPLICATE_STATE;
278       opts |= osgUtil::Optimizer::MERGE_GEOMETRY;
279       // opts |= osgUtil::Optimizer::CHECK_GEOMETRY;
280       // opts |= osgUtil::Optimizer::SPATIALIZE_GROUPS;
281       // opts |= osgUtil::Optimizer::COPY_SHARED_NODES;
282       opts |= osgUtil::Optimizer::TRISTRIP_GEOMETRY;
283       // opts |= osgUtil::Optimizer::TESSELATE_GEOMETRY;
284       // opts |= osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS;
285       optimizer.optimize(res.getNode(), opts);
286
287       // OSGFIXME
288       registry->getSharedStateManager()->share(res.getNode());
289       
290       // OSGFIXME: guard that with a flag
291       // OSGFIXME: in the long term it is unclear if we have an OpenGL context here...
292       osg::Texture::Extensions* e = osg::Texture::getExtensions(0, true);
293       if (e->isTextureCompressionARBSupported()) {
294         SGTexCompressionVisitor texComp(osg::Texture::USE_ARB_COMPRESSION);
295         res.getNode()->accept(texComp);
296       } else if (e->isTextureCompressionS3TCSupported()) {
297         SGTexCompressionVisitor texComp(osg::Texture::USE_S3TC_DXT5_COMPRESSION);
298         res.getNode()->accept(texComp);
299       }
300     }
301
302     // Add an extra reference to the model stored in the database.
303     // That it to avoid expiring the object from the cache even if it is still
304     // in use. Note that the object cache will think that a model is unused if the
305     // reference count is 1. If we clone all structural nodes here we need that extra
306     // reference to the original object
307     SGDatabaseReference* databaseReference = new SGDatabaseReference(res.getNode());
308     osg::CopyOp::CopyFlags flags = osg::CopyOp::DEEP_COPY_ALL;
309     flags &= ~osg::CopyOp::DEEP_COPY_TEXTURES;
310     flags &= ~osg::CopyOp::DEEP_COPY_IMAGES;
311     flags &= ~osg::CopyOp::DEEP_COPY_ARRAYS;
312     flags &= ~osg::CopyOp::DEEP_COPY_PRIMITIVES;
313     // This will safe display lists ...
314     flags &= ~osg::CopyOp::DEEP_COPY_DRAWABLES;
315     flags &= ~osg::CopyOp::DEEP_COPY_SHAPES;
316     res = osgDB::ReaderWriter::ReadResult(osg::CopyOp(flags)(res.getNode()));
317     res.getNode()->addObserver(databaseReference);
318
319     SGTextureUpdateVisitor liveryUpdate(osgDB::getDataFilePathList());
320     res.getNode()->accept(liveryUpdate);
321
322     // OSGFIXME: don't forget that mutex here
323     registry->getOrCreateSharedStateManager()->share(res.getNode(), 0);
324
325     return res;
326   }
327 };
328
329 class SGReadCallbackInstaller {
330 public:
331   SGReadCallbackInstaller()
332   {
333     osg::Referenced::setThreadSafeReferenceCounting(true);
334
335     osgDB::Registry* registry = osgDB::Registry::instance();
336     osgDB::ReaderWriter::Options* options = new osgDB::ReaderWriter::Options;
337     options->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_ALL);
338     registry->setOptions(options);
339     registry->getOrCreateSharedStateManager()->setShareMode(osgDB::SharedStateManager::SHARE_TEXTURES);
340     registry->setReadFileCallback(new SGReadFileCallback);
341   }
342 };
343
344 static SGReadCallbackInstaller readCallbackInstaller;
345
346 osg::Texture2D*
347 SGLoadTexture2D(const std::string& path, bool wrapu, bool wrapv,
348                 int mipmaplevels)
349 {
350   osg::Image* image = osgDB::readImageFile(path);
351   osg::Texture2D* texture = new osg::Texture2D;
352   texture->setImage(image);
353   if (wrapu)
354     texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
355   else
356     texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
357   if (wrapv)
358     texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
359   else
360     texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
361
362   if (image) {
363     int s = image->s();
364     int t = image->t();
365
366     if (mipmaplevels < 0) {
367       if (s < t) {
368         mipmaplevels = nMipMaps(s);
369       } else {
370         mipmaplevels = nMipMaps(t);
371       }
372     }
373     texture->setNumMipmapLevels(mipmaplevels);
374
375     // OSGFIXME: guard with a flag
376     if (osg::Texture::getExtensions(0, true)->isTextureCompressionARBSupported()) {
377       if (s <= t && 32 <= s) {
378         texture->setInternalFormatMode(osg::Texture::USE_ARB_COMPRESSION);
379       } else if (t < s && 32 <= t) {
380         texture->setInternalFormatMode(osg::Texture::USE_ARB_COMPRESSION);
381       }
382     } else if (osg::Texture::getExtensions(0, true)->isTextureCompressionS3TCSupported()) {
383       if (s <= t && 32 <= s) {
384         texture->setInternalFormatMode(osg::Texture::USE_S3TC_DXT5_COMPRESSION);
385       } else if (t < s && 32 <= t) {
386         texture->setInternalFormatMode(osg::Texture::USE_S3TC_DXT5_COMPRESSION);
387       }
388     }
389   }
390   return texture;
391 }
392
393 class SGSwitchUpdateCallback : public osg::NodeCallback {
394 public:
395   SGSwitchUpdateCallback(SGCondition* condition) :
396     mCondition(condition) {}
397   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
398   { 
399     assert(dynamic_cast<osg::Switch*>(node));
400     osg::Switch* s = static_cast<osg::Switch*>(node);
401
402     if (mCondition && mCondition->test()) {
403       s->setAllChildrenOn();
404       // note, callback is responsible for scenegraph traversal so
405       // should always include call traverse(node,nv) to ensure 
406       // that the rest of cullbacks and the scene graph are traversed.
407       traverse(node, nv);
408     } else
409       s->setAllChildrenOff();
410   }
411
412 private:
413   SGCondition* mCondition;
414 };
415
416 /**
417  * Locate a named node in a branch.
418  */
419 class NodeFinder : public osg::NodeVisitor {
420 public:
421   NodeFinder(const std::string& nameToFind) :
422     osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
423                      osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
424     mName(nameToFind),
425     mNode(0)
426   { }
427   virtual void apply(osg::Node& node)
428   {
429     if (mNode)
430       return;
431     if (mName == node.getName()) {
432       mNode = &node;
433       return;
434     }
435     traverse(node);
436   }
437
438   osg::Node* getNode() const
439   { return mNode; }
440 private:
441   std::string mName;
442   osg::Node* mNode;
443 };
444
445 /**
446  * Splice a branch in between all child nodes and their parents.
447  */
448 static void
449 splice_branch(osg::Group* group, osg::Node* child)
450 {
451   osg::Node::ParentList parents = child->getParents();
452   group->addChild(child);
453   osg::Node::ParentList::iterator i;
454   for (i = parents.begin(); i != parents.end(); ++i)
455     (*i)->replaceChild(child, group);
456 }
457
458 void
459 sgMakeAnimation( osg::Node * model,
460                  const char * name,
461                  vector<SGPropertyNode_ptr> &name_nodes,
462                  SGPropertyNode *prop_root,
463                  SGPropertyNode_ptr node,
464                  double sim_time_sec,
465                  SGPath &texture_path,
466                  set<osg::Node*> &ignore_branches )
467 {
468   bool ignore = false;
469   SGAnimation * animation = 0;
470   const char * type = node->getStringValue("type", "none");
471   if (!strcmp("none", type)) {
472     animation = new SGNullAnimation(node);
473   } else if (!strcmp("range", type)) {
474     animation = new SGRangeAnimation(prop_root, node);
475   } else if (!strcmp("billboard", type)) {
476     animation = new SGBillboardAnimation(node);
477   } else if (!strcmp("select", type)) {
478     animation = new SGSelectAnimation(prop_root, node);
479   } else if (!strcmp("spin", type)) {
480     animation = new SGSpinAnimation(prop_root, node, sim_time_sec );
481   } else if (!strcmp("timed", type)) {
482     animation = new SGTimedAnimation(node);
483   } else if (!strcmp("rotate", type)) {
484     animation = new SGRotateAnimation(prop_root, node);
485   } else if (!strcmp("translate", type)) {
486     animation = new SGTranslateAnimation(prop_root, node);
487   } else if (!strcmp("scale", type)) {
488     animation = new SGScaleAnimation(prop_root, node);
489   } else if (!strcmp("texrotate", type)) {
490     animation = new SGTexRotateAnimation(prop_root, node);
491   } else if (!strcmp("textranslate", type)) {
492     animation = new SGTexTranslateAnimation(prop_root, node);
493   } else if (!strcmp("texmultiple", type)) {
494     animation = new SGTexMultipleAnimation(prop_root, node);
495   } else if (!strcmp("blend", type)) {
496     animation = new SGBlendAnimation(prop_root, node);
497     ignore = true;
498   } else if (!strcmp("alpha-test", type)) {
499     animation = new SGAlphaTestAnimation(node);
500   } else if (!strcmp("material", type)) {
501     animation = new SGMaterialAnimation(prop_root, node, texture_path);
502   } else if (!strcmp("flash", type)) {
503     animation = new SGFlashAnimation(node);
504   } else if (!strcmp("dist-scale", type)) {
505     animation = new SGDistScaleAnimation(node);
506   } else if (!strcmp("noshadow", type)) {
507     animation = new SGShadowAnimation(prop_root, node);
508   } else if (!strcmp("shader", type)) {
509     animation = new SGShaderAnimation(prop_root, node);
510   } else {
511     animation = new SGNullAnimation(node);
512     SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
513   }
514
515   if (name != 0)
516       animation->setName(name);
517
518   osg::Node * object = 0;
519   if (!name_nodes.empty()) {
520     const char * name = name_nodes[0]->getStringValue();
521     NodeFinder nodeFinder(name);
522     model->accept(nodeFinder);
523     object = nodeFinder.getNode();
524     if (object == 0) {
525       SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
526       delete animation;
527       animation = 0;
528     }
529   } else {
530     object = model;
531   }
532
533   if ( animation == 0 )
534      return;
535
536   osg::Group* branch = animation->getBranch();
537   splice_branch(branch, object);
538
539   for (unsigned int i = 1; i < name_nodes.size(); i++) {
540       const char * name = name_nodes[i]->getStringValue();
541       NodeFinder nodeFinder(name);
542       model->accept(nodeFinder);
543       object = nodeFinder.getNode();
544       if (object == 0) {
545           SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
546           delete animation;
547           animation = 0;
548       } else {
549           osg::Group* oldParent = object->getParent(0);
550           branch->addChild(object);
551           oldParent->removeChild(object);
552       }
553   }
554
555   if ( animation != 0 ) {
556     animation->init();
557     branch->setUpdateCallback(animation);
558     if ( ignore ) {
559       ignore_branches.insert( branch );
560     }
561   }
562 }
563
564 \f
565 ////////////////////////////////////////////////////////////////////////
566 // Global functions.
567 ////////////////////////////////////////////////////////////////////////
568
569 osg::Node *
570 sgLoad3DModel( const string &fg_root, const string &path,
571                SGPropertyNode *prop_root,
572                double sim_time_sec, osg::Node *(*load_panel)(SGPropertyNode *),
573                SGModelData *data,
574                const SGPath& externalTexturePath )
575 {
576   osg::Switch* model = 0;
577   SGPropertyNode props;
578
579                                 // Load the 3D aircraft object itself
580   SGPath modelpath = path, texturepath = path;
581   if ( !ulIsAbsolutePathName( path.c_str() ) ) {
582     SGPath tmp = fg_root;
583     tmp.append(modelpath.str());
584     modelpath = texturepath = tmp;
585   }
586
587                                 // Check for an XML wrapper
588   if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
589     readProperties(modelpath.str(), &props);
590     if (props.hasValue("/path")) {
591       modelpath = modelpath.dir();
592       modelpath.append(props.getStringValue("/path"));
593       if (props.hasValue("/texture-path")) {
594         texturepath = texturepath.dir();
595         texturepath.append(props.getStringValue("/texture-path"));
596       }
597     } else {
598       if (model == 0)
599         model = new osg::Switch;
600     }
601   }
602
603   osgDB::FilePathList pathList = osgDB::getDataFilePathList();
604   osgDB::Registry::instance()->initFilePathLists();
605
606                                 // Assume that textures are in
607                                 // the same location as the XML file.
608   if (model == 0) {
609     if (texturepath.extension() != "")
610           texturepath = texturepath.dir();
611
612     osgDB::Registry::instance()->getDataFilePathList().push_front(texturepath.str());
613
614     osg::Node* node = osgDB::readNodeFile(modelpath.str());
615     if (node == 0)
616       throw sg_io_exception("Failed to load 3D model", 
617                             sg_location(modelpath.str()));
618     model = new osg::Switch;
619     model->addChild(node, true);
620   }
621
622   osgDB::Registry::instance()->getDataFilePathList().push_front(externalTexturePath.str());
623
624                                 // Set up the alignment node
625   osg::MatrixTransform* alignmainmodel = new osg::MatrixTransform;
626   alignmainmodel->addChild(model);
627   osg::Matrix res_matrix;
628   res_matrix.makeRotate(
629     props.getFloatValue("/offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
630     osg::Vec3(0, 0, 1),
631     props.getFloatValue("/offsets/roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
632     osg::Vec3(1, 0, 0),
633     props.getFloatValue("/offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
634     osg::Vec3(0, 1, 0));
635
636   osg::Matrix tmat;
637   tmat.makeTranslate(props.getFloatValue("/offsets/x-m", 0.0),
638                      props.getFloatValue("/offsets/y-m", 0.0),
639                      props.getFloatValue("/offsets/z-m", 0.0));
640   alignmainmodel->setMatrix(res_matrix*tmat);
641
642   unsigned int i;
643
644                                 // Load sub-models
645   vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
646   for (i = 0; i < model_nodes.size(); i++) {
647     SGPropertyNode_ptr node = model_nodes[i];
648     osg::MatrixTransform* align = new osg::MatrixTransform;
649     res_matrix.makeIdentity();
650     res_matrix.makeRotate(
651       node->getFloatValue("offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
652       osg::Vec3(0, 0, 1),
653       node->getFloatValue("offsets/roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
654       osg::Vec3(1, 0, 0),
655       node->getFloatValue("offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
656       osg::Vec3(0, 1, 0));
657     
658     tmat.makeIdentity();
659     tmat.makeTranslate(node->getFloatValue("offsets/x-m", 0.0),
660                        node->getFloatValue("offsets/y-m", 0.0),
661                        node->getFloatValue("offsets/z-m", 0.0));
662     align->setMatrix(res_matrix*tmat);
663
664     osg::Node* kid;
665     const char * submodel = node->getStringValue("path");
666     try {
667       kid = sgLoad3DModel( fg_root, submodel, prop_root, sim_time_sec, load_panel );
668
669     } catch (const sg_throwable &t) {
670       SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
671       throw;
672     }
673     align->addChild(kid);
674
675     align->setName(node->getStringValue("name", ""));
676     model->addChild(align);
677
678     SGPropertyNode *cond = node->getNode("condition", false);
679     if (cond)
680       model->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
681   }
682
683   // restore old path list
684   osgDB::setDataFilePathList(pathList);
685
686   if ( load_panel ) {
687                                 // Load panels
688     vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
689     for (i = 0; i < panel_nodes.size(); i++) {
690         SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
691         osg::Node * panel = load_panel(panel_nodes[i]);
692         if (panel_nodes[i]->hasValue("name"))
693             panel->setName((char *)panel_nodes[i]->getStringValue("name"));
694         model->addChild(panel);
695     }
696   }
697
698   if (data) {
699     alignmainmodel->setUserData(data);
700     data->modelLoaded(path, &props, alignmainmodel);
701   }
702                                 // Load animations
703   set<osg::Node*> ignore_branches;
704   vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
705   for (i = 0; i < animation_nodes.size(); i++) {
706     const char * name = animation_nodes[i]->getStringValue("name", 0);
707     vector<SGPropertyNode_ptr> name_nodes =
708       animation_nodes[i]->getChildren("object-name");
709     sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
710                      sim_time_sec, texturepath, ignore_branches);
711   }
712
713   return alignmainmodel;
714 }
715
716 // end of model.cxx