-2.4 C
New York
Monday, December 22, 2025

picture – Texture in OpenGL changing into pixelated when it will get smaller


I am engaged on a second renderer in OpenGL and I’ve acquired sprites rendering however they get pixelated and decrease high quality because the digicam strikes away from them.

enter image description here

On one thing like Krita, the picture would not get decrease high quality as you zoom out:

enter image description here

I additionally tried utilizing GL_LINEAR however that makes the picture get too blurry.
That is my texture loading code:

unsigned int LoadTexture(const char* filePath)
{
    stbi_set_flip_vertically_on_load(true);
    unsigned int textureID;
    glGenTextures(1, &textureID);

    int            width, top, nrComponents;
    unsigned char* information =
        stbi_load(filePath, &width, &top, &nrComponents, 0);
    if (information)
    {
        GLenum format{};
        if (nrComponents == 1)
            format = GL_RED;
        else if (nrComponents == 3)
            format = GL_RGB;
        else if (nrComponents == 4)
            format = GL_RGBA;

        glBindTexture(GL_TEXTURE_2D, textureID);
        glTexImage2D(GL_TEXTURE_2D, 0, format, width, top, 0,
                     format, GL_UNSIGNED_BYTE, information);
        glGenerateMipmap(GL_TEXTURE_2D);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                        format == GL_RGBA ? GL_CLAMP_TO_EDGE
                                          : GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                        format == GL_RGBA ? GL_CLAMP_TO_EDGE
                                          : GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                        GL_NEAREST_MIPMAP_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                        GL_NEAREST);

        glBindTexture(GL_TEXTURE_2D, 0);
        stbi_image_free(information);
    }
    else
    {
        std::cout << "Texture did not load at path: " << filePath
                  << 'n';
        stbi_image_free(information);
        return LoadTexture("res/textures/MissingTexture.png");
    }

    return textureID;
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles