+2  A: 

Ok, so you're trying to "paint" a given colour (in this case "red") on to a background, using a mask for the brush shape.

You need to do the following before you start rendering the "paint":

  1. First make sure your brush has an alpha channel that corresponds with its shape - that is the alpha channel should look similar to the brush image you posed.

  2. Render with these states set (note space to get around wiki markup):

// Make the current material colour track the current color
glEnable( GL_COLOR_MATERIAL );

// Multiply the texture colour by the material colour.
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// Alpha blend each "dab" of paint onto background
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

See also:
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/colormaterial.html

http://www.khronos.org/opengles/documentation/opengles1_0/html/glTexEnv.html

Justicle
Justicle-- Thank you. This achieves the effect I was looking for-- I hadn't come across mention of using the "material" stuff in the API when looking up the blending. Can you explain for me-- since I was unable to either figure out the math myself, or find the answer with online searching--why the original technique as posted in the question behaved the way it did? I would like to understand what was really going on there.Thanks!
quixoto
Glad it worked for you - I wrote a bitmap font renderer last year and found out all this the hard way. I can't really say what went wrong in your original code as I don't know what you were doing.If you are curious, I would eliminate each step I outlined (in turn) and try to reverse-engineer your bug.
Justicle
I guess I should point out that the important "tinting" operation is enabled by the `glTexEnvf` call. Each texel is multiplied by the current colour - so white (1,1,1,1) x color (R,G,B,A) = (R,G,B,A); and black (0,0,0,0) x colour = black (0,0,0,0). The other parts are just enabling the tinting to actually work.
Justicle
Thanks Justicle. It's that final bit of tinting info that I was after. Thanks!
quixoto
Hello Justicle,I've tried to do same thing as an author. Finally I've found paint example. In this example I changed background color to white by changing clear color in erase method glClearColor(1.0, 1.0, 1.0, 1.0);I've changed blending method for texture according your recommendation, but it still raw black square around my brush. Can you please advice me what to check? I'm really noob in OpenGL, sorry if my question is a bit stupid.
OgreSwamp
You should write up your steps to repeat and ask a SO question, there's about a million things that can go wrong. Sorry.
Justicle
A: 

Hi Ben,

I'm looking also for this solution, but I can't get the right result using the sample application "GLPaint". Can you share the part of your code where you setup the OpenGL environment ?

strange99
A: 

I'm in the same boat as strange99. I'm completely new to OpenGL, and I'm trying draw on a white background by reverse engineering GLPaint. I've done what you've suggested above, plus I've gone through every combination of glBlendFunc, AND just about every combination I can think of for brush texture (black on white, white on black, white on trans, alias/no alias, etc).

The best I've been able to achieve is with a white-on-trans circle, with glBlendFunc as above (GL_ SRC_ ALPHA, GL_ ONE_ MINUS_ SRC_ ALPHA), but this still gives me a dull colour, and the semi-trans outer bits are interpreted as black. It's as though it still assumes I'm on a black background.

Any advice?

Kevin Beimers