文本不渲染与FreeType/GLFW

Text not rendering at all with FreeType/GLFW

本文关键字:FreeType GLFW 文本      更新时间:2023-10-16

我只是得到清晰的颜色;文本没有渲染,我的着色器没有给出任何错误;这是我的调试输出

Initializing FreeType version 2.4.10...
Opening font file FreeSans.ttf...
Loading glyph set and shaders...
Compiling shader textshader.vs...
Compiling shader textshader.fs...
Linking program...
Drawing text...
16.666667 ms/frame

这是我的绘制函数

void text::draw(const char* text, float x, float y, float sx, float sy) {
    const char *p;
    FT_GlyphSlot g = face->glyph;
    GLuint tex;
    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glUniform1i(uniform_tex, 0);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    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);
    glEnableVertexAttribArray(attribute_coord);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(attribute_coord, 4, GL_FLOAT, GL_FALSE, 0, 0);
    for(p = text; *p; p++) {
        if(FT_Load_Char(face, *p, FT_LOAD_RENDER))
            continue;
        glTexImage2D(
            GL_TEXTURE_2D,
            0,
            GL_ALPHA,
            g->bitmap.width,
            g->bitmap.rows,
            0,
            GL_ALPHA,
            GL_UNSIGNED_BYTE,
            g->bitmap.buffer
        );
        float x2 = x + g->bitmap_left * sx;
        float y2 = -y - g->bitmap_top * sy;
        float w = g->bitmap.width * sx;
        float h = g->bitmap.rows * sy;
        GLfloat box[4][4] = {
            {x2,    -y2     , 0, 0},
            {x2 + w,-y2     , 1, 0},
            {x2,    -y2 - h , 0, 1},
            {x2 + w,-y2 - h , 1, 1}
        };
        glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
       x += (g->advance.x >> 6) * sx;
       y += (g->advance.y >> 6) * sy;
    }
    glDisableVertexAttribArray(attribute_coord);
    glDeleteTextures(1, &tex);
}

这里是我执行绘制函数的地方

void window::handleEventsAndRender() {
    if(!isOpen) {
        printf("Must open a window first to render and handle events!");
        return;
    }
    float sx = 2/1024;
    float sy = 2/786;
    text test("FreeSans.ttf");
    glUseProgram(test.textProgram);
    GLfloat black[4] = {0, 0, 0, 1};
    glUniform4fv(test.uniform_color, 1, black);
    printf("Drawing text...n");
    while(glfwGetWindowParam(GLFW_OPENED)) {
        glClearColor(1, 1, 1, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        glDisable(GL_CULL_FACE);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        test.draw("The Quick Brown Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 50 * sy, sx, sy);
        glfwSwapBuffers();
        printFPS();
    }
    glfwTerminate();
}

最后,这是我的着色器

顶点
#version 120
attribute vec4 coord;
varying vec2 texpos;
void main(void) {
    gl_Position = vec4(coord.xy, 0, 1);
    texpos = coord.zw;
}

片段

#version 120
varying vec2 texpos;
uniform sampler2d tex;
uniform vec4 color;
void main(void) {
    gl_FragColor = vec4(1, 1, 1, texture2D(tex, texpos).a) * color;
}

有人能帮帮我吗?

我想你的问题来自于sx和sy的初始化

float sx = 2/1024; // sx == 0, 2 is int and 1024 is int. 2 / 1024 -> sx == 0
// ...
test.draw("The Quick Brown Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 50 * sy, sx, sy); 
// is actually calling like this
test.draw("The Quick Brown Fox Jumps Over The Lazy Dog", -1, 1, 0, 0);
// then in text::draw
float w = g->bitmap.width * sx; -> w == 0

修复sx和sy初始化:

float sx = float(2)/float(1024);
// or
float sx = 2.0f / 1024.0f;