1. sherief
  2. Calligrapher

Wiki

Clone wiki

Calligrapher / Home

Rendering a string to an OpenGL Texture

#include <calligrapher/calligrapher.hpp>

GLuint render_string_to_texture(const std::string& String, FT_Face Face)
{
    GLuint TextureID;
    vector<calligrapher::glyph_metrics> GlyphMetrics = calligrapher::string_metrics(text, face);
    calligrapher::string_extents Dimensions = calligrapher::string_dimensions(GlyphMetrics);
    //Metrics passer to render_string*() MUST be normalized.
    normalize(GlyphMetrics, Dimensions);
    //
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, &TextureID);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glBindTexture(GL_TEXTURE_2D, TextureID);
    unsigned char* Data = new unsigned char [Dimensions.size.x * Dimensions.size.y];
    calligrapher::render_string_grayscale_u8norm(text, face, Dimensions, GlyphMetrics, Data);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA8, Dimensions.size.x, Dimensions.size.y, 0, GL_ALPHA, GL_UNSIGNED_BYTE, Data);
    delete [] Data;
    return TextureID;
}

Updated