PDA

View Full Version : Help needed: GL_COMBINE_EXT and how to map RGB to ALPHA ?



aerofly
05-21-2003, 11:39 PM
hello,

i have a question regarding the GL_COMBINE_EXT extension.

here is what i want to achieve:

i have a RGBA or RGB texture where white pixels indicate opaque and black pixels indicate transparency. i would like to map this color information to an alpha channel, so that when the texture is drawn it appears opaque where white pixels are and is transparent where black pixels in the texture are found.

can somebody give me a hint on how to do it with the GL_COMBINE_EXT extension ?

i tried this, with no luck.


glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_EXT, GL_MODULATE);

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PRIMARY_COLOR_EXT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_COLOR);

for some reason this doesnt work. what am i doing wrong here ?

i thought
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_COLOR);
is acutally the line the does it, but it doesn't seem like it?

and another question, what does GL_PREVIOUS_EXT mean in the following command:
glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_PREVIOUS_EXT);


any help is greatly appreciated!

JustHanging
05-22-2003, 12:54 AM
Hi,

You're not allowed to use the color channel as an operand for alpha functions. The only way I'm aware of to get the color to the alpha is to use GL_DOT3_RGBA to get the cross product of the color and for example (1, 0, 0) to the alpha channel.

The GL_PREVIOUS means the result of the previous combiner, or in the case of the first one, the primary color.

-Ilkka

vincoof
05-22-2003, 02:41 AM
If you want to use the alpha component for blending, note that you could use the color directly.

That is, instead of calling that :
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
You can call that :
glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);

The bad point is that you can not benefit the alpha test for discarding fragments, but if you're not using alpha test actually it is not a problem

JanHH
05-22-2003, 04:49 AM
can't you simply assign the right alpha values (according to the texel color) in the function that reads the texture image? At least, my program does it this way.

Jan

aerofly
05-22-2003, 03:37 PM
hello,

thank you for all your help. using the GL_DOT3_RGBA i finally achieved what i wanted, unfortunatly i need two texture stages for this. the second stage is used to color the texture afterwards, otherwise the color of the texture is simply the result of the DOT3_RGBA result.

here is my combine setup

// this stage converts rgb to alpha
float col[4] = { 1.0f, 0.5f, 0.5f, 0 };
glActiveTextureARB( GL_TEXTURE0_ARB );
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, col);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_DOT3_RGBA_EXT);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_EXT, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_EXT, GL_SRC_COLOR);

// this stage is used for coloring
glActiveTextureARB( GL_TEXTURE1_ARB );
glEnable( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, dummytexture);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_EXT, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_EXT, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_EXT, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_EXT, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_EXT, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_EXT, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);

does anybody know of a simpler setup, without the need of the second texture stage ?

> can't you simply assign the right alpha
> values (according to the texel color) in
> the function that reads the texture image?
> At least, my program does it this way.
> Jan

jan, i could do it this way, however the texture is generated each frame and to do this fast i use glCopyTexSubImage. other methods would be slower i assume. however, is there a way to use glCopyTexSubImage such that the rgb information is converted directly into the alpha component?

> That is, instead of calling that :
> glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
> You can call that :
> glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);

this method works however i haven't found out how i can color the texture afterwards ?



[This message has been edited by aerofly (edited 05-22-2003).]

vincoof
05-23-2003, 02:25 AM
jan, i could do it this way, however the texture is generated each frame and to do this fast i use glCopyTexSubImage. other methods would be slower i assume. however, is there a way to use glCopyTexSubImage such that the rgb information is converted directly into the alpha component?
You could allocate a GL_INTENSITY texture and the R component read will be copied to the I component (which represents an unified RGBA image where R=G=B=A=Intensity). I think it should work. I'm not sure that it will be hardware accelerated on very old hardware, though.

You still need to setup a texture enviroment combining function which separates the PRIMARY_COLOR for RGB output fragment and modulates the PRIMARY_COLOR with TEXTURE for the Alpha output fragment.




> That is, instead of calling that :
> glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
> You can call that :
> glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);

this method works however i haven't found out how i can color the texture afterwards ?
You can't.
Indeed, I didn't know you wanted to modulate your RGB output with primary color. So, just forget about it.