Problem with creating a Font under OpenGL

Started by
3 comments, last by Corpsman 17 years, 11 months ago
Hello everybody, I'm Trying to create a Font in my Application but it dous not work, and i don't know why. If read nehe's lesson 13 and createt the Following Class. Type TOpenGLFont = Class fbase: GLuint; // Base Display List For The Font Set private public Constructor create(Const h_Dc: HDC; Name: String); Procedure Textout({x, y: integer;} Text: String); Destructor destroy; override; End; Implementation Constructor TOpenGLFont.create(Const h_Dc: HDC; Name: String); Var Font: HFONT; // Windows Font ID Begin Inherited Create; fbase := glGenLists(96); // Storage For 96 Characters ( NEW ) font := CreateFont(-24, // Height Of Font ( NEW ) 0, // Width Of Font 0, // Angle Of Escapement 0, // Orientation Angle FW_BOLD, // Font Weight 0, // Italic 0, // Underline 0, // Strikeout ANSI_CHARSET, // Character Set Identifier OUT_TT_PRECIS, // Output Precision CLIP_DEFAULT_PRECIS, // Clipping Precision ANTIALIASED_QUALITY, // Output Quality FF_DONTCARE Or DEFAULT_PITCH, // Family And Pitch pchar(name)); // Font Name SelectObject(h_dc, font); // Selects The Font We Want wglUseFontBitmaps(h_dc, 32, 96, fbase); // Builds 96 Characters Starting At Character 32 End; Destructor TOpenGLFont.destroy; Begin glDeleteLists(fbase, 96); // Delete All 96 Characters ( NEW ) // Inherited destroy; // Brauchen wir net machen da wir von TObject abgeleitet haben End; Procedure TOpenGLFont.Textout({x, y: integer; }Text: String); Begin glColor3f(1, 0, 0); //1.0 * cos(cnt1), 1.0 * sin(cnt2), 1.0 - 0.5 * cos(cnt1 + cnt2)); // Pulzování barev závislé na pozici glRasterPos2f(0, 0); // Pozice textu // glPrint('Hallo Welt' {Format('Active OpenGL Text With NeHe - %7.2f',[cnt1])}); // Výpis textu glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits ( NEW ) glListBase(fbase - 32); // Sets The Base Character to 32 ( NEW ) glCallLists(length(text), GL_UNSIGNED_BYTE, Pchar(text)); // Draws The Display List Text ( NEW ) glPopAttrib; // Pops The Display List Bits ( NEW ) End; In my Application( written in non VCL ) i call in my render Function Textout('Hello world'); but i see nothing. did anybody know what i made wrong ? or maybe is there a Class existing which make what i'm trying to do ?
--You want to know more about me then visit www.Corpsman.de.vu
Advertisement
I'll suggest an alternative solution here and point you towards something like Bitmap Font Builder. Using a program like this, you can generate textures of fonts and then render them in your application. If you take a look at Martin Bell's OpenGL Text Drawer, you'll see an example of rendering a bitmap font in OpenGL. Good luck!
Rob Loach [Website] [Projects] [Contact]
take note that the raster position is transformed by the projection and modelview matrices. so just because you specify (0,0) for the raster position doesn't mean it'll draw your text at (0,0). it is better to use glWindowPos. if u don't know how to load 1.3 (if i recall right) extensions, you can use this (scroll down to glRasterPos Problems) to make your own glWindowPos function.
hmm

this is C Code , i sorry dont understand that.

I already Use a Free FontBuilder. This isn't my Problem.

I Cannot draw the Font to the screen.

I Need a way to do this.
--You want to know more about me then visit www.Corpsman.de.vu
OK i tried out to read the C code

I created this Unit.

And it works more than all my last trie's but sorry not right.

I Can Load a bmp File and the Programm paints something on the screen.

But this are only parts of the Bitmap I cannot Control which parts this really are.

MAybe someone of you find my error.

Here's the Unit

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.
--You want to know more about me then visit www.Corpsman.de.vu

This topic is closed to new replies.

Advertisement