4 Pluspunkte 0 Minuspunkte
Wie kann ich am einfachsten Text mit OpenGL rendern?
von  

2 Antworten

2 Pluspunkte 0 Minuspunkte

Eine Möglichkeit ist die Verwendung von Bibliotheken wie FreeType oder stb_truetype, um den Text in Texturdaten umzuwandeln und auf ein Rechteck zu rendern. Hier ist ein Beispiel:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <ft2build.h>
#include FT_FREETYPE_H

void errorCallback(int error, const char* description) {
    fprintf(stderr, "Error: %s\n", description);
}

int main() {
    // GLFW initialisieren
    if (!glfwInit()) {
        fprintf(stderr, "Fehler beim Initialisieren von GLFW\n");
        return -1;
    }

    // GLFW Konfiguration setzen
    glfwSetErrorCallback(errorCallback);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    // Ein Fenster erstellen
    GLFWwindow* window = glfwCreateWindow(800, 600, "Text Rendering Example", NULL, NULL);
    if (!window) {
        fprintf(stderr, "Fehler beim Erstellen des Fensters\n");
        glfwTerminate();
        return -1;
    }

    // Kontext setzen
    glfwMakeContextCurrent(window);

    // GLEW initialisieren
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Fehler beim Initialisieren von GLEW\n");
        return -1;
    }

    // FreeType initialisieren
    FT_Library ft;
    if (FT_Init_FreeType(&ft)) {
        fprintf(stderr, "Fehler beim Initialisieren von FreeType\n");
        return -1;
    }

    // Schriftart laden
    FT_Face face;
    if (FT_New_Face(ft, "path_to_font.ttf", 0, &face)) {
        fprintf(stderr, "Fehler beim Laden der Schriftart\n");
        return -1;
    }

    FT_Set_Pixel_Sizes(face, 0, 48);

    // Hier solltest du die Schritte für das Generieren der Texturdaten implementieren

    // Hier solltest du die Shader erstellen

    // Rendern-Schleife
    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT);

        // Hier solltest du den Text auf das Rechteck rendern

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // Ressourcen freigeben
    FT_Done_Face(face);
    FT_Done_FreeType(ft);
    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}

von (532 Punkte)  
2 Pluspunkte 0 Minuspunkte

Du kannst SDL nutzen.

void RenderText(std::string message, SDL_Color color, int x, int y, int size) {

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    gluOrtho2D(0, m_Width, 0, m_Height); // m_Width and m_Height is the resolution of window
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();

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

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    TTF_Font * font = TTF_OpenFont("pathToFont.ttf", size);
    SDL_Surface * sFont = TTF_RenderText_Blended(font, message, color);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sFont->w, sFont->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, sFont->pixels);

    glBegin(GL_QUADS);
    {
        glTexCoord2f(0,0); glVertex2f(x, y);
        glTexCoord2f(1,0); glVertex2f(x + sFont->w, y);
        glTexCoord2f(1,1); glVertex2f(x + sFont->w, y + sFont->h);
        glTexCoord2f(0,1); glVertex2f(x, y + sFont->h);
    }
    glEnd();

    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    glDeleteTextures(1, &texture);
    TTF_CloseFont(font);
    SDL_FreeSurface(sFont);
 
}

Aufrufen kannst du die Funktion mit

// Print "Hello World" an Position (5,10) mit einer Fontt-Size von 12px
SDL_Color color = {255, 0, 0, 0}; // Red
RenderText("Hello World", color, 5, 10, 12);

von (716 Punkte)