Plain text in OpenGL (without GLUT)

I am making a simple 2D platformer with C ++, SDL and OpenGL , and now I would like to display text on the screen, for example. dots, timer, simple messages.

On various sites, I read that Bitmap fonts will probably be for this, but someone, please give me a simple and understandable example of how to use them in OpenGL to display a test message for the screen?

EDIT: I found something interesting, FTGL . The sample code in the tutorial (2nd link) looks very simple. It was not possible to make it work, but we will return to it later.

FTGL is a free cross-platform Open Source C ++ library that uses Freetype2 to simplify font rendering in OpenGL applications. FTGL supports bitmaps, bitmaps, texture maps, paths, polygon mesh, and polygon rendering modes.

+6
source share
4 answers

You can use SDL_gfx , it can output text using bitmap fonts as follows:

#include <SDL/SDL_gfxPrimitives.h> #include <SDL/SDL_gfxPrimitives_font.h> const char *text = "text to render"; stringColor(sdlSurface, x, y, text, 0xff0000ff); 
+2
source

When you are using SDL already, why not add SDL_ttf ?

SDL_ttf is a TrueType font rendering library that is used with the SDL library, and almost as portable. It depends on freetype2 to process TrueType font data. This allows the programmer to use multiple TrueType fonts without the need to encode the font rendering procedure itself. With the power of outline fonts and anti-aliasing, high-quality text output can be obtained effortlessly.

+1
source

You need to create a bitmap containing all the characters you want to use. The easiest way is a fixed-width font. Then for each letter you bind this part of the texture. I wrote this code in Pascal / OpenGL a few years ago, I will look for it and publish it

Change the code here

 procedure DisplayString(str: string; l: integer; active: integer; align: integer); var i,n: integer; s: string; begin glPushMatrix; glTranslatef(0,-l,0); if align=1 then glTranslatef(-15,0,0); //left alignment if align=3 then glTranslatef(15,0,0);//right aligment s:=uppercase(str); // the font is uppercase only for i:=1 to length(str)-4 do begin n:=ord(s[i]); // get the ASCII code of letter glTranslatef(1,0,0); if n=32 then continue; //space if (n>=48) and (n<=58) then n:=n-47; // translate ascii code to the if (n>=65) and (n<=90) then n:=n-53; // corresponding position in the bitmap if active=0 then glBindTexture(GL_TEXTURE_2D, font2) // two different font files used else glBindTexture(GL_TEXTURE_2D, font); glBegin(GL_QUADS); // 1850 is the total width of the bitmap image, 50 is one character glTexCoord2f(((n-1)*50/1850)-1, 0.0); glVertex3f(0.0, 0.0, 1.0); glTexCoord2f((n*50/1850)-1, 0.0); glVertex3f(1.0, 0.0, 1.0); glTexCoord2f((n*50/1850)-1, 1.0); glVertex3f(1.0, 1.0, 1.0); glTexCoord2f(((n-1)*50/1850)-1, 1.0); glVertex3f(0.0, 1.0, 1.0); glEnd(); end; glPopMatrix; end; 
0
source

I used the raster fonts available in GLUT. Relevant calls in JOGL (Java + OpenGL):

 GLU glu = new GLU(); GLUT glut = new GLUT(); GL gl = drawable.getGL(); gl.glRasterPos3f(x, y, z); // move to (x, y, z) for the next text output glut.glutBitmapString(GLUT.BITMAP_HELVETICA_12, "Message"); gl.glRasterPos3f (x2, y2, z2); glut.glutBitmapCharacter (GLUT.BITMAP_HELVETICA_18, 'X'); 

It can be easily adapted to OpenGL + C ++.

-2
source

Source: https://habr.com/ru/post/895530/


All Articles