1
\$\begingroup\$

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

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ I really think it is time for me to get some sleep \$\endgroup\$
    – Chris
    Jan 11, 2011 at 0:41
  • \$\begingroup\$ 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? \$\endgroup\$
    – Chris
    Jan 11, 2011 at 1:02
  • \$\begingroup\$ Switching around the multiplication does not help either, even if a colour is applied. \$\endgroup\$
    – Chris
    Jan 11, 2011 at 1:08
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Jan 11, 2011 at 1:18
  • \$\begingroup\$ 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 \$\endgroup\$
    – Chris
    Jan 11, 2011 at 1:38

You must log in to answer this question.

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