如何在OpenGL中绘制文本?

时间:2014-11-23 21:11:39

标签: c++ opengl freetype

我正在尝试使用OpenGL绘制文本,但它无法正确显示,甚至不能显示。从我收集的内容来看,位图的创建或绑定不起作用。我承认我并不完全理解FT_Load_Char方法或glTexImage2D方法,所以很可能其中一个方法就是问题。

这是我的代码:

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <gl/gl.h>
#include <ft2build.h>
#include FT_FREETYPE_H

void drawText(char* text, FT_Face face)
{
    int i = 0;
    while (*(text + i) != '\0')
    {
        auto error = FT_Load_Char( face, text[i], FT_LOAD_RENDER );
        if (!error)
        {
            FT_GlyphSlot glyphSlot = face->glyph;
            FT_Bitmap bitmap = face->glyph->bitmap;
            unsigned char* buffer = face->glyph->bitmap.buffer;

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, face->glyph->bitmap.width, face->glyph->bitmap.rows, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
            glColor3f (1.0, 1.0, 1.0);
            glBegin (GL_QUADS);
            glTexCoord2f (0,0);
            glVertex2d (-0.9375 + 0.25*(i%8), 0.9375 - .25*(i/8));
            glTexCoord2f (0,1);
            glVertex2d (-0.9375 + 0.25*(i%8), 0.8125 - .25*(i/8));
            glTexCoord2f (1,1);
            glVertex2d (-0.8125 + 0.25*(i%8), 0.8125 - .25*(i/8));
            glTexCoord2f (1,0);
            glVertex2d (-0.8125 + 0.25*(i%8), 0.9375 - .25*(i/8));
            glEnd ();
        }
        i++;
    }
}

int main()
{
    // Create the main window
    sf::Window App(sf::VideoMode(500, 500, 32), "Text Test");

    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

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

    FT_Library library;
    FT_Face face;

    auto error = FT_Init_FreeType( &library );
    if ( error )
    {
        return 1;
    }

    error = FT_New_Face( library,
                       "arial.ttf",
                       0,
                       &face );
    if ( error == FT_Err_Unknown_File_Format )
    {
        return 2;
    }
    else if ( error )
    {
        return 3;
    }

    error = FT_Set_Char_Size(
            face,    /* handle to face object           */
            0,       /* char_width in 1/64th of points  */
            16*64,   /* char_height in 1/64th of points */
            300,     /* horizontal device resolution    */
            300 );   /* vertical device resolution      */
            if ( error )
    {
        return 4;
    }

    char * text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";

    // Start game loop
    while (App.isOpen())
    {
        // Process events
        sf::Event Event;
        while (App.pollEvent(Event))
        {
            // Close window : exit
            if (Event.type == sf::Event::Closed)
                App.close();

            if ((Event.type == sf::Event::KeyPressed))
            {
                if (Event.key.code == sf::Keyboard::Escape)// Escape key : exit
                {
                    App.close();
                }
            }
        }
        glPopMatrix();

        // Clear color and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        drawText(text, face);
        glPushMatrix();

        // Finally, display rendered frame on screen
        App.display();
    }

    return EXIT_SUCCESS;
}

当我运行它时,结果如下: broken text

有没有人知道我做错了什么或如何解决我的问题?

0 个答案:

没有答案