🔒 Locked
blending
Advertisement
Original Post
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!
I think youre looking for this:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
lemme know if it works
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
lemme know if it works
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.
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.
Load the texture as GL_LUMINANCE_ALPHA, set glColor to black (with alpha 1.0), blending to on, then render your font.
Two passes better than one? Does not compute ;)
See this : http://www.opengl.org/resources/code/rendering/mjktips/TexFont/TexFont.html
A more detailed description of what I said above ;-)
A more detailed description of what I said above ;-)
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 memoryvoid 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]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement