1
\$\begingroup\$

The following is the OpenGL ES 2.0 simple GLSL Fragment Shader, I use to place textures on polygons, to render 2D sprites.

varying mediump vec2 TextureCoordOut;
uniform sampler2D Sampler;

void main()
{
gl_FragColor = texture2D(Sampler, TextureCoordOut);
//gl_FragColor = vec4(texture2D(Sampler, TextureCoordOut).xyz, TextureCoordOut.w * 0.5);

}

The fragment shader places voxels with alpha information taken from the source 2D texutre(.png image). Apart from alpha information, I need to control overall polygon/sprite transparency to achieve Fade In/Fade Out effects.

Could you show me, please, how to modify the above shader to control the overall transparency, besides the alpha information?

Note: The commented out line is used for my attempts to achieve the transparency. I wish to combine both the alpha information with the overall polygon/sprite transparency.

Thanks.

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

This is probably cleaner solution, which also doesn't require to ask texture unit two times.

void main()
{
  gl_FragColor = texture2D(Sampler, TextureCoordOut);
  gl_FragColor.a *= 0.5;
}

(maybe compiler does that for you, but what if not. Especially on the opengl-es device performance difference may be observed)

\$\endgroup\$
2
  • \$\begingroup\$ +1, Agreed, and I will modify my code according to your idea. Let me mar yor answer as the Accepted Answer as well. \$\endgroup\$ Commented May 27, 2011 at 2:29
  • \$\begingroup\$ I was under the impression that you cannot set gl_FragColor multiple times. Once you set it, the color is fed into the frame buffer. Is that a limitation on newer version of OpenGL? Maybe ES? \$\endgroup\$ Commented Jun 1, 2013 at 22:50
2
\$\begingroup\$

Apparently, I found the soultion.

This is the correct line in Fragment Shader:

    gl_FragColor = vec4(texture2D(Sampler, TextureCoordOut).xyz, texture2D(Sampler, TextureCoordOut).w * 0.5);

Thanks anyway.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .