Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I set GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER?

By definition the GL_TEXTURE_MIN_FILTER is used when the pixel being textured maps to an area that is larger than one texture element.

Say I have a texture with 1024*768 resolution, and I want to map it to a rectangle. The projection matrix is set using:

glOrtho(0, 1024, 0, 768, 0, 1);

and the rectangle is set with:

glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1024.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1024.0f, 768.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 768.0f, 0.0f);

With the current configuration, my understanding is each pixel on the rectangle is mapped to one single pixel in the texture.

Yet after test I found out the MIN_FILTER is working for this configuration. Even if I double the size of the rectangle, namely 2048*1536 and only the lower left corner is shown in the viewport, it's still the MIN_FILTER that is functioning.

Under such situation, my personal understanding is, each pixel in the viewport is mapped to only a quarter pixel in the texture, why it's still MIN_FILTER working?

For testing which filter is working, I just comment out the line for setting that filter and see if the result is wrong. If I comment out the MIN_FILTER setting line, the result would be totally yellow, cause I did not set the max level of texture to accord with its level. But if I comment out the MAG_FILTER line, the result is still correct.

like image 280
xdu Avatar asked Sep 11 '12 05:09

xdu


People also ask

What does GL_ REPEAT do?

GL_REPEAT causes the integer part of the s coordinate to be ignored; OpenGL uses only the fractional part, thereby creating a repeating pattern. Border texture elements are accessed only if wrapping is set to GL_CLAMP. Initially, GL_TEXTURE_WRAP_S is set to GL_REPEAT.

What does glTexParameter do?

The glTexParameter function assigns the value or values in params to the texture parameter specified as pname. The target parameter defines the target texture, either GL_TEXTURE_1D or GL_TEXTURE_2D. As more texture elements are sampled in the minification process, fewer aliasing artifacts will be apparent.

What is GL_ texture_ MIN_ FILTER?

Meaning. GL_TEXTURE_MIN_FILTER. The texture minifying function is used whenever the pixel being textured maps to an area greater than one texture element. There are six defined minifying functions. Two of them use the nearest one or nearest four texture elements to compute the texture value.

What is OpenGL texture?

A texture is an OpenGL Object that contains one or more images that all have the same image format. A texture can be used in two ways: it can be the source of a texture access from a Shader, or it can be used as a render target.


1 Answers

As I understand it, you comment out the setting of GL_TEXTURE_MIN_FILTER and the texturing doesn't work anymore. From this you conclude that during texturing the min filter is used. And exactly this dedcution is wrong.

When not setting the min filter it won't just magically not work. Instead the default of GL_NEAREST_MIPMAP_LINEAR is used and since you don't specify texture images for the other mipmap layers, it won't work (as you somehow guessed yourself, I think). But it is not that the result is only wrong if the min filter is actually used for a certain pixel. The texture won't ever work no matter what filter is used, since it is incomplete (i.e. using mipmapping while not having valid images for each mipmap level). So it won't work for mag-filtering, too. It's not the filter that is broken, it is the whole texture.

On the other hand the GL_TEXTURE_MAG_FILTER doesn't have mipmapping modes and when you don't set it, it uses the default of GL_LINEAR. So the texture is always complete and works, no matter if you set the mag-filter or not.

So your method of checking if a certain filter is used is rubbish and your implementation is probably doing exactly what you have thought in theory.

So to conclude:

  1. You don't need to set the min and mag filters for a texture object. If you don't, they'll use default values. But by default the min-filter uses mipmapping and requires you to specify images for each mipmap level, otherwise the texture is incomplete (i.e. won't work), no matter what filter is used for a particular fragment.

  2. It is a good idea to always explicitly set the filter modes for each texture object to not rely on any default values you might not be sure of.

  3. When a pixel's size exactly (what is "exactly" in this contex anyway?) matches the size of a texel, it is hard to say which of the two filtering modes is used, but there is no easy way to check which one is used for a particular fragment. But if you need pixel perfect texturing anyway (like for just drawing a bitmap to a 2D screen), then using GL_NEAREST for both min and mag filters is probably the best idea and frees you from thinking about which filter is actually used, anyway.

like image 65
Christian Rau Avatar answered Oct 19 '22 13:10

Christian Rau