Question about texture blending

Very simple question i hope. I just want to texture a simple quad with a grey scale texture: the result i’m looking for is that pixels alpha should follow the texture, i.e. trasparent where texture pixels are black, alpha=1 if are white, and so on for intermediate grey values.
Can you tell me how shouldi modify the actual fragment shader ? Thanks

uniform sampler2D testTexture;

void main( void )
	vec4 texel;
	texel = texture2D( testTexture, gl_TexCoord[0].st) ;
	gl_FragColor = vec4(texel.rgb , texel.a) ;


If I correctly understand what you want to do, then you should change this line:

gl_FragColor = vec4(texel.rgb , texel.a) ;


gl_FragColor = vec4(gl_Color.rgb , texel.a) ;

This works if the texture has an alpha channel, but what if it is just a RGB ?

Try to take the average of the r, g, b:

gl_FragColor = vec4(gl_Color.rgb ,
0.3333333333333*(texel.r + texel.g + texel.b)) ;

Not sure what you’d want to do with RGBA textures…

Excuse me for poor explanation, i’ve two screenshot that will explain better, i hope, what i’d like to achieve. The shots show a sphere with a 3d noise texture applied on it.
First shot shows what it looks like now, second shot what i’d like to get.
Here are the vertex and frag shaders code.

//vertex frag
varying float LightIntensity;
varying vec3  MCposition;

uniform vec3  LightPos;
float Scale = 0.01;

void main(void)
    vec3 ECposition = vec3 (gl_ModelViewMatrix * gl_Vertex);
    MCposition      = vec3 (gl_Vertex) * Scale;
    vec3 tnorm      = normalize(vec3 (gl_NormalMatrix * gl_Normal));
    LightIntensity  = dot(normalize(LightPos - ECposition), tnorm);
    LightIntensity *= 0.75;
    gl_Position     = ftransform();
// Pixel frag
varying float LightIntensity; 
varying vec3  MCposition;

uniform sampler3D testTexture;
vec3 SkyColor;     // (0.0, 0.0, 0.8)
vec3 CloudColor;   // (0.8, 0.8, 0.8)

void main (void)
SkyColor[0] = 0.0;
SkyColor[1] = 0.0;
SkyColor[2] = 0.0;
CloudColor[0] = 0.8;
CloudColor[1] = 0.8;
CloudColor[2] = 0.8;

    vec4  noisevec  = texture3D(testTexture, MCposition);

    float intensity = (noisevec[0] + noisevec[1] + 
                       noisevec[2] + noisevec[3] + 0.03125) * 1.5;

    vec3 color   = mix(SkyColor, CloudColor, intensity) * LightIntensity - 0.25;

    gl_FragColor = vec4 (color, 1.0);	


Thanks for any help.

Have you tried using “intensity” as the alpha?

gl_FragColor = vec4 (color, intensity);

Yes, but it doesnt work.

What i dont understand is:

when i call gl_FragColor = vec4(color, test) ;

test is the alpha of the resulting image ?
If so, it doesn’t make any difference wether is 0.1 or 1.0, the transparency doesn’t change.

Stupid question:
Is alpha blending enabled?
Is the alpha blend function set to something like SRC_ALPHA/ONE_MINUS_SRC_ALPHA?

No it’s not enabled ! Probably (i hope) that’s the problem, but shaders do not operate regardless of fixed opengl blend status ?

Alpha blending is done after the pixel shader is run. It works whether you’re in fixed pipeline or in programmable pipeline. You should have it enabled.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.