开发者

OpenGL - Textures - Replacing black (white) with color

I am generating a bitmap for a text display with OpenGL using Cairo/Pango. I generate the bitmap as RGBA with a transparent background and with the text in either black or white. (Let's assume black.)

If I load this bitmap as an OpenGL texture and display it, it appears as black text or white text, as expected.

I'd like to be able to color the text using only the original texture, but with OpenGL taking care of the coloring.

Preferably, I'd like to use glColor to set the color, but I'm willing to use glBlendColor or GL_TEXTURE_ENV_COLOR.

However, I can't get any of those options to work.

I've try what seem like innumerable combinations of - white text or black text - enabling blending or using GL_TEXTURE_ENV_MODE's GL_BLEND - trying GL_MODULATE, GL_REPLACE, and GL_COMBINE - tryin开发者_如何转开发g various differnt glBlendFunc combinations

I've been searching online and reading the spec for a few hours and I'm really at the end of my rope.

Can anyone point me to the right place to get the answer to this?


The simplest way to go is to have your text bitmap with white text and transparent background.

Then, to color it, you have to make sure that TEXTURE_ENV_MODE is set to MODULATE :

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

With this, the draw color will be glColor * textureColor == glColor (as textureColor is white)

Then you have to enable blending to handle your transparent background :

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

or, if your bitmaps are in premultiplied alpha form :

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);


Instead of supplying a full RGBA set, let the text just determinine the non-/transparency, i.e. just supply an alpha channel. Then use the normal glColor to set the text color.


It sounds like you can create the bitmap as you see fit. If that's the case, instead of black on white, I'd draw the text into the alpha channel. Then to color the text, you draw a rectangle (or whatever) of the color you want. Then draw your texture over it. Where the texture is opaque, you'll get the texture color. Where the texture is transparent, the background color will show through.

To do that, you will have to set up glBlendFunc. The usual should be fine:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

You also have to turn on blending for texturing:

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜