1

当我运行我的程序时,窗口显示大约 3 秒(如预期的那样,背景为白色,但没有文本)然后中断,说在 FreeType 代码中存在访问冲突。我很确定这是加载字体,因为我的调试文本打印它找不到它,而且它也不能使用面部的宽度和高度,所以它返回一个错误。我试过将 arial.ttf 放在 .exe 目录、项目目录、任何地方。没有骰子。这是我的 main.cpp:

int main()
{
        int running = GL_TRUE;

        debug.Print("Program initialized...");
        window.Create( 800, 600, "OpenGL");

        TEXT text;

        while( running )
        {
                glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
                glClear( GL_COLOR_BUFFER_BIT );

                glEnable(GL_BLEND);
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

                text.Create("The quick brown fox jumps over the lazy dog", "arial.ttf", 300, 400, 1, 1, 1,   2/600, 2/800);

                glfwSwapBuffers();
        }

        glDeleteTextures(1, &tex);

        glDeleteProgram( shaderProgram );
        glDeleteShader( fragmentShader );
        glDeleteShader( vertexShader );

        glDeleteBuffers( 1, &vbo );

        debug.Print("Program terminated.");
}

这是我的Text.h:

#ifndef _TEXT
#define _TEXT

#include "debug.h"
#include <GL/glew.h>
#include <GL/glfw.h>
#include <ft2build.h>
#include FT_FREETYPE_H

class TEXT
{
public:
        void Create(char* Text, char* fontName, int posx, int posy, int r, int g, int b, int sizex, int sizey);
};

#endif

这是我的 Text.cpp:

#define GLEW_STATIC
#include "text.h"

const char* textvertexSource =
        "version 150\n"
        "in vec4 coord;"
        "out vec2 texcoord;"
        "void main {"
        "       gl_Position = vec4(coord.xy, 0, 1);"
        "       texcoord = coord.zw;"
        "}";

const char* textfragmentSource =
        "version  150\n"
        "in vec2 texcoord;"
        "uniform sampler2D tex;"
        "uniform vec4 color;"
        "void main() {"
        "       gl_FragColor = vec4(1, 1, 1, texture2D(tex, texcoord).a * color;"
        "}";

void TEXT::Create(char* text, char* fontName, int posx, int posy, int r, int g, int b, int sizex, int sizey)
{

        // OpenGL functions
        GLuint vbo;
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);

        GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
        glShaderSource( vertexShader, 1, &textvertexSource, NULL );
        glCompileShader( vertexShader );

        GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
        glShaderSource( fragmentShader, 1, &textfragmentSource, NULL );
        glCompileShader( fragmentShader );

        GLuint shaderProgram = glCreateProgram();
        glAttachShader( shaderProgram, vertexShader );
        glAttachShader( shaderProgram, fragmentShader );
        glBindFragDataLocation( shaderProgram, 0, "outColor" );
        glLinkProgram( shaderProgram );
        glUseProgram( shaderProgram );

        GLint posAttrib = glGetAttribLocation( shaderProgram, "coord" );
        glEnableVertexAttribArray(posAttrib);
        glVertexAttribPointer(posAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0);

        GLint colAttrib = glGetAttribLocation( shaderProgram, "color" );
        glEnableVertexAttribArray(colAttrib);
        glVertexAttribPointer(colAttrib, 4, GL_INT, GL_FALSE, 0, 0);

        GLint uniform_tex = glGetUniformLocation(shaderProgram, "tex");

        GLuint tex;
        glActiveTexture(GL_TEXTURE0);
        glGenTextures(1, &tex);
        glBindTexture(GL_TEXTURE_2D, tex);
        glUniform1i(uniform_tex, 0);

        GLuint uniform_color = glGetUniformLocation(shaderProgram, "color");

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

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

        // Actual font rendering
        FT_Library ft;

        if(FT_Init_FreeType(&ft))
                debug.Print("Could not initiate freetype library.");

        FT_Face face;
        if(FT_New_Face(ft, fontName, 0, &face))
        {
                debug.Print("Could not load font: ");
                debug.Print(fontName);
        }

        FT_Set_Pixel_Sizes(face, sizex, sizey);

        FT_GlyphSlot glyph = face->glyph;

        const char *p;

        for(p = text; *p; p++) {
                if(FT_Load_Char(face, *p, FT_LOAD_RENDER))
                        continue;

                glTexImage2D(
                  GL_TEXTURE_2D,
                  0,
                  GL_ALPHA,
                  glyph->bitmap.width,
                  glyph->bitmap.rows,
                  0,
                  GL_ALPHA,
                  GL_UNSIGNED_BYTE,
                  glyph->bitmap.buffer
                );

                float x2 = posx + glyph->bitmap_left * sizex;
                float y2 = -posy - glyph->bitmap_top * sizey;
                float w = glyph->bitmap.width * sizex;
                float h = glyph->bitmap.rows * sizey;

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

                posx += (glyph->advance.x >> 6) * sizex;
                posy += (glyph->advance.y >> 6) * sizey;

                GLfloat color[4] = {r, g, b, 1};
                glUniform4fv(uniform_color, 1, color);
  }
}
4

2 回答 2

0

我将尝试扩展我的评论。

在您的while( running )循环中,您调用text.Create("The quick brown fox jumps over the lazy dog", "arial.ttf", 300, 400, 1, 1, 1, 2/600, 2/800);. 当查看这个函数的细节时,它不仅仅是创建一个新的文本条目,它还初始化FreeType、加载fonts等等......

您希望您的游戏/模拟FreeType只初始化一次并text.Create返回一个将在您的游戏/模拟中持续存在的对象。

在该while(running)部分中,您应该调用Render对象上的方法以将其呈现在屏幕上。

于 2012-11-23T03:02:48.983 回答
0

问题是您使用 sizex 和 sizey 设置 FT_Set_Pixel_Sizes ,然后在为文本创建框时使用 sizex 和 sizey 来缩放框。

因此,如果您将 sizex 和 sizey 设置为 48,这也意味着您将文本框缩放 48,这是一种很大的方式,如果您将 sizex 和 sizey 设置为 1,那么您的字体的像素大小会非常小。

修复:添加参数大小或高度和宽度以创建函数并将其用于 FT_Set_Pixel_Size

前任。

void Create(char* Text, char* fontName, int posx, int posy, int r, int g, int b, int sizex, int sizey, int height, int width){
...
FT_Set_Pixel_Size(face, width, height); //Not sizex and sizey because thats what you are using to scale the text box
}
于 2014-08-01T12:31:37.280 回答