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:
HFONT m_font = ::CreateFont(m_description->m_fontSize, 0, 0, 0, true, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH, "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), (int)stringRoundedToPowerOf2Size.cx, (int)stringRoundedToPowerOf2Size.cy, 1, 32, };
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; ::GetObject(m_font, sizeof(LOGFONT), &mFontDesc);
RECT area = {0, 0, stringSize.cx,stringSize.cy};
int drawRet = ::DrawText(m_hdc,
m_string.c_str(),
-1, &area,
DT_CENTER | DT_SINGLELINE | DT_VCENTER);
if (0 != drawRet) {
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);
glEnable (GL_TEXTURE_2D);
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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, stringRoundedToPowerOf2Size.cx, stringRoundedToPowerOf2Size.cy, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageBuff);
m_LastRoundedToPowerOf2Size = stringRoundedToPowerOf2Size; 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!