Unravel Engine C++ Reference
Loading...
Searching...
No Matches
subdivide_shape.hpp
Go to the documentation of this file.
1#ifndef GENERATOR_SUBDIVIDESHAPE_HPP
2#define GENERATOR_SUBDIVIDESHAPE_HPP
3
4#include "edge.hpp"
5#include "shape_vertex.hpp"
6#include "utils.hpp"
7
8namespace generator
9{
10
12template<typename shape_t>
14{
15public:
16 class edges_t
17 {
18 public:
19 bool done() const noexcept
20 {
21 return edges_.done();
22 }
23
25 {
26 edge_t edge_ = edges_.generate();
27
28 if(i_ % 2 == 0)
29 return edge_t{{edge_.vertices[0], static_cast<int>(shape_->vertex_cache_.size()) + i_ / 2}};
30
31 return edge_t{{static_cast<int>(shape_->vertex_cache_.size()) + i_ / 2, edge_.vertices[1]}};
32 }
33
34 void next()
35 {
36 ++i_;
37 if(i_ % 2 == 0)
38 edges_.next();
39 }
40
41 private:
42 const subdivide_shape_t* shape_;
43
45
46 int i_;
47
48 explicit edges_t(const subdivide_shape_t& shape) : shape_{&shape}, edges_{shape.shape_.edges()}, i_{0}
49 {
50 }
51
52 friend class subdivide_shape_t;
53 };
54
56 {
57 public:
58 bool done() const noexcept
59 {
60 return vertex_index_ == shape_->vertex_cache_.size() && edges_.done();
61 }
62
64 {
65 if(vertex_index_ < shape_->vertex_cache_.size())
66 return shape_->vertex_cache_[vertex_index_];
67
68 const edge_t edge = edges_.generate();
69 const shape_vertex_t& v1 = shape_->vertex_cache_[edge.vertices[0]];
70 const shape_vertex_t& v2 = shape_->vertex_cache_[edge.vertices[1]];
71
72 shape_vertex_t vertex;
73 vertex.position = gml::mix(v1.position, v2.position, 0.5);
74 vertex.tangent = gml::normalize(gml::mix(v1.tangent, v2.tangent, 0.5));
75 vertex.tex_coord = 0.5 * v1.tex_coord + 0.5 * v2.tex_coord;
76 return vertex;
77 }
78
79 void next()
80 {
81 if(vertex_index_ < shape_->vertex_cache_.size())
82 ++vertex_index_;
83 else
84 edges_.next();
85 }
86
87 private:
88 const subdivide_shape_t* shape_;
89
90 int vertex_index_;
91
93
94 explicit vertices_t(const subdivide_shape_t& shape)
95 : shape_{&shape}
96 , vertex_index_{0}
97 , edges_{shape.shape_.edges()}
98 {
99 }
100
101 friend class subdivide_shape_t;
102 };
103
104 subdivide_shape_t(shape_t shape) : shape_(std::move(shape)), vertex_cache_{}
105 {
106 for(const shape_vertex_t& vertex : shape_.vertices())
107 {
108 vertex_cache_.push_back(vertex);
109 }
110 }
111
112 edges_t edges() const noexcept
113 {
114 return edges_t{*this};
115 }
116
117 vertices_t vertices() const noexcept
118 {
119 return vertices_t{*this};
120 }
121
122private:
123 shape_t shape_;
124
125 std::vector<shape_vertex_t> vertex_cache_;
126};
127} // namespace generator
128
129#endif
gml::ivec2 vertices
Definition edge.hpp:12
Cuts each edge in half.
edges_t edges() const noexcept
vertices_t vertices() const noexcept