0
votes

I have an OpenGL application that shows a transparent pipe and some colorful liquid that runs through it.

The picture shows the pipe empty on the left side and full on the right side.

Transparent pipe

When I change the background to white, the transparent pipe vanishes (I guess it becomes white, too due to the blending).

I use the following GL-Code:

gl.glColor4d( 240, 240, 240, 0.1); // transparent color
gl.glEnable( gl.GL_BLEND );
gl.glBlendFunc( gl.GL_SRC_ALPHA, gl.GL_ONE );
glu.gluCylinder( quadObj, width, width, length, 32, 1 );

How should I adjust the blend func (and/or color, if necessary) to achieve the same effect with a white background?

1

1 Answers

3
votes

I suggest glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

BTW. the values going into glColor…{f,d} are clipped to the range [0,1]. I suggest you try some light gray for the outer pipe, i.e. glColor4f(0.75, 0.75, 0.75, 0.1)

Also be aware that OpenGL transparency is not order independent. Faces must be sorted back to front for blending to work properly (if you look at the far end of the cylinder in your picture you can see, that it misses the back face, which could be, because it gets drawn last, but then it already is occluded by the rest of the cylinder.

Update: There's an easy fix for the transparency order problem: Draw the cylinder twice with face culling enabled. In the first step draw with the front faces being culled, then draw a second time with the backfaces being culled:

glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
gluCylinder( quadObj, width, width, length, 32, 1 );
glCullFace(GL_BACK);
gluCylinder( quadObj, width, width, length, 32, 1 );
glDisable(GL_CULL_FACE);