Question

I want to draw with a texture brush, in a color of my choice, on a white background using OpenGLES.

I have a bitmap image which I use CG to load and turn into a texture. This bitmap is mostly black, but has a white circle in the center that I want to use as the "brush". In other words, I want the black part to vanish in the final compositing, but the white part to take on the color that I set using glColor.

The best I can get is with the blend parameters (GL_SRC_ALPHA, GL_ONE) after setting some opaque bright color is a faded color line on a grey (not pure white background). But when I set the background to pure white, the line isn't visible.

At least in the current situation, the black edges of the original texture don't appear. Most other blend combinations I'm trying cause either nothing to show up even on grey, or to see the entire brush including the black edges, which is no good.

Is anyone willing to explain to me how I should set up my texture and/or GL states to make the bright color show through on pure white, without showing the black texture edges at all? This might be a newbie question, but I've tried working through the blend math, and I still just don't understand how the colors are all being factored together.

Here's the image I'm using as the brush:

alt text http://www.coldcoffeeandjuice.com/OpaqueBrush.png

Here's some resulting output, when the background is grey, and the glColor4f is set to (1, 0, 0, 1), eg pure red, and the brush is used on a bunch of consecutive GL_POINTs. Note that what's good about this is that only the white part of the brush image shows the color red-- that's right. The bad parts is that the red which I want to be pure and bright is pale due to being blended with the background (?) and/or the white of the brush (?) so that it washes out entirely if the background is pure white. This uses the blend params as given above (src_alpha, one).

alt text
(source: coldcoffeeandjuice.com)

Here's what I want to see, given the pure red color (thanks Paintbrush):

alt text
(source: coldcoffeeandjuice.com)

Can anyone help me understand what I'm doing wrong?

Thanks!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top