- Missiles attack
- mp3 (saw chili came out with a new vid about it, would be nice to decrease the size of my massive project)
- Alpha blending
- Ok, so the rotation in the Advance tutorials deals with rasterizing triangles at different angles.
- Mp3 support in the chili framework is coming via chili adding it to the Sound class, so unless you are wanting to know how he does it using the Media Foundation Class of the Win32 API, I'd just wait.
- Alpha blending is pretty straight forward once you understand that it's all about balancing an equation.
For alpha blending, say you have two colors:
Code: Select all
Color white = { 255, 255, 255, 255 };
Color black = { 255, 0, 0, 0 };
// Note that the alpha channel of white is 255. 255 is the highest value you can use for a color
// channel, which means that the resulting color after the alpha blend is going to be all white and no
// black.
// Now say you have the following colors:
Color white = { 127, 255, 255, 255 };
Color black = { 255, 0, 0, 0 };
// Note this time the alpha channel of white is 127, which is half the max value of a color channel.
// This means that the result will be half white and half black, producing a grey color.
// Also, note that the second color's alpha value isn't used. As stated, alpha blending is about balancing an equation. You use the one color's alpha value ( 127 ) for source 1 and the remainder of ( 255 - 127 ) for source 2 alpha.
You also might be getting confused because chili's implementation uses integers which is a bit more difficult to understand. If you were using floats for instance and the range was from 0.0f to 1.0f, you'd notice right away that:
Code: Select all
// In this case, the alpha channel is 1.f, so there is no remainder.
Color white = { 1.f, 1.f, 1.f, 1.f };
Color black = { 1.f, 0.f, 0.f, 0.f };
// In this case, the alpha channel is 0.5f, so there is a remainder of .5f
Color white = { .5f, 1.f, 1.f, 1.f };
Color black = { 1.f, 0.f, 0.f, 0.f };
If we were to blend these two colors, you'd do something like
Code: Select all
Color result;
const float alpha1 = white.a;
const float alpha2 = 1.f - white.a;
result.a = 1.f
result.r = (alpha1 * white.r) + (alpha2 * black.r );
result.g = (alpha1 * white.g) + (alpha2 * black.g );
result.b = (alpha1 * white.b) + (alpha2 * black.b );
Now, if you take into account, that the max value of a color channel is 255, that makes the range of values from 0 to 255 instead of 0.f to 1.f.
The reason I said integers might be the confusing part is because of the 255 max. When you multiply 255 by 127, you get a number way larger than 255. In order to bring it back into the 0 to 255 range, you have to divide the result by 255. Just to make sure it doesn't look pointless to do that, I'll use different numbers.
Code: Select all
Color colorA = { 64, 128, 0, 0 };
Color colorB = { 0, 0, 128 }; // left the alpha out since it doesn't play into the formula
int alphaA = colorA.a;
int alphaB = 255 - colorA.a;
Color result;
int ra = 255;
int rr = (64 * 128) + (191 * 0); // = 8192 + 0
int rg = (64 * 0) + (191 * 0); // = 0 + 0
int rb = (64 * 0) + (191 * 128); // = 0 + 24448
result.a = ra;
result.r = 8192 / 255 // = 32
result.g = 0;
result.b = 24440 / 255 // = 95
// As you can see, 64 is about a quarter of 255, and so the result of 32 is a quarter of 128 and 191 is about 3/4 of 255 and 95 is about 3/4 of 128 so the blue channel contributes more to the final color than the red channel. It's the ratio of alpha1 to alpha2. In this case 1 part red and 3 parts blue.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com