Search code examples
opengl3dtextures

How to avoid blurry effect?


I'm currently working on a 3d project with Opengl but i have a blurry effect problem with distant block.

enter image description here

Like you can see, the nearby blocks are fine but the the blocks become blurry very quickly with the distance. (ps: ignore red block)

I tried differents resolutions of image (1024x1024 and 2048x2048) but with same result.

I tried to modify GL_TEXTURE_2D options but not fixed my problem.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

if someone have an idea how to deal with this problem, it will be good. thx in advance


Solution

  • Posted as comment, now as proper answer so it can get marked as answered.

    When looking at textured surfaces, especially distant ones, the frequency of texture features may exceed the necessary sampling frequency (pixels of the framebuffer/screen; see Nyquist limit). To combat this, MIP-mapping is employed, which reduces the feature frequency (minification). However, this does not account for perspective distortion. Anisotropic filtering places samples on the texture in a trapeze to correct for angled textures.

    In OpenGL, you need either version 4.6 or EXT_texture_filter_anisotropic. You can then enable anisoptropic filtering per texture with glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, num_samples) - this sets the maximum number of samples the hardware may use, any given texture fetch may use less though if it deems more to be excessive. Be aware that the upper limit you can set is implementation-defined and must be queried with glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_samples). More samples mean higher quality (reasonably up until 16 or so), but also higher performance cost (not usually a deal-breaker).