Search code examples
opengltexturesmipmaps

Texture getting pixelated instead of blurry


I have created a simple OpenGL application. When zooming into a textured quad, the texture becomes pixelated instead of blurry. I would guess that is due to missing mipmaps?

I create my texture like this:

  glGenTextures(1, &mTexture);
  glBindTexture(GL_TEXTURE_2D, mTexture);
  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_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

And I update it from a PBO like this:

 glBindTexture(GL_TEXTURE_2D, mTexture);
  glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, mPboIds[mPboIndex]);

  glTexSubImage2D(
    GL_TEXTURE_2D,
    0, 0, 0,
    frame->GetWidth(),
    frame->GetHeight(),
    GL_RGB,
    GL_UNSIGNED_BYTE,
    0);

I thought that GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER would tell OpenGL to generate the mipmaps. Ain't that the case? How can I enable mipmap generation for my texture?


Solution

  • The magnification filter is used when you increase the zoom on a texture, and can have two values:

    • GL_NEAREST - Returns the value of the texture element that is nearest (in Manhattan distance) to the specified texture coordinates.

    • GL_LINEAR - Returns the weighted average of the texture elements that are closest to the specified texture coordinates. These can include items wrapped or repeated from other parts of a texture, depending on the values of GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T, and on the exact mapping.

    In your case, you are use the wrong magnification filter. It must be GL_LINEAR to minimize the pixelated effect.

    MipMaps, for the other hand is used when you want to decrease the zoom and want to get a smooth transition when the texture start to become too far away from the viewer. To get more information about MipMaps, you can look at glTexParameter manual pages, at section GL_TEXTURE_MIN_FILTER, and how to generate in glGenerateMipmap manual page.