From 4c0a861f91414c26dde1476c6dae24f05c73af6d Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Sat, 13 Jan 2018 21:25:47 +0100 Subject: [PATCH] add static helper func in order to generate Samplers of a Samplerless StateSet +fix ident --- include/osg/Sampler | 15 ++++++++------ src/osg/Sampler.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/include/osg/Sampler b/include/osg/Sampler index 1ffdda1ab..6cb4c1192 100644 --- a/include/osg/Sampler +++ b/include/osg/Sampler @@ -70,10 +70,10 @@ class OSG_EXPORT Sampler : public osg::StateAttribute * filtering, use normal filtering (equivalent to a max anisotropy * value of 1.0. Valid range is 1.0f upwards. The maximum value * depends on the graphics system. */ - void setMaxAnisotropy(float anis); + void setMaxAnisotropy(float anis); - /** Gets the maximum anisotropy value. */ - inline float getMaxAnisotropy() const { return _maxAnisotropy; } + /** Gets the maximum anisotropy value. */ + inline float getMaxAnisotropy() const { return _maxAnisotropy; } void setMinLOD(float anis); @@ -90,6 +90,9 @@ class OSG_EXPORT Sampler : public osg::StateAttribute /** Gets the maximum anisotropy value. */ inline float getLODBias() const { return _lodbias; } + /** helper method to generate Sampler from Texture's sampling parameters (except shadow_texture_mode left to NONE) */ + static void generateSamplerObjects(StateSet&); + virtual void apply(State& state) const; virtual void compileGLObjects(State&) const; @@ -107,12 +110,12 @@ class OSG_EXPORT Sampler : public osg::StateAttribute Texture::ShadowTextureMode _shadow_texture_mode; Vec4d _borderColor; - Texture::FilterMode _min_filter; - Texture::FilterMode _mag_filter; + Texture::FilterMode _min_filter; + Texture::FilterMode _mag_filter; float _maxAnisotropy, _minlod, _maxlod, _lodbias; mutable buffered_value _PCsampler; - mutable buffered_value _PCdirtyflags; + mutable buffered_value _PCdirtyflags; }; } #endif diff --git a/src/osg/Sampler.cpp b/src/osg/Sampler.cpp index d33d725a9..c5597acd8 100644 --- a/src/osg/Sampler.cpp +++ b/src/osg/Sampler.cpp @@ -225,13 +225,14 @@ void Sampler::compileGLObjects(State& state) const extensions->glSamplerParameterf(samplerobject, GL_TEXTURE_MAX_ANISOTROPY_EXT, _maxAnisotropy); } - if(_maxlod - _minlod > 0) + if(_maxlod - _minlod >= 0) { // if range is valid extensions->glSamplerParameterf(samplerobject, GL_TEXTURE_MIN_LOD, _minlod); extensions->glSamplerParameterf(samplerobject, GL_TEXTURE_MAX_LOD, _maxlod); } extensions->glSamplerParameterf(samplerobject, GL_TEXTURE_LOD_BIAS, _lodbias); + _PCdirtyflags[contextID]=false; } } @@ -244,7 +245,7 @@ void Sampler::apply(State&state) const unsigned int contextID = state.getContextID(); if( _PCdirtyflags[contextID] ) compileGLObjects(state); - + state.get()->glBindSampler( state.getActiveTextureUnit(), _PCsampler[contextID] ); } @@ -274,3 +275,48 @@ int Sampler::compare(const StateAttribute& sa) const COMPARE_StateAttribute_Parameter(_lodbias) return 0; // passed all the above comparison macros, must be equal. } + +void Sampler::generateSamplerObjects(StateSet& ss) +{ + const osg::StateSet::TextureAttributeList& texAttributes = ss.getTextureAttributeList(); + for(unsigned int unit=0; unitsecond.first.get()->getType() != StateAttribute::TEXTURE ) + { + if( aitr->second.first.get()->getType() == StateAttribute::SAMPLER) + sampler = static_cast< Sampler* > (aitr->second.first.get()); + } + else attmode= aitr->second; + } + + if( attmode.first.valid() ) + { + if( !sampler ) + { + ///create new Sampler and add it to this + sampler = new Sampler(); + Texture * tex = attmode.first->asTexture(); + sampler->setFilter( Texture::MIN_FILTER, tex->getFilter(Texture::MIN_FILTER) ); + sampler->setFilter( Texture::MAG_FILTER, tex->getFilter(Texture::MAG_FILTER) ); + sampler->setWrap( Texture::WRAP_S, tex->getWrap(Texture::WRAP_S) ); + sampler->setWrap( Texture::WRAP_T, tex->getWrap(Texture::WRAP_T) ); + sampler->setWrap( Texture::WRAP_R, tex->getWrap(Texture::WRAP_R) ); + sampler->setMaxAnisotropy( tex->getMaxAnisotropy() ); + sampler->setShadowCompareFunc( tex->getShadowCompareFunc() ); + //sampler->setShadowTextureMode( tex->getShadowTextureMode() ); default LUMINANCE tex param incompatible with default NONE Sampler param + sampler->setBorderColor( tex->getBorderColor() ); + sampler->setLODBias( tex->getLODBias() ); + sampler->setMinLOD( tex->getMinLOD() ); + sampler->setMaxLOD( tex->getMaxLOD() ); + ss.setTextureAttributeAndModes(unit,sampler,attmode.second); + } + } + } +}