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

gen_base.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SHAPES_GEN_BASE_HPP
10 #define EAGINE_SHAPES_GEN_BASE_HPP
11 
12 #include "../assert.hpp"
13 #include "../compare.hpp"
14 #include "../integer_range.hpp"
15 #include "../interface.hpp"
16 #include "../math/primitives.hpp"
17 #include "../span.hpp"
18 #include "../types.hpp"
19 #include "drawing.hpp"
20 #include "gen_capabilities.hpp"
21 #include "vertex_attrib.hpp"
22 #include <eagine/config/basic.hpp>
23 #include <array>
24 #include <memory>
25 
26 namespace eagine {
27 namespace shapes {
28 //------------------------------------------------------------------------------
32 //------------------------------------------------------------------------------
35 struct generator : interface<generator> {
36 
38  virtual auto attrib_bits() noexcept -> vertex_attrib_bits = 0;
39 
41  auto has(vertex_attrib_kind attrib) noexcept {
42  return bool(attrib_bits() | attrib);
43  }
44 
46  virtual auto enable(generator_capability cap, bool value = true) noexcept
47  -> bool = 0;
48 
50  auto disable(generator_capability cap) noexcept {
51  return enable(cap, false);
52  }
53 
55  virtual auto is_enabled(generator_capability cap) noexcept -> bool = 0;
56 
58  auto strips_allowed() noexcept -> bool {
60  }
61 
63  auto fans_allowed() noexcept -> bool {
65  }
66 
68  auto primitive_restart() noexcept -> bool {
70  }
71 
73  virtual auto vertex_count() -> span_size_t = 0;
74 
77 
79  virtual auto variant_name(vertex_attrib_variant vav) -> string_view = 0;
80 
84  const span_size_t n = attribute_variants(attrib);
85  span_size_t index{-1};
86  for(auto i : integer_range(n)) {
87  if(are_equal(name, variant_name({attrib, i}))) {
88  index = i;
89  break;
90  }
91  }
92  return {attrib, index};
93  }
94 
97 
100  return vertex_count() * values_per_vertex(vav);
101  }
102 
104  virtual auto attrib_type(vertex_attrib_variant vav) -> attrib_data_type = 0;
105 
107  virtual auto is_attrib_normalized(vertex_attrib_variant vav) -> bool = 0;
108 
110  virtual void attrib_values(vertex_attrib_variant, span<byte> dest) = 0;
111 
113  virtual void attrib_values(vertex_attrib_variant, span<std::int16_t>) = 0;
114 
116  virtual void attrib_values(vertex_attrib_variant, span<std::int32_t>) = 0;
117 
119  virtual void attrib_values(vertex_attrib_variant, span<std::uint16_t>) = 0;
120 
122  virtual void attrib_values(vertex_attrib_variant, span<std::uint32_t>) = 0;
123 
125  virtual void attrib_values(vertex_attrib_variant, span<float> dest) = 0;
126 
128  virtual auto draw_variant_count() -> span_size_t = 0;
129 
132  return index;
133  }
134 
136  virtual auto index_type(drawing_variant) -> index_data_type = 0;
137 
140  return index_type(0);
141  }
142 
144  virtual auto index_count(drawing_variant) -> span_size_t = 0;
145 
147  auto index_count() {
148  return index_count(0);
149  }
150 
152  virtual void indices(drawing_variant, span<std::uint8_t> dest) = 0;
153 
155  void indices(span<std::uint8_t> dest) {
156  indices(0, dest);
157  }
158 
160  virtual void indices(drawing_variant, span<std::uint16_t> dest) = 0;
161 
163  void indices(span<std::uint16_t> dest) {
164  indices(0, dest);
165  }
166 
168  virtual void indices(drawing_variant, span<std::uint32_t> dest) = 0;
169 
171  void indices(span<std::uint32_t> dest) {
172  indices(0, dest);
173  }
174 
176  virtual auto operation_count(drawing_variant) -> span_size_t = 0;
177 
180  return operation_count(0);
181  }
182 
184  virtual void instructions(drawing_variant, span<draw_operation> dest) = 0;
185 
187  void instructions(span<draw_operation> dest) {
188  return instructions(0, dest);
189  }
190 
192  virtual auto bounding_sphere() -> math::sphere<float, true>;
193 
195  virtual void ray_intersections(
197  span<const math::line<float, true>> rays,
198  span<optionally_valid<float>> intersections);
199 
202  span<const math::line<float, true>> rays,
203  span<optionally_valid<float>> intersections) {
204  return ray_intersections(0, rays, intersections);
205  }
206 
208  auto
211  optionally_valid<float> result{};
212  ray_intersections(var, view_one(ray), cover_one(result));
213  return result;
214  }
215 
219  optionally_valid<float> result{};
220  ray_intersections(0, view_one(ray), cover_one(result));
221  return result;
222  }
223 };
224 //------------------------------------------------------------------------------
227 class generator_base : public generator {
228 public:
229  auto attrib_bits() noexcept -> vertex_attrib_bits final {
230  return _attr_bits;
231  }
232 
233  auto enable(generator_capability cap, bool value) noexcept -> bool final {
234  if(value) {
235  _caps |= cap;
236  } else {
237  _caps &= cap;
238  }
239  return true;
240  }
241 
242  auto is_enabled(generator_capability cap) noexcept -> bool final {
243  return _caps.has(cap);
244  }
245 
247  return has(attrib) ? 1U : 0U;
248  }
249 
251  return {};
252  }
253 
254  auto has_variant(vertex_attrib_variant vav) -> bool {
255  EAGINE_ASSERT(vav.has_valid_index());
256  return vav.index() < attribute_variants(vav.attribute());
257  }
258 
260  return has_variant(vav) ? attrib_values_per_vertex(vav) : 0U;
261  }
262 
265  }
266 
268  return false;
269  }
270 
271  void attrib_values(vertex_attrib_variant, span<byte>) override {
272  EAGINE_UNREACHABLE("Generator failed to get byte attribute values.");
273  }
274 
275  void attrib_values(vertex_attrib_variant, span<std::int16_t>) override {
276  EAGINE_UNREACHABLE("Generator failed to get int16 attribute values.");
277  }
278 
279  void attrib_values(vertex_attrib_variant, span<std::int32_t>) override {
280  EAGINE_UNREACHABLE("Generator failed to get int32 attribute values.");
281  }
282 
283  void attrib_values(vertex_attrib_variant, span<std::uint16_t>) override {
284  EAGINE_UNREACHABLE("Generator failed to get uint16 attribute values.");
285  }
286 
287  void attrib_values(vertex_attrib_variant, span<std::uint32_t>) override {
288  EAGINE_UNREACHABLE("Generator failed to get uint32 attribute values.");
289  }
290 
291  void attrib_values(vertex_attrib_variant, span<float>) override {
292  EAGINE_UNREACHABLE("Generator failed to get float attribute values.");
293  }
294 
295  auto draw_variant_count() -> span_size_t override {
296  return 1;
297  }
298 
299  auto index_type(drawing_variant) -> index_data_type override;
300 
301  auto index_count(drawing_variant) -> span_size_t override;
302 
303  void indices(drawing_variant, span<std::uint8_t> dest) override;
304 
305  void indices(drawing_variant, span<std::uint16_t> dest) override;
306 
307  void indices(drawing_variant, span<std::uint32_t> dest) override;
308 
309 protected:
310  generator_base(vertex_attrib_bits attr_bits) noexcept
311  : _attr_bits(attr_bits) {}
312 
313 private:
314  vertex_attrib_bits _attr_bits;
316 };
317 //------------------------------------------------------------------------------
321 public:
322  void attrib_values(vertex_attrib_variant vav, span<float> dest) override;
323 
324 protected:
326  : generator_base(attr_bits) {}
327 };
328 //------------------------------------------------------------------------------
329 static inline auto operator+(
330  std::unique_ptr<generator>&& l,
331  std::unique_ptr<generator>&& r) noexcept
332  -> std::array<std::unique_ptr<generator>, 2> {
333  return {{std::move(l), std::move(r)}};
334 }
335 //------------------------------------------------------------------------------
336 template <std::size_t N, std::size_t... I>
337 static inline auto _add_to_array(
338  std::array<std::unique_ptr<generator>, N>&& l,
339  std::unique_ptr<generator>&& r,
340  std::index_sequence<I...>) noexcept
341  -> std::array<std::unique_ptr<generator>, N + 1> {
342  return {{std::move(l[I])..., std::move(r)}};
343 }
344 //------------------------------------------------------------------------------
345 template <std::size_t N>
346 static inline auto operator+(
347  std::array<std::unique_ptr<generator>, N>&& l,
348  std::unique_ptr<generator>&& r) noexcept
349  -> std::array<std::unique_ptr<generator>, N + 1> {
350  return _add_to_array(
351  std::move(l), std::move(r), std::make_index_sequence<N>());
352 }
353 //------------------------------------------------------------------------------
354 } // namespace shapes
355 } // namespace eagine
356 
357 #if !EAGINE_LINK_LIBRARY || defined(EAGINE_IMPLEMENTING_LIBRARY)
358 #include <eagine/shapes/gen_base.inl>
359 #endif
360 
361 #endif // EAGINE_SHAPES_GEN_BASE_HPP
auto values_per_vertex(vertex_attrib_variant vav) -> span_size_t override
Returns the number of values per vertex for the specified variant.
Definition: gen_base.hpp:259
virtual auto is_enabled(generator_capability cap) noexcept -> bool=0
Indicates if the specified generator capability is enabled.
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
auto has(vertex_attrib_kind attrib) noexcept
Tests if the specified attribute is supported by this generator.
Definition: gen_base.hpp:41
bitfield< vertex_attrib_kind > vertex_attrib_bits
Alias for vertex_attrib_kind bitfield type.
Definition: vertex_attrib.hpp:85
Basic template for spheres in N-dimensional space.
Definition: primitives.hpp:122
auto draw_variant(span_size_t index) -> drawing_variant
Returns the identifier of the drawing variant at the specified index.
Definition: gen_base.hpp:131
Basic template for lines in N-dimensional space.
Definition: primitives.hpp:19
Common code is placed in this namespace.
Definition: eagine.hpp:21
void indices(drawing_variant, span< std::uint8_t > dest) override
Fetches the index data for the specified drawing variant.
virtual auto bounding_sphere() -> math::sphere< float, true >
Returns the bounding sphere for the generated shape.
auto variant_name(vertex_attrib_variant) -> string_view override
Returns the name of the specified attribute variant.
Definition: gen_base.hpp:250
auto operation_count()
Returns the number of drawing instructions for the default variant.
Definition: gen_base.hpp:179
void attrib_values(vertex_attrib_variant, span< std::int16_t >) override
Fetches the vertex attribute data for the specified variant as integers.
Definition: gen_base.hpp:275
virtual auto is_attrib_normalized(vertex_attrib_variant vav) -> bool=0
Indicates if the specified variant attribute values should be normalized.
virtual auto values_per_vertex(vertex_attrib_variant) -> span_size_t=0
Returns the number of values per vertex for the specified variant.
void attrib_values(vertex_attrib_variant, span< std::uint32_t >) override
Fetches the vertex attribute data for the specified variant as integers.
Definition: gen_base.hpp:287
Interface for shape loaders or generators.
Definition: gen_base.hpp:35
Primary template for conditionally valid values.
Definition: decl.hpp:49
constexpr auto has(bit_type bit) const noexcept
Tests if the specified bit is set.
Definition: bitfield.hpp:70
virtual void instructions(drawing_variant, span< draw_operation > dest)=0
Fetches the drawing operations for the specified drawing variant.
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
auto is_enabled(generator_capability cap) noexcept -> bool final
Indicates if the specified generator capability is enabled.
Definition: gen_base.hpp:242
basic_span< T, T *, S > span
Default alias for basic memory spans with native pointer type.
Definition: span.hpp:415
virtual auto attribute_variants(vertex_attrib_kind) -> span_size_t=0
Returns the count of shape attribute variants.
auto ray_intersection(const math::line< float, true > &ray) -> optionally_valid< float >
Returns the parameter for the nearest intersection with a ray.
Definition: gen_base.hpp:217
Class designating an vertex attribute variant in a shape generator.
Definition: vertex_attrib.hpp:104
auto primitive_restart() noexcept -> bool
Indicates if primitive restart is enabled.
Definition: gen_base.hpp:68
auto attrib_bits() noexcept -> vertex_attrib_bits final
Returns the set of vertex attributes supported by this generator.
Definition: gen_base.hpp:229
bitfield< generator_capability > generator_capabilities
Alias for generator_capability bitfield type.
Definition: gen_capabilities.hpp:32
virtual auto variant_name(vertex_attrib_variant vav) -> string_view=0
Returns the name of the specified attribute variant.
Base template for abstract interfaces, implements common functionality.
Definition: interface.hpp:18
virtual void indices(drawing_variant, span< std::uint8_t > dest)=0
Fetches the index data for the specified drawing variant.
auto enable(generator_capability cap, bool value) noexcept -> bool final
Enables or disables the specified generator capability.
Definition: gen_base.hpp:233
auto strips_allowed() noexcept -> bool
Indicates if element strips are enabled.
Definition: gen_base.hpp:58
Common base implementation of the shape generator interface.
Definition: gen_base.hpp:227
void indices(span< std::uint16_t > dest)
Fetches the index data for the default drawing variant.
Definition: gen_base.hpp:163
auto disable(generator_capability cap) noexcept
Disables the specified generator capability.
Definition: gen_base.hpp:50
auto is_attrib_normalized(vertex_attrib_variant) -> bool override
Indicates if the specified variant attribute values should be normalized.
Definition: gen_base.hpp:267
generator_capability
Shape generator capability bit enumeration.
Definition: gen_capabilities.hpp:21
virtual auto attrib_type(vertex_attrib_variant vav) -> attrib_data_type=0
Returns the attribute data type for the specified variant.
virtual auto vertex_count() -> span_size_t=0
Returns the shaped vertex count.
@ element_strips
Line or triangle strips should be generated if possible.
span_size_t drawing_variant
Alias for shape drawing variant index type.
Definition: gen_base.hpp:31
@ primitive_restart
Primitive restart functionality should be used if possible.
auto ray_intersection(drawing_variant var, const math::line< float, true > &ray) -> optionally_valid< float >
Returns the parameter for the nearest intersection with a ray.
Definition: gen_base.hpp:209
@ element_fans
Line or triangle fanst should be generated if possible.
Base class for shape generators re-calculating the center.
Definition: gen_base.hpp:320
auto index_type() -> index_data_type
Returns the index data type for the default draw variant.
Definition: gen_base.hpp:139
void attrib_values(vertex_attrib_variant vav, span< float > dest) override
Fetches the vertex attribute data for the specified variant as floats.
virtual void ray_intersections(drawing_variant, span< const math::line< float, true >> rays, span< optionally_valid< float >> intersections)
Calculates the intersections of the shape geometry with a ray.
void attrib_values(vertex_attrib_variant, span< byte >) override
Fetches the vertex attribute data for the specified variant as bytes.
Definition: gen_base.hpp:271
void attrib_values(vertex_attrib_variant, span< float >) override
Fetches the vertex attribute data for the specified variant as floats.
Definition: gen_base.hpp:291
vertex_attrib_kind
Shape vertex attribute kind enumeration.
Definition: vertex_attrib.hpp:25
auto fans_allowed() noexcept -> bool
Indicates if element fans are enabled.
Definition: gen_base.hpp:63
virtual auto enable(generator_capability cap, bool value=true) noexcept -> bool=0
Enables or disables the specified generator capability.
void ray_intersections(span< const math::line< float, true >> rays, span< optionally_valid< float >> intersections)
Calculates the intersections of the shape geometry with a ray.
Definition: gen_base.hpp:201
virtual void attrib_values(vertex_attrib_variant, span< byte > dest)=0
Fetches the vertex attribute data for the specified variant as bytes.
attrib_data_type
Shape vertex attribute data type enumeration.
Definition: drawing.hpp:67
void instructions(span< draw_operation > dest)
Fetches the drawing operations for the default drawing variant.
Definition: gen_base.hpp:187
void attrib_values(vertex_attrib_variant, span< std::int32_t >) override
Fetches the vertex attribute data for the specified variant as integers.
Definition: gen_base.hpp:279
auto attribute_variants(vertex_attrib_kind attrib) -> span_size_t override
Returns the count of shape attribute variants.
Definition: gen_base.hpp:246
auto value_count(vertex_attrib_variant vav) -> span_size_t
Returns the total number of values for the specified attribute variant.
Definition: gen_base.hpp:99
auto attrib_type(vertex_attrib_variant) -> attrib_data_type override
Returns the attribute data type for the specified variant.
Definition: gen_base.hpp:263
auto draw_variant_count() -> span_size_t override
Returns the count of possible shape draw variants.
Definition: gen_base.hpp:295
integer_range(B, E) -> integer_range< std::common_type_t< B, E >>
Deduction guide for integer_range.
void indices(span< std::uint32_t > dest)
Fetches the index data for the default drawing variant.
Definition: gen_base.hpp:171
void indices(span< std::uint8_t > dest)
Fetches the index data for the default drawing variant.
Definition: gen_base.hpp:155
void attrib_values(vertex_attrib_variant, span< std::uint16_t >) override
Fetches the vertex attribute data for the specified variant as integers.
Definition: gen_base.hpp:283
virtual auto attrib_bits() noexcept -> vertex_attrib_bits=0
Returns the set of vertex attributes supported by this generator.
index_data_type
Shape element index type enumeration.
Definition: drawing.hpp:111
virtual auto draw_variant_count() -> span_size_t=0
Returns the count of possible shape draw variants.
auto find_variant(vertex_attrib_kind attrib, string_view name) -> vertex_attrib_variant
Finds attribute variant by kind and name.
Definition: gen_base.hpp:82
auto index_count()
Returns the index count for the default drawing variant.
Definition: gen_base.hpp:147

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