Jump to content


Transparency using GLSL


2 replies to this topic

#1 cristoir

    New Member

  • Members
  • Pip
  • 2 posts

Posted 02 March 2007 - 01:39 PM

Hi everyone,

I was wondering if anyone could help me with a problem I'm having with my GLSL shaders. I'm trying to make certain vertices in my geometry (a skydome) partially transparent. The intent being to create a day/night transition by having an outer skydome to represent space beyond the atmosphere, as well as an inner skydome to model the clouds.

I'm running into a problem before I even get off the ground though; I can't actually get any of the pixels to be tranparent. The way I've tried it so far is as follows (this is for the clouds)...

Vertex shader...
varying vec2 texCoords_Clouds;
void main(void)
{
	texCoords_Clouds = gl_MultiTexCoord0.st;
    gl_Position = ftransform();
}

Fragment shader...
uniform sampler2D cloudsTexture;
varying vec2 texCoords_Clouds;

void main (void)
{
	vec3 color_Clouds = vec3(texture2D(cloudsTexture, texCoords_Clouds));
	float alphaFade = color_Clouds.r / 256.0;
    gl_FragColor = vec4(color_Clouds,alphaFade);
}

I'm using Java3D, and the texture being passed in is a BufferedImage.TYPE_BYTE_GRAY if that makes any difference.

The idea behind my fragment shader is that the grey value determines the transparency of a pixel (black pixels being totally transparent, white pixels being totally opaque).

It makes sense to me in theory to approach it like this, but when I run the program, all the pixels are totally opaque. Even when I just use an artificial value of say... "gl_FragColor = vec4(color_Clouds,0.1);", I still get all pixels being totally opaque!!

Does anyone have any idea what the problem might be here? Am I even using the fourth value in the vec4 correctly?

Thanks for any advice you can give.

#2 hovermonkey

    Member

  • Members
  • PipPip
  • 38 posts

Posted 02 March 2007 - 02:07 PM

Have you enabled GL_BLEND and set glBlendFunc to (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)?

#3 cristoir

    New Member

  • Members
  • Pip
  • 2 posts

Posted 02 March 2007 - 02:32 PM

Hi hovermonkey,

Thank you, that was exactly the problem. Although, as I mentioned, I am using the Java3D API, so am not making any direct calls to OpenGL.

As a point of interest for anyone who is curious about how to do this in Java3D, the code is as follows...

ShaderAppearance app = new ShaderAppearance();

...
// Other work goes here, like attaching shader programs etc.
...

TransparencyAttributes tranAttrib = new TransparencyAttributes(TransparencyAttributes.BLENDED,1.0f,TransparencyAttributes.BLEND_SRC_ALPHA,TransparencyAttributes.BLEND_ONE_MINUS_SRC_ALPHA);

app.setTransparencyAttributes(tranAttrib);

Thanks again hovermonkey, I've now got some fairly acceptable looking skies in my project!!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users