Question

I have a terrain in OpenGL, and two textures which I am combining using GLSL mix() function. Here are the textures I am using. enter image description here

Now I am able to combine and mix these two textures, but for some reason, when I render the textures on the terrain, the terrain becomes transparent. I render the LHS texture first, and then I render the RHS texture with alpha channel, I don't understand why it is transparent.

enter image description here Here is an interesting fact, in the screenshot, you can see the result of the terrain when rendered on Nvidia GPU, when I render the same thing on interl HD 3k, I get different result. This result is how it is supposed to be, nothing is transparent in this following screenshot.

enter image description here

Here is my fragment shader code..

void main() 
{
vec4 dryTex   = texture( u_dryTex, vs_texCoord *1 );
vec4 grassTex = texture( u_grassTex, vs_texCoord *1 );
vec4 texColor1= mix(dryTex, grassTex , grassTex.a);   
out_fragColor = texColor1;
}
Was it helpful?

Solution

Looks like you're interpolating the alpha channel as well. Component-wise, the mix does:

texColor1.r = dryTex.r*(1-grassTex.a) + grassTex.r*grassTex.a
texColor1.g = dryTex.g*(1-grassTex.a) + grassTex.g*grassTex.a
texColor1.b = dryTex.b*(1-grassTex.a) + grassTex.b*grassTex.a
texColor1.a = dryTex.a*(1-grassTex.a) + grassTex.a*grassTex.a

The alpha channel for an opaque dryTex will thus be

grassTex.a^2

which is transparent most of the time.

Edit: The fix would be:

void main() 
{
    vec4 dryTex   = texture( u_dryTex, vs_texCoord *1 );
    vec4 grassTex = texture( u_grassTex, vs_texCoord *1 );
    vec4 texColor1= vec4(mix(dryTex.rgb, grassTex.rgb, grassTex.a), 1);   
    out_fragColor = texColor1;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top