Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello!

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.

Example

Here is the code I use:
C++
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,
     "Compress");

 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};

 stringRoundedToPowerOf2Size.cx = RoundUpToPowerOf2(stringSize.cx);
 stringRoundedToPowerOf2Size.cy = RoundUpToPowerOf2(stringSize.cy);

 BITMAPINFOHEADER bih =
 {
     sizeof (BITMAPINFOHEADER),  // biSize;
     (int)stringRoundedToPowerOf2Size.cx,            // biWidth;
     (int)stringRoundedToPowerOf2Size.cy,        // 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, stringSize.cx,stringSize.cy};
 int drawRet = ::DrawText(m_hdc,
              m_string.c_str(),
              -1, // null terminated string
              &area,
              DT_CENTER | DT_SINGLELINE | DT_VCENTER);

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

     BITMAPINFOHEADER   bi;
     bi.biSize = sizeof(BITMAPINFOHEADER);
     bi.biWidth = stringRoundedToPowerOf2Size.cx;
     bi.biHeight = stringRoundedToPowerOf2Size.cy;
     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 = stringRoundedToPowerOf2Size.cx * bi.biBitCount * stringRoundedToPowerOf2Size.cy;
     long* imageBuff = (LONG*)::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwBmpSize);
     ::GetDIBits(m_hdc, hBitMap, 0, stringRoundedToPowerOf2Size.cy, imageBuff, (BITMAPINFO *)&bi, DIB_RGB_COLORS);

     WTRects tempRect(0,0,stringRoundedToPowerOf2Size.cy, stringRoundedToPowerOf2Size.cx);
     WTRects tempStringSize(0,0,stringRoundedToPowerOf2Size.cy, stringRoundedToPowerOf2Size.cx);
     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 ((stringRoundedToPowerOf2Size.cx != m_LastRoundedToPowerOf2Size.cx) ||
            (stringRoundedToPowerOf2Size.cy != m_LastRoundedToPowerOf2Size.cy))
        {
             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_WRAP_S, GL_CLAMP);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
     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, stringRoundedToPowerOf2Size.cx,  stringRoundedToPowerOf2Size.cy, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageBuff);
     m_LastRoundedToPowerOf2Size = stringRoundedToPowerOf2Size; //for use to decide if we need to regenerate a texture.
     glDisable(GL_TEXTURE_2D);
     ::HeapFree(GetProcessHeap(), 0, imageBuff);
 }

 ::SelectObject(m_hdc, prevBitMap);
 ::DeleteObject(hBitMap);
 ::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?

Thanks!
Posted
Updated 11-Jun-12 22:53pm
v4

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.
 
Share this answer
 
Comments
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?

Thanks!
nv3 12-Jun-12 7:47am    
Arial or Calibri should work.
Everything works fine.
It was another bug in our system.
Thanks everyone, sorry.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900