Game Development Reference
In-Depth Information
Figure 79 - Radial gradient
We may also want to control the size of the gradient; by decreasing the radius we will re-
duce the size of the gradient, leaving a larger empty edge around it.
class gradient_radial : public gradient
{
public:
gradient_radial(unsigned int size, const color& startColor, const color& endColor, float sizeRatio=0.75f, bool reverse=false)
: gradient(size, startColor, endColor, reverse)
{
m_data = new unsigned int[size*size];
const math::vector2 center(size/2, size/2);
const float radius = center.Length() * sizeRatio;
for ( unsigned int y = 0; y < size; ++y ) {
for ( unsigned int x = 0; x < size; ++x ) {
float xx = x - center.x() + 0.5f;
float yy = y - center.y() + 0.5f;
float r = sqrtf(xx*xx + yy*yy) / radius;
color c = reverse ? (startColor * (1-r)) + (endColor * (r)) : (startColor * r) + (endColor * (1 - r));
m_data[y * size + x] = c.ToU32();
}
}
}
};
There are more optimal methods to calculate gradients, and it is also possible to add more
features to the linear and radial gradients presented here, for example, we can provide the
center point of a radial gradient as a user defined parameter, allowing the radial gradient
to be centered about any point within the image. Another useful feature would be to allow
more than just a start and end color, but rather to add color ranges, this would be a useful
feature to generate more complex gradients, such as the look up texture gradient used to
colorize heat maps. For further reading, see (Lomont, 2002).
Search WWH ::




Custom Search