Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Earlier I required assistance getting to grips with how to retain the alpha channel of a transparent texture in my colourised texture shader program.

Whilst playing with that first version of my program (before obtaining the solution to my first requirement), I managed to enable transparency for the whole texture (effectively blending via GLSL), and I quite liked this, and I would now like to know if and how it is possible to retain this blending effect, on top of the existing output without affecting the original alpha channel - as I don't know how to input this transparency via the parameter that is already being provided with the textures alpha channel.

A basic example of the blending program I am referring to (minus any other functionality) is as follows...

varying vec2 texCoord;
uniform sampler2D texSampler;

void main() {
    gl_FragColor = vec4(texture2D(texSampler,texCoord).xyz,0.5);

Where 0.5 is the transparency (blending effect) of the whole texture.

This is the current version of my program, which provides the ability to colour a texture according the colour parameter passed to the program, and retains the alpha channel of the original texture.

varying vec2 texCoord;
uniform sampler2D texSampler;
uniform vec3 colour;

void main() {
    gl_FragColor = vec4(colour,1)
    * vec4(texture2D(texSampler,texCoord).xyz,texture2D(texSampler,texCoord).w);

I need to know if it is possible to apply transparency on top this program, without affecting the original alpha channel which I have already preserved.

I hope this makes enough sense, I am sure it is possible, and if so I should imagine it is rather simple, but this has me stumped. Any help much appreachiated.

Cheers, Chris

share|improve this question
up vote 2 down vote accepted

Are you just trying to scale the value of the alpha coming out of the texture? If so you can just supply a floating point value between 0 and 1 as a scaling value in the part "vec4(colour,1)" instead of the 1.

share|improve this answer
I really think it is time for me to get some sleep – Chris Jan 11 '11 at 0:41
Actually .. after testing this further; changing vec4(colour,1) to vec4(colour,0.5) only works on my textures with an alpha channel. Any texture which does not have transparency no longer applies this effect. Very strange, any idea what's going wrong there? – Chris Jan 11 '11 at 1:02
Switching around the multiplication does not help either, even if a colour is applied. – Chris Jan 11 '11 at 1:08
It may well be that you haven't got your blend modes set up correctly and that when a texture has an alpha channel it's automatically activating one of the source blend modes (or another bit of your codes does). I'm not fully up to speed on all things OpenGL as I'm more a D3D person. – Roger Perkins Jan 11 '11 at 1:18
Thanks Roger! your advice made me realise that the non transparent texture was being passed through an alternate draw list, which it turns out; does not have the correct blend mode enabled. This 3d world I have dived into is so hectic! :-D – Chris Jan 11 '11 at 1:38

Your Answer


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.