How to modulate every texture unit in OpenGL ES 1.1?

Posted by Jesse Beder on Stack Overflow See other posts from Stack Overflow or by Jesse Beder
Published on 2010-04-17T16:54:45Z Indexed on 2010/04/17 23:53 UTC
Read the original article Hit count: 334

Filed under:
|
|

I have two textures and a "blend factor", and I'd like to mix them, modulated by the current color; in effect, I want to use the following shader:

gl_FragColor = gl_Color * mix(tex0, tex1, blendFactor);

I'm using OpenGL ES 1.1, targeting all versions of the iPhone, so I can't use shaders, and I have two texture units. My best attempt is:

// texture 0
glActiveTexture(GL_TEXTURE0);
image1.Bind();

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

// texture 1
glActiveTexture(GL_TEXTURE1);
image2.Bind();

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_ALPHA, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);

const float factor[] = { 0, 0, 0, blendFactor };
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, factor);

This has the effect of the shader:

gl_FragColor = mix(gl_Color * tex0, tex1, blendFactor);

but I don't see how to module texture 1 by the color. Is there any way to have the color provided by a texture unit automatically modulated by the incoming primary color?

Or any other way to do what I want that I'm missing? Multiple passes are definitely allowed, but they have to have the proper blend effect; I have

glBlend(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

enabled, so it can be tricky to get right with multiple passes.

© Stack Overflow or respective owner

Related posts about opengl-es

Related posts about iphone