15 #include <vsg/maths/clamp.h>
16 #include <vsg/maths/color.h>
17 #include <vsg/state/Sampler.h>
23 typename A::value_type sample(
const Sampler& sampler,
const A& image, vec2 coord)
25 using value_type =
typename A::value_type;
27 if (image.size() == 0 || !clamp(sampler.addressModeU, coord.x) || !clamp(sampler.addressModeV, coord.y))
29 return color_cast<value_type>(sampler.borderColor);
32 vec2 tc_scale(
static_cast<float>(image.width()) - 1.0f,
static_cast<float>(image.height()) - 1.0f);
33 vec2 tc_index = coord * tc_scale;
35 if (sampler.magFilter == VK_FILTER_NEAREST)
37 uint32_t i =
static_cast<uint32_t
>(tc_index.x + 0.5f);
38 uint32_t j =
static_cast<uint32_t
>(tc_index.y + 0.5f);
39 if (i >= image.width()) i = image.width() - 1;
40 if (j >= image.height()) j = image.height() - 1;
42 return image.at(i, j);
46 uint32_t i =
static_cast<uint32_t
>(tc_index.x);
47 uint32_t j =
static_cast<uint32_t
>(tc_index.y);
48 float r = tc_index.x -
static_cast<float>(i);
49 float s = tc_index.y -
static_cast<float>(j);
51 if (i >= image.width() - 1)
53 i = image.width() - 1;
57 if (j >= image.height() - 1)
59 j = image.height() - 1;
63 auto v = image.at(i, j) * ((1.0f - r) * (1.0f - s));
64 if (r > 0.0) v += image.at(i + 1, j) * (r * (1.0f - s));
67 v += image.at(i, j + 1) * ((1.0f - r) * s);
68 if (r > 0.0) v += image.at(i + 1, j + 1) * (r * s);