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?
- Reuse the same sampling settings across multiple textures without duplicating parameters.
- Swap filtering modes at draw time without touching the texture state.
- Useful for atlases where different draw calls need different wrap modes on the same texture.
Factory helpers
Pre-built samplers for common configurations:
auto linearClamp = easygl::Sampler::create_linear_clamp(); // linear filter, clamp to edge
auto nearestClamp = easygl::Sampler::create_nearest_clamp(); // nearest filter, clamp to edge
auto linearRepeat = easygl::Sampler::create_linear_repeat(); // linear filter, repeat wrap
auto mipLinear = easygl::Sampler::create_mipmap_linear(); // trilinear mipmap, repeat wrap
Creating a trilinear sampler
easygl::Sampler trilinear;
trilinear.create();
trilinear.set_parameter(easygl::SamplerParameter::MinFilter,
static_cast<int>(easygl::TextureMinFilter::LinearMipmapLinear));
trilinear.set_parameter(easygl::SamplerParameter::MagFilter,
static_cast<int>(easygl::TextureMagFilter::Linear));
trilinear.set_parameter(easygl::SamplerParameter::WrapS,
static_cast<int>(easygl::TextureWrapMode::Repeat));
trilinear.set_parameter(easygl::SamplerParameter::WrapT,
static_cast<int>(easygl::TextureWrapMode::Repeat));
Creating a shadow-comparison sampler
easygl::Sampler shadowSampler;
shadowSampler.create();
shadowSampler.set_parameter(easygl::SamplerParameter::CompareMode,
static_cast<int>(easygl::TextureCompareMode::CompareRefToTexture));
shadowSampler.set_parameter(easygl::SamplerParameter::CompareFunc,
static_cast<int>(easygl::CompareFunc::Lequal));
shadowSampler.set_parameter(easygl::SamplerParameter::MinFilter,
static_cast<int>(easygl::TextureMinFilter::Linear));
shadowSampler.set_parameter(easygl::SamplerParameter::WrapS,
static_cast<int>(easygl::TextureWrapMode::ClampToEdge));
shadowSampler.set_parameter(easygl::SamplerParameter::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
| Method | Description |
|---|---|
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). |
create_linear_clamp() [static] | Linear filter, clamp-to-edge wrap. |
create_nearest_clamp() [static] | Nearest filter, clamp-to-edge wrap. |
create_linear_repeat() [static] | Linear filter, repeat wrap. |
create_mipmap_linear() [static] | Trilinear mipmap, repeat wrap. |
is_created() | True if GL handle allocated. |
native_handle() | Raw GL integer ID. |
reset_handle_no_gl() | Context-loss recovery. |