I'm trying to add fog to my sprite system, and I'm having a hard time coming up with the right equations to use.
Here's the problem:
To blit a sprite with fog, I first blit the sprite, then I blit a solid color version of the sprite over it. The alpha of the solid version is set accoring to how much fog is desired.
Sounds simple enough and works just fine when the sprite's alpha is 1.
But when the sprite's alpha is 0.5, sprite should be 50% transparent regardless of whether the fog is set to 1.0, or 0.5.
But if I simply multiply the fog's value by the alpha value and use that for my fog, then this is what happens:
Alpha = 0.5
Fog = 0.5
Fog*Alpha = Adjusted Fog = 0.25
Alpha + Adjusted Fog = 0.75
0.75 is how much the end result would cover up the pixels behind the sprite. One sprite blitted with 0.5 alpha, and one with 0.25 alpha comes to a total coverage of 0.75.
That not the desired level of transparency of the sprite though. It should only have a total alpha of 0.5.
I tried scaling the alpha values for both by a scaling factor to get them into the range I wanted:
factor = alpha/totalalpha
If totalalpha = 0 Then factor = 0
alpha*factor
fog*alpha*factor
But the result was that if the alpha was 1.0, and the fog was 0.25, then the total was 1.25, and this scaled down the alpha from 1.0 where it should have remained, meaning the end result was a sprite whose alpha was 1, but which was being drawn semi transparent.
Clamping the total to 1.0 would be the obvious next thing to try, but I'm pretty sure that that won't get me the result I want either.
Here's the problem:
To blit a sprite with fog, I first blit the sprite, then I blit a solid color version of the sprite over it. The alpha of the solid version is set accoring to how much fog is desired.
Sounds simple enough and works just fine when the sprite's alpha is 1.
But when the sprite's alpha is 0.5, sprite should be 50% transparent regardless of whether the fog is set to 1.0, or 0.5.
But if I simply multiply the fog's value by the alpha value and use that for my fog, then this is what happens:
Alpha = 0.5
Fog = 0.5
Fog*Alpha = Adjusted Fog = 0.25
Alpha + Adjusted Fog = 0.75
0.75 is how much the end result would cover up the pixels behind the sprite. One sprite blitted with 0.5 alpha, and one with 0.25 alpha comes to a total coverage of 0.75.
That not the desired level of transparency of the sprite though. It should only have a total alpha of 0.5.
I tried scaling the alpha values for both by a scaling factor to get them into the range I wanted:
factor = alpha/totalalpha
If totalalpha = 0 Then factor = 0
alpha*factor
fog*alpha*factor
But the result was that if the alpha was 1.0, and the fog was 0.25, then the total was 1.25, and this scaled down the alpha from 1.0 where it should have remained, meaning the end result was a sprite whose alpha was 1, but which was being drawn semi transparent.
Clamping the total to 1.0 would be the obvious next thing to try, but I'm pretty sure that that won't get me the result I want either.