Alpha blending with a texture

There are two textures, one color, the other transparency (for the rings of the planet) I don't understand how the shader should be made so that in places where black, the color was transparent in the color map.

enter a description of the image here

enter a description of the image here

Doing something like this

alphaColor= texture(alphaTexture, fs_in.TexCoords).rgb;
diffuseColor= texture(mainDiffuseTexture, fs_in.TexCoords).rgb + alphaColor;

But the result is this..In this case, the black color is cut off, leaving white, but the color map does not depend on the transparency map. enter a description of the image here

The solution was written in in the response, here is the result) enter a description of the image here

Author: Глеб Труфанов, 2021-01-06

2 answers

You must enable alpha blending at the rasterization stage

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

And then output the color in the shader as RGBA.

 2
Author: user7860670, 2021-01-06 07:44:01

Actually, comrade user7860670 was right, thank you) Personally for myself in the form of a solution I did this:

vec4 alpha = texture(alphaTexture, fs_in.TexCoords);
vec4 ringColor = vec4(texture(mainDiffuseTexture, fs_in.TexCoords).rgb, (alpha.r + alpha.g + alpha.b) * 0.33333)

The last parameter 0.33333 can be changed as you like, and in general, the formula can be adjusted to suit yourself. Don't forget to enable alpha blending before rendering:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
_planetaryRing->Draw();
glDisable(GL_BLEND);
 1
Author: Глеб Труфанов, 2021-01-06 08:51:07