0
votes

How can i emulate legacy opengls texenv gl_replace mode using es 2? Basically, if the texture fragments alpha is 0, i want to use the primitives color. But otherwise not affect the fragments color. Im new to glsl and could use the help.

Im on android and ive already got textures working with transparency. And i want to use just one shader program. What ive got: gl_FragColor=texture2D(u_TextureUnit,v_TextureCoords); And id like to add a color to the primitive that only shows up where the texture is transparent

2

2 Answers

2
votes

This snippet will output a blend between the vertex colour and the texture colour, controlled by the texture alpha, which I think is what you wanted. If not, please try to describe in more precise terms what your inputs and desired outputs are.

lowp vec4 vPrimitiveCol = colour_from_vertex_passed_as_varying;
lowp vec4 vTexCol = texture2D(u_TextureUnit,v_TextureCoords);
gl_FragColor.rgb = mix(vPrimitiveCol.rgb, vTexCol.rgb, vTexCol.a);
gl_FragColor.a = 1.0;
0
votes

What you want isn't GL_REPLACE which would completely replace the fragment color. It sounds more like GL_BLEND. Anyway, you can emulate it using the GLSL mix function:

solid_color = ...;
tex_color = texture(texture_sampler, texture_coord);
gl_FragColor = mix(solid_color, tex_color, tex_color.a);

Note that I replaced the older texture2D function by the more recent (and recommended) generic type texture function.