How to use OpenGL LOD in Texturing?

debonair picture debonair · Apr 4, 2013 · Viewed 7.5k times · Source

How do GL_TEXTURE_MIN_LOD, GL_TEXTURE_MAX_LOD and LOD_BIAS work?

To check it visually, i have created 6*6 texture with mipmapping and for values > 0.5 for MIN_LOD I get a 3*3 texture irrespective of values for MAX_LOD. If I change LOD_BIAS it does not affect my o/p. I am not able to figure out how it works exactly.

Can anyone explain it by stating an example?

Edit: I am creating mipmap levels manually so that I can observe which level it is picking up. Here is my code:

glTexImage2D(target, 0, GL_RGBA,9 ,9, 0, GL_RGBA, GL_BYTE,subpix);
glTexImage2D(target, 1, GL_RGBA,4 ,4, 0, GL_RGBA, GL_BYTE,&subpix[4]);
glTexImage2D(target, 2, GL_RGBA,2 ,2, 0, GL_RGBA, GL_BYTE,&subpix[10]);
glTexImage2D(target, 3, GL_RGBA,1 ,1, 0, GL_RGBA, GL_BYTE,&subpix[18]);
glSamplerParameterf(sampler,GL_TEXTURE_MIN_LOD,0.862);
glSamplerParameterf(sampler,GL_TEXTURE_MAX_LOD,0.99);
glSamplerParameterf(sampler,GL_TEXTURE_LOD_BIAS,0.0);

In this case I am expecting it would take 2nd mipmap level which is of 2*2 but It chooses 1st mipmap level of 4*4. When I set min lod < 0.5, It takes 0th level of 9*9. And it this happens irrespective of the value set to max lod.

Answer

Bartek Banachewicz picture Bartek Banachewicz · Apr 4, 2013

First some references:

TEXTURE_MIN_LOD Sets the minimum level-of-detail parameter. This floating-point value limits the selection of highest resolution mipmap (lowest mipmap level). The initial value is -1000.

TEXTURE_LOD_BIAS specifies a fixed bias value that is to be added to the level-of-detail parameter for the texture before texture sampling. The specified value is added to the shader-supplied bias value (if any) and subsequently clamped into the implementation-defined range - bias max bias max , where bias max is the value of the implementation defined constant GL_MAX_TEXTURE_LOD_BIAS. The initial value is 0.0

So if you are surprised by the mipmap selection, I suggest following steps (not necessarily in order):

  • Create mipmaps by hand so that they are visually distinctive from each other
  • Verify your mipmaps are supplied correctly.
  • Verify that your LOD_BIAS setting doesn't just put all of the values outside the range, making the sampler effectively always use the maximum or minimum LOD.

Searching around this, I've found textureQueryLod . It might also be of some interest to you to aid debugging.