PDA

View Full Version : how to copy texture alpha to RGB ?



speedy gonzalez
03-19-2004, 10:23 AM
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

dorbie
03-22-2004, 11:42 AM
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.

Bob
03-22-2004, 12:56 PM
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.

zeckensack
03-22-2004, 02:44 PM
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_AL PHA);Requires ARB_texture_env_combine (http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_env_combine.txt) , which became a core feature in GL version 1.3. It's widely supported (http://www.delphi3d.net/hardware/extsupport.php?extension=GL_ARB_texture_env_combin e) .

Bob
03-23-2004, 12:33 AM
Duh, I suppose that solution was too easy to see :)

speedy gonzalez
03-23-2004, 07:28 AM
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_MODULAT E);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_TEXTURE );
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_AL PHA);

glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,fac tor);

glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_CONSTAN T_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_CO LOR);