Merge pull request #444 from mp3butcher/osgSampler2

Osg sampler2  add static helper func in order to generate Samplers of a Samplerless StateSet
This commit is contained in:
OpenSceneGraph git repository
2018-01-14 13:25:23 +00:00
committed by GitHub
2 changed files with 57 additions and 8 deletions

View File

@@ -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<GLuint> _PCsampler;
mutable buffered_value<uint8_t> _PCdirtyflags;
mutable buffered_value<uint8_t> _PCdirtyflags;
};
}
#endif

View File

@@ -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<GLExtensions>()->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; unit<texAttributes.size(); ++unit)
{
StateSet::RefAttributePair attmode;
Sampler * sampler = 0;
const StateSet::AttributeList& tex_attributes = texAttributes[unit];
for(StateSet::AttributeList::const_iterator aitr = tex_attributes.begin();
aitr!=tex_attributes.end();
++aitr)
{
if( aitr->second.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);
}
}
}
}