HonestHeuristic
HonestHeuristic

Reputation: 336

Freetype glyphs wrap when loaded into openGL

I'm trying to load TrueTypeFonts through freetype and display them using openGL and this is what I'm getting:

Font application

As you can see mostly it's fine but if you look closer each individual glyph seems to have some small incongruities around the borders. I noticed that these strange lines are actually carried over pixels from the other side. Look at the 'T' and 'h' in particular where you can see small bars corresponding with the opposite side of the texture. This happens with different fonts as well. Here is the code responsible for copying the glyph bitmap buffer into openGL:

    void load(FT_GlyphSlot glyphSlot, double loadedHeight){
        this->loadedHeight = loadedHeight;
        int width = glyphSlot->bitmap.width;
        int height = glyphSlot->bitmap.rows;

        sizeRatios = Vector2D(width / loadedHeight, height / loadedHeight);
        offsetRatios = Vector2D(glyphSlot->bitmap_left / loadedHeight, glyphSlot->metrics.horiBearingY / loadedHeight);
        advanceRatios = Vector2D((glyphSlot->advance.x >> 6) / loadedHeight, (glyphSlot->advance.y >> 6) / loadedHeight);

        std::cout << width << ", " << height << std::endl;

        GLubyte * textureData = new GLubyte[width * height * 4];

        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                for(int i = 0; i < 4; i++){
                    textureData[(x + y * width) * 4 + i] = glyphSlot->bitmap.buffer[x + width * y];
                }
            }
        }

        texture.bind();
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        texture.load(GL_TEXTURE_2D, 0, GL_RGBA, glyphSlot->bitmap.width, glyphSlot->bitmap.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
        // glGenerateMipmap(GL_TEXTURE_2D);

        delete [] textureData;
    }

The size of the font face is set elsewhere and passed into this method along with the glyph slot that I want to load. The texture object is just a class that creates a texture handle and keeps track of it, load just passes the parameters directly into glTexImage2D().

I've tried shifting the pixels by one using modulus rotation and it worked vertically but not horizontally. I have also tried loading the texture by passing the buffer directly into load by changing the format to GL_RED as described here but the problem doesn't go away, so I think that maybe it might even be a flaw in freetype?

I wonder is there is some basic element of texture loading that I do not understand.

If you need some additional source code to understand what is wrong please ask.

Upvotes: 4

Views: 728

Answers (1)

genpfault
genpfault

Reputation: 52090

GL_TEXTURE_WRAP_S/GL_TEXTURE_WRAP_T default to GL_REPEAT.

Use GL_CLAMP_TO_EDGE instead.

Upvotes: 4

Related Questions