PDA

View Full Version : glcolormask with only alpha enabled does not work?



noeska
25-08-2009, 10:52 AM
Who can provide me with extra info on the glcolormask?
For my vector graphics project i need 2 different fills one with color and one on the alpha channel.
So i thought i could do:
Code:

//draw fill
glColorMask(TRUE,TRUE, TRUE, FALSE); //but not yet alpha
fStyle.DrawFill(fBoundBoxRadius);

//draw fill alpha
glColorMask(FALSE,FALSE,FALSE, TRUE);
fStyle.DrawAlphaFill(fBoundBoxRadius);



But the alpha channel does not get set. Only if i enable the colors in the second part i get the alpha channel working. But then the collors are all wrong... as i blend white over other colors.

Thanks in advance for your ideas on how to solve this.



Meanwhile i have been playing around with using different blend modes together with the colormask settings
on drawing the fill i use glBlendFunc(GL_DST_COLOR, GL_ZERO) and on the alpha glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) and the result looks ok'ish.

More on blend modes: http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial9.php

User137
25-08-2009, 02:46 PM
Render without any of the R,G,B is not really doing anything at all. Alpha value is merely a multiplicator for them which isn't written to any buffer as itself. I think you will need to use
glColorMask(TRUE,TRUE,TRUE,TRUE);
for desired effect but we may need more detailed info about the wanted looks to help :)

noeska
25-08-2009, 03:21 PM
what i want is to fill a shape with a raidial gradient and for the tranparenci i want to use a different radial fill.

E.g. Like this: (color fill, alpha fill, blended end result)
http://www.noeska.net/images/blendex1.jpg

This is my current code that works ok'ish ( but i suspect it to bleech )

//draw alpha fill
glBlendFunc(GL_DST_COLOR, GL_ZERO);
glColorMask(TRUE,TRUE,TRUE, TRUE);
fStyle.DrawAlphaFill(fBoundBoxRadius,fboundboxminp oint, fboundboxmaxpoint);

//draw fill
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
glColorMask(TRUE,TRUE, TRUE, FALSE); //but not alpha
fStyle.DrawFill(fBoundBoxRadius,fboundboxminpoint, fboundboxmaxpoint);

noeska
25-08-2009, 06:18 PM
Another problem came along.
As i use the the stencil buffer now i get problems when 2 shapes overlap. E.g. no transparency anymore. An shape that is not drawn with the stencil buffer gives proper transparency. :'(

noeska
26-08-2009, 08:28 AM
Another testcase: (seems to work ok'ish)
It draws 2 quads (red and blue) on a green quad. The 2 quads have gradient transparency.


glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

glcolor4f(0,1,0,1);
glbegin(GL_QUADS);
glVertex3f(0, 480, 0.0); // Top Left
glVertex3f(640, 480, 0.0); // Top Right

glVertex3f(640, 0, 0.0); // Bottom Right
glVertex3f(0, 0, 0.0); // Bottom Left
glend;

//FIRST QUAD

//turning off writing to the color buffer and depth buffer so we only
//write to stencil buffer
glColorMask(FALSE, FALSE, FALSE, FALSE);
//enable stencil buffer
glEnable(GL_STENCIL_TEST);
//write a one to the stencil buffer everywhere we are about to draw
glStencilFunc(GL_ALWAYS, 2, $FFFFFFFF);
//this is to always pass a one to the stencil buffer where we draw
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
//draw shape

glbegin(GL_QUADS);
glVertex3f(0, 260, 0.0); // Top Left
glVertex3f(280, 260, 0.0); // Top Right
glVertex3f(280, 0, 0.0); // Bottom Right
glVertex3f(0, 0, 0.0); // Bottom Left
glend;

//until stencil test is diabled, only write to areas where the
//stencil buffer has a one. This fills the shape
glStencilFunc(GL_EQUAL, 2, $FFFFFFFF);
// don't modify the contents of the stencil buffer
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);


//draw alpha fill
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMask(FALSE,FALSE,FALSE, TRUE);

glbegin(GL_QUADS);
glcolor4f(1,1,1,1); //SOLID
glVertex3f(0, 260, 0.0); // Top Left
glVertex3f(280, 260, 0.0); // Top Right
glcolor4f(1,1,1,0.0); //TRANSPARENT
glVertex3f(280, 0, 0.0); // Bottom Right
glVertex3f(0, 0, 0.0); // Bottom Left
glend;

//draw fill
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
glColorMask(TRUE,TRUE, TRUE, FALSE); //but not alpha

glbegin(GL_QUADS);
glcolor4f(1,0,0,1); //RED
glVertex3f(0, 260, 0.0); // Top Left
glVertex3f(280, 260, 0.0); // Top Right
glVertex3f(280, 0, 0.0); // Bottom Right
glVertex3f(0, 0, 0.0); // Bottom Left
glend;

//'default' rendering again
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMask(TRUE,TRUE, TRUE, TRUE);
glDisable(GL_STENCIL_TEST);

//SECOND QUAD

//turning off writing to the color buffer and depth buffer so we only
//write to stencil buffer
glColorMask(FALSE, FALSE, FALSE, FALSE);
//enable stencil buffer
glEnable(GL_STENCIL_TEST);
//write a one to the stencil buffer everywhere we are about to draw
glStencilFunc(GL_ALWAYS, 4, $FFFFFFFF);
//this is to always pass a one to the stencil buffer where we draw
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
//draw shape

glbegin(GL_QUADS);
glVertex3f(100, 360, 0.0); // Top Left
glVertex3f(380, 360, 0.0); // Top Right
glVertex3f(380, 100, 0.0); // Bottom Right
glVertex3f(100, 100, 0.0); // Bottom Left
glend;

//until stencil test is diabled, only write to areas where the
//stencil buffer has a one. This fills the shape
glStencilFunc(GL_EQUAL, 4, $FFFFFFFF);
// don't modify the contents of the stencil buffer
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);


//draw alpha fill
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMask(FALSE,FALSE,FALSE, TRUE);

glbegin(GL_QUADS);
glcolor4f(1,1,1,1); //SOLID
glVertex3f(100, 360, 0.0); // Top Left
glVertex3f(380, 360, 0.0); // Top Right
glcolor4f(1,1,1,0.0); //TRANSPARENT
glVertex3f(380, 0, 0.0); // Bottom Right
glVertex3f(100, 100, 0.0); // Bottom Left
glend;

//draw fill
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
glColorMask(TRUE,TRUE, TRUE, FALSE); //but not alpha

glbegin(GL_QUADS);
glcolor4f(0,0,1,1); //BLUE
glVertex3f(100, 360, 0.0); // Top Left
glVertex3f(380, 360, 0.0); // Top Right
glVertex3f(380, 100, 0.0); // Bottom Right
glVertex3f(100, 100, 0.0); // Bottom Left
glend;

//'default' rendering again
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMask(TRUE,TRUE, TRUE, TRUE);
glDisable(GL_STENCIL_TEST);

User137
26-08-2009, 11:07 AM
Maybe multitexturing is thing you are after? It allows you to use another texture for transparency only.

noeska
26-08-2009, 03:38 PM
I try to avoid going the texture way. I am afraid to end up with large textures.