c++ - 使用 FreeType/GLFW 根本不呈现文本

标签 c++ text glfw freetype

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

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; 

关于c++ - 使用 FreeType/GLFW 根本不呈现文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16628515/

相关文章:

3d - GLFW - 不支持 Vulkan

c++ - 无法部署 GLFW 3.2

c++ - 为什么我的程序会跳过 while 循环? 2 大于 -1

c++ - 在这种情况下是否有必要进行范围解析?

html - 无法将标题旁边的段落与多行换行对齐

php - 如何从没有任何定界符或分隔符的文本文件中导入数据?

macos - GLFW 打开 OpenGL 3.2 上下文但 Freeglut 不能 - 为什么?

c++ - 如何将 void* 放入 COM VARIANT,以便它被 COM-Interop 编码为 IntPtr 到 .NET?

c++ - 我可以在 C++ 中执行以下操作吗

html - 如何将颜色应用于 SVG 文本元素