0
\$\begingroup\$

I have a transparent png texture and I'd like to create a white background and then blend this on top of that. Is this possible using just GLSL? I can't multiply, add or mix colors because I don't want to overlay the color white. I want it to be in the background of the texture.

I can achieve what I want by creating 2 objects with the exact same dimensions and position, set the color of the object behind to white and the object in the front to the transparent texture. But this seems less than ideal to me.

Any suggestions will be much appreciated!

\$\endgroup\$
0
\$\begingroup\$

Okay this is explained on this page. The following code in the fragment shader works perfectly for me:

#version 330 core
uniform sampler2D in_texture;
uniform bool fill_transparent;

in vec2 tex_coords;
out vec4 vertex_color;
void main() {
  vec4 vcolor = texture(in_texture, tex_coords);

  if (fill_transparent) {
    vec4 white = vec4(1.0f, 1.0f, 1.0f, 1.0f);
    vertex_color = (vcolor * vcolor.w) + (white * (1 - vcolor.w));
    // vertex_color = mix(white, vcolor, vcolor.w); // This also works.
  } else {
    vertex_color = vcolor;
  }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.