From 6f2be58007c38e9476097b9e1bc51b95e04bd900 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 17 Dec 2011 18:00:41 +0900 Subject: [PATCH] =?UTF-8?q?Sprout.Darkroom=20=E5=A4=96=E9=83=A8=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=8B=E3=82=89=E3=83=86=E3=82=AF?= =?UTF-8?q?=E3=82=B9=E3=83=81=E3=83=A3=E8=AA=AD=E3=81=BF=E8=BE=BC=E3=81=BF?= =?UTF-8?q?=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD=E5=8A=A0=20texture=5Fmap?= =?UTF-8?q?=20=E3=82=AF=E3=83=A9=E3=82=B9=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sprout/darkroom.hpp | 1 + sprout/darkroom/load/texture.hpp | 50 ++++++ sprout/darkroom/materials.hpp | 1 + sprout/darkroom/materials/texture_map.hpp | 202 ++++++++++++++++++++++ sprout/darkroom/objects/sphere.hpp | 22 +-- sprout/darkroom/textures.hpp | 7 + sprout/darkroom/textures/texture.hpp | 96 ++++++++++ 7 files changed, 368 insertions(+), 11 deletions(-) create mode 100644 sprout/darkroom/load/texture.hpp create mode 100644 sprout/darkroom/materials/texture_map.hpp create mode 100644 sprout/darkroom/textures.hpp create mode 100644 sprout/darkroom/textures/texture.hpp diff --git a/sprout/darkroom.hpp b/sprout/darkroom.hpp index f18cdae5..f514da10 100644 --- a/sprout/darkroom.hpp +++ b/sprout/darkroom.hpp @@ -14,5 +14,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_DARKROOM_HPP diff --git a/sprout/darkroom/load/texture.hpp b/sprout/darkroom/load/texture.hpp new file mode 100644 index 00000000..83adf641 --- /dev/null +++ b/sprout/darkroom/load/texture.hpp @@ -0,0 +1,50 @@ + +#ifndef DARKROOM_DEF_LOAD_TEXTURE_IDENTIFIER +# error should define DARKROOM_DEF_LOAD_TEXTURE_IDENTIFIER. +#endif + +#ifndef DARKROOM_DEF_LOAD_TEXTURE_FILE +# error should define DARKROOM_DEF_LOAD_TEXTURE_FILE. +#endif + +#define DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT(id, suffix) DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT_I(id, suffix) +#define DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT_I(id, suffix) DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT_II(id ## suffix) +#define DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT_II(res) res + +#define DARKROOM_LOAD_IDENTIFIER DARKROOM_DEF_LOAD_TEXTURE_IDENTIFIER +#define DARKROOM_LOAD_DETAIL_IDENTIFIER(id) DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT( \ + DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT(DARKROOM_DEF_LOAD_TEXTURE_IDENTIFIER, id), \ + _darkroom_load_texture_detail \ + ) + +SPROUT_STATIC_CONSTEXPR sprout::darkroom::textures::version_type DARKROOM_LOAD_DETAIL_IDENTIFIER(version) = +# define DARKROOM_LOADING_TEXTURE_VERSION +# include DARKROOM_DEF_LOAD_TEXTURE_FILE +# undef DARKROOM_LOADING_TEXTURE_VERSION + ; +SPROUT_STATIC_CONSTEXPR sprout::darkroom::textures::info_type DARKROOM_LOAD_DETAIL_IDENTIFIER(info) = { +# define DARKROOM_LOADING_TEXTURE_INFO +# include DARKROOM_DEF_LOAD_TEXTURE_FILE +# undef DARKROOM_LOADING_TEXTURE_INFO + }; + +SPROUT_STATIC_CONSTEXPR sprout::darkroom::textures::image_type< + DARKROOM_LOAD_DETAIL_IDENTIFIER(info).width, + DARKROOM_LOAD_DETAIL_IDENTIFIER(info).height +> DARKROOM_LOAD_IDENTIFIER( + DARKROOM_LOAD_DETAIL_IDENTIFIER(info).image_format, + DARKROOM_LOAD_DETAIL_IDENTIFIER(info).pixel_format, +# define DARKROOM_LOADING_TEXTURE_PIXEL +# include DARKROOM_DEF_LOAD_TEXTURE_FILE +# undef DARKROOM_LOADING_TEXTURE_PIXEL + ); + +#undef DARKROOM_LOAD_IDENTIFIER +#undef DARKROOM_LOAD_DETAIL_IDENTIFIER + +#undef DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT +#undef DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT_I +#undef DARKROOM_LOAD_IDENTIFIER_DETAIL_CAT_II + +#undef DARKROOM_DEF_LOAD_TEXTURE_IDENTIFIER +#undef DARKROOM_DEF_LOAD_TEXTURE_FILE diff --git a/sprout/darkroom/materials.hpp b/sprout/darkroom/materials.hpp index d91e8e97..fac1f936 100644 --- a/sprout/darkroom/materials.hpp +++ b/sprout/darkroom/materials.hpp @@ -5,5 +5,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_DARKROOM_MATERIALS_HPP diff --git a/sprout/darkroom/materials/texture_map.hpp b/sprout/darkroom/materials/texture_map.hpp new file mode 100644 index 00000000..108f7392 --- /dev/null +++ b/sprout/darkroom/materials/texture_map.hpp @@ -0,0 +1,202 @@ +#ifndef SPROUT_DARKROOM_MATERIALS_TEXTURE_MAP_HPP +#define SPROUT_DARKROOM_MATERIALS_TEXTURE_MAP_HPP + +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace darkroom { + namespace materials { + // + // texture_map_interpolation + // + struct texture_map_interpolation { + public: + enum values { + nearest_neighbor, + bilinear, + bicubic + }; + }; + // + // texture_map_placement + // + struct texture_map_placement { + public: + enum values { + tile, + once + }; + }; + // + // texture_map + // + template + class texture_map { + public: + typedef Texture texture_type; + typedef typename texture_type::value_type result_type; + typedef Scale unit_type; + typedef sprout::darkroom::materials::texture_map_interpolation texture_map_interpolation; + typedef sprout::darkroom::materials::texture_map_placement texture_map_placement; + private: + static SPROUT_CONSTEXPR unit_type fmod(unit_type const& n, unit_type const& d) { + return n - std::intmax_t(n / d) * d; + } + private: + sprout::value_holder texture_; + unit_type scale_; + unit_type offset_u_; + unit_type offset_v_; + result_type default_color_; + texture_map_interpolation::values interpolation_value_; + texture_map_placement::values placement_value_; + private: + SPROUT_CONSTEXPR bool is_nearest() const { + return interpolation_value_ == texture_map_interpolation::nearest_neighbor; + } + SPROUT_CONSTEXPR bool is_bilinear() const { + return interpolation_value_ == texture_map_interpolation::bilinear; + } + SPROUT_CONSTEXPR bool is_bicubic() const { + return interpolation_value_ == texture_map_interpolation::bicubic; + } + SPROUT_CONSTEXPR bool is_tile() const { + return placement_value_ == texture_map_placement::tile; + } + SPROUT_CONSTEXPR bool is_once() const { + return placement_value_ == texture_map_placement::once; + } + SPROUT_CONSTEXPR result_type get_color(unit_type const& x, unit_type const& y) const { + return is_tile() + ? texture_.get()( + x < 0 + ? texture_.get().width() - 1 - static_cast(-x) % texture_.get().width() + : static_cast(x) % texture_.get().width() + , + y < 0 + ? texture_.get().height() - 1 - static_cast(-y) % texture_.get().height() + : static_cast(y) % texture_.get().height() + ) + : x < 0 || x >= texture_.get().width() + || y < 0 || y >= texture_.get().height() + ? default_color_ + : texture_.get()(static_cast(x), static_cast(y)) + ; + } + template + SPROUT_CONSTEXPR result_type calc_nearest(Unit const& x, Unit const& y) const { + return get_color(x, y); + } + template + SPROUT_CONSTEXPR result_type calc_bilinear_1( + Unit const& x, + Unit const& xf, + Unit const& xc, + Unit const& y, + Unit const& yf, + Unit const& yc + ) const + { + return sprout::darkroom::colors::add( + sprout::darkroom::colors::mul( + sprout::darkroom::colors::add( + sprout::darkroom::colors::mul(get_color(xf, yf), 1 - (x - xf)), + sprout::darkroom::colors::mul(get_color(xc, yf), 1 - (xc - x)) + ), + 1 - (y - yf) + ), + sprout::darkroom::colors::mul( + sprout::darkroom::colors::add( + sprout::darkroom::colors::mul(get_color(xf, yc), 1 - (x - xf)), + sprout::darkroom::colors::mul(get_color(xc, yc), 1 - (xc - x)) + ), + 1 - (yc - y) + ) + ); + } + template + SPROUT_CONSTEXPR result_type calc_bilinear(Unit const& x, Unit const& y) const { + using std::floor; + return calc_bilinear_1(x, floor(x - 0.5), floor(x + 0.5), y, floor(y - 0.5), floor(y + 0.5)); + } + template + SPROUT_CONSTEXPR result_type calc(Unit const& x, Unit const& y) const { + return is_nearest() ? calc_nearest(x, y) + : calc_bilinear(x, y) + ; + } + public: + SPROUT_CONSTEXPR explicit texture_map( + texture_type const& texture, + unit_type const& scale = 1, + unit_type const& offset_u = 0, + unit_type const& offset_v = 0, + result_type const& default_color = result_type(), + texture_map_interpolation::values interpolation_value = texture_map_interpolation::nearest_neighbor, + 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 + SPROUT_CONSTEXPR result_type operator()(Unit const& u, Unit const& v) const { + return calc( + (u - offset_u_ + scale_ / 2) / scale_ * texture_.get().width(), + (-(v - offset_v_) + scale_ / 2) / scale_ * texture_.get().height() + ); + } + }; + // + // make_texture_map + // + template + SPROUT_CONSTEXPR sprout::darkroom::materials::texture_map + make_texture_map(Texture const& texture) { + return sprout::darkroom::materials::texture_map(texture); + } + template + SPROUT_CONSTEXPR sprout::darkroom::materials::texture_map + make_texture_map( + Texture const& texture, + Unit const& scale, + Unit const& offset_u + = 0 + , + Unit const& offset_v + = 0 + , + typename sprout::darkroom::materials::texture_map::result_type const& default_color + = typename sprout::darkroom::materials::texture_map::result_typ() + , + sprout::darkroom::materials::texture_map_interpolation::values interpolation_value + = sprout::darkroom::materials::texture_map_interpolation::nearest_neighbor + , + sprout::darkroom::materials::texture_map_placement::values placement_value + = sprout::darkroom::materials::texture_map_placement::tile + ) + { + return sprout::darkroom::materials::texture_map( + 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 diff --git a/sprout/darkroom/objects/sphere.hpp b/sprout/darkroom/objects/sphere.hpp index db8d759f..d8fead33 100644 --- a/sprout/darkroom/objects/sphere.hpp +++ b/sprout/darkroom/objects/sphere.hpp @@ -135,12 +135,12 @@ namespace sprout { ) ; } - template + template SPROUT_CONSTEXPR typename intersection::type intersect_6( Ray const& ray, zwo_type const& zwo, drei_type const& drei, - Point const& poi + Vec const& normal ) const { using std::atan2; @@ -153,19 +153,19 @@ namespace sprout { sprout::darkroom::materials::calc_material( // ! Spherical mat_, atan2( - sprout::darkroom::coords::x(poi), - sprout::darkroom::coords::z(poi) + sprout::darkroom::coords::z(normal), + sprout::darkroom::coords::x(normal) ) - / (2 * pi) + / pi , atan2( + sprout::darkroom::coords::y(normal), sqrt( - sprout::darkroom::coords::x(poi) * sprout::darkroom::coords::x(poi) - + sprout::darkroom::coords::y(poi) * sprout::darkroom::coords::y(poi) - ), - sprout::darkroom::coords::z(poi) + sprout::darkroom::coords::x(normal) * sprout::darkroom::coords::x(normal) + + sprout::darkroom::coords::z(normal) * sprout::darkroom::coords::z(normal) + ) ) - / (2 * pi) + / (pi / 2) ) ); } @@ -180,7 +180,7 @@ namespace sprout { ray, zwo, drei, - sprout::tuples::get(drei) + sprout::tuples::get(drei) ); } template diff --git a/sprout/darkroom/textures.hpp b/sprout/darkroom/textures.hpp new file mode 100644 index 00000000..d6f45050 --- /dev/null +++ b/sprout/darkroom/textures.hpp @@ -0,0 +1,7 @@ +#ifndef SPROUT_DARKROOM_TEXTURES_HPP +#define SPROUT_DARKROOM_TEXTURES_HPP + +#include +#include + +#endif // #ifndef SPROUT_DARKROOM_TEXTURES_HPP diff --git a/sprout/darkroom/textures/texture.hpp b/sprout/darkroom/textures/texture.hpp new file mode 100644 index 00000000..85c153a8 --- /dev/null +++ b/sprout/darkroom/textures/texture.hpp @@ -0,0 +1,96 @@ +#ifndef SPROUT_DARKROOM_TEXTURES_TEXTURE_HPP +#define SPROUT_DARKROOM_TEXTURES_TEXTURE_HPP + +#include +#include +#include +#include +#include + +// +// DARKROOM_LOAD_TEXTURE +// +#define DARKROOM_LOAD_TEXTURE + +// +// DARKROOM_TEX_VERSION +// +#define DARKROOM_TEX_VERSION(NUM) NUM + +// +// DARKROOM_TEX_IMAGE_DEFAULT +// +#define DARKROOM_TEX_IMAGE_DEFAULT 0 + +// +// DARKROOM_TEX_PIXEL_INT_R8G8B8 +// +#define DARKROOM_TEX_PIXEL_INT_R8G8B8 0 + +namespace sprout { + namespace darkroom { + namespace textures { + // + // version_type + // + typedef unsigned long version_type; + // + // info_type + // + struct info_type { + public: + unsigned long image_format; + unsigned long pixel_format; + std::size_t width; + std::size_t height; + }; + // + // image_type + // + template + struct image_type { + public: + typedef Color color_type; + typedef color_type value_type; + typedef std::size_t size_type; + SPROUT_STATIC_CONSTEXPR size_type static_width = Width; + SPROUT_STATIC_CONSTEXPR size_type static_height = Height; + typedef sprout::array pixels_type; + typedef typename sprout::darkroom::access::unit::type color_component_type; + private: + pixels_type pixels_; + public: + template + SPROUT_CONSTEXPR image_type( + unsigned long image_format, + unsigned long pixel_format, + Elems const&... elems + ) + : pixels_{{ + color_type( + static_cast((elems >> 16) & 0xFF) / 0xFF, + static_cast((elems >> 8) & 0xFF) / 0xFF, + static_cast(elems & 0xFF) / 0xFF + )... + }} + { + static_assert(sizeof...(Elems) == static_width * static_height, "image_type<>: unmatch image size"); + } + SPROUT_CONSTEXPR value_type const& operator()(size_type x, size_type y) const { + return pixels_[y * static_width + x]; + } + SPROUT_CONSTEXPR size_type width() const { + return static_width; + } + SPROUT_CONSTEXPR size_type height() const { + return static_height; + } + SPROUT_CONSTEXPR pixels_type const& pixels() const { + return pixels_; + } + }; + } // namespace textures + } // namespace darkroom +} // namespace sprout + +#endif // #ifndef SPROUT_DARKROOM_TEXTURES_TEXTURE_HPP