easy-gl › Sampler

Sampler

A Sampler Object decouples texture sampling parameters (filtering, wrap mode, LOD bias) from the texture itself. Bind a Sampler to a texture unit to override the texture's own sampling parameters. Requires OpenGL 3.3+.

Why use sampler objects?

Creating a trilinear sampler

easygl::Sampler trilinear;
trilinear.create();
trilinear.set_parameter(easygl::TextureParameter::MinFilter,
    static_cast<int>(easygl::TextureMinFilter::LinearMipmapLinear));
trilinear.set_parameter(easygl::TextureParameter::MagFilter,
    static_cast<int>(easygl::TextureMagFilter::Linear));
trilinear.set_parameter(easygl::TextureParameter::WrapS,
    static_cast<int>(easygl::TextureWrapMode::Repeat));
trilinear.set_parameter(easygl::TextureParameter::WrapT,
    static_cast<int>(easygl::TextureWrapMode::Repeat));

Creating a shadow-comparison sampler

easygl::Sampler shadowSampler;
shadowSampler.create();
shadowSampler.set_parameter(easygl::TextureParameter::CompareMode,
    static_cast<int>(easygl::TextureCompareMode::CompareRefToTexture));
shadowSampler.set_parameter(easygl::TextureParameter::CompareFunc,
    static_cast<int>(easygl::CompareFunc::Lequal));
shadowSampler.set_parameter(easygl::TextureParameter::MinFilter,
    static_cast<int>(easygl::TextureMinFilter::Linear));
shadowSampler.set_parameter(easygl::TextureParameter::WrapS,
    static_cast<int>(easygl::TextureWrapMode::ClampToEdge));
shadowSampler.set_parameter(easygl::TextureParameter::WrapT,
    static_cast<int>(easygl::TextureWrapMode::ClampToEdge));

Binding at render time

shadowTex.active_bind(easygl::TextureUnit::Texture3,
                        easygl::TextureTarget::Texture2D);
shadowSampler.bind(3); // unit index as integer

// Unbind later:
easygl::Sampler::unbind(3);

Method reference

MethodDescription
create()Allocate GL sampler handle.
destroy()Free handle. Noexcept.
bind(unit)Bind sampler to texture unit index.
unbind(unit) [static]Bind sampler 0 to unit.
set_parameter(pname, int)Set an integer sampler parameter.
set_parameter(pname, float)Set a float sampler parameter (e.g. LOD bias).
is_created()True if GL handle allocated.
native_handle()Raw GL integer ID.
reset_handle_no_gl()Context-loss recovery.