Jump to content
  • Advertisement
Sign in to follow this  
Four

blending

This topic is 4763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have made some font as bitmaps (letter's are white, and backgrounds are black), I just want the black part to be transparent and also to draw black colored text. I have loaded the font as textures but have problems with how it looks, black is transperent (even the black letters) and the font seems to blend in with what has already been drawn(when font is other than black). Currently the blend func is glBlendFunc(GL_SRC_ALPHA, GL_ONE). Thank you!

Share this post


Link to post
Share on other sites
Advertisement
What you wanna do is called masking.
As far as i know you cant mask with a single pass (unless your bitmaps
are 32 bits with alpha channel). You need to draw each letter twice, each
time with different glBlendFunc(). Or you can avoid the problem simply by using
the z-buffer and drawing your letters first closer than the rest of your scene
(on a black background ofcourse).

without using the z-buffer, try the following:

glColor3f(1, 1, 1);
glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
/* Draw your bitmap here */
glColor3f(the color you want your letters to be);
glBlendFunc(GL_ONE, GL_ONE);
/* draw your bitmap a second time */

edit: In order for this to work you should set
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
edit2: Forget about what i said about the z-buffer - it wont work.

Share this post


Link to post
Share on other sites
Or another possibility:
glBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
glBlendFunc(GL_ONE, GL_ONE);

Enigma

Share this post


Link to post
Share on other sites
Thank you!

IceFront's method works best.

Quote:

I think youre looking for this:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


lemme know if it works

that doesn't work

Share this post


Link to post
Share on other sites
See this : http://www.opengl.org/resources/code/rendering/mjktips/TexFont/TexFont.html

A more detailed description of what I said above ;-)

Share this post


Link to post
Share on other sites
Quote:
Original post by trippytarka
See this : http://www.opengl.org/resources/code/rendering/mjktips/TexFont/TexFont.html

A more detailed description of what I said above ;-)


That works very well :)! Thanks

Edit:
After lots of testing I found a nice method to make the text look like its painted on glass using a combination of all the methods stated above.

Set up OpenGL like this:
[source lang=cpp]
void setup()
{
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(red, green, blue, how much not to see through);
// draw text
}
// For the font texture convert it to GL_LUMINANCE_ALPHA, it won't work if the bitmap is GL_RGB
// e.g.
double ColorDistancec(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
double red;
double green;
double blue;
double alpha;

red = (double)r;
green = (double)g;
blue = (double)b;
alpha = (double)a;
red = red*red + blue*blue + green*green + alpha*alpha;
return sqrt(red);
}
// This function converts an RGB image to GL_LUMINANCE_ALPHA
// alpha is 0 if the luminance is close to 0
// I had all my font's go through this function before uploaded to video memory

void ConvertToLuminanceAlpha(void*in, void*out, int width, int height)
{
int i;
unsigned char* outc;
unsigned char* inc;
double max;

outc = (unsigned char*)out;
inc = (unsigned char*)in;
// 195075 = 3*255*255 -> maximum "distance" of RGB value
max = 255.0f/sqrtf(195075.0f);

for(i = (height*width); i > 0; i --)
{
*outc = (unsigned char)(ColorDistancec(inc[0], inc[1], inc[2], 0)*max);
if(*outc <= 1)
{
// So close that you could say it is invisible
outc++;
*outc = 0;
}
else
{
outc++;
*outc = 255;
}
outc++;
inc += 3;
}
}
// take what comes out, and upload it to video memory like this:
void UploadImage(void* data, long w, long h)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, w, h,0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data);
}




The font looks very nice!

[Edited by - Four on July 30, 2005 5:49:41 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!