PDA

View Full Version : Blending or painting transparent to texture



hockeyman
08-17-2012, 05:15 AM
So I need to draw transparent brush strokes to OpenGL texture, to use it as mask later on. I have brush image (black square with transparent circle). And I need to draw brush stroke of that transparent part of brush texture to other opengl texture. I know how to do it directly drawing and blending, but now firstly I need to draw to texture. I was using code like this (http://stackoverflow.com/a/5098999/1428622) for that what I want to achieve. And it was working. But how can I firstly draw mask to texture (i know how to draw something to texture using FBO, but how to draw nice transparent brush strokes to it and make texture for example fully black, with only brushstrokes transparent?), and only then use this texture as mask. So if now I am acting like that:



glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO);
//draw background

glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
//Draw vertexarray for brush strokes.

glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
//draw foreground


I want to act like that:



glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, someFramebufferForBrushStroke);
//draw vertexarray for brush strokes to texture
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO);
//draw background

glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
//Draw mask quad texture (texture witch is binded as GL_COLOR_ATTACHMENT to my frame buffer

glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
//draw foreground


Now I drag mouse, and OpenGL draws textured(my brush texture) quad at every mouse event location.
I want to: when I drag mouse openGL draws whole vertex array to texture (all these textured quads at mouse location, witch in my current method draws directly to screen), and then uses that texture as mask (draws full size quad).

Its OK with drawing to texture, but just what blending or something I should use to tell opengl to make transparent parts of texture where in my brush texture is transparent circle and to ignore black square around transparent circle?

hockeyman
08-20-2012, 01:11 AM
Some visualization (lets say that white color is transparent part, and my mask is black quad with transparent circle in it):

Now when I render my brushstroke to FBO i get something like that:
853


And I need result like that:
854

So I need to ignore black part of mask while drawing it to empty, cleared with black, fbo.

tonyo_au
08-20-2012, 06:50 PM
But how can I firstly draw mask to texture
You don't need to draw the brush to a texture; just load the data directly with something like glTexImage2D. The pixel data should be of the form RGBA with the alpha used to set your mask.
Now you can use this texture in your code.

If you are making the texture via some draw function to a bound texture just make sure you are writing the alpha component as well as the rgb.