Alpha blending - what am I missing?

I tried the following code, thinking it would give me a translucent blue rectangle overlapping a red rectangle - but the blue rectangle is also opaque - am I doing something wrong?
I tried specifying 8 alpha bits in the AWTGLCanvas constructor, but no luck…


GL11.glColor4f(0, 0, 1, 1f);
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glVertex2f(10, 10);
GL11.glVertex2f(10, 30);
GL11.glVertex2f(30, 30);
GL11.glVertex2f(30,10);
	
			
}
GL11.glEnd();

GL11.glColor4f(1, 0, 0, 0.1f);
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glVertex2f(15, 20);
GL11.glVertex2f(15, 40);
GL11.glVertex2f(25, 40);
GL11.glVertex2f(25,20);
				
						
}
GL11.glEnd();


Before you draw the second triangle at least, you need to set the blending function:


GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

which tells OpenGL how to blend the pixels of the source (what you’re trying to draw) with the destination (what’s already been drawn). GL_SRC_ALPHA says, multiply the colour of the source pixels - the red ones - by their alpha, which is 0.1f; GL_ONE_MINUS_SRC_ALPHA says multiply the colour of the destination pixels - the blue ones already drawn - by 1-0.1f, which is 0.9f; then the two values are added together and that’s the final colour.

Cas :slight_smile:

Thanks :slight_smile: I couldn’t get to work but looking around in here I saw I also had to call GL11.glEnable(GL_BLEND);
Works like a charm now:)