Problem with Alpha Mask

Hi,

I’ve been trying to create an easy (:mad:) test for alpha blending. And even if I’ve done this so often before, I can now not figure out, how to realise it.
I only want to show a quad with alpha 0.5, but for this example with a mask created before. Here’s my code (should hopefully reason out the imprecise explanation :)):

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class GLAlphaExample{
	private static final int width = 500, height = 500;
	
	public static void main(String[] args){
		new GLAlphaExample();
	}
	
	
	public GLAlphaExample(){
		run();
	}
	
	private void run(){
		initDisplay();
		initGL();
		while(!Display.isCloseRequested()){
			render();
			Display.update();
			try{
				Thread.sleep(1);
			}catch(InterruptedException ex){
				
			}
		}
		Display.destroy();
	}
	private void initDisplay(){
		try{
			Display.setDisplayMode(new DisplayMode(width, height));
			Display.create();
		}catch(LWJGLException ex){
			throw new RuntimeException("Fehler bei der Initialisierung des Displays", ex);
		}
	}
	private void initGL(){
		GL11.glViewport(0, 0, width, height);
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, width, height, 0, 0, 1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
	}
	private void render(){
		final float squareWidth = width * 0.5f, squareHeight = height * 0.5f;
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glLoadIdentity();
		GL11.glDisable(GL11.GL_BLEND);
		GL11.glColorMask(false, false, false, true);
		renderRect(0, 0, width, height, 0, 0, 0, 0.5f);
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glColorMask(true, true, true, false);
		GL11.glBlendFunc(GL11.GL_DST_ALPHA, GL11.GL_ONE_MINUS_DST_ALPHA);
		renderRect((width - squareWidth) * 0.5f, (height - squareHeight) * 0.5f, squareWidth, squareHeight, 1, 0, 0, 0);
	}
	private void renderRect(float x, float y, float width, float height, float red, float green, float blue, float alpha){
		final float x2 = x + width, y2 = y + height;
		GL11.glColor4f(red, green, blue, alpha);
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x, y);
		GL11.glVertex2f(x2, y);
		GL11.glVertex2f(x2, y2);
		GL11.glVertex2f(x, y2);
		GL11.glEnd();
	}
}

The red rectangle should be displayed a bit darker, because the mask was drawn with 0.5 alpha, but it doesn’t, it has full brightness.

Please help!

Your drawing code looks like it should do precisely what you’re expecting. The only thing i can think of is that your framebuffer pixel format doesn’t have the alpha channel. Try to query the pixel format upon creating the rendering context to make sure it’s RGBA.

Thanks, the pixel format was the mistake! I thought the default pixel format would provide alpha, but it doesn’t. Simple solution:

Display.create(new PixelFormat(8, 8, 0));