0

What I want to do is multiple a grayscale alpha bitmap with an RGB color value, and then draw the resulting RGB bitmap to screen additively, that is, at each pixel I want:

p_new[r/g/b] = p_old[r/g/b] + colorValue[r/g/b] * grayscaleBitmapValue

I know I can construct an intermediate RGB bitmap or texture holding (colorValue[r/g/b] * grayscaleBitmapValue), but I'd rather avoid this wasteful step if possible, because each alpha bitmap will be multiplied with quite a number of different color values.

What's the fastest way to do this in either 2D or OpenGL ES ?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
b_yang
  • 109
  • 6

1 Answers1

1

For 2D drawing, set up your Paint object like this:

paint.setColorFilter(
    new PorterDuffColorFilter(colorValue, PorterDuff.Mode.MULTIPLY));
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));

For OpenGL, your fragment shader could look like this:

varying vec2 texcoord;
uniform sampler2D texture;
uniform vec4 multColor;
void main() {
    gl_FragColor = texture2D(texture, texcoord)*multColor;
}

... and call glBlendFunc(GL_ONE,GL_ONE) to do additive blending.

Matthew Marshall
  • 5,793
  • 2
  • 21
  • 14
  • I'll test this. Are support all transfer modes and filters required for Android device/hardware ? Are they hardware-accelerated or am I better off (for performance) implementing it myself (I already have the C/C++ code) ? – b_yang Sep 14 '11 at 19:34
  • Drawing with the Paint object won't be hardware accelerated, but it'll work on all devices. It'll be about as fast, if not faster, than your own C/C++ code. – Matthew Marshall Sep 14 '11 at 21:00