2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2011-12-17 09:00:41 +00:00
|
|
|
#ifndef SPROUT_DARKROOM_MATERIALS_TEXTURE_MAP_HPP
|
|
|
|
#define SPROUT_DARKROOM_MATERIALS_TEXTURE_MAP_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2012-07-04 15:09:44 +00:00
|
|
|
#include <sprout/math/floor.hpp>
|
2013-04-22 04:04:27 +00:00
|
|
|
#include <sprout/utility/value_holder.hpp>
|
2012-08-13 01:56:48 +00:00
|
|
|
#include <sprout/darkroom/materials/interpolation.hpp>
|
2011-12-17 09:00:41 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace darkroom {
|
|
|
|
namespace materials {
|
|
|
|
//
|
|
|
|
// texture_map_interpolation
|
|
|
|
//
|
2012-08-13 01:56:48 +00:00
|
|
|
typedef sprout::darkroom::materials::interpolation_type texture_map_interpolation;
|
2011-12-17 09:00:41 +00:00
|
|
|
//
|
|
|
|
// texture_map_placement
|
|
|
|
//
|
|
|
|
struct texture_map_placement {
|
|
|
|
public:
|
|
|
|
enum values {
|
|
|
|
tile,
|
|
|
|
once
|
|
|
|
};
|
|
|
|
};
|
|
|
|
//
|
|
|
|
// texture_map
|
|
|
|
//
|
|
|
|
template<typename Texture, typename Scale = double>
|
|
|
|
class texture_map {
|
|
|
|
public:
|
|
|
|
typedef Texture texture_type;
|
|
|
|
typedef typename texture_type::value_type result_type;
|
|
|
|
typedef Scale unit_type;
|
2012-08-13 01:56:48 +00:00
|
|
|
typedef sprout::darkroom::materials::interpolation_type interpolation_type;
|
2011-12-17 09:00:41 +00:00
|
|
|
typedef sprout::darkroom::materials::texture_map_placement texture_map_placement;
|
|
|
|
private:
|
|
|
|
sprout::value_holder<texture_type const&> texture_;
|
|
|
|
unit_type scale_;
|
|
|
|
unit_type offset_u_;
|
|
|
|
unit_type offset_v_;
|
|
|
|
result_type default_color_;
|
2012-08-13 01:56:48 +00:00
|
|
|
interpolation_type::values interpolation_value_;
|
2011-12-17 09:00:41 +00:00
|
|
|
texture_map_placement::values placement_value_;
|
|
|
|
private:
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR bool
|
|
|
|
is_nearest() const {
|
2012-08-13 01:56:48 +00:00
|
|
|
return interpolation_value_ == interpolation_type::nearest_neighbor;
|
2011-12-17 09:00:41 +00:00
|
|
|
}
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR bool
|
|
|
|
is_bilinear() const {
|
2012-08-13 01:56:48 +00:00
|
|
|
return interpolation_value_ == interpolation_type::bilinear;
|
2011-12-17 09:00:41 +00:00
|
|
|
}
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR bool
|
|
|
|
is_bicubic() const {
|
2012-08-13 01:56:48 +00:00
|
|
|
return interpolation_value_ == interpolation_type::bicubic;
|
2011-12-17 09:00:41 +00:00
|
|
|
}
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR bool
|
|
|
|
is_tile() const {
|
2011-12-17 09:00:41 +00:00
|
|
|
return placement_value_ == texture_map_placement::tile;
|
|
|
|
}
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR bool
|
|
|
|
is_once() const {
|
2011-12-17 09:00:41 +00:00
|
|
|
return placement_value_ == texture_map_placement::once;
|
|
|
|
}
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR result_type
|
|
|
|
get_color(unit_type const& x, unit_type const& y) const {
|
2011-12-17 09:00:41 +00:00
|
|
|
return is_tile()
|
|
|
|
? texture_.get()(
|
|
|
|
x < 0
|
|
|
|
? texture_.get().width() - 1 - static_cast<std::size_t>(-x) % texture_.get().width()
|
|
|
|
: static_cast<std::size_t>(x) % texture_.get().width()
|
|
|
|
,
|
|
|
|
y < 0
|
|
|
|
? texture_.get().height() - 1 - static_cast<std::size_t>(-y) % texture_.get().height()
|
|
|
|
: static_cast<std::size_t>(y) % texture_.get().height()
|
|
|
|
)
|
|
|
|
: x < 0 || x >= texture_.get().width()
|
|
|
|
|| y < 0 || y >= texture_.get().height()
|
|
|
|
? default_color_
|
|
|
|
: texture_.get()(static_cast<std::size_t>(x), static_cast<std::size_t>(y))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Unit>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR result_type
|
2013-09-24 06:08:36 +00:00
|
|
|
calculate_nearest(Unit const& x, Unit const& y) const {
|
2011-12-17 09:00:41 +00:00
|
|
|
return get_color(x, y);
|
|
|
|
}
|
|
|
|
template<typename Unit>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR result_type
|
2013-09-24 06:08:36 +00:00
|
|
|
calculate_bilinear_1(
|
2012-08-13 01:56:48 +00:00
|
|
|
Unit const& x, Unit const& x0,
|
|
|
|
Unit const& y, Unit const& y0
|
2011-12-17 09:00:41 +00:00
|
|
|
) const
|
|
|
|
{
|
2012-08-13 01:56:48 +00:00
|
|
|
return sprout::darkroom::materials::bilinear_interpolate(
|
|
|
|
get_color(x0, y0), get_color(x0 + 1, y0),
|
|
|
|
get_color(x0, y0 + 1), get_color(x0 + 1, y0 + 1),
|
2012-08-13 14:55:30 +00:00
|
|
|
x - x0, y - y0
|
2011-12-17 09:00:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename Unit>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR result_type
|
2013-09-24 06:08:36 +00:00
|
|
|
calculate_bilinear(Unit const& x, Unit const& y) const {
|
|
|
|
return calculate_bilinear_1(
|
2012-08-13 01:56:48 +00:00
|
|
|
x, sprout::floor(x),
|
|
|
|
y, sprout::floor(y)
|
2012-07-06 14:10:49 +00:00
|
|
|
);
|
2011-12-17 09:00:41 +00:00
|
|
|
}
|
|
|
|
template<typename Unit>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR result_type
|
|
|
|
calc(Unit const& x, Unit const& y) const {
|
2013-09-24 06:08:36 +00:00
|
|
|
return is_nearest() ? calculate_nearest(x, y)
|
|
|
|
: calculate_bilinear(x, y)
|
2011-12-17 09:00:41 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
public:
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR texture_map(
|
2012-10-05 15:58:56 +00:00
|
|
|
texture_type const& texture, unit_type const& scale = 1,
|
|
|
|
unit_type const& offset_u = 0, unit_type const& offset_v = 0,
|
2011-12-17 09:00:41 +00:00
|
|
|
result_type const& default_color = result_type(),
|
2012-08-13 01:56:48 +00:00
|
|
|
interpolation_type::values interpolation_value = interpolation_type::nearest_neighbor,
|
2011-12-17 09:00:41 +00:00
|
|
|
texture_map_placement::values placement_value = texture_map_placement::tile
|
|
|
|
)
|
|
|
|
: texture_(texture)
|
|
|
|
, scale_(scale)
|
|
|
|
, offset_u_(offset_u)
|
|
|
|
, offset_v_(offset_v)
|
|
|
|
, default_color_(default_color)
|
|
|
|
, interpolation_value_(interpolation_value)
|
|
|
|
, placement_value_(placement_value)
|
|
|
|
{}
|
|
|
|
template<typename Unit>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR result_type
|
|
|
|
operator()(Unit const& u, Unit const& v) const {
|
2011-12-17 09:00:41 +00:00
|
|
|
return calc(
|
|
|
|
(u - offset_u_ + scale_ / 2) / scale_ * texture_.get().width(),
|
|
|
|
(-(v - offset_v_) + scale_ / 2) / scale_ * texture_.get().height()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//
|
|
|
|
// make_texture_map
|
|
|
|
//
|
|
|
|
template<typename Texture>
|
2012-07-15 07:04:05 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::darkroom::materials::texture_map<Texture>
|
2011-12-17 09:00:41 +00:00
|
|
|
make_texture_map(Texture const& texture) {
|
|
|
|
return sprout::darkroom::materials::texture_map<Texture>(texture);
|
|
|
|
}
|
|
|
|
template<typename Texture, typename Unit>
|
2012-07-15 07:04:05 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::darkroom::materials::texture_map<Texture, Unit>
|
2011-12-17 09:00:41 +00:00
|
|
|
make_texture_map(
|
2012-10-05 15:58:56 +00:00
|
|
|
Texture const& texture, Unit const& scale,
|
|
|
|
Unit const& offset_u = 0, Unit const& offset_v = 0,
|
2011-12-17 09:00:41 +00:00
|
|
|
typename sprout::darkroom::materials::texture_map<Texture, Unit>::result_type const& default_color
|
2012-10-06 04:53:07 +00:00
|
|
|
= typename sprout::darkroom::materials::texture_map<Texture, Unit>::result_typ(),
|
2012-08-13 01:56:48 +00:00
|
|
|
sprout::darkroom::materials::interpolation_type::values interpolation_value
|
2012-10-06 04:53:07 +00:00
|
|
|
= sprout::darkroom::materials::interpolation_type::nearest_neighbor,
|
2011-12-17 09:00:41 +00:00
|
|
|
sprout::darkroom::materials::texture_map_placement::values placement_value
|
|
|
|
= sprout::darkroom::materials::texture_map_placement::tile
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::darkroom::materials::texture_map<Texture, Unit>(
|
|
|
|
texture,
|
|
|
|
scale,
|
|
|
|
offset_u,
|
|
|
|
offset_v,
|
|
|
|
default_color,
|
|
|
|
interpolation_value,
|
|
|
|
placement_value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace materials
|
|
|
|
} // namespace darkroom
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_DARKROOM_MATERIALS_TEXTURE_MAP_HPP
|