01-07-2006, 04:00 AM
I have a bitmap that contains some text against a back background. Now, I want to display this bitmap in my OpenGL context. However, I want the black areas of the bitmap to be transparant i.e. use my OpenGL context background, but I want the text itself to remain the original color.
I tried various blending functions, but they also altar the text color, of course. Is there a way to achieve such effect?
01-07-2006, 05:43 AM
Well, I could create a bitmap with an alpha channel on the fly. However, I do not know what to do with it.
Basically, here is what the function is supposed to do:
Create a bitmap in memory with a certain text.
Render that bitmap in an OpenGL context. The background for the bitmap should be transparant. The text should retain its original color.
So, here is how I generate the text
HDC desktop = ::GetDC(NULL);
HDC hdc = ::CreateCompatibleDC(desktop);
HFONT font = CreateFont(-70,0,0,0,400,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRE CIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,DEFAULT_PITCH, "Arial");
HFONT ff = (HFONT) SelectObject(hdc,font);
bi.bV5Size = sizeof(BITMAPV5HEADER);
bi.bV5Width = mysize.cx;
bi.bV5Height = mysize.cy;
bi.bV5Planes = 1;
bi.bV5BitCount = 32;
bi.bV5Compression = BI_BITFIELDS;
// The following mask specification specifies a supported 32 BPP
// alpha format for Windows XP.
bi.bV5RedMask = 0x00FF0000;
bi.bV5GreenMask = 0x0000FF00;
bi.bV5BlueMask = 0x000000FF;
bi.bV5AlphaMask = 0xFF000000;
// Create bitmap
bitmap = CreateDIBSection(desktop, (BITMAPINFO *)(&bi), DIB_RGB_COLORS, &bits, 0, 0);
HGDIOBJ oldThing = SelectObject(hdc,bitmap);
HFONT oldFont = (HFONT) SelectObject(hdc,font);
// write out anti-aliased text
// unselect & release everything
// Now I can go through the image and set the alpha channel
lpdwPixel = (DWORD *)bits;
for (long x=0;x less than mysize.cx;x++)
for (long y=0;y less than mysize.cy;y++)
if ((*lpdwPixel & 0x00FFFFFF) == 0)
// Set alpha bit to 0.
*lpdwPixel &= 0x00FFFFFF;
// Clear the alpha bits
*lpdwPixel &= 0x00FFFFFF;
// Set the alpha bits to 0xFF
*lpdwPixel |= 0xFF000000;
Now, with OpenGL I do the following:
glDrawPixels(mysize.cx, mysize.cy, GL_BGRA_EXT, GL_UNSIGNED_BYTE, bits);
This does what it's supposed to do. But the anti-aliasing is gone now for the text. Does anyone know how I can get around that!
if you are using this Windows dependant code, why don't you use wglUseFontBitmaps?
or else, if your image is simple black and white, you can load it as a alpha texture.
then you render it with alpha testing enabled.
01-09-2006, 05:00 AM
wglUseFontBitmaps looks terrible.
The main reason for this was to use anti-aliasing even with Unicode characters.
Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.