1. 4FipS.com
  2. Photos
  3. Videos
  4. Code
  5. Forums
  6. pfQuizzz
  7. About

4FipS.com Forums

Forums dedicated to the projects hosted at 4FipS.com

Skip to content

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

> About coding, hacking, photography, and such...

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

Postby FipS on Fri Aug 29, 2014 9:43 pm

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);
    }
}
User avatar
FipS
Site Admin
 
Posts: 144
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague

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

Sponsor

Sponsor
 

Return to 4FipS.com General Discussion & Blog

Who is online

Users browsing this forum: Google Adsense [Bot], Majestic-12 [Bot] and 2 guests