rougier / freetype-gl

OpenGL text using one vertex buffer, one texture and FreeType
Other
1.65k stars 266 forks source link

Problems loading/getting glyphs #178

Closed DarkShinz closed 6 years ago

DarkShinz commented 6 years ago

Hello guys, Its my first time with this library, Ive been trying to implement it in my C++ app to use. As Im starting, ive using functions from demo projects to learn, though during some tests Ive tracked an error back to the glyphs loading moment when it adds the text with the "add_text(...)" function. Its not returning a null glyph but an empty one. Ive read that it may happen if the atlas size is not big enough, though Ive checked that also. Im using same configurations from benchmark.c (VeraMono.ttf, font size: 12 and atlas size: 512x512x1). Also Ive tried loading the font file with texture_font_new_from_file and texture_font_new_from_memory and both, binding the texture before loadin glyphs: same error. Also I don't know how to debugg into the library to track where the line error is. What could cause it to fail loading? Help please :) -----Code---- These functions are being compilated with MSVS 2015 My app uses GLAD as OpenGL API: Ive edited opengl.h to load with it

This is how the variables are created:

std::string fullPath = "files/fonts/VeraMono.tff";
_fontSize = 12.0f; //float type
size_t depth = 1;
size_t width = 512;
size_t height = 512;

_fontTextureAtlas = texture_atlas_new(width, height, depth);
_font = texture_font_new_from_file(_fontTextureAtlas, _fontSize, fullPath.c_str());
if (!_font)
    logger.fatal("Error loading font.");

    //Gen texture
    glGenTextures(1, &_fontTextureAtlas->id);
    glBindTexture(GL_TEXTURE_2D, _fontTextureAtlas->id);
    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_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, _fontTextureAtlas->width, _fontTextureAtlas->height,
        0, GL_RED, GL_UNSIGNED_BYTE, _fontTextureAtlas->data);

    if (_fontTextureAtlas->id == 0)
        logger.fatal("No texture loaded.");

This is the function I've copied and adapted from benchmark.c

void Font::addText(const char* text, vertex_buffer_t* VBO, PointF& pen, const handAbilities& hAbilities)
{
    //Setup hand abilities
    _font->hinting = hAbilities.hinting;
    _font->rendermode = hAbilities.rendermode;
    _font->outline_thickness = hAbilities.outline_thickness;
    _font->filtering = hAbilities.filtering;
    _font->kerning = hAbilities.kerning;

    //This function will setup buffer object.
    size_t i;
    float r = hAbilities.color.rF(), g = hAbilities.color.gF(), b = hAbilities.color.bF(), a = hAbilities.color.aF();
    for (i = 0; i < strlen(text); ++i)
    {
        texture_glyph_t *glyph = texture_font_get_glyph(_font, text + i);
        if (glyph != NULL)
        {
            float kerning = 0.0f;
            if (i > 0)
            {
                kerning = texture_glyph_get_kerning(glyph, text+i - 1);
            }

            pen.x += kerning;
            int x0 = (int)(pen.x + glyph->offset_x);
            int y0 = (int)(pen.y + glyph->offset_y);
            int x1 = (int)(x0 + glyph->width);
            int y1 = (int)(y0 - glyph->height);
            float s0 = glyph->s0;
            float t0 = glyph->t0;
            float s1 = glyph->s1;
            float t1 = glyph->t1;
            GLuint index = VBO->vertices->size;
            GLuint indices[6] = { index, index + 1, index + 2,
                index, index + 2, index + 3 };

            vertex_t vertices[4] = { 
            { (float)x0,(float)y0, 0,  s0,t0,  r,g,b,a },
            { (float)x0,(float)y1, 0,  s0,t1,  r,g,b,a },
            { (float)x1,(float)y1, 0,  s1,t1,  r,g,b,a },
            { (float)x1,(float)y0, 0,  s1,t0,  r,g,b,a } };

            logger.print(stdext::format("Vertice Rect: x%f y%f w%f h%f : GLYPHS: x%f y%f w%f h%f ", x0, y0, x1, y1, glyph->offset_x, glyph->offset_y, glyph->width, glyph->height));

            vertex_buffer_push_back_indices(VBO, indices, 6);
            vertex_buffer_push_back_vertices(VBO, vertices, 4);

            pen.x += glyph->advance_x;
        }
    }
}
rougier commented 6 years ago

Do the demo files work as expected ?

DarkShinz commented 6 years ago

@rougir Thanks for the answer. Yes they are working. I thought the possibility that a linkage error may be wrong with FreeType lib, though If that were true the texture_font struct would fail at initialization..

rougier commented 6 years ago

It's hard to tell where is the error in your implementation. One option would be to start with a working demo and to repplace some parts with your C++ port to check when it breaks.

DarkShinz commented 6 years ago

@rougier Ill do that, Ill start replacing GLEW for GLAD for benchmark demo. Lets see ^^

DarkShinz commented 6 years ago

The demo's ran as expected with GLAD. Just had to switch for GLEW includes in files and project.. and Ive noticed a mistake by my side. I was printing the glyphs values as float and obviously the results were 0.00. So that was not the problem at all. Sorry about that. It mostly an OpenGL error by my lacking of expertise in the matter producing a black screen. @rougier Thank you for the attention and sorry for the bothering! Happy new year ! :)