When should I set GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER?

12,343

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.

Share:
12,343
xdu
Author by

xdu

Minimalist

Updated on June 27, 2022

Comments

  • xdu
    xdu almost 2 years

    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.