4

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

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;
}

有人可以帮帮我吗?

4

1 回答 1

1

我认为您的问题来自 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; 
于 2015-12-11T12:38:23.630 回答