Home > Software design >  weird line pattern when rendering big images
weird line pattern when rendering big images

Time:11-22

i'm trying to render a font, but when i'm rendering it small relative to the texture (the texture is 2048x2048 one channel, but it's a full bitmap, so every character is small in the image) it creates a weird squares/lines pattern.

enter image description here

(the texture is blurry because it's sdf, but i've used a normal shader for debugging)

vertex shader: very visible on the 'e' and 'p'

#version 330 core
in vec4 vertex;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec2 TexCoord;

void main()
{
    gl_Position = projection * view * model * vec4(vertex.xy, 0.0, 1.0);
    TexCoord = vertex.zw;
}

fragment shader:

#version 330 core
out vec4 FragColor;

in vec2 TexCoord;

uniform sampler2D ourTexture;

void main()
{
    FragColor = vec4(1.0, 1.0, 1.0, texture(ourTexture, TexCoord).r);
}

texture code:

int width, height, nrChannels;
    unsigned char *img_data = stbi_load(image, &width, &height, &nrChannels, 1);

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

    glBindTexture(GL_TEXTURE_2D, font->texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, img_data);
    glGenerateMipmap(GL_TEXTURE_2D);

(yes, the image only has one channel)

that effect is very anoying and doesn't seem to go away. it persists even when GL_TEXTURE_MIN_FILTER is set to GL_LINEAR

CodePudding user response:

glTexParameteri sets a parameter of the currently bound texture. Therefore you have to call glTexParameteri after glBindTexture. e.g.:

glBindTexture(GL_TEXTURE_2D, font->texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  • Related