How to do OpenGL live text-rendering for a GUI?

后端 未结 7 1946
面向向阳花
面向向阳花 2020-12-04 08:12

I\'m implementing a GUI built on top of OpenGL. I came to the problem that each GUI will have -- text rendering. I know of several methods of rendering text in OpenGL, howev

相关标签:
7条回答
  • 2020-12-04 08:45

    could just use these two functions:

    void renderstring2d(char string[], float r, float g, float b, float x, float y)
    {
        glColor3f(r, g, b);
    
        glRasterPos2f(x, y);
        for(unsigned int i = 0; i < strlen(string); i++)
            glutBitmapCharacter(GLUT_BITMAP_9_BY_15, string[i]);
    }
    
    void renderstring3d(char string[], float r, float g, float b, float x, float y, float z)
    {
        glDisable(GL_LIGHTING);
        glColor3f(r, g, b);
    
        glRasterPos3f(x, y, z);
        for(unsigned int i = 0; i < strlen(string); i++)
            glutBitmapCharacter(GLUT_BITMAP_9_BY_15, string[i]);
    }

    then when you render 2D text, don't forget to reset both the modelview and projection matrices.

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    
    // Render text and quads
    
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();

    You don't need to render to a quad using these functions, because these functions render directly to the framebuffer.

    And if that doesn't work for you, check this out. http://www.opengl.org/resources/faq/technical/fonts.htm

    It talks about rendering text using glBitmap, glDrawPixels, and alpha blending.

    0 讨论(0)
提交回复
热议问题