PDA

View Full Version : transparancy question



xargon
01-07-2006, 04:00 AM
Hi everyone,

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?

Thanks,

xarg

xargon
01-07-2006, 05:43 AM
Hi,

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

SetBkMode(hdc,TRANSPARENT);
SetTextColor(hdc,0xff0000);

BITMAPV5HEADER bi;
ZeroMemory(&bi,sizeof(BITMAPV5HEADER));
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;

if (bitmap)
DeleteObject(bitmap);
// 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
TextOut(hdc,0,0,str,strlen(str));

// unselect & release everything
SelectObject(hdc,oldFont);
SelectObject(hdc,oldThing);
DeleteObject(font);
DeleteDC(hdc);
::ReleaseDC(NULL,desktop);

// 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;
}
else
{
// Clear the alpha bits
*lpdwPixel &= 0x00FFFFFF;
// Set the alpha bits to 0xFF
*lpdwPixel |= 0xFF000000;
}
lpdwPixel++;
}

Now, with OpenGL I do the following:


glEnable(G_ALPHA_TEST);
glAlphaFunc(GL_GREATER ,GL_ZERO);
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!

Thanks,
xarg

API
01-07-2006, 06:06 AM
Hi,

if you are using this Windows dependant code, why don't you use wglUseFontBitmaps?

API

dmy
01-08-2006, 12:47 PM
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.

xargon
01-09-2006, 05:00 AM
wglUseFontBitmaps looks terrible.

The main reason for this was to use anti-aliasing even with Unicode characters.