Click here to Skip to main content
13,591,506 members
Rate this:
Please Sign up or sign in to vote.

I am trying to draw text string to string with openGL.
I generate string to texture and then I draw the texture. Despite the fact that I setting ANTIALIASED_QUALITY fonts still pixelate and ugly.


Here is the code I use:
HFONT m_font = ::CreateFont(m_description->m_fontSize, // nHeight,
     0, // nWidth,
     0, // nEscapement,
     0, // nOrientation,
     true,  // fnWeight,
     false,  //fdwItalic,
     false,  //fdwUnderline,
     false, //fdwStrikeOut,
     ANSI_CHARSET, //fdwCharSet,
     OUT_DEFAULT_PRECIS, // fdwOutputPrecision,
     CLIP_DEFAULT_PRECIS, // fdwClipPrecision,
     ANTIALIASED_QUALITY, // fdwQuality,
     DEFAULT_PITCH, // fdwPitchAndFamily,

 m_hdc = ::CreateCompatibleDC(0);

 HFONT prevFont      = (HFONT)::SelectObject(m_hdc, m_font);
 int imode           = ::SetBkMode(m_hdc, TRANSPARENT);
 COLORREF colorref   = ::SetTextColor(m_hdc,0x00ff0000);
 SIZE stringSize     = {0,0};
 SIZE stringRoundedToPowerOf2Size = {0,0}; = RoundUpToPowerOf2(; = RoundUpToPowerOf2(;

     sizeof (BITMAPINFOHEADER),  // biSize;
     (int),            // biWidth;
     (int),        // biWidth;
     1,                          // biPlanes;
     32,                         // biBitCount;

 void* pPixels       = NULL;
 HBITMAP hBitMap     = ::CreateDIBSection(m_hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &pPixels, NULL, 0);
 HBITMAP prevBitMap  = (HBITMAP)::SelectObject (m_hdc, hBitMap);

 LOGFONT mFontDesc;  // To check if all the parameters are correct - they are !!!
 ::GetObject(m_font, sizeof(LOGFONT), &mFontDesc);

 RECT area   = {0, 0,,};
 int drawRet = ::DrawText(m_hdc,
              -1, // null terminated string

 if (0 != drawRet) // If DrawText fails, the return value is zero.
     m_stringSize.Width() =;
     m_stringSize.Height() =;

     bi.biSize = sizeof(BITMAPINFOHEADER);
     bi.biWidth =;
     bi.biHeight =;
     bi.biPlanes = 1;
     bi.biBitCount = 32;
     bi.biCompression = BI_RGB;
     bi.biSizeImage = 0;
     bi.biXPelsPerMeter = 0;
     bi.biYPelsPerMeter = 0;
     bi.biClrUsed = 0;
     bi.biClrImportant = 0;

     DWORD dwBmpSize = * bi.biBitCount *;
     long* imageBuff = (LONG*)::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwBmpSize);
     ::GetDIBits(m_hdc, hBitMap, 0,, imageBuff, (BITMAPINFO *)&bi, DIB_RGB_COLORS);

     WTRects tempRect(0,0,,;
     WTRects tempStringSize(0,0,,;
     ColorBuffer(m_textColor, (uint32_t*)imageBuff, tempRect, tempStringSize); //(unsigned long)lpbi->bmiHeader.biSizeImage

     glEnable (GL_TEXTURE_2D);

     //glDirect does not seem to like resizing the textures, so we delete and create new, if size has changed.
     if (0 != m_texture_name)
        if (( != ||
            ( !=
             glDeleteTextures(1, &m_texture_name);
             m_texture_name = 0;

     if (0 == m_texture_name)
         glGenTextures(1, &m_texture_name);

     glBindTexture(GL_TEXTURE_2D, m_texture_name);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  // we use nearest to avoid corrupt alpha
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  // TT 11717
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,,, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageBuff);
     m_LastRoundedToPowerOf2Size = stringRoundedToPowerOf2Size; //for use to decide if we need to regenerate a texture.
     ::HeapFree(GetProcessHeap(), 0, imageBuff);

 ::SelectObject(m_hdc, prevBitMap);
 ::SelectObject(m_hdc, prevFont);

Anyone can help with that?
Maybe it is because my background is transparent so there is nothing to anti alias with?

Posted 11-Jun-12 22:50pm
Updated 11-Jun-12 22:53pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

First check whether your font "Compress" allows anti-aliasing, e.g. being a TrueType font.

The operating system also plays a role in the game. The MSDN documentation of the LOGFONT structure contains the following hint:

Windows 95 Plus!, Windows 98/Me: The display must greater than 8-bit color, it must be a single plane device, it cannot be a palette display, and it cannot be in a multiple display monitor setup. In addition, you must select a TrueType font into a screen DC prior to using it in a DIBSection, otherwise antialiasing does not occur.

So that might also be a reason.
Iron-Eagle 12-Jun-12 7:17am
I am using windows 7.
As for True-Type fonts - How can I check it?
Can you suggest font that surely should work?

nv3 12-Jun-12 7:47am
Arial or Calibri should work.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Everything works fine.
It was another bug in our system.
Thanks everyone, sorry.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.180618.1 | Last Updated 12 Jun 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100