Game Development Reference
In-Depth Information
: m_device(device)
, m_gradient(gradient)
{
m_texture = std::shared_ptr<texture>( texture::Create(device, gradient.Size(), gradient.Size(), 1));
auto context = device->GetImmediateContext();
auto resource = m_texture->GetTexture2D();
D3D11_MAPPED_SUBRESOURCE res;
context->Map(*resource, 0, D3D11_MAP_WRITE_DISCARD, 0, &res);
memcpy(res.pData, gradient.Data(), m_texture->Size());
context->Unmap(*resource, 0);
}
std::shared_ptr<texture> Texture() const { return m_texture; }
private:
device_direct3d* m_device;
gradient& m_gradient;
std::shared_ptr<texture> m_texture;
};
The gradient_texture class isa Direct3D specific example that given agradient, it will cre-
ate the texture object necessary to render the gradient and also provides a way to save the
texture to file. In a multi-platform project it would be advisable to create a base class or
interface and derive classes that do the platform -specific operations.
5.13.1 Linear Gradient
A linear gradient creates a smooth pattern from a start color to some end color, gradually
blending both colors using linear interpolation. To create a linear gradient we begin by
providing a start color and end color, we will use the size of the image to determine the
gradient, or ramp between the two colors. Additionally, we can provide an angle, allowing
us to create the linear gradient in any orientation we specify.
Figure 78 - Linear gradient.
To generate the gradient we need to iterate over the x and y axes of the image we are gen-
erating, for simplicity we will assume the image is square, x and y both are in the range
[0..size]. For each point in x,y we will calculate a rotated point.
Search WWH ::




Custom Search