OpenGL ES: How to tint texture with color

天涯浪子 提交于 2019-12-19 19:52:22

问题


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:

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


回答1:


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




回答2:


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.




回答3:


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);


来源:https://stackoverflow.com/questions/8559394/opengl-es-how-to-tint-texture-with-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!