From ad9341835f43433d179444ce707f6a013572468d Mon Sep 17 00:00:00 2001 From: frohlich Date: Sun, 28 Jan 2007 20:04:56 +0000 Subject: [PATCH] Modified Files: model.cxx: Better texture sharing, fix problem with rotation order --- simgear/scene/model/model.cxx | 43 +++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/simgear/scene/model/model.cxx b/simgear/scene/model/model.cxx index f1d61444..7a35768a 100644 --- a/simgear/scene/model/model.cxx +++ b/simgear/scene/model/model.cxx @@ -7,8 +7,6 @@ #include #endif -#include // for strcmp() - #include #include #include @@ -346,7 +344,7 @@ osg::Texture2D* SGLoadTexture2D(const std::string& path, bool wrapu, bool wrapv, int) { osg::Image* image = osgDB::readImageFile(path); - osg::Texture2D* texture = new osg::Texture2D; + osg::ref_ptr texture = new osg::Texture2D; texture->setImage(image); if (wrapu) texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); @@ -376,7 +374,28 @@ SGLoadTexture2D(const std::string& path, bool wrapu, bool wrapv, int) } } } - return texture; + + // Make sure the texture is shared if we already have the same texture + // somewhere ... + { + osg::ref_ptr tmpNode = new osg::Node; + osg::StateSet* stateSet = tmpNode->getOrCreateStateSet(); + stateSet->setTextureAttribute(0, texture.get()); + + // OSGFIXME: don't forget that mutex here + osgDB::Registry* registry = osgDB::Registry::instance(); + registry->getOrCreateSharedStateManager()->share(tmpNode.get(), 0); + + // should be the same, but be paranoid ... + stateSet = tmpNode->getStateSet(); + osg::StateAttribute* stateAttr; + stateAttr = stateSet->getTextureAttribute(0, osg::StateAttribute::TEXTURE); + osg::Texture2D* texture2D = dynamic_cast(stateAttr); + if (texture2D) + texture = texture2D; + } + + return texture.release(); } class SGSwitchUpdateCallback : public osg::NodeCallback { @@ -465,12 +484,12 @@ sgLoad3DModel( const string &fg_root, const string &path, alignmainmodel->addChild(model); osg::Matrix res_matrix; res_matrix.makeRotate( - props.getFloatValue("/offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS, - osg::Vec3(0, 0, 1), + props.getFloatValue("/offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS, + osg::Vec3(0, 1, 0), props.getFloatValue("/offsets/roll-deg", 0.0)*SG_DEGREES_TO_RADIANS, osg::Vec3(1, 0, 0), - props.getFloatValue("/offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS, - osg::Vec3(0, 1, 0)); + props.getFloatValue("/offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS, + osg::Vec3(0, 0, 1)); osg::Matrix tmat; tmat.makeTranslate(props.getFloatValue("/offsets/x-m", 0.0), @@ -485,12 +504,12 @@ sgLoad3DModel( const string &fg_root, const string &path, osg::MatrixTransform* align = new osg::MatrixTransform; res_matrix.makeIdentity(); res_matrix.makeRotate( - node->getDoubleValue("offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS, - osg::Vec3(0, 0, 1), + node->getDoubleValue("offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS, + osg::Vec3(0, 1, 0), node->getDoubleValue("offsets/roll-deg", 0.0)*SG_DEGREES_TO_RADIANS, osg::Vec3(1, 0, 0), - node->getDoubleValue("offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS, - osg::Vec3(0, 1, 0)); + node->getDoubleValue("offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS, + osg::Vec3(0, 0, 1)); tmat.makeIdentity(); tmat.makeTranslate(node->getDoubleValue("offsets/x-m", 0), -- 2.39.5