Archived

This topic is now archived and is closed to further replies.

Spider_In_A_Web

OpengGl text, An easy way???

Recommended Posts

Hello all, Iam quickly writing a small asteroids game using open Gl, and in need to get some text on screen, i currently have the text drawn using Windows GDI but this is far from a good solution, and i was wondering, does OpenGL have any text drawing features, or is there any good tut''s on this topic Cheers, Paul

Share this post


Link to post
Share on other sites
What I did was get a 256x256 gdi bitmap and draw all the ascii letters on to it, and draw each letter on a quad. Unfortunately I''m going to school right now so if you want some source or help I''ll get back to you tonight or tomorrow morning.

It was a pain in the ass to get it working, unless you do some tricky texcoord and texture carving work you can''t use any point size text you want.

------------
- outRider -

Share this post


Link to post
Share on other sites
For openGL, the best way is probably to use texture bitmap fonts. They are probably the most versatile and easy to use (After you get the basic stuff down). Basically you do what outrider was saying with the 256x256 bitmap and you use quads. There are many programs out that will generate this kind of bitmap from a font if you don''t want to do it the hard way. www.lmnopc.com has one that works rather well. As for what outRider was saying about point sizes. Thats not hard at all. Simply call a glScalef(x,y) and that scale the text smaller or larger depending on what values you put inside it. If you need more help, NeHe has some great tutorials on fonts at nehe.gamedev.net

-Shaun (Salec)

Share this post


Link to post
Share on other sites
Salec, true about the glScale(), but I meant working with larger point sizes when you're creating the font, which means you'd have to use a larger texture, which would leave empty spaces, which would require you to find texcoords that are not related to the image dimentions, and you'd have to carve the texture up for cards that don't support larger textures, etc etc. It was a while before I realized that you could glScale without much blurring and save yourself the work.

Anyway, here's some source for you.


FONT* CreateTTFont(HDC hDC, const char *pszFontname)
{
FONT *Font;
HFONT hFont;
HBITMAP hBmp;
HBRUSH hBrush;
LOGBRUSH LogBrush;
HDC hTempDC;
int x, y;
char cCurrentChar = 0;
BITMAPINFO BmpInfo;
bool bNullDC;
ABC CharWidths[256];

if (hDC == NULL)
{
hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hTempDC = CreateCompatibleDC(hDC);
bNullDC = true;
}
else
{
hTempDC = CreateCompatibleDC(hDC);
bNullDC = false;
}

hFont = CreateFont(-14, 0, 0, 0, FW_NORMAL, false, false, false, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FF_DONTCARE | DEFAULT_PITCH, pszFontname);
SelectObject(hTempDC, hFont);

SetBkMode(hTempDC, TRANSPARENT);
SetTextColor(hTempDC, 0xFFFFFF);

memset(&BmpInfo, 0, sizeof(BITMAPINFO));
BmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BmpInfo.bmiHeader.biBitCount = 32;
BmpInfo.bmiHeader.biPlanes = 1;
BmpInfo.bmiHeader.biWidth = 256;
BmpInfo.bmiHeader.biHeight = 256;
BmpInfo.bmiHeader.biCompression = BI_RGB;

hBmp = CreateDIBSection(hTempDC, &BmpInfo, DIB_RGB_COLORS, NULL, NULL, NULL);
SelectObject(hTempDC, hBmp);

LogBrush.lbStyle = BS_SOLID;
LogBrush.lbColor = 0xFF00FF;

hBrush = CreateBrushIndirect(&LogBrush);
SelectObject(hTempDC, hBrush);

ExtFloodFill(hTempDC, 0, 0, 0x000000, FLOODFILLSURFACE);

GetCharABCWidths(hTempDC, 0, 255, CharWidths);

for (y = 0; y < 256; y += 16)
{
for (x = 0; x < 256; x += 16)
{
TextOut(hTempDC, x - CharWidths[cCurrentChar].abcA, y, &cCurrentChar, 1);
cCurrentChar++;
}
}

Font = (FONT *)malloc(sizeof(FONT));
Font->byStartChar = 0;
Font->nChars = 256;
Font->iFontTextureWidth = 256;
Font->iFontTextureHeight = 256;

memset(&BmpInfo, 0, sizeof(BITMAPINFO));
BmpInfo.bmiHeader.biSize = sizeof(BITMAPINFO);
GetDIBits(hTempDC, hBmp, 0, 0, NULL, &BmpInfo, DIB_RGB_COLORS);
Font->pFontTexture = (BYTE *)malloc(BmpInfo.bmiHeader.biWidth * BmpInfo.bmiHeader.biHeight * BmpInfo.bmiHeader.biBitCount / 8);
GetDIBits(hTempDC, hBmp, 0, BmpInfo.bmiHeader.biHeight, Font->pFontTexture, &BmpInfo, DIB_RGB_COLORS);

// If the pixel is black, then it has an alpha of 0, so it wont be drawn.
for (x = 0; x < 256 * 256 * 4; x += 4)
{
if (Font->pFontTexture[x] == 255 && Font->pFontTexture[x + 1] == 0 && Font->pFontTexture[x + 2] == 255)
Font->pFontTexture[x + 3] = 0;
else
Font->pFontTexture[x + 3] = 255;
}

glGenTextures(1, &Font->iFontTextureID);
glBindTexture(GL_TEXTURE_2D, Font->iFontTextureID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE4_ALPHA4, BmpInfo.bmiHeader.biWidth, BmpInfo.bmiHeader.biHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, Font->pFontTexture);

Font->iFontListID = CreateFontDisplayLists(Font->iFontTextureID, CharWidths);

DeleteObject(hFont);
DeleteObject(hBmp);
DeleteObject(hBrush);
DeleteDC(hTempDC);
if (bNullDC)
DeleteDC(hDC);

return Font;
}

int CreateFontDisplayLists(int iFontTextureID, ABC *CharWidths)
{
int iFontListID;
int x, y;
int CurrentChar = 0;

iFontListID = glGenLists(256);

for (y = 16; y > 0; y--)
{
for (x = 0; x < 16; x++)
{
glNewList(iFontListID + CurrentChar, GL_COMPILE);
glTranslatef((float)CharWidths[CurrentChar].abcA, 0.0f, 0.0f);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(GRID_DIVISOR * x, GRID_DIVISOR * (y - 1)); glVertex2f(0.0f, 0.0f);
glTexCoord2f(GRID_DIVISOR * (x + 1), GRID_DIVISOR * (y - 1)); glVertex2f(16.0f, 0.0f);
glTexCoord2f(GRID_DIVISOR * x, GRID_DIVISOR * y); glVertex2f(0.0f, 16.0f);
glTexCoord2f(GRID_DIVISOR * (x + 1), GRID_DIVISOR * y); glVertex2f(16.0f, 16.0f);
glEnd();
glTranslatef((float)(CharWidths[CurrentChar].abcB + CharWidths[CurrentChar].abcC), 0.0f, 0.0f);
glEndList();
CurrentChar++;
}
}

return iFontListID;
}


The size of the font was 14 I think, thats because with overhanging and underhanging some characters were 16 pts high. So on a 256x256 texture you could fit 256 characters each with a cell of 16x16. GRID_DIVISOR was 16/256, 0.0625, which is needed for the texcoords since they have to be clamped to [0, 1].

------------
- outRider -

Edited by - outRider on October 18, 2001 10:15:24 AM

Share this post


Link to post
Share on other sites