#### Archived

This topic is now archived and is closed to further replies.

# A simple question about alpha blending

This topic is 5653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If the background is black (0,0,0) and I blend another black (0,0,0) texture onto it, will the result always be black(0,0,0), regardless of blend factor? My understanding was that this was a true statement but I am seeing different behavior in my attempt at implementing my game shroud. Is my shroud sprite alpha channel broken, my code buggy or my understanding of alpha channel wrong?

##### Share on other sites
Yes, it must be black.
To blend accuratly, you can use the simple following equation:
Xb = sqrt( X1*X1*Percent + X2*X2*(100-Percent) );
where X is a color component, such as Red, Green and Blue.

Hope I helped.

##### Share on other sites
quote:
Original post by The C modest god
To blend accuratly, you can use the simple following equation:
Xb = sqrt( X1*X1*Percent + X2*X2*(100-Percent) );

WTF?

Alpha blending is simply defined by:

ci = (1 - alpha) * desti + alpha * srci

alpha ranging 0..1 - if you want another range, jsut multiply by the maximum desired alpha value (e.g. 255). ci is the color component i (e.g. c0=red, c1=green, c2=blue) and desti and srci ar the destination(read: background)and the source (read: texture)
pixel''s color components.

Much simpler and doesn''t envolve any sqrt or multiplications at all (the latter depends on the alpha-range you''re using so its only for max. alpha that are powers of two, were multiplication simplifies to a _single_ bit shift [yes I know - _every_ number can be multiplied by another using only adds and shifts ]).

Cheers,
Pat

##### Share on other sites
quote:
Original post by darookie
WTF?

Alpha blending is simply defined by:

ci = (1 - alpha) * desti + alpha * srci

alpha ranging 0..1 - if you want another range, jsut multiply by the maximum desired alpha value (e.g. 255). ci is the color component i (e.g. c0=red, c1=green, c2=blue) and desti and srci ar the destination(read: background)and the source (read: texture)
pixel''s color components.

Much simpler and doesn''t envolve any sqrt or multiplications at all (the latter depends on the alpha-range you''re using so its only for max. alpha that are powers of two, were multiplication simplifies to a _single_ bit shift [yes I know - _every_ number can be multiplied by another using only adds and shifts ]).

Cheers,
Pat

No, that is a fast but inaccurate way to do alpha blending.
If you will try to blen black and white withe a 0.5 factor, the result wont look like a middle tone. it will look like a lot more darker then it should look like.

I forgot to mention, that you need to divide the equation I wrote by 100, so it will result in the correct range.

##### Share on other sites

      BYTE NewR;BYTE NewG;BYTE NewB;NewR = (BYTE)(OldR*Factor);  // modify the Red component of the desired pixelNewG = (BYTE)(OldG*Factor);  // modify the Green component of the desired pixelNewB = (BYTE)(OldB*Factor);  // modify the Blue component of the desired pixel// Factor must be a 0.0f to 1.0f float value

Kamikaze

[edited by - Kamikaze15 on December 2, 2002 11:51:59 AM]

##### Share on other sites
quote:
Original post by The C modest god
No, that is a fast but inaccurate way to do alpha blending.
If you will try to blen black and white withe a 0.5 factor, the result wont look like a middle tone. it will look like a lot more darker then it should look like.

I forgot to mention, that you need to divide the equation I wrote by 100, so it will result in the correct range.

Regards,
Pat

##### Share on other sites
hey there; (stretches) I''m back and swooning from tiredness, but, well, let''s see here... r=0, g=0, b=0 a=1 redered in the back buffer, and you try to render r=0, g=0, b=0 over it? Hmmm... check the alpha of the black overlay (the to-be-rendered black, not the already rendered black - God, I sound like a lunatic, now, don''t I? :D). I mean, if your info has a=0, it WON''T be rendered, period (and I REALLY mean that period part). The blending modes do modify the way colors are blended, but I don''t think that a black, transparent (argb=0) pixel written (which is practically nothing - null, the void, emptiness) - ''course don''t take my word for granted, there might be a blending mode that inverts all color (and transparency) info - check the sdk (you might want to write down ehich pixel is the source and which is the destination - I always confuse these two)

BTW, you really ahouldn''t go around usind blending modes different from ADD, since they are a bit of technology not included in most mid-generation graphics cards and your device might not be able to handle them if the machine the program is running on doesn''t support them.

In any case, I know I''m sounding somewhat... uh... I dunno, weird? since I came back from the university tired as hell, I almost immediately dropped dead asleep and I''ve just woken up : )

Steve, one more thing: maybe you could tell us exactly the nature of your problem (fog of war problems, mesmells) so that we can work something out... keep in touch mate

Back to sleep
Spyros

-----------<<>>-----------
flareman@freemail.gr
-----------<<>>------------

##### Share on other sites
quote:
Original post by darookie

Regards,
Pat

What do you mean?
Anyway, the equation I wrote is true not only to colors but to every wave form. Meaning, it is also true to sound wave, I think there is an explaination about sound wave decibels in directsound and that there is no linear connection between the sound amplitude and the strength you hear it.

##### Share on other sites
Sounds like I missed the boat completely (which does not surprise me the least bit).

I am trying to implement fog of war in a 2d tiles based game.
My tiles are diamond shaped. I would like to have a linear transition from shroud to no shroud.

I was thinking that it would be simpler and could be made to look more realistic (and maybe be more efficient) by defining the alpha grid manually within my shroud tile rather than within my code in the sort of manner which is mentioned in the replies to my original post.

My question arises when two shrouded tiles are neighboring and thus overlap since my shroud tile is larger than tile dimensions in all directions. I figured that the overlap regions would simply result in black, but this is not happening. The blending of the tapered shroud and the unshrouded terrain is working nicely however.

So in paint shop pro I drew a black rectangle larger than my tile dimensions. Then draw a grey tone mask as my alpha channel and store as a .tga file. To draw the tile in my game I simply do a spriteinterface->draw() call. Can this approach be made to work?

I have not looked at individual bits within the tga file to see what I am actually loading in. I am a newbie to paint shop pro and could be creating the alpha channel incorrectly. However when I draw just one shroud tile over unshrouded terrain it looks correct to me.

thanks
steve coward

##### Share on other sites
I''m not sure, maybe it''s the way you render the shroud tiles? I mean, perhaps you should render them in such a way that the shrouded/unshrouded tiles are rendered first, then the others... I can''t obviously see why this is happening, though... perhaps it''s the .tga file, maybe even it''s an alpha blending render states problem - however, do check on the .tga file''s alpha channel, maybe that''s the culprit

If I come up w/ anything else, I''ll be back

Flare

-----------<<>>-----------
flareman@freemail.gr
-----------<<>>------------

• 15
• 9
• 13
• 41
• 15