Unit OpenGL_Font;
Interface
Uses
glu,
gl,
glext, textures;
Type
TOpenGLFont =
Class
mFontTexId: GLuint;
mBaseListId: gluint;
fbase: gluint;
Procedure Go2d;
Procedure exit2d;
private
public
Constructor create();
Procedure Textout(x, y: integer; fmt:
String);
Destructor destroy;
override;
End;
Implementation
Const
NUMCHARS = 256;
Constructor TOpenGLFont.create();
Const
bh = 20;
// Height of one Letter
bw = 20;
// Width of one Letter
Var
cx, cy: single;
loop: integer;
Begin
Inherited Create;
LoadTexture('
D:\Tools\Projects\Balanced\Pic400_400N.bmp', mFontTexId);
mBaseListId := glGenLists(NUMCHARS);
// Creating NUMCHARS Display Lists
glBindTexture(GL_TEXTURE_2D, mFontTexId);
For loop := 0
To numchars - 1
Do Begin
cx := (loop
Mod bw) / bw;
// X Position Of Current Character
cy := (loop / bh) / bh;
// Y Position Of Current Character
glNewList(mBaseListId + loop, GL_COMPILE);
// Start Building A List
glBegin(GL_QUADS);
// Use A Quad For Each Character
glTexCoord2f(cx, 1 - cy - (1 / bh));
// Texture Coord (Bottom Left)
glVertex2i(0, 0);
// Vertex Coord (Bottom Left)
glTexCoord2f(cx + (1 / bw), 1 - cy - (1 / bh));
// Texture Coord (Bottom Right)
glVertex2i(bw, 0);
// Vertex Coord (Bottom Right)
glTexCoord2f(cx + (1 / bw), 1 - cy);
// Texture Coord (Top Right)
glVertex2i(bw, bh);
// Vertex Coord (Top Right)
glTexCoord2f(cx, 1 - cy);
// Texture Coord (Top Left)
glVertex2i(0, bh);
// Vertex Coord (Top Left)
glEnd();
// Done Building Our Quad (Character)
glTranslatef(bw, 0, 0);
// Move To The Right Of The Character
glEndList();
// Done Building The Display List
End;
End;
Destructor TOpenGLFont.destroy;
Begin
glDeleteLists(mBaseListId, NUMCHARS);
// Delete All Display Lists
// Inherited destroy; // Brauchen wir net machen da wir von TObject abgeleitet haben
End;
Procedure TOpenGLFont.Go2d();
Begin
glMatrixMode(GL_PROJECTION);
glPushMatrix();
// Store The Projection Matrix
glLoadIdentity();
// Reset The Projection Matrix
glOrtho(0, 640, 0, 480, -1, 1);
// Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Store old Modelview Matrix
glLoadIdentity();
// Reset The Modelview Matrix
End;
Procedure TOpenGLFont.Exit2d();
Begin
glMatrixMode(GL_PROJECTION);
glPopMatrix();
// Restore old Projection Matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
// Restore old Projection Matrix
End;
Procedure TOpenGLFont.Textout(x, y: integer; fmt:
String);
Begin
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mFontTexId);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
// For transparent background
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Go2d();
glTranslated(x, y, 0);
glListBase(mBaseListId - 32);
// Font bitmaps starts at ' ' (space/32).
glcolor3f(1, 0, 0);
glCallLists(length(fmt), GL_BYTE, pchar(fmt));
// Write The Text To The Screen
Exit2d();
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
End;
End.