opengl es texture combine interpolate - iphone

Hi everyone:

I am new to OpenGL, and hope you guys can help me.

In my app, I have two textures. I would like to use the opengl texture combine mode to do a a linear interpolation of the two texture and display the resulted texture. So in the displayed texture, the pixel value will be the linear combination of the values of the same pixel from my original two textures,. I used the following code, but looks like the first texture is OK, but the second is totally white. So the results is a “whited” texture1. Why?

Please help me…

void generateTexture(int id)
{
int width = 128;
int height = 128;
unsigned char* data = …

glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

int main_function()
{

GLuint tex[2];
glGenTextures(2, tex);

generateTexture(1);
generateTexture(2);
GLint iUnits;

glBindTexture(GL_TEXTURE_2D, 1);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE1);

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_SRC_COLOR);
//------------------------
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); //Interpolate ALPHA with ALPHA
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE);
//------------------------
t = 0.5;
float mycolor[4];
mycolor[0]=mycolor[1]=mycolor[2]=t; //RGB doesn’t matter since we are not using it
mycolor[3]=t; //Set the blend factor with this
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, mycolor);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

[context presentRenderbuffer:GL_RENDERBUFFER_OES];

I don’t work on GL-ES, has been a coons age since I used reg/tex combiners, and I’m no guru on them, but a few thoughts…

Does your hardware support texture combine crossbar (ARB_texture_env_crossbar, or OES_texture_env_crossbar)? You’re assuming it does.

I don’t see you binding two textures when setting up combine, yet you’re trying to read 2. I don’t see any glActiveTexture(GL_TEXTUREn)/enable texunit n calls before the binds.

And shouldn’t your OPERAND2 be SRC_ALPHA, not SRC_COLOR?

P.S. Is that you, J.R.? :slight_smile:

Also check this for some examples:

Hi Dark Photon:

Thank you very much for you reply. I have a few questions:

  1. How do you know if the hardware support texture combine crossbar?
  2. What is the difference of “SRC_ALPHA” and 'SRC_COLOR" in the context?

Thank you again.

have a look at the extension
http://www.opengl.org/registry/specs/ARB/texture_env_combine.txt

SRC_ALPHA is the alpha value of the operand and SRC_COLOR is the RGB value. In the “alpha combiner” you can only use ALPHA sources whilst in the color combiner stage you can use both, and alpha will be treated as grey-scale value.

Check the extension list. glGetStringi( GL_EXTENSIONS, i ) or glGetString( GL_EXTENSIONS ), depending on what GL-ES supports.

CrazyButcher got your other question, better than I could.