View Full Version : Question about texture blending

10-24-2005, 01:20 PM
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) ;


10-24-2005, 01:24 PM
If I correctly understand what you want to do, then you should change this line:

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

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

10-24-2005, 01:29 PM
This works if the texture has an alpha channel, but what if it is just a RGB ?

10-24-2005, 01:37 PM
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...

10-24-2005, 03:17 PM
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 (http://www.flightzone.us/1.jpg) shows what it looks like now, second shot (http://www.flightzone.us/2.jpg) 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.

10-24-2005, 03:31 PM
Have you tried using "intensity" as the alpha?

gl_FragColor = vec4 (color, intensity);

10-24-2005, 11:17 PM
Yes, but it doesnt work.

10-24-2005, 11:20 PM
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.

10-24-2005, 11:51 PM
Stupid question:
Is alpha blending enabled?
Is the alpha blend function set to something like SRC_ALPHA/ONE_MINUS_SRC_ALPHA?

10-25-2005, 05:00 AM
No it's not enabled ! Probably (i hope) that's the problem, but shaders do not operate regardless of fixed opengl blend status ?

10-25-2005, 10:08 AM
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.