PDA

View Full Version : Alpha blending with Cg shaders



mosen
01-18-2009, 06:54 AM
I am trying to use alpha blending in my Cg shaders. This is my code in the application where I first render a plane (behind) and then render a sphere (in front of plane that should be transparent):



glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ZERO);

// Test alpha blending.
render_plane(-3, 0,2, 0, 0,0, 1, 10);

// Render transparent sphere.
glPushMatrix();
glScalef(2.0, 2.0, 2.0);
glTranslatef(0.0, 0.0, 1.5);
render_sphere();
glPopMatrix();

//--- Disable GL blending
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);


I would like the sphere to be transparent so I can see the plane thats behind it.

In the fragment program for the plane I set the

OUT.color.a = 1.0

and in the fragment program for the sphere I set the

OUT.color.a = 0.5.

But the sphere is not transparent, what am I doing wrong?

ZbuffeR
01-18-2009, 09:49 AM
You blend func is wrong.
Try one of these instead :

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); // classic transparency
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ); // pre-multiplied alpha