Home > Back-end >  Character goes diagonal by diffrent font sizes in c opengl true type font
Character goes diagonal by diffrent font sizes in c opengl true type font

Time:04-10

I've been trying to get TTF to work with my OpenGL project and this is the closest I've come to, here is my code:

#include "App.h"

std::array <float, 4> background_col = {0.25, 0.25, 0.25, 1.0}  ;
std::string name                     = "OpenGL"                 ;
std::string icon                     = "data/images/dot.png"    ;
const int width                      = 800                      ;
const int height                     = 800                      ;
bool anti_aliasing                   = true                     ;
bool FPS_60_CAP                      = true                     ;
bool movable                         = false                    ;
    
Core::Base::MVP mvp;

int a = 32;

void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
    if (action == GLFW_PRESS) { a  = 1; }
    info(a);
}

double mouseX, mouseY;
double pmouseX, pmouseY;

glm::vec2 t(0);

void mouse(GLFWwindow * window) {
    pmouseX = mouseX;
    pmouseY = mouseY;

    double xpos, ypos;
    glfwGetCursorPos(window, &xpos, &ypos);
    mouseX = xpos;
    mouseY = height - ypos;

    if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)  && movable) {
        t.x  = mouseX - pmouseX;
        t.y  = mouseY - pmouseY;
        mvp.translate(0, t);
    }
}

void App::run()
{
    Core::Window window(true, anti_aliasing);
    window.make_Window(glfwCreateWindow(width, height, name.c_str(), NULL, NULL), key_callback, icon);

    srand(time(NULL));
    mvp.view = glm::ortho(0.0f, (float)width, 0.0f, (float)height);

    Core::Renderer renderer;
    Core::Player player;

    // 1. TODO: Implement Text
    // 2. TODO: Implement UI

    FT_Library ft;
    FT_Error err = FT_Init_FreeType(&ft);
    
    if (err != 0) {
        printf("Failed to initialize FreeType\n");
        exit(EXIT_FAILURE);
    }
    
    FT_Int major, minor, patch;
    FT_Library_Version(ft, &major, &minor, &patch);
    printf("FreeType's version is %d.%d.%d\n", major, minor, patch);
    
    FT_Face face;
    err = FT_New_Face(ft, "data/fonts/arial.ttf", 0, &face);

    if (err != 0) {
        printf("Failed to load face\n");
        exit(EXIT_FAILURE);
    }

    while (!glfwWindowShouldClose(window.window))
    {
        glClearColor(background_col[0], background_col[1], background_col[2], background_col[3]);
        glClear(GL_COLOR_BUFFER_BIT);

//        mouse(window.window);
//        renderer.Render({ Core::Draw::Ellipse({400, 400, {1,0,1,1}}, 100, 75, true) }, mvp);

        err = FT_Set_Pixel_Sizes(face, 0, a);
    
        if (err != 0) {
            printf("Failed to set pixel size\n");
            exit(EXIT_FAILURE);
        }
        
        FT_UInt glyph_index = FT_Get_Char_Index(face, 'o');
        FT_Int32 load_flags = FT_LOAD_DEFAULT;
        
        err = FT_Load_Glyph(face, glyph_index, load_flags);
        
        if (err != 0) {
            printf("Failed to load glyph\n");
            exit(EXIT_FAILURE);
        }
        
        err = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
        
        if (err != 0) {
            printf("Failed to render the glyph\n");
            exit(EXIT_FAILURE);
        }

        glDrawPixels( face->glyph->bitmap.width, face->glyph->bitmap.rows, GL_ALPHA, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);

        Core::viewport(window.window);
        glfwSwapBuffers(window.window);
        glfwSwapInterval(FPS_60_CAP);
        glfwPollEvents();
    }
    
    alcCloseDevice(player.Device);
    window.~Window();
}

here is an example of what i mean: Error

It renders correctly sometimes but when you change the font size it gets messed up and I don't understand what I did wrong here, Any help would be appreciated !

CodePudding user response:

By default each row of the pixels is assumed be aligned to 4 bytes, but your pixel data is not alligned at all. You can change the alignment requirements by setting GL_UNPACK_ALIGNMENT (see glDrawPixels and glPixelStore):

glPixelStore(GL_UNPACK_ALIGNMENT, 1);
glDrawPixels( face->glyph->bitmap.width, face->glyph->bitmap.rows, GL_ALPHA, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
  • Related