Warm tip: This article is reproduced from serverfault.com, please click

OpenGL Alpha Mask

发布于 2014-09-11 13:37:26

I'm trying to use alhpa masking with glAlphaBlend and all his parameters. I found a lot similar answer on stackoverflow but i doesn't realise anything.

I have three images, a background (1), a mask (2), and text (3).

What i want is to draw the background, substract my mask to the text to finally obtain the image (A).

Image Example

Draw background

glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
glBegin(GL_QUADS);
glColor4ub(0,0,0,255);
glVertex2d(mxIncrust0 - bgwidth, myIncrust0);
glVertex2d(mxIncrust0, myIncrust0);
glVertex2d(mxIncrust0, myIncrust0 + textheight);
glVertex2d(mxIncrust0 - bgwidth, myIncrust0 + textheight);
glEnd();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA, GL_ZERO, GL_ONE);

Draw Text

But Anything is working !

What is the real way to achieve it ?

My question is different because it is not based on OpenGL ES but only Open GL.

Questioner
Maypeur
Viewed
0
Maypeur 2014-09-23 22:20:49

I finally found a solution with glClipPlane, i didn't find any solution with shader since it isn't texture.

So how it works ? 1) I draw my background (quad, lines...) 2) I define and active a limit below nothing is draw on the left and a limit above nothing is draw on the right 3) I draw everything i need (quad, lines, my text...) 4) i disable the clip.

// Draw the background
double* leftMask = new double[4];
leftMask[0] = 1;
leftMask[3] = -mClipXL; // Be careful to use -Limit for the left limit
glClipPlane(GL_CLIP_PLANE0, leftMask);
delete[] leftMask;
glEnable(GL_CLIP_PLANE0);

double* rightMask = new double[4];
rightMask[0] = -1;  // Be careful to use -1 for the right limit
rightMask[3] = mClipXR;
glClipPlane(GL_CLIP_PLANE1, rightMask);
delete[] rightMask;
glEnable(GL_CLIP_PLANE1);

// draw the text

// disable the clip to draw anything else.
glDisable(GL_CLIP_PLANE0);
glDisable(GL_CLIP_PLANE1);

It's working very very well !!!