Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Pre-multiplied alpha  (Read 8489 times)

0 Members and 1 Guest are viewing this topic.

JoshuaT

  • Newbie
  • *
  • Posts: 5
    • View Profile
Pre-multiplied alpha
« on: June 09, 2011, 05:44:22 pm »
I use pre-multiplied alpha almost exclusively, since it is useful for being able to perform alpha blending and additive blending in a single operation. In pre-multiplied alpha, instead of doing a (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) blend operation you do (GL_ONE, GL_ONE_MINUS_SRC_ALPHA) and assume that the source color has already been multiplied by its alpha value.

This way, if you want to do regular alpha blending, you supply a texture in which the color values are scaled by the alpha value. If you want to do additive blending, you supply a texture in which the color is not scaled, and the alpha is 0. You can thus combine the two types of blending in a single operation, in a single texture.

The fix I applied is simple, consisting of another enum field added to sf::Blend::Mode, and the line case Blend::PreMultiply: GLCheck(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)); break added to Renderer::SetBlendMode.

Perhaps you guys could consider making it official?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Pre-multiplied alpha
« Reply #1 on: June 09, 2011, 08:44:45 pm »
I don't know... "PreMultiply" is not very intuitive compared to Add, Multiply or Alpha. And what kind of use cases are there for such an operation?
Laurent Gomila - SFML developer

JoshuaT

  • Newbie
  • *
  • Posts: 5
    • View Profile
Pre-multiplied alpha
« Reply #2 on: June 09, 2011, 09:47:27 pm »
http://home.comcast.net/~tom_forsyth/blog.wiki.html#%5B%5BPremultiplied%20alpha%5D%5D

http://keithp.com/~keithp/porterduff/p253-porter.pdf

http://blogs.msdn.com/b/shawnhar/archive/2009/11/06/premultiplied-alpha.aspx

Premultiplying alpha can help reduce or even eliminate "haloing" around alpha-blended sprites if the sprites are filtered. It can contribute to better DXT compression. But the main reason I use it is to be able to have particles and sprites that include both alpha-blended and additive areas in the same operation.

In a standard alpha blend using (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) the equation of course is (src.rgb * src.a) + (dest.rgb * (1-src.a)). With premultiplied alpha, you are just doing the (src.rgb * src.a) step separately, when the texture bitmap is created. For additive blending, we don't bother multiplying the src.rgb by src.a during texture creation. This way, if a src pixel has color but 0 alpha, the color is added on top of the destination pixel rather than blended with it.

In other words, for something like a particle flame, you can have both flame and smoke particles drawn from the same texture, without switching texture state between passes. I use it pretty extensively for various magical spell effects in my current game project.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Pre-multiplied alpha
« Reply #3 on: June 09, 2011, 10:49:46 pm »
I see, thanks for the links.

I'll have to think about it.
Laurent Gomila - SFML developer

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Pre-multiplied alpha
« Reply #4 on: June 10, 2011, 06:39:01 am »
This actually would be really cool.  It would be nice if there was an option for SFML to do the alpha pre-multiplying on image load as well.


EDIT:

If it matters, I vote "yay" for adding this officially to SFML.

It's a simple change to make, it's very functional, and it makes for a very nice effect.

Tronic

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • http://performous.org/
Pre-multiplied alpha
« Reply #5 on: July 06, 2011, 11:57:21 pm »
Ideally this could be the default, with alphas premultiplied during image loading as necessary (e.g. PNG does not use premultiplied alpha, TIFF does). There are no drawbacks of doing premultiplied alpha, only benefits.

Haikarainen

  • Guest
Pre-multiplied alpha
« Reply #6 on: July 16, 2011, 06:35:15 am »
Would also like to see this benefit :) Gogo laurent, doesnt look like that much work :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Pre-multiplied alpha
« Reply #7 on: July 16, 2011, 09:50:31 am »
Quote
doesnt look like that much work

The amount of work is never a problem, my main concern is always design decisions ;)
Laurent Gomila - SFML developer

 

anything