Skip to main content
gamedev.net
🔒 Locked

Multitexture problems

Started by Quasius Jul 1, 2008 at 10:51 PM 2 replies 1.6k views
Original Post
Quasius
Quasius
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.
Lord_Evil
Lord_Evil
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.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
dpadam450
dpadam450
I'm not entirely sure. This would be easier done in GLSL. I never really used TexEnv so I'm not really any help.
NBA2K, Madden, Maneater, Killing Floor, Sims 
V-man
V-man
If your alpha mask is another texture, in GLSL

uniform sampler2D Texture0;uniform sampler2D Texture1;uniform sampler2D Texture2;   //Maskvoid 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;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.