Jump to content
  • Advertisement
Sign in to follow this  
Hastur

OpenGL Create antialising font

This topic is 4809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I wanna know how can I create an antialiasing font using CreateFont function.. I'm working on OpenGL. thanx

Share this post


Link to post
Share on other sites
Advertisement
here's the routine i use to create antialiased strings into textures in gl.


char *fonts[] = {
"Times New Roman",
"Arial",
};

void texture_text(Texture *tex, unsigned font_index, char *string, unsigned size, int xpos, int ypos, unsigned color) {
const unsigned w = tex->get_w();
const unsigned h = tex->get_h();
const unsigned width = 1 << w;
const unsigned height = 1 << h;
const char *font_name = fonts[font_index];

unsigned *ptr = (unsigned*)tex->get_data();

HDC hdc = GetDC(0);
HBITMAP bitmap = CreateCompatibleBitmap(hdc, width << 2, height << 2);
HDC hdc_mem = CreateCompatibleDC(hdc);
SelectObject(hdc_mem, bitmap);

static LOGFONT lf = {0, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, ANTIALIASED_QUALITY, 0, 0};
lf.lfHeight = -int(size) * 4;
strcpy(lf.lfFaceName, font_name);
HFONT font = CreateFontIndirect(&lf);

SetBkColor(hdc_mem, 0);
SelectObject(hdc_mem, font);
SetTextColor(hdc_mem, 0xFF0000);

SetTextAlign(hdc_mem, TA_BASELINE | TA_LEFT | TA_NOUPDATECP);
TextOut(hdc_mem, xpos * 4, ypos * 4, string, unsigned(strlen(string)));

unsigned *buffer = new unsigned[width * height * 4 * 4];
static BITMAPINFO bi = {{sizeof(BITMAPINFOHEADER), 0, 0, 1, 32, BI_RGB, 0, 0, 0, 0, 0}, {0 ,0, 0}};
bi.bmiHeader.biWidth = width * 4;
bi.bmiHeader.biHeight = int(height * 4);
GetDIBits(hdc_mem, bitmap, 0, height * 4, buffer, &bi, DIB_RGB_COLORS);

DeleteDC(hdc_mem);
DeleteObject(bitmap);
DeleteObject(font);

unsigned color_alpha = color >> 24;

unsigned int *src = buffer;
for (unsigned y = 0; y < height; y++) {
for (unsigned x = 0; x < width; x++) {
unsigned accum = 0;
unsigned offs = (x << 2) + (y << 4) * width;

for (unsigned i=4; i; i--) {
accum += src[offs] + src[offs+1] + src[offs+2] + src[offs+3];
offs += width << 2;
}

unsigned char alpha = unsigned char(accum >> 4);

alpha = ( alpha * color_alpha ) >> 8;
unsigned int rb1 = *ptr & 0xFF00FF;
unsigned int g1 = *ptr & 0x00FF00;
unsigned int rb2 = color & 0xFF00FF;
unsigned int g2 = color & 0x00FF00;

unsigned dst_alpha = *ptr >> 24;
dst_alpha += alpha;
if (dst_alpha > 255) dst_alpha = 255;

*ptr++ = ((rb1+(((rb2-rb1)*alpha)>>8))&0xFF00FF|(g1+(((g2-g1)*alpha)>>8))&0x00FF00) | (dst_alpha << 24);
}
}
delete[] buffer;
}

Share this post


Link to post
Share on other sites
man glutStrokeCharacter

witch glLineWidth(2) and GL_LINE_SMOOTH. i have great results with this.

Share this post


Link to post
Share on other sites
thank you guys =))))

thank you...I'll try this routine

Eitsch...I do not wanna use the glut =(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!