glBlendFunc: Use texture as mask

Hi!

I have two textures: One is a usual RGBA semi-transparent texture showing an image which is rendered as usual.

The second texture has just one channel (float, values in inverval [0,1]). I want to implement something like a cut-tool or a “rubber”-tool. The second texture shall now act as additional alpha-mask for the first one: If the second texture is zero, also the first texture shall become invisible. And vice versa, if the second texture is one, the first shall be visible (but not ignore the alpha value of the first one).

Can I realise this using glBlendfunc?

Exemplary code:


glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, uint_image_texture);
// render the texture
// ...
glBlendFunc(???,???);
glBindTexture(GL_TEXTURE_2D, unit_masking_texture);
// overlay the cut-texture
// ...

Thanks in advance,
Olli

From what I understood, that’s a task for multitexturing (easiest with GLSL), not the blend func.

it seems a bit complicated for the blend func, i would use a shader.

  1. That is definitely not advanced. Wrong forum.

  2. Something like that should do the trick using the fixed pipeline:


glBindTexture( GL_TEXTURE_2D, texture );
glTexCoordPointerEXT( 2, GL_FLOAT, 0, nbvertex*2, array_uv );

glActiveTextureARB( GL_TEXTURE1_ARB );
glClientActiveTextureARB( GL_TEXTURE1_ARB );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, textureAlphaMask );
glTexCoordPointerEXT( 2, GL_FLOAT, 0, nbvertex*2, array_uv );

#if 0
// Method 1
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

#else
// Or Method 2, to get more control:
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE_EXT);
// Color channel - Keep
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB_EXT,GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB_EXT,GL_PREVIOUS_EXT);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB_EXT,GL_SRC_COLOR);
// Alpha channel - Merge
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_ALPHA_EXT,GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_ALPHA_EXT,GL_PREVIOUS_EXT);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_ALPHA_EXT,GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_ALPHA_EXT,GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_ALPHA_EXT,GL_SRC_ALPHA);
#endif

glRenderblabla

// Cleanup
glDisable( GL_TEXTURE_2D );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
glActiveTextureARB( GL_TEXTURE0_ARB );
glClientActiveTextureARB( GL_TEXTURE0_ARB );

In a pixel shader, simply do this with your two samplers:


float4 texture = tex2D(first texture blabla);
float4 textureAlphaMask = tex2D(second texture blabla);
texture.a *= textureAlphaMask.a;

Thank you for your reply.

By the way, do I need these ARB commands at all if I just use this modulate function?