سؤال

I am working on engine (http://b2dengine.com/) to implement my game idea, which is already in progress. Now for quite a while I am stuck with this problem with text rendering.

I have texture atlas that contains all supported/needed characters in a single png image. Character mapping is defined by Font class. Everything works fine, characters are rendered correctly and accurately. Thing is that I also need ability to change color of the text. This sounds and I bet it is simple but I cant make it work.

I am using this gl blend function to get desired effect.

GL.instance.glBlendFunc(GL.instance.GL_SRC_ALPHA, GL.instance.GL_ONE_MINUS_SRC_ALPHA);

In most cases it works, well as long as the color is somewhat dark. But when its set to white, I can see artifacts. I can provider screenshot if needed. Also for this to work, "text" in texture must be white.

I am using opengl 1.1 ( yes I know its old, but I am just not ready for shaders ).

So in short, my question is: how to render a texture and completely replace the color? Keeping only alpha channel. E.g. if its black and 80% transparent in png, when I render I want it green at 80% transparency on screen.

هل كانت مفيدة؟

المحلول

I think this will work, though these are some anciently deprecated functions and I don't remember too much about them anymore.

With your texture unit active which has the text texture, try setting these options:

glTexEnv(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnv(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnv(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PRIMARY_COLOR);
glTexEnv(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE);

I believe this will replace the RGB of your fragment with the incoming fragment color (glColor), and completely ignore the RGB channel of the texture. Having said this, it would be a lot better if you just uploaded an alpha channel texture to begin with.

Make sure to check for errors, and if it doesn't work, you can see if I've grabbed something incorrectly from here: http://www.opengl.org/sdk/docs/man/xhtml/glTexEnv.xml

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top