Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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)

share|improve this answer
    
+1, Agreed, and I will modify my code according to your idea. Let me mar yor answer as the Accepted Answer as well. –  Bunkai.Satori May 27 '11 at 2:29
    
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? –  Utkarsh Sinha Jun 1 '13 at 22:50
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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