Page 1 of 1

Vector font rasterization using MSAA (C++/OpenGL)

PostPosted: Fri Aug 29, 2014 9:43 pm
by FipS
I've been recently playing with OpenGL graphics a bit and needed a simple way to print text. Instead of spending days and weeks implementing a proper bitmap font renderer, I decided to go quick and dirty this time with minimal effort. So I took Simplex - A freely available vector font, and in a couple of minutes wrote a simple text rendering routine. I was quite curious how good or bad the result would look like, and frankly was quite horrified after seeing the aliased shapes, which came out (especially in small fonts), see Fig1. below. So I went on by enabling MSAA (Multisample anti-aliasing) to make this thing usable, and after all it's not that bad, see Fig2. I think it's good enough for my purpose.

Fig1. Anti-aliasing: OFF
Image

Fig2. Anti-aliasing: ON / MSAA 16x
Image

For the sake of completeness, here's the text rendering routine:
Code: Select all
extern const int8_t g_simplex_font_data[95][112];
void put_text(canvas &cvs, const vec3_f &pos, const char *text, const color4 &color, float scale)
{
    const auto v3 = vec3_make<float>;
    auto pen = pos;

    for(const auto *c = text; *c; ++c)
    {
        const auto ch = *c;
        const auto *it = g_simplex_font_data[ch - 32];

        const auto nvtcs = *it++;
        const auto spacing = *it++;

        auto from = v3(-1, -1, 0);

        for(size_t i = 0; i < nvtcs; ++i)
        {
            const auto x = *it++;
            const auto y = *it++;

            const auto to = v3(x, y, 0);

            if((from.x != -1 || from.y != -1) && (to.x != -1 || to.y != -1))
            {
                cvs.put_line(pen + from * scale, pen + to * scale, color);
            }

            from = to;
        }

        pen += v3(spacing * scale, 0, 0);
    }
}