From: frohlich Date: Sun, 3 Jun 2007 18:21:04 +0000 (+0000) Subject: Modified Files: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=04cd9b3eb60f1b920d2270b3f87d8bc05af505b8;p=simgear.git Modified Files: simgear/scene/model/model.cxx simgear/scene/util/SGSceneFeatures.cxx simgear/scene/util/SGSceneFeatures.hxx: Make sure textures are shared. Do not rely on a graphics context to be available on model loading. --- diff --git a/simgear/scene/model/model.cxx b/simgear/scene/model/model.cxx index 11cd0a85..1d278f7e 100644 --- a/simgear/scene/model/model.cxx +++ b/simgear/scene/model/model.cxx @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -128,10 +129,6 @@ private: class SGTexCompressionVisitor : public SGTextureStateAttributeVisitor { public: - SGTexCompressionVisitor(osg::Texture::InternalFormatMode formatMode) : - mFormatMode(formatMode) - { } - virtual void apply(int, osg::StateSet::RefAttributePair& refAttr) { osg::Texture2D* texture; @@ -145,15 +142,26 @@ public: int s = image->s(); int t = image->t(); + if (s <= t && 32 <= s) { - texture->setInternalFormatMode(mFormatMode); + SGSceneFeatures::instance()->setTextureCompression(texture); } else if (t < s && 32 <= t) { - texture->setInternalFormatMode(mFormatMode); + SGSceneFeatures::instance()->setTextureCompression(texture); } } +}; -private: - osg::Texture::InternalFormatMode mFormatMode; +class SGTexDataVarianceVisitor : public SGTextureStateAttributeVisitor { +public: + virtual void apply(int, osg::StateSet::RefAttributePair& refAttr) + { + osg::Texture* texture; + texture = dynamic_cast(refAttr.first.get()); + if (!texture) + return; + + texture->setDataVariance(osg::Object::STATIC); + } }; class SGAcMaterialCrippleVisitor : public SGStateAttributeVisitor { @@ -276,23 +284,17 @@ public: if (needTristrip) opts |= osgUtil::Optimizer::TRISTRIP_GEOMETRY; // opts |= osgUtil::Optimizer::TESSELATE_GEOMETRY; - opts |= osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS; + // opts |= osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS; optimizer.optimize(res.getNode(), opts); - // OSGFIXME + // Make sure the data variance of sharable objects is set to STATIC ... + SGTexDataVarianceVisitor dataVarianceVisitor; + res.getNode()->accept(dataVarianceVisitor); + // ... so that textures are now globally shared registry->getSharedStateManager()->share(res.getNode()); - // OSGFIXME: guard that with a flag - // OSGFIXME: in the long term it is unclear if we have an OpenGL - // context here... - osg::Texture::Extensions* e = osg::Texture::getExtensions(0, true); - if (e->isTextureCompressionARBSupported()) { - SGTexCompressionVisitor texComp(osg::Texture::USE_ARB_COMPRESSION); - res.getNode()->accept(texComp); - } else if (e->isTextureCompressionS3TCSupported()) { - SGTexCompressionVisitor texComp(osg::Texture::USE_S3TC_DXT5_COMPRESSION); - res.getNode()->accept(texComp); - } + SGTexCompressionVisitor texComp; + res.getNode()->accept(texComp); } // Add an extra reference to the model stored in the database. @@ -313,10 +315,14 @@ public: res = osgDB::ReaderWriter::ReadResult(osg::CopyOp(flags)(res.getNode())); res.getNode()->addObserver(databaseReference); + // Update liveries SGTextureUpdateVisitor liveryUpdate(osgDB::getDataFilePathList()); res.getNode()->accept(liveryUpdate); - // OSGFIXME: don't forget that mutex here + // Make sure the data variance of sharable objects is set to STATIC ... + SGTexDataVarianceVisitor dataVarianceVisitor; + res.getNode()->accept(dataVarianceVisitor); + // ... so that textures are now globally shared registry->getOrCreateSharedStateManager()->share(res.getNode(), 0); return res; @@ -359,19 +365,10 @@ SGLoadTexture2D(const std::string& path, bool wrapu, bool wrapv, int) int s = image->s(); int t = image->t(); - // OSGFIXME: guard with a flag - if (osg::Texture::getExtensions(0, true)->isTextureCompressionARBSupported()) { - if (s <= t && 32 <= s) { - texture->setInternalFormatMode(osg::Texture::USE_ARB_COMPRESSION); - } else if (t < s && 32 <= t) { - texture->setInternalFormatMode(osg::Texture::USE_ARB_COMPRESSION); - } - } else if (osg::Texture::getExtensions(0, true)->isTextureCompressionS3TCSupported()) { - if (s <= t && 32 <= s) { - texture->setInternalFormatMode(osg::Texture::USE_S3TC_DXT5_COMPRESSION); - } else if (t < s && 32 <= t) { - texture->setInternalFormatMode(osg::Texture::USE_S3TC_DXT5_COMPRESSION); - } + if (s <= t && 32 <= s) { + SGSceneFeatures::instance()->setTextureCompression(texture.get()); + } else if (t < s && 32 <= t) { + SGSceneFeatures::instance()->setTextureCompression(texture.get()); } } diff --git a/simgear/scene/util/SGSceneFeatures.cxx b/simgear/scene/util/SGSceneFeatures.cxx index be0183a4..7cc71262 100644 --- a/simgear/scene/util/SGSceneFeatures.cxx +++ b/simgear/scene/util/SGSceneFeatures.cxx @@ -29,12 +29,14 @@ #include #include #include +#include #include #include #include SGSceneFeatures::SGSceneFeatures() : + _textureCompression(UseARBCompression), _shaderLights(true), _pointSpriteLights(true), _distanceAttenuationLights(true) @@ -55,6 +57,28 @@ SGSceneFeatures::instance() return sceneFeatures; } +void +SGSceneFeatures::setTextureCompression(osg::Texture* texture) const +{ + switch (_textureCompression) { + case UseARBCompression: + texture->setInternalFormatMode(osg::Texture::USE_ARB_COMPRESSION); + break; + case UseDXT1Compression: + texture->setInternalFormatMode(osg::Texture::USE_S3TC_DXT1_COMPRESSION); + break; + case UseDXT3Compression: + texture->setInternalFormatMode(osg::Texture::USE_S3TC_DXT3_COMPRESSION); + break; + case UseDXT5Compression: + texture->setInternalFormatMode(osg::Texture::USE_S3TC_DXT5_COMPRESSION); + break; + default: + texture->setInternalFormatMode(osg::Texture::USE_IMAGE_DATA_FORMAT); + break; + } +} + bool SGSceneFeatures::getHavePointSprites(unsigned contextId) const { diff --git a/simgear/scene/util/SGSceneFeatures.hxx b/simgear/scene/util/SGSceneFeatures.hxx index 0aa46abc..c38921e8 100644 --- a/simgear/scene/util/SGSceneFeatures.hxx +++ b/simgear/scene/util/SGSceneFeatures.hxx @@ -24,10 +24,26 @@ #include +namespace osg { class Texture; } + class SGSceneFeatures : public SGReferenced { public: static SGSceneFeatures* instance(); + enum TextureCompression { + DoNotUseCompression, + UseARBCompression, + UseDXT1Compression, + UseDXT3Compression, + UseDXT5Compression + }; + + void setTextureCompression(TextureCompression textureCompression) + { _textureCompression = textureCompression; } + TextureCompression getTextureCompression() const + { return _textureCompression; } + void setTextureCompression(osg::Texture* texture) const; + void setEnablePointSpriteLights(bool enable) { _pointSpriteLights = enable; } bool getEnablePointSpriteLights(unsigned contextId) const @@ -67,6 +83,7 @@ private: SGSceneFeatures(const SGSceneFeatures&); SGSceneFeatures& operator=(const SGSceneFeatures&); + TextureCompression _textureCompression; bool _shaderLights; bool _pointSpriteLights; bool _distanceAttenuationLights;