how to copy texture alpha to RGB ?

Hello, I have a RGBA texture and when a given mode is ON I would like the alpha of the texture to be used as replacement of the RGB components.

I would like to do this without having to redefine my texture or manually copy the pixels etc.

glTexEnv doesnt seem to support this.

Maybe the extension ARB_texture_env_combine would do it but I was hoping to find something a bit simpler to use.

Any clue how to achieve this ?

Thanks

I think it would take a fragment program but you could use combiners on simpler hardware. Fragment programs will ultimately be more portable on better hardware.

Without thinking too much about a better solution, it should be possible to do it like this using the standard combiners.

glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_2D, GL_SOURCE0_RGB, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_2D, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_2D, GL_OPERAND1_RGB, GL_SRC_ALPHA);

Not tested, but the idea is to modulate the constant (or primary if you like) color using the alpha channel. If the constant color is white, the result will be like copying the alpha value into the RGB-channels.

Bob, the correct target for TexEnvi is GL_TEXTURE_ENV, not GL_TEXTURE_*D. Your solution is also overkill. There’s no need to use a constant.

This will do:

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_ALPHA);

Requires ARB_texture_env_combine , which became a core feature in GL version 1.3. It’s widely supported .

Duh, I suppose that solution was too easy to see :slight_smile:

Thank you all I really appreciate !

It works fine with GL_COMBINE !

Now I also have to pre-scale the alpha using a constant so is the following the easiest way ?

float factor[4] = {
m_fCopyAlphaToRGBFactor, m_fCopyAlphaToRGBFactor, m_fCopyAlphaToRGBFactor,
m_fCopyAlphaToRGBFactor
};

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_ALPHA);

glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,factor);

glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_CONSTANT_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);