0

I'm using OpenTK wrapper in C# for OpenGL 3.1. I'm rendering a scene where the same textures are used for near and far away objects. When I don't use MipMap the textures are very sharp (even if the textured object is very small) but the scintillation (if I understand the term properly) is visible - pixels of the texture seems blinking when the textured object is moving far away. So I tried to to use mipmaps. After changing to TextureMinFilter to LinearMipmapLinear and calling GL.GenerateMipmap(GenerateMipmapTarget.Texture2D) the scintillation effects is gone, but the textures are very blurred, here are images for comparison:

no mipmaps mipmaps

It looks like automatic generation of mipmaps require some tweaking, but I couldn't find any way of telling OpenTK how many mipmaps should be build or what method should be used to downscale original images. This is a code used for loading textures:

var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.BindTexture(TextureTarget.Texture2D, textures[idx]);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp.Width, bmp.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);  
bmp.UnlockBits(data);

What could be done to improve texture sharpness when using mipmaps?

UPDATE:

After generating mipmaps manually the effect is exactly the same.

SOLUTION: I followed datenwolf's answer and used anisotropic texture filtering - that eliminated the problem. I enabled it with code:

        float maxAniso;
        GL.GetFloat((GetPName)ExtTextureFilterAnisotropic.MaxTextureMaxAnisotropyExt, out maxAniso);
        GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, maxAniso);

List of supported extensions can be obtained with:

var extensions = GL.GetString(StringName.Extensions).Split(' ');
PanJanek
  • 6,593
  • 2
  • 34
  • 41
  • 1
    Regarding how many mipmaps are built, there's a very systematic way that is done. I have outlined that in another [answer to a similar question](http://stackoverflow.com/questions/21284586/glteximage2d-vs-glubuild2dmipmaps/21290595#21290595). You may find glancing over that useful. – Andon M. Coleman Oct 31 '14 at 16:17

1 Answers1

1

There are two main possible culprits here: There may be onla a low quality downscaling filter in the OpenGL implementation's glGenerateMipmap. A slight anisotropy in texture coordinates that may cause a blurrier filtering.

Remedies:

  • Implement your own image downscaling and pass each mipmap level individually (glTexImage2D level parameter) and not rely on glGenerateMipmaps

  • Use anisotropic texture filtering

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I'll give that a try. As for now I noticed that adding GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 3); makes the textures less blurred, but that's only because that forces OpneGL to use only few of generated mipmaps. – PanJanek Oct 31 '14 at 10:03
  • I tested applying my own downscaled images (they are sharp) but they are blurred the same way when used as textures by openGL. So it's not the problem with GenerateMipmap but with the way LinearMipmapLinear option is interpreted. I couldn't find how to use anisothropic filtering in OpenTK – PanJanek Oct 31 '14 at 11:04
  • @PanJanek: That's because for historical intellectual property (i.e. patent) reasons it's a non-ARB OpenGL extension. See the end of the [Arcsynthesis Anisotropy Tutorial](http://www.arcsynthesis.org/gltut/Texturing/Tut15%20Anisotropy.html) for the detailed explanation. – datenwolf Oct 31 '14 at 12:01