Sign in to follow this  

Problem with creating a Font under OpenGL

Recommended Posts

Corpsman    160
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 ?

Share this post

Link to post
Share on other sites
yadango    567
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.

Share this post

Link to post
Share on other sites
Corpsman    160

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.

Share this post

Link to post
Share on other sites
Corpsman    160
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;


glext, textures;

TOpenGLFont = Class
mFontTexId: GLuint;
mBaseListId: gluint;
fbase: gluint;
Procedure Go2d;
Procedure exit2d;
Constructor create();
Procedure Textout(x, y: integer; fmt: String);
Destructor destroy; override;



Constructor TOpenGLFont.create();
bh = 20; // Height of one Letter
bw = 20; // Width of one Letter
cx, cy: single;
loop: integer;
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

Destructor TOpenGLFont.destroy;
glDeleteLists(mBaseListId, NUMCHARS); // Delete All Display Lists
// Inherited destroy; // Brauchen wir net machen da wir von TObject abgeleitet haben

Procedure TOpenGLFont.Go2d();
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(0, 640, 0, 480, -1, 1); // Set Up An Ortho Screen
glPushMatrix(); // Store old Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix

Procedure TOpenGLFont.Exit2d();
glPopMatrix(); // Restore old Projection Matrix
glPopMatrix(); // Restore old Projection Matrix

Procedure TOpenGLFont.Textout(x, y: integer; fmt: String);
glBindTexture(GL_TEXTURE_2D, mFontTexId);
glEnable(GL_BLEND); // For transparent background
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


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this