Home Game Development graphics – How can I render a font in C with OpenGL?

graphics – How can I render a font in C with OpenGL?

0
graphics – How can I render a font in C with OpenGL?

[ad_1]

I used to be testing some issues with a view to render textual content with stb_truetype.h and OpenGL in C.

I took as a reference the instance that seems right here.
Basically, this instance, hundreds a .ttf file and returns the uncooked data in bytes, that can be utilized to generate a texture in OpenGL.

I tailored the instance, talked about earlier than, into fashionable OpenGL, as a result of, the instance makes use of OpenGL deprecated capabilities, like glVertex2f. The solely factor I get to output on display screen was this sort of noise of unusual colours:

Noisy output

texture_t fnt_texture;
GLuint fnt_shader;

unsigned char ttf_buffer[1 << 20];
unsigned char temp_bitmap[512 * 512];

stbtt_bakedchar cdata[96]; // ASCII 32..126 is 95 glyphs

#outline FONT_VS
    "#model 330 coren"
    "format(location = 0) in vec3 m_Position;"
    "format(location = 1) in vec2 m_TexCoords;"
    "out vec2 TexCoords;n"
    "void predominant() {n"
    "TexCoords = m_TexCoords;n"
    "gl_Position = vec4(m_Position, 1.0);n"
    "}n"

#outline FONT_FS
    "#model 330 coren"
    "in vec2 TexCoords;n"
    "uniform sampler2D Texture;n"
    "void predominant() {n"
    "gl_FragColor = texture(Texture, TexCoords);n"
    "}n"

void font_init(void)
{
    fread(ttf_buffer, 1, 1<<20, fopen("c:/home windows/fonts/instances.ttf", "rb"));
    stbtt_BakeFontBitmap(ttf_buffer, 0, 32.0, temp_bitmap, 512, 512, 32, 96, cdata); // no assure this suits!

    glGenTextures(1, &fnt_texture[3]); // My texture kind, is an array that saves the feel on the third place.
    glBindTexture(GL_TEXTURE_2D, fnt_texture[3]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp_bitmap);
    glGenerateMipmap(GL_TEXTURE_2D);

    // can free temp_bitmap at this level
    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_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glBindTexture(GL_TEXTURE_2D, 0);

    fnt_shader = shader_init(FONT_VS, FONT_FS);
}

void font_render(model_t mannequin) 
{
    shader_bind(fnt_shader);
    texture_bind(fnt_texture, 0);

    model_begin(mannequin);
    model_draw(mannequin, GL_TRIANGLES); // The mannequin (vao, vbo, ibo) is rendering the entire buffer, not particular person glyphs
    model_end();

    texture_unbind();
    shader_unbind();
}

Can somebody inform me what I’m doing improper, and, how I’m suposed to render appropriately textual content, with fashionable OpenGL, with textures and buffers, with a view to learn the .ttf file and create the required data with stb_truetype.h and, then, render the textual content?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here