Is it possible to apply a separate interpolation method when creating a texture from a cropped image?

I am not cropping the source image in memory, but I am specifying a cropping area and generating only that part as a texture. The problem is that when I use GL_LINEAR, the edges are referenced outside of the crop area, so I don’t get the texture I want. On the other hand, when I use GL_NEAREST, the edges are textured with the color I want, but the rest of the image looks bad quality.

For example,
I want to crop 120 pixels from the top and bottom of a 1280x720 source image and set (x, y, width, height) : (0, 120, 1280, 480), and make this a texture.
When I use GL_LINEAR, the line (x, y) : (0, 121) to (1280, 121), which is the boundary in coordinates, is referenced by bilinear to the line (0, 120) to (1280, 120), so the boundary part is filled with pixel values that I don’t want.

I have no idea how to solve this problem, I don’t know if I should create separate textures for each and control the coordinates in the shader, or if a mipmap or something could be a solution. Anyone has good idea?

It’s unclear what you’re doing. In one part you said you’re generating a part as a texture; in another, you’re just contemplating this.

It sounds like you want to render a subset of an “existing” texture with linear interpolation:

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

but you want the edges of the subset to look like you’d created a new texture for the subset and rendered it with:

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

Right?

If so, you can get close by “insetting” your texture coordinates by 1/2 pixel. Then the linear interpolation will never pull in texels outside the subset region.

To take as an example using the “the whole texture” as your subset (yes, not so useful, but easy to explain):
instead of running your texcoords from 0..1, run them from x..(1-x), where x = 1 / ( 2 * width ) where width is the width of your texture (in texels). x is of course 1/2 the width of a texel in texcoords.

For other subsets, do the same, but instead of starting with 0…1, start with the texcoord range defining your subset (texel edge to texel edge).

You’re right, thanks for clarifying.

Thanks for your idea, I’m currently implementing based on weston’s gl-renderer code which is open source, so controlling the texture coordinates will be a difficult task for me. I’ll check if it’s possible with a simple test code first.

If you control the shaders, keep in mind you can embed this texcoord tweak logic in the shader code. Then the source texcoords in the vertex data don’t need to change. Of course, your shader generator needs to know when to apply it.

1 Like