views:

38

answers:

1

I have a simple one channel (8bit) bitmap with luminance data only, and I want to blend it with the existing framebufer like Screen blending mode does it in Photoshop.

So the source's white pixels (255) should result white, source's 50% gray pixels (128) should result the framebuffer pixel enlighted by 50%, and source's black pixels should leave the result alone. Do I have to set glColor4f as well?

Can some glBlendFunc expert of you help me here?

+1  A: 

Screen blending is C = S + (1 - S) × D, so what you want is glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR). If you ever introduce an alpha channel, you should still be able to get correct results if you keep your image data in premultiplied format.

Pivot
Let me see, let say source is a 0.5 gray pixel. If source is white, then 1.0 + (1 - 1.0) x 0.5 is white indeed. If source is 0.5, then 0.5 + (1 - 0.5) x 0.5 is 0.75 indeed. Fine, cool. The black then 0.0 + (1 - 0) x 0.5 is 0.5, the unchanged. Cool thanks, great.
Geri
so glBlendfunc is simply a source- and a destination multiplier? Seems nothin' more difficult. It is more clear now, thanks. Can you please tell me how glColor4f come into play?
Geri
Ohh, another short question. Premultiplied means that the RGB value is premultiplied somehow by the aplha value? So the "alpha-divide" during composition won't loose RGB information? Something like this? Is PNG premultiplied? Or only TGA is?
Geri
With blending enabled, instead of writing S directly to the framebuffer, OpenGL ES writes C = S × Sfactor + D × Dfactor. D is the value currently in the framebuffer, and you pick Sfactor and Dfactor with the arguments you pass to glBlendFunc. glColor4f sets the current vertex color, which is used when you don’t have a color array setup and enabled. The current vertex color starts out as (1, 1, 1, 1). What the color actually does depends on how you configure the texture environment. By default, it’s used to modulate the color from your texture.
Pivot
Premultiplied means that the color (R, G, B, A) is represented as (R × A, G × A, B × A, A). There’s no alpha divide during composition—rather, one of the multiplications that’s normally done during composition has already been done, which is why standard blending for premultiplied alpha uses glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) instead of glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). PNG isn’t premultiplied, but TGA can be. You can get premultiplied image data from a non-premultiplied image by drawing it into a CGBitmapContext, which uses a premultiplied representation.
Pivot