-
OpenGL Masks
I have been trying to implement a sort of mask with OpenGL for the last few days and have been unsuccessful so far.
I found this solution - c++ - OpenGL - mask with multiple textures - Stack Overflow which I tried but was only partly successful in that the mask worked but then my foreground had no alpha blending. This was in an application for PS3. I haven't recently written a short test program on Windows to try and get a ful working solution but seem to have gone backwards.
My current code:
Code:
gluOrtho2D(0.0f, 1.0f, 1.0f, 0.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO);
renderBackground(); // Draws multiple primitives
glEnableClientState(GL_VERTEX_ARRAY);
// Next, we want a blendfunc that doesn't change the color of any pixels,
// but rather replaces the framebuffer alpha values with values based
// on the mask. In other words, if a pixel is alpha value 1 in the mask,
// then the corresponding framebuffer pixel's alpha will be set to 1.
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ZERO);
// Now "draw" the mask (again, this doesn't produce a visible result, it just
// changes the alpha values in the framebuffer)
GLfloat mask[16] = {0,0, 1,0, 0,0.1, 1,0.1, 0,0.9, 1,0.9, 0,1, 1,1 };
GLfloat colour[32] = { 0,0,0,0,
0,0,0,0,
0,0,0,1,
0,0,0,1,
0,0,0,1,
0,0,0,1,
0,0,0,0,
0,0,0,0 };
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, mask);
glColorPointer(4, GL_FLOAT, 0, colour);
glDrawArrays(GL_QUAD_STRIP, 0, 8);
glDisableClientState(GL_COLOR_ARRAY);
// Finally, we want a blendfunc that makes the foreground visible only in
// areas with high alpha.
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
renderForeground(); //Draws multiple primitives which also include alpha values
glFlush(); // Flush the OpenGL buffers to the window
On the PS3 the mask was replacing all the alpha values of the foreground rather than merging the alpha values from mask and foreground, but on Windows its not even doing that and all I get is the foreground without any transparency. Hope I've been concise enough.
Thanks in advance.