OGLplus  (0.59.0) a C++ wrapper for rendering APIs

vertex_attrib.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SHAPES_VERTEX_ATTRIB_HPP
10 #define EAGINE_SHAPES_VERTEX_ATTRIB_HPP
11 
12 #include "../bitfield.hpp"
13 #include "../reflect/map_enumerators.hpp"
14 #include "../types.hpp"
15 #include <array>
16 #include <cstdint>
17 #include <type_traits>
18 #include <utility>
19 
20 namespace eagine {
21 namespace shapes {
22 //------------------------------------------------------------------------------
25 enum class vertex_attrib_kind : std::uint16_t {
27  object_id = 1U << 0U,
29  position = 1U << 1U,
31  normal = 1U << 2U,
33  tangential = 1U << 3U,
35  bitangential = 1U << 4U,
37  pivot = 1U << 5U,
39  pivot_pivot = 1U << 6U,
41  vertex_pivot = 1U << 7U,
43  box_coord = 1U << 8U,
45  face_coord = 1U << 9U,
47  wrap_coord = 1U << 10U,
49  color = 1U << 11U,
51  weight = 1U << 12U,
53  occlusion = 1U << 13U,
55  polygon_id = 1U << 14U,
57  material_id = 1U << 15U
58  // also fix all_vertex_attrib_bits
59 };
60 //------------------------------------------------------------------------------
61 template <typename Selector>
62 constexpr auto
63 enumerator_mapping(type_identity<vertex_attrib_kind>, Selector) noexcept {
64  return enumerator_map_type<vertex_attrib_kind, 16>{
65  {{"object_id", vertex_attrib_kind::object_id},
66  {"position", vertex_attrib_kind::position},
67  {"normal", vertex_attrib_kind::normal},
68  {"tangential", vertex_attrib_kind::tangential},
69  {"bitangential", vertex_attrib_kind::bitangential},
70  {"pivot", vertex_attrib_kind::pivot},
71  {"pivot_pivot", vertex_attrib_kind::pivot_pivot},
72  {"vertex_pivot", vertex_attrib_kind::vertex_pivot},
73  {"box_coord", vertex_attrib_kind::box_coord},
74  {"face_coord", vertex_attrib_kind::face_coord},
75  {"wrap_coord", vertex_attrib_kind::wrap_coord},
76  {"color", vertex_attrib_kind::color},
77  {"weight", vertex_attrib_kind::weight},
78  {"occlusion", vertex_attrib_kind::occlusion},
79  {"polygon_id", vertex_attrib_kind::polygon_id},
80  {"material_id", vertex_attrib_kind::material_id}}};
81 }
82 //------------------------------------------------------------------------------
86 //------------------------------------------------------------------------------
89 static constexpr auto all_vertex_attrib_bits() noexcept -> vertex_attrib_bits {
90  return vertex_attrib_bits{(1U << 16U) - 1U};
91 }
92 //------------------------------------------------------------------------------
95 static constexpr auto
98  return {a, b};
99 }
100 //------------------------------------------------------------------------------
105 public:
108  : _attrib{a} {}
109 
112  : _attrib{a}
113  , _index{std::int16_t(v)} {}
114 
119  : _attrib{a}
120  , _index{vav._index} {}
121 
123  constexpr auto attribute() const noexcept -> vertex_attrib_kind {
124  return _attrib;
125  }
126 
128  constexpr auto has_valid_index() const noexcept {
129  return _index >= 0;
130  }
131 
134  constexpr explicit operator bool() const noexcept {
135  return has_valid_index();
136  }
137 
139  constexpr auto index() const noexcept -> span_size_t {
140  return _index;
141  }
142 
143  constexpr
144  operator std::array<const vertex_attrib_variant, 1>() const noexcept {
145  return {{*this}};
146  }
147 
148  constexpr auto as_tuple() const noexcept {
149  return std::make_tuple(_attrib, _index);
150  }
151 
153  friend constexpr auto
155  return l.as_tuple() == r.as_tuple();
156  }
157 
159  friend constexpr auto
161  return l.as_tuple() != r.as_tuple();
162  }
163 
165  friend constexpr auto
167  return l.as_tuple() < r.as_tuple();
168  }
169 
171  friend constexpr auto
173  return l._attrib == r;
174  }
175 
177  friend constexpr auto
179  return l._attrib != r;
180  }
181 
182 private:
183  vertex_attrib_kind _attrib;
184  std::int16_t _index{0};
185 };
186 //------------------------------------------------------------------------------
189 static constexpr auto
190 operator/(vertex_attrib_kind attrib, span_size_t variant_index) noexcept
192  return {attrib, variant_index};
193 }
194 //------------------------------------------------------------------------------
198 template <std::size_t N>
199 using vertex_attrib_variants = std::array<const vertex_attrib_variant, N>;
200 //------------------------------------------------------------------------------
205 static constexpr auto operator+(vertex_attrib_variant a) noexcept
207  return {{a}};
208 }
209 //------------------------------------------------------------------------------
214 static constexpr auto
217  return {{a, b}};
218 }
219 //------------------------------------------------------------------------------
220 // append_attrib
221 template <std::size_t N, std::size_t... I>
222 static constexpr auto do_append_attrib(
223  const vertex_attrib_variants<N>& a,
224  vertex_attrib_variant b,
225  std::index_sequence<I...>) noexcept {
226  return vertex_attrib_variants<N + 1>{{a[I]..., b}};
227 }
228 //------------------------------------------------------------------------------
233 template <std::size_t N>
234 static constexpr auto
237  return do_append_attrib(a, b, std::make_index_sequence<N>());
238 }
239 //------------------------------------------------------------------------------
243 template <std::size_t N>
244 static inline auto
246  -> vertex_attrib_bits {
247  vertex_attrib_bits res;
248 
249  for(const vertex_attrib_variant& vaal : vaals) {
250  res = res | vaal.attribute();
251  }
252 
253  return res;
254 }
255 //------------------------------------------------------------------------------
258 static inline auto attrib_values_per_vertex(vertex_attrib_kind attr) noexcept
259  -> span_size_t {
260  switch(attr) {
262  return 4;
272  return 3;
274  return 2;
280  return 1;
281  }
282  return 0;
283 }
284 //------------------------------------------------------------------------------
287 static inline auto attrib_values_per_vertex(vertex_attrib_variant vav) noexcept {
288  return attrib_values_per_vertex(vav.attribute());
289 }
290 //------------------------------------------------------------------------------
291 } // namespace shapes
292 } // namespace eagine
293 
294 #endif // EAGINE_SHAPES_VERTEX_ATTRIB_HPP
@ material_id
Face material id value.
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
constexpr vertex_attrib_variant(vertex_attrib_kind a, vertex_attrib_variant vav)
Construction from vertex attribute kind and another attribute variant.
Definition: vertex_attrib.hpp:116
@ polygon_id
Face polygon id value (multiple faces can belong to the same polygon)
Common code is placed in this namespace.
Definition: eagine.hpp:21
constexpr vertex_attrib_variant(vertex_attrib_kind a, span_size_t v)
Construction from vertex attribute kind enumerator and index.
Definition: vertex_attrib.hpp:111
constexpr friend auto operator==(vertex_attrib_variant l, vertex_attrib_kind r) noexcept
Variant and kind equality comparison.
Definition: vertex_attrib.hpp:172
constexpr friend auto operator<(vertex_attrib_variant l, vertex_attrib_variant r) noexcept
Less-than comparison.
Definition: vertex_attrib.hpp:166
@ tangential
Vertex tangential vector.
constexpr friend auto operator==(vertex_attrib_variant l, vertex_attrib_variant r) noexcept
Equality comparison.
Definition: vertex_attrib.hpp:154
@ bitangential
Vertex bi-tangential vector.
constexpr friend auto operator!=(vertex_attrib_variant l, vertex_attrib_variant r) noexcept
Nonequality comparison.
Definition: vertex_attrib.hpp:160
constexpr auto attribute() const noexcept -> vertex_attrib_kind
Return the attribute kind.
Definition: vertex_attrib.hpp:123
static auto attrib_values_per_vertex(vertex_attrib_kind attr) noexcept -> span_size_t
Gets the default number of values per vertex for an attribute kind.
Definition: vertex_attrib.hpp:258
static constexpr auto operator/(vertex_attrib_kind attrib, span_size_t variant_index) noexcept -> vertex_attrib_variant
Operator for constructing of vertex_attrib_variant from kind and index.
Definition: vertex_attrib.hpp:190
@ wrap_coord
UV-texture wrapping coordinate.
constexpr auto index() const noexcept -> span_size_t
Returns the index of the attribute variant.
Definition: vertex_attrib.hpp:139
Class designating an vertex attribute variant in a shape generator.
Definition: vertex_attrib.hpp:104
static constexpr auto operator+(vertex_attrib_variant a) noexcept -> vertex_attrib_variants< 1 >
Operator for creating of single element array of vertex attrib variant.
Definition: vertex_attrib.hpp:205
constexpr friend auto operator!=(vertex_attrib_variant l, vertex_attrib_kind r) noexcept
Variant and kind nonequality comparison.
Definition: vertex_attrib.hpp:178
constexpr auto has_valid_index() const noexcept
Check if the stored index is valid.
Definition: vertex_attrib.hpp:128
@ object_id
The object id attributes (typically unique integer).
@ box_coord
Normalized coordinate within shape bounding box.
constexpr vertex_attrib_variant(vertex_attrib_kind a)
Construction from vertex attribute kind enumerator.
Definition: vertex_attrib.hpp:107
vertex_attrib_kind
Shape vertex attribute kind enumeration.
Definition: vertex_attrib.hpp:25
@ pivot_pivot
Pivot of vertex pivot point.
static auto get_attrib_bits(const vertex_attrib_variants< N > &vaals) noexcept -> vertex_attrib_bits
Extracts vertex attribute kind bits from and vertex attribute variants array.
Definition: vertex_attrib.hpp:245
@ occlusion
Vertex (ambient) light occlusion value.
@ face_coord
Generic face coordinate.
std::array< const vertex_attrib_variant, N > vertex_attrib_variants
Array of several vertex attribute variant instances.
Definition: vertex_attrib.hpp:199
Template type used mostly for function type-tag dispatching.
Definition: type_identity.hpp:19
@ weight
Generic vertex weight value.
@ vertex_pivot
Vertex pivot point.
static constexpr auto all_vertex_attrib_bits() noexcept -> vertex_attrib_bits
Returns vertex_attrib_bits value with all bits set.
Definition: vertex_attrib.hpp:89
@ normal
Vertex normal vector.
static constexpr auto operator|(generator_capability a, generator_capability b) noexcept -> generator_capabilities
Bitwise-or operator for generator_capability enumerators.
Definition: gen_capabilities.hpp:37

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).