Sprout/sprout/darkroom/materials/material.hpp

186 lines
7.2 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 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-11-26 06:20:35 +00:00
#ifndef SPROUT_DARKROOM_MATERIALS_MATERIAL_HPP
#define SPROUT_DARKROOM_MATERIALS_MATERIAL_HPP
#include <type_traits>
2011-11-26 06:20:35 +00:00
#include <sprout/config.hpp>
#include <sprout/tuple/tuple.hpp>
#include <sprout/tuple/indexes.hpp>
2011-11-26 06:20:35 +00:00
#include <sprout/utility/forward.hpp>
#include <sprout/type_traits/enabler_if.hpp>
2011-11-26 06:20:35 +00:00
#include <sprout/darkroom/access/access.hpp>
#include <sprout/darkroom/colors/rgb.hpp>
#include <sprout/darkroom/materials/calculate.hpp>
2011-11-26 06:20:35 +00:00
namespace sprout {
namespace darkroom {
namespace materials {
//
// material
//
typedef sprout::tuples::tuple<sprout::darkroom::colors::rgb_f_t, double, double, double> material;
2011-11-26 06:20:35 +00:00
//
// has_color
// has_reflection
// has_alpha
// has_refraction
//
template<typename T>
struct has_color
2014-12-10 10:54:12 +00:00
: public sprout::bool_constant<(sprout::darkroom::access::size<T>::value >= 1)>
{};
template<typename T>
struct has_reflection
2014-12-10 10:54:12 +00:00
: public sprout::bool_constant<(sprout::darkroom::access::size<T>::value >= 2)>
{};
2011-11-26 06:20:35 +00:00
template<typename T>
struct has_alpha
2014-12-10 10:54:12 +00:00
: public sprout::bool_constant<(sprout::darkroom::access::size<T>::value >= 3)>
{};
template<typename T>
struct has_refraction
2014-12-10 10:54:12 +00:00
: public sprout::bool_constant<(sprout::darkroom::access::size<T>::value >= 4)>
{};
//
// color
//
template<
typename T,
typename sprout::enabler_if<sprout::darkroom::materials::has_color<typename std::decay<T>::type>::value>::type = sprout::enabler
>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR auto
color(T&& t)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(sprout::darkroom::access::get<0>(SPROUT_FORWARD(T, t)))
-> decltype(sprout::darkroom::access::get<0>(SPROUT_FORWARD(T, t)))
2011-11-26 06:20:35 +00:00
{
return sprout::darkroom::access::get<0>(SPROUT_FORWARD(T, t));
2011-11-26 06:20:35 +00:00
}
template<
typename T,
typename sprout::enabler_if<!sprout::darkroom::materials::has_color<typename std::decay<T>::type>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::darkroom::access::element<0, sprout::darkroom::materials::material>::type
color(T&&)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR((typename sprout::darkroom::access::element<0, sprout::darkroom::materials::material>::type()))
{
return typename sprout::darkroom::access::element<0, sprout::darkroom::materials::material>::type();
}
//
// reflection
//
template<
typename T,
typename sprout::enabler_if<sprout::darkroom::materials::has_reflection<typename std::decay<T>::type>::value>::type = sprout::enabler
>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR auto
reflection(T&& t)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(sprout::darkroom::access::get<1>(SPROUT_FORWARD(T, t)))
-> decltype(sprout::darkroom::access::get<1>(SPROUT_FORWARD(T, t)))
2011-11-26 06:20:35 +00:00
{
return sprout::darkroom::access::get<1>(SPROUT_FORWARD(T, t));
2011-11-26 06:20:35 +00:00
}
template<
typename T,
typename sprout::enabler_if<!sprout::darkroom::materials::has_reflection<typename std::decay<T>::type>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::darkroom::access::element<1, sprout::darkroom::materials::material>::type
reflection(T&&)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR((typename sprout::darkroom::access::element<1, sprout::darkroom::materials::material>::type()))
{
return typename sprout::darkroom::access::element<1, sprout::darkroom::materials::material>::type();
}
//
// alpha
//
template<
typename T,
typename sprout::enabler_if<sprout::darkroom::materials::has_alpha<typename std::decay<T>::type>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR auto
alpha(T&& t)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(sprout::darkroom::access::get<2>(SPROUT_FORWARD(T, t)))
-> decltype(sprout::darkroom::access::get<2>(SPROUT_FORWARD(T, t)))
{
return sprout::darkroom::access::get<2>(SPROUT_FORWARD(T, t));
}
template<
typename T,
typename sprout::enabler_if<!sprout::darkroom::materials::has_alpha<typename std::decay<T>::type>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::darkroom::access::element<2, sprout::darkroom::materials::material>::type
alpha(T&&)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR((typename sprout::darkroom::access::element<2, sprout::darkroom::materials::material>::type()))
{
return typename sprout::darkroom::access::element<2, sprout::darkroom::materials::material>::type();
}
//
// refraction
//
template<
typename T,
typename sprout::enabler_if<sprout::darkroom::materials::has_refraction<typename std::decay<T>::type>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR auto
refraction(T&& t)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(sprout::darkroom::access::get<3>(SPROUT_FORWARD(T, t)))
-> decltype(sprout::darkroom::access::get<3>(SPROUT_FORWARD(T, t)))
{
return sprout::darkroom::access::get<3>(SPROUT_FORWARD(T, t));
}
template<
typename T,
typename sprout::enabler_if<!sprout::darkroom::materials::has_refraction<typename std::decay<T>::type>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::darkroom::access::element<3, sprout::darkroom::materials::material>::type
refraction(T&&)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR((typename sprout::darkroom::access::element<3, sprout::darkroom::materials::material>::type()))
{
return typename sprout::darkroom::access::element<3, sprout::darkroom::materials::material>::type();
}
2011-11-26 06:20:35 +00:00
//
// calculate_material
2011-11-26 06:20:35 +00:00
//
namespace detail {
template<typename Material, typename Unit, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR auto
calculate_material_impl(Material const& mat, Unit const& u, Unit const& v, sprout::index_tuple<Indexes...>)
-> decltype(sprout::tuples::make_tuple(
sprout::darkroom::materials::calculate(sprout::darkroom::access::get<Indexes>(mat), u, v)...
))
{
return sprout::tuples::make_tuple(
sprout::darkroom::materials::calculate(sprout::darkroom::access::get<Indexes>(mat), u, v)...
);
}
} // namespace detail
template<typename Material, typename Unit>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR auto
calculate_material(Material const& mat, Unit const& u, Unit const& v)
-> decltype(sprout::darkroom::materials::detail::calculate_material_impl(mat, u, v, sprout::tuples::tuple_indexes<Material>::make()))
2011-11-26 06:20:35 +00:00
{
return sprout::darkroom::materials::detail::calculate_material_impl(mat, u, v, sprout::tuples::tuple_indexes<Material>::make());
2011-11-26 06:20:35 +00:00
}
//
// make_material_image
//
template<typename... Images>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<Images...>
make_material_image(Images const&... images) {
return sprout::tuples::make_tuple(images...);
2011-11-26 06:20:35 +00:00
}
} // namespace materials
} // namespace darkroom
} // namespace sprout
#endif // #ifndef SPROUT_DARKROOM_MATERIALS_MATERIAL_HPP