user4179961
user4179961

Reputation:

glsl fragment shader - how to sample a GL_NEAREST texture as GL_LINEAR

I've set up a texture with MAG_FILTERING = GL_NEAREST.

I do not wish to set it to GL_LINEAR, but in one of my shaders, I want to fetch the content of the texture as if it was GL_LINEAR.

I've been trying to find the answer all over, but can't find anything reliable, nor any examples.

The current code is:

uniform sampler2DRect sampler1;
in vec2 vTexCoo;

main() {
   out_diffuse = texture(sampler1, vTexCoo);
}

I could in theory build a function that interpolates 4 pixels, but meh... Surely there must be some way of specifying what filtering to use.

Upvotes: 4

Views: 1789

Answers (2)

derhass
derhass

Reputation: 45362

OpenGL 3.3 introduced Sampler Objects, which technically allow what you request. When a sampler object is bound to a texture unit, the state from the sampler object overrides the sampler state stored in the texture object.

Using it would reuire you to bind the sampler object for this particular draw call, and unbind it afterwards - but if you can do that, you could simply modify the texture filter parameters and restore them as well, so it's not clear what you gain by that.

However, there is another possibility here. With sampler objects, you can bind the same texture to different units and use different sampler options. That might allow you to keep the sampler object bound to some unused texture unit, and have shaders who need the other filtering just use a different unit - but that will require that you bind your texture(s) to more than one unit, so it's still unclear what you can gain by that...

Upvotes: 4

BDL
BDL

Reputation: 22174

Starting from OpenGL 3.3 Core Profile, you can use Sampler Objects to specify the sampling parameters. When a sample object is bound to a texture image unit, it basically overrides all internal sampler settings of the bound texture object.

Use a sampler similar to the following one to always use linear filtering:

GLuint sampler = 0;
glGenSamplers(1, &sampler);

glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

When binding the texture to the texture unit, you then also have to bind the sampler object.

GLuint texture_unit = //The texture unit to which your texture is bound
glBindSampler(texture_unit, sampler);

Don't forget to unbind the sampler after drawing with glBindSampler(texture_unit, 0); since samplers are bound to the texture unit and not to a specific shader.

Note, that since you are using rectangular textures, mipmapping is not supported.

Upvotes: 4

Related Questions