Unravel Engine C++ Reference
Loading...
Searching...
No Matches
extrude_mesh.hpp
Go to the documentation of this file.
1#ifndef GENERATOR_EXTRUDEMESH_HPP
2#define GENERATOR_EXTRUDEMESH_HPP
3
4#include "edge.hpp"
5#include "iterator.hpp"
6#include "mesh_vertex.hpp"
7#include "path_vertex.hpp"
8#include "shape_vertex.hpp"
9#include "triangle.hpp"
10
11namespace generator
12{
13
17template<typename shape_t, typename path_t>
19{
20public:
22 {
23 public:
25 {
26 triangle_t triangle;
27
28 const auto& shapeEdge = shape_edges_.generate();
29 const auto& pathEdge = path_edges_.generate();
30
31 if(odd_ == 0)
32 {
33 triangle.vertices[0] = shapeEdge.vertices[0] + pathEdge.vertices[0] * mesh_->shape_vertex_count_;
34 triangle.vertices[1] = shapeEdge.vertices[1] + pathEdge.vertices[1] * mesh_->shape_vertex_count_;
35 triangle.vertices[2] = shapeEdge.vertices[0] + pathEdge.vertices[1] * mesh_->shape_vertex_count_;
36 }
37 else
38 {
39 triangle.vertices[0] = shapeEdge.vertices[0] + pathEdge.vertices[0] * mesh_->shape_vertex_count_;
40 triangle.vertices[1] = shapeEdge.vertices[1] + pathEdge.vertices[0] * mesh_->shape_vertex_count_;
41 triangle.vertices[2] = shapeEdge.vertices[1] + pathEdge.vertices[1] * mesh_->shape_vertex_count_;
42 }
43
44 return triangle;
45 }
46
47 bool done() const noexcept
48 {
49 return path_edges_.done();
50 }
51
52 void next()
53 {
54 odd_ = !odd_;
55
56 if(odd_)
57 {
58 shape_edges_.next();
59 if(shape_edges_.done())
60 {
61 path_edges_.next();
62 shape_edges_ = mesh_->shape_.edges();
63 }
64 }
65 }
66
67 private:
68 const extrude_mesh_t* mesh_;
69
70 typename shape_t::edges_t shape_edges_;
71
72 typename path_t::edges_t path_edges_;
73
74 bool odd_;
75
76 explicit triangles_t(const extrude_mesh_t& mesh)
77 : mesh_{&mesh}
78 , shape_edges_{mesh.shape_.edges()}
79 , path_edges_{mesh.path_.edges()}
80 , odd_{true}
81 {
82 }
83
84 friend class extrude_mesh_t;
85 };
86
88 {
89 public:
91 {
92 mesh_vertex_t vertex;
93
94 const auto& shapeVertex = shape_vertices_.generate();
95 const auto& pathVertex = path_vertices_.generate();
96
97 gml::dvec3 pathBinormal = pathVertex.binormal();
98 vertex.position = pathVertex.position + shapeVertex.position[0] * pathVertex.normal +
99 shapeVertex.position[1] * pathBinormal;
100
101 gml::dvec2 shapeNormal = shapeVertex.normal();
102 vertex.normal = shapeNormal[0] * pathVertex.normal + shapeNormal[1] * pathBinormal;
103
104 vertex.tex_coord[0] = shapeVertex.texCoord;
105 vertex.tex_coord[1] = pathVertex.texCoord;
106
107 return vertex;
108 }
109
110 bool done() const noexcept
111 {
112 return path_vertices_.done();
113 }
114
115 void next()
116 {
117 shape_vertices_.next();
118 if(shape_vertices_.done())
119 {
120 path_vertices_.next();
121 shape_vertices_ = mesh_->shape_.vertices();
122 }
123 }
124
125 private:
126 const extrude_mesh_t* mesh_;
127
128 typename shape_t::vertices_t shape_vertices_;
129
130 typename path_t::vertices_t path_vertices_;
131
132 explicit vertices_t(const extrude_mesh_t& mesh)
133 : mesh_{&mesh}
134 , shape_vertices_{mesh.shape_.vertices()}
135 , path_vertices_{mesh.path_.vertices()}
136 {
137 }
138
139 friend class extrude_mesh_t;
140 };
141
142 triangles_t triangles() const noexcept
143 {
144 return triangles_t{*this};
145 }
146
147 vertices_t vertices() const noexcept
148 {
149 return vertices_t{*this};
150 }
151
154 extrude_mesh_t(shape_t shape, path_t path)
155 : shape_{std::move(shape)}
156 , path_{std::move(path)}
157 , shape_vertex_count_{count(shape_.vertices())}
158 {
159 }
160
161private:
162 shape_t shape_;
163
164 path_t path_;
165
166 int shape_vertex_count_;
167};
168
169template<typename shape_t, typename path_t>
171{
172 return extrude_mesh_t<shape_t, path_t>{std::move(shape), std::move(path)};
173}
174} // namespace generator
175
176#endif
extrude_mesh_t(shape_t shape, path_t path)
vertices_t vertices() const noexcept
triangles_t triangles() const noexcept
gml::dvec2 tex_coord
UV texture coordinates.
gml::dvec3 normal
Unit vector perpendicular to the surface.
gml::ivec3 vertices
Zero based indices of the triangle vertices in counterclockwise order.
Definition triangle.hpp:13
int count(const generator_t &generator) noexcept
Counts the number of steps left in the generator.
Definition utils.hpp:70
extrude_mesh_t< shape_t, path_t > extrude_mesh(shape_t shape, path_t path)