2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2011-2013 Bolero MURAKAMI
|
|
|
|
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_RENDERERS_WHITTED_STYLE_HPP
|
|
|
|
#define SPROUT_DARKROOM_RENDERERS_WHITTED_STYLE_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2011-11-26 06:20:35 +00:00
|
|
|
#include <sprout/tuple/functions.hpp>
|
|
|
|
#include <sprout/darkroom/access/access.hpp>
|
|
|
|
#include <sprout/darkroom/colors/rgb.hpp>
|
|
|
|
#include <sprout/darkroom/coords/vector.hpp>
|
|
|
|
#include <sprout/darkroom/rays/ray.hpp>
|
|
|
|
#include <sprout/darkroom/materials/material.hpp>
|
|
|
|
#include <sprout/darkroom/intersects/intersection.hpp>
|
|
|
|
#include <sprout/darkroom/objects/intersect.hpp>
|
2013-09-24 06:08:36 +00:00
|
|
|
#include <sprout/darkroom/lights/calculate.hpp>
|
|
|
|
#include <sprout/darkroom/renderers/calculate.hpp>
|
2012-08-13 14:55:30 +00:00
|
|
|
#include <sprout/darkroom/renderers/infinity.hpp>
|
2011-11-26 06:20:35 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace darkroom {
|
|
|
|
namespace renderers {
|
|
|
|
//
|
|
|
|
// whitted_mirror
|
|
|
|
//
|
|
|
|
class whitted_mirror {
|
|
|
|
private:
|
|
|
|
template<
|
|
|
|
typename Color,
|
2012-10-05 15:58:56 +00:00
|
|
|
typename Camera, typename Objects, typename Lights,
|
2013-09-24 06:08:36 +00:00
|
|
|
typename Ray, typename Intersection, typename Renderer,
|
2011-11-26 06:20:35 +00:00
|
|
|
typename Direction
|
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR Color
|
|
|
|
color_1(
|
|
|
|
Camera const& camera, Objects const& objs, Lights const& lights,
|
2013-09-24 06:08:36 +00:00
|
|
|
Ray const& ray, Intersection const& inter, Renderer const& renderer,
|
2011-11-26 06:20:35 +00:00
|
|
|
std::size_t depth_max,
|
2013-12-05 16:57:31 +00:00
|
|
|
Direction const& new_dir
|
2011-11-26 06:20:35 +00:00
|
|
|
) const
|
|
|
|
{
|
2013-09-24 06:08:36 +00:00
|
|
|
return sprout::darkroom::renderers::calculate<Color>(
|
|
|
|
renderer,
|
2012-10-05 15:58:56 +00:00
|
|
|
camera, objs, lights,
|
2012-03-31 07:24:13 +00:00
|
|
|
sprout::tuples::remake<Ray>(
|
2011-11-26 06:20:35 +00:00
|
|
|
ray,
|
2013-12-05 16:57:31 +00:00
|
|
|
sprout::darkroom::rays::detach_position(sprout::darkroom::intersects::point_of_intersection(inter), new_dir),
|
|
|
|
new_dir
|
2011-11-26 06:20:35 +00:00
|
|
|
),
|
|
|
|
depth_max - 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
template<
|
|
|
|
typename Color,
|
2012-10-05 15:58:56 +00:00
|
|
|
typename Camera, typename Objects, typename Lights,
|
2013-09-24 06:08:36 +00:00
|
|
|
typename Ray, typename Intersection, typename Renderer
|
2011-11-26 06:20:35 +00:00
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR Color
|
|
|
|
operator()(
|
|
|
|
Camera const& camera, Objects const& objs, Lights const& lights,
|
2013-09-24 06:08:36 +00:00
|
|
|
Ray const& ray, Intersection const& inter, Renderer const& renderer,
|
2011-11-26 06:20:35 +00:00
|
|
|
std::size_t depth_max
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
typedef typename std::decay<
|
|
|
|
decltype(sprout::darkroom::materials::reflection(sprout::darkroom::intersects::material(inter)))
|
|
|
|
>::type reflection_type;
|
|
|
|
return depth_max > 0
|
|
|
|
&& sprout::darkroom::intersects::does_intersect(inter)
|
|
|
|
&& sprout::darkroom::materials::reflection(sprout::darkroom::intersects::material(inter))
|
2013-08-06 15:15:09 +00:00
|
|
|
> sprout::numeric_limits<reflection_type>::epsilon()
|
2011-11-26 06:20:35 +00:00
|
|
|
? color_1<Color>(
|
2012-10-05 15:58:56 +00:00
|
|
|
camera, objs, lights,
|
2013-09-24 06:08:36 +00:00
|
|
|
ray, inter, renderer,
|
2011-11-26 06:20:35 +00:00
|
|
|
depth_max,
|
|
|
|
sprout::darkroom::coords::reflect(
|
|
|
|
sprout::darkroom::rays::direction(ray),
|
|
|
|
sprout::darkroom::intersects::normal(inter)
|
|
|
|
)
|
|
|
|
)
|
2012-03-31 07:24:13 +00:00
|
|
|
: sprout::tuples::make<Color>(0, 0, 0)
|
2011-11-26 06:20:35 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//
|
2013-12-05 16:57:31 +00:00
|
|
|
// whitted_transparent
|
|
|
|
//
|
|
|
|
class whitted_transparent {
|
|
|
|
private:
|
|
|
|
template<
|
|
|
|
typename Color,
|
|
|
|
typename Camera, typename Objects, typename Lights,
|
|
|
|
typename Ray, typename Intersection, typename Renderer,
|
|
|
|
typename Direction
|
|
|
|
>
|
|
|
|
SPROUT_CONSTEXPR Color
|
|
|
|
color_1(
|
|
|
|
Camera const& camera, Objects const& objs, Lights const& lights,
|
|
|
|
Ray const& ray, Intersection const& inter, Renderer const& renderer,
|
|
|
|
std::size_t depth_max,
|
|
|
|
Direction const& new_dir
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
return !sprout::darkroom::coords::is_zero(new_dir)
|
|
|
|
? sprout::darkroom::renderers::calculate<Color>(
|
|
|
|
renderer,
|
|
|
|
camera, objs, lights,
|
|
|
|
sprout::tuples::remake<Ray>(
|
|
|
|
ray,
|
|
|
|
sprout::darkroom::rays::detach_position(sprout::darkroom::intersects::point_of_intersection(inter), new_dir),
|
|
|
|
new_dir
|
|
|
|
),
|
|
|
|
depth_max - 1
|
|
|
|
)
|
|
|
|
: sprout::tuples::make<Color>(0, 0, 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
template<
|
|
|
|
typename Color,
|
|
|
|
typename Camera, typename Objects, typename Lights,
|
|
|
|
typename Ray, typename Intersection, typename Renderer
|
|
|
|
>
|
|
|
|
SPROUT_CONSTEXPR Color
|
|
|
|
operator()(
|
|
|
|
Camera const& camera, Objects const& objs, Lights const& lights,
|
|
|
|
Ray const& ray, Intersection const& inter, Renderer const& renderer,
|
|
|
|
std::size_t depth_max
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
typedef typename std::decay<
|
|
|
|
decltype(sprout::darkroom::materials::alpha(sprout::darkroom::intersects::material(inter)))
|
|
|
|
>::type alpha_type;
|
|
|
|
typedef typename std::decay<
|
|
|
|
decltype(sprout::darkroom::materials::refraction(sprout::darkroom::intersects::material(inter)))
|
|
|
|
>::type refraction_type;
|
|
|
|
return depth_max > 0
|
|
|
|
&& sprout::darkroom::intersects::does_intersect(inter)
|
|
|
|
&& sprout::darkroom::materials::alpha(sprout::darkroom::intersects::material(inter))
|
|
|
|
> sprout::numeric_limits<alpha_type>::epsilon()
|
|
|
|
&& sprout::darkroom::materials::refraction(sprout::darkroom::intersects::material(inter))
|
|
|
|
> sprout::numeric_limits<refraction_type>::epsilon()
|
|
|
|
? color_1<Color>(
|
|
|
|
camera, objs, lights,
|
|
|
|
ray, inter, renderer,
|
|
|
|
depth_max,
|
|
|
|
sprout::darkroom::coords::refract(
|
|
|
|
sprout::darkroom::rays::direction(ray),
|
|
|
|
sprout::darkroom::intersects::normal(inter),
|
|
|
|
sprout::darkroom::intersects::is_from_inside(inter)
|
|
|
|
? 1 / sprout::darkroom::materials::refraction(sprout::darkroom::intersects::material(inter))
|
|
|
|
: sprout::darkroom::materials::refraction(sprout::darkroom::intersects::material(inter))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
: sprout::tuples::make<Color>(0, 0, 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//
|
2011-11-26 06:20:35 +00:00
|
|
|
// whitted_style
|
|
|
|
//
|
2012-08-13 14:55:30 +00:00
|
|
|
template<typename InfinityColor = sprout::darkroom::renderers::direction_gradation>
|
2011-11-26 06:20:35 +00:00
|
|
|
class whitted_style {
|
2012-08-13 14:55:30 +00:00
|
|
|
public:
|
|
|
|
typedef InfinityColor infinity_color_type;
|
|
|
|
private:
|
|
|
|
infinity_color_type infinity_color_;
|
2011-11-26 06:20:35 +00:00
|
|
|
private:
|
2012-10-05 15:58:56 +00:00
|
|
|
template<typename Color, typename Ray, typename Intersection>
|
|
|
|
SPROUT_CONSTEXPR Color
|
|
|
|
color_3(
|
|
|
|
Ray const& ray, Intersection const& inter,
|
2013-12-05 16:57:31 +00:00
|
|
|
Color const& diffuse_color, Color const& mirror_color, Color const& transparent_color
|
2011-11-26 06:20:35 +00:00
|
|
|
) const
|
|
|
|
{
|
|
|
|
return sprout::darkroom::intersects::does_intersect(inter)
|
|
|
|
? sprout::darkroom::colors::add(
|
|
|
|
sprout::darkroom::colors::mul(
|
|
|
|
diffuse_color,
|
2013-12-05 16:57:31 +00:00
|
|
|
1
|
|
|
|
- sprout::darkroom::materials::reflection(sprout::darkroom::intersects::material(inter))
|
|
|
|
- sprout::darkroom::materials::alpha(sprout::darkroom::intersects::material(inter))
|
2011-11-26 06:20:35 +00:00
|
|
|
),
|
|
|
|
sprout::darkroom::colors::mul(
|
2012-08-14 09:17:48 +00:00
|
|
|
mirror_color,
|
2011-11-26 06:20:35 +00:00
|
|
|
sprout::darkroom::materials::reflection(sprout::darkroom::intersects::material(inter))
|
2013-12-05 16:57:31 +00:00
|
|
|
),
|
|
|
|
sprout::darkroom::colors::mul(
|
|
|
|
transparent_color,
|
|
|
|
sprout::darkroom::materials::alpha(sprout::darkroom::intersects::material(inter))
|
2011-11-26 06:20:35 +00:00
|
|
|
)
|
|
|
|
)
|
2013-09-24 06:08:36 +00:00
|
|
|
: sprout::darkroom::renderers::calculate_infinity<Color>(infinity_color_, sprout::darkroom::rays::direction(ray))
|
2011-11-26 06:20:35 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<
|
|
|
|
typename Color,
|
2012-10-05 15:58:56 +00:00
|
|
|
typename Camera, typename Objects, typename Lights,
|
|
|
|
typename Ray, typename Intersection
|
2011-11-26 06:20:35 +00:00
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR Color
|
|
|
|
color_2(
|
|
|
|
Camera const& camera, Objects const& objs, Lights const& lights,
|
|
|
|
Ray const& ray, std::size_t depth_max, Intersection const& inter,
|
2011-11-26 06:20:35 +00:00
|
|
|
Color const& diffuse_color
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
return color_3<Color>(
|
2012-10-05 15:58:56 +00:00
|
|
|
ray, inter,
|
2012-08-14 09:17:48 +00:00
|
|
|
diffuse_color,
|
|
|
|
sprout::darkroom::renderers::whitted_mirror().template operator()<Color>(
|
2013-12-05 16:57:31 +00:00
|
|
|
camera, objs, lights,
|
|
|
|
ray, inter, *this,
|
|
|
|
depth_max
|
|
|
|
),
|
|
|
|
sprout::darkroom::renderers::whitted_transparent().template operator()<Color>(
|
2012-10-05 15:58:56 +00:00
|
|
|
camera, objs, lights,
|
|
|
|
ray, inter, *this,
|
2011-11-26 06:20:35 +00:00
|
|
|
depth_max
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<
|
|
|
|
typename Color,
|
2012-10-05 15:58:56 +00:00
|
|
|
typename Camera, typename Objects, typename Lights,
|
|
|
|
typename Ray, typename Intersection
|
2011-11-26 06:20:35 +00:00
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR Color
|
|
|
|
color_1(
|
|
|
|
Camera const& camera, Objects const& objs, Lights const& lights,
|
|
|
|
Ray const& ray, std::size_t depth_max, Intersection const& inter
|
2011-11-26 06:20:35 +00:00
|
|
|
) const
|
|
|
|
{
|
|
|
|
return color_2<Color>(
|
2012-10-05 15:58:56 +00:00
|
|
|
camera, objs, lights,
|
|
|
|
ray, depth_max, inter,
|
2013-09-24 06:08:36 +00:00
|
|
|
sprout::darkroom::lights::calculate(lights, inter, objs)
|
2011-11-26 06:20:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
public:
|
2012-08-13 14:55:30 +00:00
|
|
|
SPROUT_CONSTEXPR whitted_style()
|
|
|
|
: infinity_color_()
|
|
|
|
{}
|
2013-10-29 10:15:52 +00:00
|
|
|
whitted_style(whitted_style const&) = default;
|
2012-08-13 14:55:30 +00:00
|
|
|
explicit SPROUT_CONSTEXPR whitted_style(infinity_color_type const& infinity_color)
|
|
|
|
: infinity_color_(infinity_color)
|
|
|
|
{}
|
2012-10-05 15:58:56 +00:00
|
|
|
template<typename Color, typename Camera, typename Objects, typename Lights, typename Ray>
|
|
|
|
SPROUT_CONSTEXPR Color
|
|
|
|
operator()(
|
|
|
|
Camera const& camera, Objects const& objs, Lights const& lights,
|
|
|
|
Ray const& ray, std::size_t depth_max
|
2011-11-26 06:20:35 +00:00
|
|
|
) const
|
|
|
|
{
|
|
|
|
return color_1<Color>(
|
2012-10-05 15:58:56 +00:00
|
|
|
camera, objs, lights,
|
|
|
|
ray, depth_max,
|
2013-09-24 01:05:47 +00:00
|
|
|
sprout::darkroom::objects::intersect(objs, ray)
|
2011-11-26 06:20:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2012-08-13 14:55:30 +00:00
|
|
|
//
|
|
|
|
// make_whitted_style
|
|
|
|
//
|
|
|
|
inline SPROUT_CONSTEXPR sprout::darkroom::renderers::whitted_style<>
|
|
|
|
make_whitted_style() {
|
|
|
|
return sprout::darkroom::renderers::whitted_style<>();
|
|
|
|
}
|
|
|
|
template<typename InfinityColor>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::darkroom::renderers::whitted_style<InfinityColor>
|
|
|
|
make_whitted_style(InfinityColor const& infinity_color) {
|
|
|
|
return sprout::darkroom::renderers::whitted_style<InfinityColor>(infinity_color);
|
|
|
|
}
|
2011-11-26 06:20:35 +00:00
|
|
|
} // namespace renderers
|
|
|
|
} // namespace darkroom
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_DARKROOM_RENDERERS_WHITTED_STYLE_HPP
|