Original Post
I am trying to render a 2D sprite with an image overlay that gets applied according to an alpha mask. Here is an image of it working correctly:
The base texture is on the bottom, the overlay texture is on top and the alpha mask is on the left. The new image in is the center. The problem is the only way I've been able to do it is with 2 rendering passes. By rendering the base texture first and then rendering the water overlay with: It seems like this should be possible with one pass and 3 texture units. I tried this (along with many other things): but that results in this:
What am I doing wrong? I know I'm not fully understanding what's going on here. For example, my understanding is that the image will be drawn starting with texture unit 0 up to texture unit n. Wouldn't that mean the GL_TEXTURE_ENV_MODE I set for texture0 (the alpha mask) wouldn't matter since it's getting overwritten by subsequent texture units? But it does seem to matter since setting it to GL_DECAL or something produces different results. Thanks for any help.
The base texture is on the bottom, the overlay texture is on top and the alpha mask is on the left. The new image in is the center. The problem is the only way I've been able to do it is with 2 rendering passes. By rendering the base texture first and then rendering the water overlay with:
/*Render base texture on its own*/
glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_pMaskImage->GetTextureID());
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE0);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
glActiveTextureARB(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_pOverlayImage->GetTextureID());
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE1);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE0);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
/*calculate texture coords and render quad*/
glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_pMaskImage->GetTextureID());
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE0);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
glActiveTextureARB(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_pOverlayImage->GetTextureID());
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE1);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE0);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_pBaseImage->GetTextureID());
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE2);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE0);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_ONE_MINUS_SRC_ALPHA);
/*calculate texture coords and render quad*/
What am I doing wrong? I know I'm not fully understanding what's going on here. For example, my understanding is that the image will be drawn starting with texture unit 0 up to texture unit n. Wouldn't that mean the GL_TEXTURE_ENV_MODE I set for texture0 (the alpha mask) wouldn't matter since it's getting overwritten by subsequent texture units? But it does seem to matter since setting it to GL_DECAL or something produces different results. Thanks for any help.