Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Text in OpenGL (without GLUT)

Tags:

text

opengl

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

On various site's I've read that Bitmap Fonts would probably be the way to go for this, but could someone please give me a straightforward, easy to understand example of how to use them in OpenGL to output a test-message to the screen?

EDIT: I found something interesting, FTGL. The example-code in the tutorial (2nd link) looks very straightforward. Couldn't get it to work atm but will go back to it later.

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

  1. http://sourceforge.net/projects/ftgl/
  2. http://ftgl.sourceforge.net/docs/html/ftgl-tutorial.html
  3. http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/
like image 280
Ben Avatar asked Aug 21 '11 13:08

Ben


2 Answers

You can use SDL_gfx, it can output text using bitmap fonts like this:

#include <SDL/SDL_gfxPrimitives.h>
#include <SDL/SDL_gfxPrimitives_font.h>

const char *text = "text to render";
stringColor(sdlSurface, x, y, text, 0xff0000ff);
like image 118
Nikolay Kasyanov Avatar answered Sep 20 '22 10:09

Nikolay Kasyanov


You have to create a bitmap image, containing all the characters you want to use. The easiest way is with a fixed-width font. Then for each letter you bind that part of the texture. I've written such code in Pascal/OpenGL years ago, will search for it and post

Edit here is the code

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;
like image 35
Maxim Krizhanovsky Avatar answered Sep 17 '22 10:09

Maxim Krizhanovsky