Game Development Reference
In-Depth Information
list of points. If the heatmap is being generated in realtime we'd want to do it in immediate
mode, if it is being generated from collected data then deferred would work well. When
generating the heatmap in immediate mode, it's important to understand the game engine's
rendering architecture, in particular if it is multithreaded, we need to be certain that we
don't write into the heatmap data while it is being accessed by the rendering thread.
We will see how to generate the heatmap in immediate mode, the algorithm is the same
in deferred mode except we iterate over the list of points, but we avoid using the function
AddPoint .
void heatmap::AddPoint(const math::point& point)
{
const unsigned int* gradient = m_gradient.Data();
const auto gradientSize = m_gradient.Size();
float gradientRadius = gradientSize * 0.5f;
unsigned int offsetX = static_cast<int>( point.x() - gradientRadius );
unsigned int offsetY = static_cast<int>( point.y() - gradientRadius );
math::Clamp(offsetX, 0U, m_size);
math::Clamp(offsetY, 0U, m_size);
for (unsigned int y = 0; y < gradientSize; ++y )
{
for (unsigned int x = 0; x < gradientSize; ++x )
{
unsigned int indexX = (offsetX + x);
unsigned int indexY = (offsetY + y);
math::Clamp(indexX, 0U, m_size-1);
math::Clamp(indexY, 0U, m_size-1);
auto& sourcePixel = m_data[indexY * m_size + indexX ];
const color source = color(sourcePixel);
const color grad = color(gradient[y * gradientSize + x]);
const color destination = (source) + (grad * grad.A()) ;
sourcePixel = destination.ToU32();
}
}
}
We will draw a radial gradient at each point's position, we start by offsetting the starting
position by the radius of the gradient, this will ensure that the center of the gradient will be
at the point's position. If the offset should fall outside the boundaries of the heatmap, we
will clamp it to the edge.
Now we will begin drawing the gradient image into the heatmap image, we iterate over
each pixel in the gradient and we calculate the corresponding pixel index to the position to
which we want to draw, index and indexY .
Search WWH ::




Custom Search