OpenGL ES: How to tint texture with color

2019-02-23 00:50发布

I have texture with alpha. And I want to tint it with some color, so it will be colored depending on color alpha value, but overal opacity will be defined only by texture alpha.

This is similar to multi-texturing but with color instead of second texture. How to do it?

(Updated) I have tried to set up texture combiner. Color is tinted fine, but there is problem with alpha - it doesn't take value from texture (like mask). My code at this moment:

glActiveTexture (GL_TEXTURE0); // do we need stage #1?
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (GLfloat*) &tintColor_);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_ONE_MINUS_SRC_ALPHA);
// this doesn't work for alpha:
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);

Screenshots:

No tinting:

enter image description here

Tint with RGBA = (0, 0.5, 1, 0.5):

enter image description here

3条回答
何必那么认真
2楼-- · 2019-02-23 00:54

I get the same results (running on Android Samsung Galaxy S, for what it's worth). I don't understand why it doesn't work. The docs seem reasonably clear.

For me it works if I use GL_MODULATE for the RGB. It fails again if I use GL_ADD or GL_ADD_SIGNED. Even using GL_MODULATE on the alpha gives the same erroneous behaviour when using those modes on the RGB. You might be stuck with modulating your colours instead of interpolating them.

In summary, this works for me:

glActiveTexture (GL_TEXTURE0); // do we need stage #1?
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (GLfloat*) &tintColor_);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
查看更多
看我几分像从前
3楼-- · 2019-02-23 01:16

Have you tried the above code with glEnable(GL_BLEND)?

查看更多
ら.Afraid
4楼-- · 2019-02-23 01:20

You might want to read the OpenGL documentation on transparency, 15.020: "How can I achieve a transparent effect?"

Make sure you are rendering the scene in the right order (your answer does not show any rendering).

Make sure you are using the desired blend function.

Another suggestion I could make is to start using shaders. I always feel a bit more in control when I use them.

查看更多
登录 后发表回答