Jump to content
  • Advertisement
Sign in to follow this  
Quasius

Multitexture problems

This topic is 3645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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: TexCombineRight 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*/



It seems like this should be possible with one pass and 3 texture units. I tried this (along with many other things):
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*/



but that results in this: TexCombineWrong 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.

Share this post


Link to post
Share on other sites
Advertisement
Try GL_COMBINE_ALPHA instead of GL_COMBINE_RGB for texture unit 1.

Here's the official OpenGL man page for reference.

Additionally, keep in mind that your final texture's alpha will most certainly be changed so either need to disable blending or reset alpha to 1 in order to not have a partly transparent quad.

Share this post


Link to post
Share on other sites
I'm not entirely sure. This would be easier done in GLSL. I never really used TexEnv so I'm not really any help.

Share this post


Link to post
Share on other sites
If your alpha mask is another texture, in GLSL


uniform sampler2D Texture0;
uniform sampler2D Texture1;
uniform sampler2D Texture2; //Mask

void main()
{
vec4 texel0, texel1, texel2, resultColor;

texel0 = texture2D(Texture0, gl_TexCoord[0].xy);
texel1 = texture2D(Texture1, gl_TexCoord[0].xy);
texel2 = texture2D(Texture2, gl_TexCoord[0].xy);

resultColor = mix(texel0, texel1, texel2.a);
gl_FragColor = resultColor;
}



If alpha mask is in texture 0


uniform sampler2D Texture0;
uniform sampler2D Texture1;

void main()
{
vec4 texel0, texel1, resultColor;

texel0 = texture2D(Texture0, gl_TexCoord[0].xy);
texel1 = texture2D(Texture1, gl_TexCoord[0].xy);

resultColor = mix(texel0, texel1, texel0.a);
gl_FragColor = resultColor;
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!